Skip to content

Build your personal knowledge base with Trilium Notes

feat(share): show subpages as the app's cards, with an excerpt

Elian Doran487ba6df86b8
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Sharing.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Sharing.html
index 282550d095..13231617ff 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Sharing.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Sharing.html
@@ -22,6 +22,10 @@ class="image">
between the pages of the site, and shows the current note.</li>
<li>Customizable logo.</li>
<li>Quick navigation buttons (previous and next note).</li>
+ <li>The subpages of a text or code note shown below its content as cards,
+ as in the application. Each card shows the icon of the subpage, the start
+ of its text (or its <code>#shareDescription</code>) and how many subpages
+ it has itself. The cards can be hidden with <code>#hideChildrenOverview</code>.</li>
<li>Displaying the date of the last update of the note, written out in the
application's language.</li>
<li>Marking the page with the application's language and its text direction,
@@ -110,12 +114,15 @@ class="image">
</th>
<td>
<ul>
- <li>The child notes are displayed in a fixed format.&nbsp;</li>
+ <li>The child notes are shown as cards, or as a list when the view type of
+ the collection is <em>List</em>. Each shows its icon, the start of its text
+ (or its <code>#shareDescription</code>) and how many subpages it has itself.</li>
</ul>
</td>
<td>
<ul>
- <li>More advanced view types such as the calendar view are not supported.</li>
+ <li>The other view types (such as the calendar, the table or the board) are
+ not supported, and the child notes are shown as cards instead.</li>
</ul>
</td>
</tr>
@@ -404,7 +411,9 @@ class="admonition note">
<tr>
<td><code>#shareDescription</code>
</td>
- <td>define text to be added to the HTML meta tag for description</td>
+ <td>define text to be added to the HTML meta tag for description. It also
+ describes the note in the list of subpages of its parent, in place of the
+ start of its text.</td>
</tr>
<tr>
<td><code>#shareRaw</code>
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Sharing/Custom share template.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Sharing/Custom share template.html
index 9c733d91a1..31a4696900 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Sharing/Custom share template.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Sharing/Custom share template.html
@@ -98,7 +98,17 @@
<tr>
<td><code>childLinks</code>
</td>
- <td>The links to the note's visible children, each with <code>title</code>, <code>href</code>, <code>isExternal</code> and <code>type</code>.</td>
+ <td>The links to the note's visible children, each with <code>title</code>, <code>href</code>, <code>isExternal</code>, <code>type</code>, <code>icon</code> (its
+ icon classes), <code>excerpt</code> (its <code>#shareDescription</code> or
+ the start of its text, or <code>null</code>) and <code>childCount</code> (how
+ many visible children it has). Empty for a note that is not a collection,
+ a text note or a code note, and with <code>#hideChildrenOverview</code>.</td>
+ </tr>
+ <tr>
+ <td><code>childLinksLayout</code>
+ </td>
+ <td>How the default template lays <code>childLinks</code> out: <code>list</code> for
+ a collection whose view type is list, <code>grid</code> otherwise.</td>
</tr>
<tr>
<td><code>prevNext</code>
diff --git a/apps/server/src/assets/translations/en/server.json b/apps/server/src/assets/translations/en/server.json
index 477cd8c146..c020ad43d8 100644
--- a/apps/server/src/assets/translations/en/server.json
+++ b/apps/server/src/assets/translations/en/server.json
@@ -468,7 +468,9 @@
"heading-link": "Link to This Section",
"previous": "Previous",
"next": "Next",
- "page-navigation": "Previous and next pages"
+ "page-navigation": "Previous and next pages",
+ "subpage-count_one": "{{count}} subpage",
+ "subpage-count_other": "{{count}} subpages"
},
"hidden_subtree_templates": {
"text-snippet": "Text Snippet",
diff --git a/docs/User Guide/User Guide/Advanced Usage/Sharing.md b/docs/User Guide/User Guide/Advanced Usage/Sharing.md
index 7c3dcc7cb0..e94924e61e 100644
--- a/docs/User Guide/User Guide/Advanced Usage/Sharing.md
+++ b/docs/User Guide/User Guide/Advanced Usage/Sharing.md
@@ -11,6 +11,7 @@ Trilium allows you to share selected notes as **publicly accessible** read-only
* Collapsible tree with the same note icons as the application. It keeps the sections the visitor expanded and its scroll position while they move between the pages of the site, and shows the current note.
* Customizable logo.
* Quick navigation buttons (previous and next note).
+* The subpages of a text or code note shown below its content as cards, as in the application. Each card shows the icon of the subpage, the start of its text (or its `#shareDescription`) and how many subpages it has itself. The cards can be hidden with `#hideChildrenOverview`.
* Displaying the date of the last update of the note, written out in the application's language.
* Marking the page with the application's language and its text direction, and the content of the note with its own [content language](../Note%20Types/Text/Content%20language%20%26%20Right-to-left%20support.md), for screen readers and right-to-left text.
@@ -62,8 +63,8 @@ Trilium allows you to share selected notes as **publicly accessible** read-only
</tr>
<tr>
<th><a class="reference-link" href="../Collections.md">Collections</a></th>
- <td><ul><li>The child notes are displayed in a fixed format.&nbsp;</li></ul></td>
- <td><ul><li>More advanced view types such as the calendar view are not supported.</li></ul></td>
+ <td><ul><li>The child notes are shown as cards, or as a list when the view type of the collection is <em>List</em>. Each shows its icon, the start of its text (or its <code>#shareDescription</code>) and how many subpages it has itself.</li></ul></td>
+ <td><ul><li>The other view types (such as the calendar, the table or the board) are not supported, and the child notes are shown as cards instead.</li></ul></td>
</tr>
<tr>
<th><a class="reference-link" href="../Note%20Types/Mermaid%20Diagrams.md">Mermaid Diagrams</a></th>
@@ -265,7 +266,7 @@ The URL must be absolute and include the scheme (e.g. `https://`).
</tr>
<tr>
<td><code>#shareDescription</code></td>
- <td>define text to be added to the HTML meta tag for description</td>
+ <td>define text to be added to the HTML meta tag for description. It also describes the note in the list of subpages of its parent, in place of the start of its text.</td>
</tr>
<tr>
<td><code>#shareRaw</code></td>
diff --git a/docs/User Guide/User Guide/Advanced Usage/Sharing/Custom share template.md b/docs/User Guide/User Guide/Advanced Usage/Sharing/Custom share template.md
index db4c1ffc1a..b99e1d13b7 100644
--- a/docs/User Guide/User Guide/Advanced Usage/Sharing/Custom share template.md
+++ b/docs/User Guide/User Guide/Advanced Usage/Sharing/Custom share template.md
@@ -34,7 +34,8 @@ Your template is rendered with a context object exposing the note and its render
| `logo` | The site logo above the navigation tree and, on a narrow screen, in the header: `href` (`#shareRootLink`, otherwise the site's root page), `image` (the `~shareLogo` image, or `null`), `icon` (the icon classes of the site's root note, shown when there is no `image`), and `width` and `height` for the image. |
| `navigation` | The navigation tree of the site: its visible pages, each with `title`, `href`, `isExternal`, `type`, `icon`, `isActive` (the page being shown), `isExpanded` (that page or one of its ancestors) and `children`. |
| `contentClasses` | The classes of the content element: `type-<note type>`, `ck-content` for text and Markdown notes, and `no-content` when the note has no content of its own. |
-| `childLinks` | The links to the note's visible children, each with `title`, `href`, `isExternal` and `type`. |
+| `childLinks` | The links to the note's visible children, each with `title`, `href`, `isExternal`, `type`, `icon` (its icon classes), `excerpt` (its `#shareDescription` or the start of its text, or `null`) and `childCount` (how many visible children it has). Empty for a note that is not a collection, a text note or a code note, and with `#hideChildrenOverview`. |
+| `childLinksLayout` | How the default template lays `childLinks` out: `list` for a collection whose view type is list, `grid` otherwise. |
| `prevNext` | The pages before and after the note in the navigation tree, as `previous` and `next`, each with `title` and `href`, or `null`. |
| `language` | The languages of the page: `page` with the `lang` and `dir` of the application's language, and `content` with those of the note's content language, or `null` when they are the same. |
| `lastUpdated` | When the note was last changed, as `iso` for a `<time>` element and as `text` in the application's language, or `null`. |
diff --git a/packages/share-theme/src/base.css b/packages/share-theme/src/base.css
index be167bb80c..24b08f1fd1 100644
--- a/packages/share-theme/src/base.css
+++ b/packages/share-theme/src/base.css
@@ -46,6 +46,9 @@ html.theme-light {
--quick-search-button-active-color: #383838;
/* The right pane colors of `theme-next-light.css`. */
--main-border-color: #dbdbdb;
+ --muted-text-color: #666;
+ --card-background-color: #0000000d;
+ --card-background-hover-color: #0000001c;
--right-pane-heading-color: gray;
--right-pane-item-hover-background: #00000013;
--mermaid-theme: default;
@@ -81,6 +84,9 @@ html.theme-dark {
--quick-search-button-active-color: #000000;
/* The right pane colors of `theme-next-dark.css`. */
--main-border-color: #454545;
+ --muted-text-color: #bbb;
+ --card-background-color: #ffffff12;
+ --card-background-hover-color: #ffffff20;
--right-pane-heading-color: gray;
--right-pane-item-hover-background: #ffffff26;
--mermaid-theme: dark;
diff --git a/packages/share-theme/src/model/page.spec.ts b/packages/share-theme/src/model/page.spec.ts
index 52c907372f..f6918e1581 100644
--- a/packages/share-theme/src/model/page.spec.ts
+++ b/packages/share-theme/src/model/page.spec.ts
@@ -1,7 +1,7 @@
import { describe, expect, it, vi } from "vitest";
import {
- getChildLinks, getContentClasses, getHtmlSnippets, getLastUpdated, getNavigationTree, getPageHead, getPageLanguages,
+ getChildLinks, getChildLinksLayout, getContentClasses, getHtmlSnippets, getLastUpdated, getNavigationTree, getPageHead, getPageLanguages,
getPrevNextLinks, getShareLink, getSiteAncestorIds, getSiteLogo, getTableOfContents,
type NavigationItem, type PageHeading, type ShareNote
} from "./page.js";
@@ -430,16 +430,91 @@ describe("getContentClasses", () => {
});
describe("getChildLinks", () => {
- it("links to the visible children of a note, in order", () => {
+ const options = { sanitizeUrl: (url: string) => url, iconPackPrefixes: [ "pack" ] };
+
+ it("links to the visible children of a note, in order, with their icon and subpage count", () => {
const parent = fakeNote({ noteId: "parent" });
- addChild(parent, fakeNote({ noteId: "x", type: "book" }));
+ const x = addChild(parent, fakeNote({ noteId: "x", type: "book", icon: "bx bx-book" }));
+ addChild(x, fakeNote({ noteId: "grandchild" }));
addChild(parent, fakeNote({ noteId: "hidden" }), true);
addChild(parent, fakeNote({ noteId: "y", labels: { shareExternal: "https://example.com" } }));
- expect(getChildLinks(parent, (url) => url)).toStrictEqual([
- { href: "./x-alias", isExternal: false, title: "x", type: "book" },
- { href: "https://example.com", isExternal: true, title: "y", type: "text" }
+ expect(getChildLinks(parent, options)).toStrictEqual([
+ {
+ href: "./x-alias", isExternal: false, title: "x", type: "book",
+ icon: "bx bx-book pack", excerpt: null, childCount: 1
+ },
+ {
+ href: "https://example.com", isExternal: true, title: "y", type: "text",
+ icon: "bx bx-note pack", excerpt: null, childCount: 0
+ }
+ ]);
+ });
+
+ it("lists the children only under the note types the app does, unless #hideChildrenOverview", () => {
+ const withChild = (note: FakeNote) => {
+ addChild(note, fakeNote({ noteId: `${note.noteId}-child` }));
+ return getChildLinks(note, options).length;
+ };
+
+ expect(withChild(fakeNote({ noteId: "text" }))).toBe(1);
+ expect(withChild(fakeNote({ noteId: "code", type: "code", mime: "text/x-markdown" }))).toBe(1);
+ expect(withChild(fakeNote({ noteId: "book", type: "book" }))).toBe(1);
+ expect(withChild(fakeNote({ noteId: "canvas", type: "canvas" }))).toBe(0);
+ expect(withChild(fakeNote({ noteId: "hidden", labels: { hideChildrenOverview: "" } }))).toBe(0);
+ expect(withChild(fakeNote({ noteId: "shown", labels: { hideChildrenOverview: "false" } }))).toBe(1);
+ });
+
+ it("describes a child by #shareDescription, else by the start of its text", () => {
+ const parent = fakeNote({ noteId: "parent" });
+ const sentences = "First sentence of the note, which says what it is about. "
+ + "A second one goes on about it at length, well past where a summary should stop. "
+ + "A third one is there only to make the whole too long for a summary.";
+ const texts: Record<string, string | null> = {
+ described: "Ignored",
+ short: " A short\n note. ",
+ sentences,
+ words: "word ".repeat(60),
+ unbroken: "x".repeat(200),
+ empty: " ",
+ unreadable: null
+ };
+ for (const noteId of Object.keys(texts)) {
+ addChild(parent, fakeNote({
+ noteId,
+ labels: noteId === "described" ? { shareDescription: "About it" } : {}
+ }));
+ }
+
+ const excerpts = getChildLinks(parent, { ...options, getText: (note) => texts[note.noteId] })
+ .map((link) => link.excerpt);
+
+ expect(excerpts).toStrictEqual([
+ "About it",
+ "A short note.",
+ "First sentence of the note, which says what it is about. "
+ + "A second one goes on about it at length, well past where a summary should stop.",
+ `${"word ".repeat(32).trim()}…`,
+ `${"x".repeat(160)}…`,
+ null,
+ null
]);
+ expect(getChildLinks(parent, options).map((link) => link.excerpt))
+ .toStrictEqual([ "About it", null, null, null, null, null, null ]);
+ });
+});
+
+describe("getChildLinksLayout", () => {
+ const layout = (type: string, labels: Record<string, string> = {}) =>
+ getChildLinksLayout(fakeNote({ noteId: "page", type, labels }));
+
+ it("shows a grid, unless a collection asks for a list", () => {
+ expect(layout("book")).toBe("grid");
+ expect(layout("book", { viewType: "grid" })).toBe("grid");
+ expect(layout("book", { viewType: "list" })).toBe("list");
+ expect(layout("book", { viewType: "calendar" })).toBe("grid");
+ expect(layout("text")).toBe("grid");
+ expect(layout("text", { viewType: "list" })).toBe("grid");
});
});
diff --git a/packages/share-theme/src/model/page.ts b/packages/share-theme/src/model/page.ts
index 38f7ae01ec..962a553192 100644
--- a/packages/share-theme/src/model/page.ts
+++ b/packages/share-theme/src/model/page.ts
@@ -230,6 +230,31 @@ function readLabel(note: ShareNote, name: string) {
return note.getLabelValue(name)?.trim() || null;
}
+const CHILD_LIST_NOTE_TYPES = [ "book", "text", "code" ];
+
+/** The most characters of a child's text its excerpt shows. */
+const EXCERPT_LENGTH = 160;
+
+/**
+ * Shortens `text` to an excerpt of at most {@link EXCERPT_LENGTH} characters: whole sentences when
+ * they fill at least a third of it, else whole words followed by an ellipsis.
+ */
+function toExcerpt(text: string) {
+ const plain = text.replace(/\s+/g, " ").trim();
+ if (plain.length <= EXCERPT_LENGTH) {
+ return plain || null;
+ }
+
+ const head = plain.slice(0, EXCERPT_LENGTH);
+ const sentenceEnd = Math.max(head.lastIndexOf(". "), head.lastIndexOf("! "), head.lastIndexOf("? "));
+ if (sentenceEnd >= EXCERPT_LENGTH / 3) {
+ return head.slice(0, sentenceEnd + 1);
+ }
+
+ const wordEnd = head.lastIndexOf(" ");
+ return `${wordEnd > 0 ? head.slice(0, wordEnd) : head}…`;
+}
+
/** Resolves `address` against `base` unless it is already absolute or `base` is not a URL. */
function toAbsoluteUrl(address: string, base: string) {
if (URL.canParse(address) || !URL.canParse(base)) {
@@ -349,15 +374,58 @@ export function getContentClasses(note: ShareNote, isEmpty = false) {
].filter(Boolean).join(" ");
}
-/** Returns the links to the visible children of `note`, for its list of subpages. */
-export function getChildLinks(note: ShareNote, sanitizeUrl: (url: string) => string) {
+/** A child of a page, as its list of subpages shows it. */
+export interface ChildLink extends ShareLink {
+ title: string;
+ type: string;
+ /** The icon's CSS classes. */
+ icon: string;
+ /** `#shareDescription`, else the start of the child's text, or `null`. */
+ excerpt: string | null;
+ /** How many visible children the child has itself. */
+ childCount: number;
+}
+
+/** What {@link getChildLinks} needs besides the note. */
+export interface ChildLinksOptions {
+ sanitizeUrl: (url: string) => string;
+ /** The prefixes of the icon packs available to the page, for the children's icons. */
+ iconPackPrefixes?: string[];
+ /**
+ * The plain text a child's excerpt starts from, or `null` for none, such as a child the
+ * visitor is not allowed to read. Without it, only `#shareDescription` describes a child.
+ */
+ getText?: (note: ShareNote) => string | null;
+}
+
+/**
+ * Returns the links to the visible children of `note`, for its list of subpages. As in the app,
+ * only a collection, a text note and a code note list their children, and `#hideChildrenOverview`
+ * hides the list.
+ */
+export function getChildLinks(note: ShareNote, options: ChildLinksOptions): ChildLink[] {
+ if (!CHILD_LIST_NOTE_TYPES.includes(note.type) || note.isLabelTruthy("hideChildrenOverview")) {
+ return [];
+ }
+
return note.getVisibleChildNotes().map((child) => ({
- ...getShareLink(child, sanitizeUrl),
+ ...getShareLink(child, options.sanitizeUrl),
title: child.title,
- type: child.type
+ type: child.type,
+ icon: child.getIcon(options.iconPackPrefixes),
+ excerpt: readLabel(child, "shareDescription") ?? toExcerpt(options.getText?.(child) ?? ""),
+ childCount: child.getVisibleChildNotes().length
}));
}
+/**
+ * Returns how the list of subpages is laid out: a grid, unless the note is a collection whose
+ * `#viewType` is `list`.
+ */
+export function getChildLinksLayout(note: ShareNote): "grid" | "list" {
+ return note.type === "book" && note.getLabelValue("viewType") === "list" ? "list" : "grid";
+}
+
/** What {@link getNavigationTree} needs besides the notes. */
export interface NavigationTreeOptions {
sanitizeUrl: (url: string) => string;
diff --git a/packages/share-theme/src/page/child_links.css b/packages/share-theme/src/page/child_links.css
index 7bb2b6a3e2..f4d4eb7334 100644
--- a/packages/share-theme/src/page/child_links.css
+++ b/packages/share-theme/src/page/child_links.css
@@ -1,56 +1,152 @@
-#childLinks,
-#childLinks ul,
-#childLinks li {
- list-style: none;
- display: flex;
- align-items: center;
-}
-
#childLinks {
- padding-top: 10px;
- flex-direction: column;
- gap: 0px;
- justify-content: center;
- border-top: 1px solid var(--background-highlight);
+ margin-top: 2rem;
+ padding-top: 1rem;
+ border-top: 1px solid var(--main-border-color);
}
.no-content + #childLinks {
+ margin-top: 0;
+ padding-top: 0;
border: 0;
}
+#childLinks > span {
+ display: block;
+ margin-bottom: 0.5rem;
+ font-size: 0.85em;
+ font-weight: 600;
+ letter-spacing: 0.3pt;
+ color: var(--right-pane-heading-color);
+}
+
#childLinks ul {
+ margin: 0;
padding: 0;
- gap: 10px;
+ list-style: none;
+}
+
+#childLinks a {
+ display: flex;
+ align-items: flex-start;
+ gap: 0.75rem;
+ color: var(--text-primary);
+ text-decoration: none;
+}
+
+#childLinks .tn-icon {
+ flex-shrink: 0;
+ font-size: 1.25rem;
+ line-height: 1.3;
+ color: var(--muted-text-color);
+}
+
+.child-link-text {
+ display: flex;
+ flex-direction: column;
+ min-width: 0;
+ line-height: 1.3;
+}
+
+.child-link-title {
+ font-weight: 500;
+ color: var(--text-link);
+ overflow-wrap: break-word;
+}
+
+.child-link-excerpt,
+.child-link-count {
+ margin-top: 0.25rem;
+ font-size: 0.9em;
+ color: var(--muted-text-color);
+}
+
+.child-link-count {
+ font-size: 0.8em;
+}
+
+#childLinks.list ul {
+ display: flex;
+ flex-direction: column;
+ gap: 2px;
+}
+
+#childLinks.list a {
+ padding: 0.5rem 0.75rem;
+ border-radius: 6px;
+ transition: background-color 150ms ease-in;
+}
+
+#childLinks.list a:hover {
+ background: var(--right-pane-item-hover-background);
+}
+
+#childLinks.grid ul {
+ display: flex;
flex-wrap: wrap;
- justify-content: center;
+ gap: 10px;
}
-#childLinks li {
- padding: 0;
- background: var(--background-highlight);
- border-radius: 12px;
+#childLinks.grid li {
+ display: flex;
+ flex: 1 0 300px;
}
-#childLinks li a {
- padding: 2px 12px;
- background: var(--background-highlight);
- border-radius: 12px;
- transform: translateY(0);
- transition: transform 200ms ease, background-color 200ms ease, color 200ms ease;
+#childLinks.grid a {
+ display: grid;
+ grid-template-columns: auto 1fr;
+ grid-template-rows: minmax(calc(32px + 1rem), auto);
+ align-content: start;
+ flex: 1;
+ gap: 0;
+ padding-bottom: 1rem;
+ border-radius: 8px;
+ background: var(--card-background-color);
+ transition: background-color 200ms ease-out;
}
-#childLinks li a:hover {
- background: var(--background-active);
- color: var(--background-secondary);
- text-decoration: none;
- transform: translateY(-2px);
+#childLinks.grid a:hover {
+ background-color: var(--card-background-hover-color);
}
-#childLinks.grid li a {
- padding: 50px;
- color: var(--text-primary);
+#childLinks.grid a:active {
+ transform: scale(0.98);
}
-#childLinks.grid li a:hover {
- transform: translateY(-5px);
-}
\ No newline at end of file
+#childLinks.grid .child-link-text {
+ display: contents;
+}
+
+#childLinks.grid .tn-icon {
+ align-self: center;
+ padding: 0 8px 0 1rem;
+ font-size: 1.2em;
+ color: inherit;
+}
+
+#childLinks.grid .child-link-title {
+ align-self: center;
+ padding: 0.5rem 1rem 0.5rem 0;
+ color: inherit;
+}
+
+#childLinks.grid :is(.child-link-excerpt, .child-link-count) {
+ grid-column: 1 / -1;
+ margin: 0;
+ padding: 0 1rem;
+ font-size: 0.8rem;
+}
+
+#childLinks.grid .child-link-excerpt {
+ padding-top: 0.5rem;
+ color: inherit;
+}
+
+#childLinks.grid .child-link-count {
+ margin-top: 0.25rem;
+}
+
+@media (max-width: 48em) {
+ #childLinks.grid li {
+ flex-basis: 150px;
+ }
+}
diff --git a/packages/share-theme/src/templates/page.ejs b/packages/share-theme/src/templates/page.ejs
index afe8a7dc0d..11bb4cd8dd 100644
--- a/packages/share-theme/src/templates/page.ejs
+++ b/packages/share-theme/src/templates/page.ejs
@@ -84,7 +84,7 @@
</div>
<% if (childLinks.length) { %>
- <nav id="childLinks" class="<% if (isEmpty) { %>grid<% } else { %>list<% } %>">
+ <nav id="childLinks" class="<%= childLinksLayout %>">
<% if (!isEmpty) { %>
<span><%= t("share_theme.subpages") %></span>
<% } %>
@@ -92,7 +92,18 @@
<ul>
<% for (const child of childLinks) { %>
<li>
- <a class="type-<%= child.type %> no-arrow" href="<%= child.href %>"<% if (child.isExternal) { %> target="_blank" rel="noopener noreferrer"<% } %>><%= child.title %></a>
+ <a class="type-<%= child.type %> no-arrow" href="<%= child.href %>"<% if (child.isExternal) { %> target="_blank" rel="noopener noreferrer"<% } %>>
+ <span class="tn-icon <%= child.icon %>" aria-hidden="true"></span>
+ <span class="child-link-text">
+ <span class="child-link-title"><%= child.title %></span>
+ <% if (child.excerpt) { %>
+ <span class="child-link-excerpt"><%= child.excerpt %></span>
+ <% } %>
+ <% if (child.childCount) { %>
+ <span class="child-link-count"><%= t("share_theme.subpage-count", { count: child.childCount }) %></span>
+ <% } %>
+ </span>
+ </a>
</li>
<% } %>
</ul>
diff --git a/packages/trilium-core/src/share/content_renderer.spec.ts b/packages/trilium-core/src/share/content_renderer.spec.ts
index 332d3f0f9f..690f7f4a72 100644
--- a/packages/trilium-core/src/share/content_renderer.spec.ts
+++ b/packages/trilium-core/src/share/content_renderer.spec.ts
@@ -1,6 +1,6 @@
import { trimIndentation } from "@triliumnext/commons";
import {
- getChildLinks, getContentClasses, getHtmlSnippets, getPageHead, getPrevNextLinks, getSiteLogo
+ getChildLinks, getChildLinksLayout, getContentClasses, getHtmlSnippets, getPageHead, getPrevNextLinks, getSiteLogo
} from "@triliumnext/share-theme/model/page";
import ejs from "ejs";
import { t } from "i18next";
@@ -447,7 +447,12 @@ describe("content_renderer", () => {
id: shareRoot.SHARE_ROOT_NOTE_ID,
children: [
{ id: "emptyBook", type: "book", content: "" },
- { id: "fullBook", type: "book", content: "", children: [ { id: "bookChild", title: "Child" } ] }
+ {
+ id: "fullBook",
+ type: "book",
+ content: "",
+ children: [ { id: "bookChild", title: "Child", content: "" } ]
+ }
]
});
const [ emptyBook, fullBook ] = shareRootNote.getChildNotes()
@@ -455,8 +460,49 @@ describe("content_renderer", () => {
expect(emptyBook.querySelector("#childLinks") === null).toBe(true);
expect(emptyBook.querySelector("#content")?.classList.contains("no-content")).toBe(true);
- expect(fullBook.querySelectorAll("#childLinks a").map((link) => link.text))
+ expect(fullBook.querySelectorAll("#childLinks .child-link-title").map((link) => link.text))
.toStrictEqual([ "Child" ]);
+ expect(fullBook.querySelector("#childLinks")?.classList.contains("grid")).toBe(true);
+ });
+
+ it("describes each subpage by its icon, the start of its text and its own subpages", () => {
+ const shareRootNote = buildShareNote({
+ id: shareRoot.SHARE_ROOT_NOTE_ID,
+ children: [{
+ id: "listParent",
+ content: "<p>Parent</p>",
+ children: [
+ {
+ "id": "readable",
+ "title": "Readable",
+ "#iconClass": "bx bx-rocket",
+ "content": "<h2>Heading</h2><p>First &amp; <b>bold</b>.</p><p>Second.</p>",
+ "children": [ { id: "grandchild", content: "" } ]
+ },
+ { id: "locked", content: "<p>Secret</p>", isProtected: true },
+ { "id": "guarded", "content": "<p>Guarded</p>", "#shareCredentials": "u:p" },
+ { id: "drawing", type: "canvas", content: "{}" },
+ { id: "bytes", content: Buffer.from("<p>Bytes</p>") }
+ ]
+ }]
+ });
+
+ const page = parse(String(renderNoteContent(shareRootNote.getChildNotes()[0],
+ (note) => note.getCredentials().length === 0)));
+ const items = page.querySelectorAll("#childLinks li").map((item) => ({
+ icon: item.querySelector(".tn-icon")?.classList.contains("bx-rocket"),
+ excerpt: item.querySelector(".child-link-excerpt")?.text,
+ count: item.querySelector(".child-link-count")?.text
+ }));
+
+ expect(page.querySelector("#childLinks")?.classList.contains("grid")).toBe(true);
+ expect(items).toStrictEqual([
+ { icon: true, excerpt: "First & bold. Second.", count: t("share_theme.subpage-count", { count: 1 }) },
+ { icon: false, excerpt: undefined, count: undefined },
+ { icon: false, excerpt: undefined, count: undefined },
+ { icon: false, excerpt: undefined, count: undefined },
+ { icon: false, excerpt: undefined, count: undefined }
+ ]);
});
it("keeps the alt text of an image on the page", () => {
@@ -1305,29 +1351,29 @@ describe("content_renderer", () => {
});
const anchors = renderPageAnchors("pageParent");
- const bothLabels = anchors.find((a) => a.textContent === "Both labels");
+ const bothLabels = anchors.find((a) => a.textContent.trim() === "Both labels");
expect(bothLabels?.getAttribute("href")).toBe("https://example.com/other");
expect(bothLabels?.getAttribute("target")).toBe("_blank");
- const external = anchors.find((a) => a.textContent === "External");
+ const external = anchors.find((a) => a.textContent.trim() === "External");
expect(external?.getAttribute("href")).toBe("https://example.com/page");
expect(external?.getAttribute("target")).toBe("_blank");
expect(external?.getAttribute("rel")).toBe("noopener noreferrer");
expect(Object.keys(external?.attributes ?? {}).sort())
.toEqual([ "class", "href", "rel", "target" ]);
- const internal = anchors.find((a) => a.textContent === "Internal");
+ const internal = anchors.find((a) => a.textContent.trim() === "Internal");
expect(internal?.getAttribute("href")).toBe("./pageInternal");
expect(Object.keys(internal?.attributes ?? {}).sort()).toEqual([ "class", "href" ]);
- const twoUrls = anchors.find((a) => a.textContent === "Two URLs");
+ const twoUrls = anchors.find((a) => a.textContent.trim() === "Two URLs");
expect(twoUrls?.getAttribute("href")).toBe("https://example.com/documented");
const whitespaceWithLegacy = anchors
- .find((a) => a.textContent === "Whitespace with legacy");
+ .find((a) => a.textContent.trim() === "Whitespace with legacy");
expect(whitespaceWithLegacy?.getAttribute("href")).toBe("https://example.com/legacy2");
- const whitespaceOnly = anchors.find((a) => a.textContent === "Whitespace only");
+ const whitespaceOnly = anchors.find((a) => a.textContent.trim() === "Whitespace only");
expect(whitespaceOnly?.getAttribute("href")).toBe("./pageWhitespaceOnly");
expect(Object.keys(whitespaceOnly?.attributes ?? {}).sort())
.toEqual([ "class", "href" ]);
@@ -1344,7 +1390,7 @@ describe("content_renderer", () => {
]
});
- const titles = renderPageAnchors("bookParent").map((a) => a.textContent);
+ const titles = renderPageAnchors("bookParent").map((a) => a.textContent.trim());
expect(titles).toEqual([ "Visible" ]);
});
@@ -1379,7 +1425,8 @@ describe("content_renderer", () => {
logo: getSiteLogo(note, { sanitizeUrl: sanitize.sanitizeUrl, image: null }),
prevNext: getPrevNextLinks(note, note),
navigation: [],
- childLinks: getChildLinks(note, sanitize.sanitizeUrl),
+ childLinks: getChildLinks(note, { sanitizeUrl: sanitize.sanitizeUrl }),
+ childLinksLayout: getChildLinksLayout(note),
contentClasses: getContentClasses(note, isEmpty),
language: { page: { lang: "en", dir: "ltr" }, content: null },
lastUpdated: null,
diff --git a/packages/trilium-core/src/share/content_renderer.ts b/packages/trilium-core/src/share/content_renderer.ts
index 170187564c..7bf41c901c 100644
--- a/packages/trilium-core/src/share/content_renderer.ts
+++ b/packages/trilium-core/src/share/content_renderer.ts
@@ -8,7 +8,7 @@ import { renderToHtml as renderMarkdownToHtml } from "@triliumnext/commons/src/l
import { renderSpreadsheetToHtml } from "@triliumnext/commons/src/lib/spreadsheet/render_to_html.js";
import { getLanguage, highlight, highlightAuto, syncMimeTypes } from "@triliumnext/highlightjs";
import {
- getChildLinks, getContentClasses, getHtmlSnippets, getLastUpdated, getNavigationTree, getPageHead, getPageLanguages,
+ getChildLinks, getChildLinksLayout, getContentClasses, getHtmlSnippets, getLastUpdated, getNavigationTree, getPageHead, getPageLanguages,
getPrevNextLinks, getShareLink, getSiteAncestorIds, getSiteLogo, getTableOfContents,
type PageHeading
} from "@triliumnext/share-theme/model/page";
@@ -227,7 +227,12 @@ function renderNoteContentInternal(note: SNote | BNote, renderArgs: RenderArgs)
sanitizeUrl: sanitize.sanitizeUrl,
iconPackPrefixes: renderArgs.iconPackSupportedPrefixes
}),
- childLinks: getChildLinks(note, sanitize.sanitizeUrl),
+ childLinks: getChildLinks(note, {
+ sanitizeUrl: sanitize.sanitizeUrl,
+ iconPackPrefixes: renderArgs.iconPackSupportedPrefixes,
+ getText: (child) => getExcerptSource(child as SNote | BNote, renderArgs.canAccessEmbed)
+ }),
+ childLinksLayout: getChildLinksLayout(note),
contentClasses: getContentClasses(note, isEmpty),
language: getPageLanguages(note, {
displayLanguage,
@@ -345,6 +350,30 @@ function getShareAssetPath() {
return utils.isDev() ? `${assetUrlFragment}/src` : `../${assetUrlFragment}`;
}
+/**
+ * Returns the text of the paragraphs of a text note, which the excerpt of its entry in its parent's
+ * list of subpages starts from, or `null` for a note of another type, a protected one, or one the
+ * caller is not allowed to read. Only the start of the note is parsed, enough for any excerpt.
+ */
+function getExcerptSource(note: SNote | BNote, canAccessEmbed?: CanAccessEmbed) {
+ // `canAccessEmbed` is only given with a shaca note, whose children are shaca notes too.
+ if (note.type !== "text" || note.isProtected || canAccessEmbed?.(note as SNote) === false) {
+ return null;
+ }
+
+ const content = note.getContent();
+ if (typeof content !== "string") {
+ return null;
+ }
+
+ return parse(content.slice(0, EXCERPT_SOURCE_LENGTH)).querySelectorAll("p")
+ .map((paragraph) => paragraph.text)
+ .join(" ");
+}
+
+/** How much of a text note's HTML {@link getExcerptSource} parses. */
+const EXCERPT_SOURCE_LENGTH = 10_000;
+
/**
* Decides whether the caller is allowed to read a note that an embed pulls in. The share routes
* pass their `shareCredentials` check here so that an embed cannot hand out a note the same