Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(share): list a subpage's own subpages on its card, as the app does
Elian Doran5cfe2443d2f4
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.htmlindex 13231617ff..7e5548e4cf 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@@ -23,9 +23,10 @@ class="image"><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>+ as in the application. Each card shows the icon of the subpage and the+ start of its text (or its <code>#shareDescription</code>), or, for a subpage+ without text, links to its own first subpages. The cards can be hidden+ with <code>#hideChildrenOverview</code>.</li><li>Displaying the date of the last update of the note, written out in theapplication's language.</li><li>Marking the page with the application's language and its text direction,@@ -115,8 +116,9 @@ class="image"><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>+ the collection is <em>List</em>. Each shows its icon and the start of its+ text (or its <code>#shareDescription</code>), or, for a child note without+ text, links to its own first child notes.</li></ul></td><td>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.htmlindex 31a4696900..464cee06f9 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@@ -100,9 +100,10 @@</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> (itsicon 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>+ the start of its text, or <code>null</code>) and <code>children</code> (when+ it has no excerpt, its first ten visible children, each with <code>title</code>, <code>href</code>, <code>isExternal</code> and <code>icon</code>).+ 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>diff --git a/apps/server/src/assets/translations/en/server.json b/apps/server/src/assets/translations/en/server.jsonindex c020ad43d8..477cd8c146 100644--- a/apps/server/src/assets/translations/en/server.json+++ b/apps/server/src/assets/translations/en/server.json@@ -468,9 +468,7 @@"heading-link": "Link to This Section","previous": "Previous","next": "Next",- "page-navigation": "Previous and next pages",- "subpage-count_one": "{{count}} subpage",- "subpage-count_other": "{{count}} subpages"+ "page-navigation": "Previous and next pages"},"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.mdindex e94924e61e..53f8771045 100644--- a/docs/User Guide/User Guide/Advanced Usage/Sharing.md+++ b/docs/User Guide/User Guide/Advanced Usage/Sharing.md@@ -11,7 +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`.+* 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 and the start of its text (or its `#shareDescription`), or, for a subpage without text, links to its own first subpages. 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.@@ -63,7 +63,7 @@ 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 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 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 and the start of its text (or its <code>#shareDescription</code>), or, for a child note without text, links to its own first child notes.</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>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.mdindex b99e1d13b7..d602cdc77c 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,7 @@ 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`, `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`. |+| `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 `children` (when it has no excerpt, its first ten visible children, each with `title`, `href`, `isExternal` and `icon`). 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. |diff --git a/packages/share-theme/src/model/page.spec.ts b/packages/share-theme/src/model/page.spec.tsindex f6918e1581..7e854b5e01 100644--- a/packages/share-theme/src/model/page.spec.ts+++ b/packages/share-theme/src/model/page.spec.ts@@ -432,25 +432,49 @@ describe("getContentClasses", () => {describe("getChildLinks", () => {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", () => {+ it("links to the visible children of a note, in order, with their icon and own children", () => {const parent = fakeNote({ noteId: "parent" });const x = addChild(parent, fakeNote({ noteId: "x", type: "book", icon: "bx bx-book" }));addChild(x, fakeNote({ noteId: "grandchild" }));+ addChild(x, fakeNote({ noteId: "far", labels: { shareExternal: "https://example.org" } }));addChild(parent, fakeNote({ noteId: "hidden" }), true);addChild(parent, fakeNote({ noteId: "y", labels: { shareExternal: "https://example.com" } }));expect(getChildLinks(parent, options)).toStrictEqual([{href: "./x-alias", isExternal: false, title: "x", type: "book",- icon: "bx bx-book pack", excerpt: null, childCount: 1+ icon: "bx bx-book pack", excerpt: null,+ children: [+ { href: "./grandchild-alias", isExternal: false, title: "grandchild", icon: "bx bx-note pack" },+ { href: "https://example.org", isExternal: true, title: "far", icon: "bx bx-note pack" }+ ]},{href: "https://example.com", isExternal: true, title: "y", type: "text",- icon: "bx bx-note pack", excerpt: null, childCount: 0+ icon: "bx bx-note pack", excerpt: null, children: []}]);});+ it("lists the first ten children of a child only when it has no excerpt, as the app does", () => {+ const parent = fakeNote({ noteId: "parent" });+ for (const noteId of [ "empty", "described" ]) {+ const child = addChild(parent, fakeNote({+ noteId,+ labels: noteId === "described" ? { shareDescription: "About it" } : {}+ }));+ for (let index = 0; index < 12; index++) {+ addChild(child, fakeNote({ noteId: `${noteId}-${index}` }));+ }+ }++ const [ empty, described ] = getChildLinks(parent, options);++ expect(empty.children.map((child) => child.title))+ .toStrictEqual(Array.from({ length: 10 }, (_, index) => `empty-${index}`));+ expect(described.children).toStrictEqual([]);+ });+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` }));diff --git a/packages/share-theme/src/model/page.ts b/packages/share-theme/src/model/page.tsindex 962a553192..27867637a7 100644--- a/packages/share-theme/src/model/page.ts+++ b/packages/share-theme/src/model/page.ts@@ -232,6 +232,9 @@ function readLabel(note: ShareNote, name: string) {const CHILD_LIST_NOTE_TYPES = [ "book", "text", "code" ];+/** The most children of a child its card lists, as many as the app's card does. */+const CHILD_PREVIEW_LENGTH = 10;+/** The most characters of a child's text its excerpt shows. */const EXCERPT_LENGTH = 160;@@ -382,8 +385,18 @@ export interface ChildLink extends ShareLink {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;+ /**+ * The first visible children of the child, which the app's card shows in place of a preview+ * when the child has none; empty when it has an excerpt.+ */+ children: ChildLinkChild[];+}++/** A child of a child of a page, as its card lists it. */+export interface ChildLinkChild extends ShareLink {+ title: string;+ /** The icon's CSS classes. */+ icon: string;}/** What {@link getChildLinks} needs besides the note. */@@ -408,14 +421,23 @@ export function getChildLinks(note: ShareNote, options: ChildLinksOptions): Chilreturn [];}- return note.getVisibleChildNotes().map((child) => ({- ...getShareLink(child, options.sanitizeUrl),- title: child.title,- type: child.type,- icon: child.getIcon(options.iconPackPrefixes),- excerpt: readLabel(child, "shareDescription") ?? toExcerpt(options.getText?.(child) ?? ""),- childCount: child.getVisibleChildNotes().length- }));+ return note.getVisibleChildNotes().map((child) => {+ const excerpt = readLabel(child, "shareDescription")+ ?? toExcerpt(options.getText?.(child) ?? "");+ const children = excerpt ? [] : child.getVisibleChildNotes().slice(0, CHILD_PREVIEW_LENGTH);+ return {+ ...getShareLink(child, options.sanitizeUrl),+ title: child.title,+ type: child.type,+ icon: child.getIcon(options.iconPackPrefixes),+ excerpt,+ children: children.map((grandchild) => ({+ ...getShareLink(grandchild, options.sanitizeUrl),+ title: grandchild.title,+ icon: grandchild.getIcon(options.iconPackPrefixes)+ }))+ };+ });}/**diff --git a/packages/share-theme/src/page/child_links.css b/packages/share-theme/src/page/child_links.cssindex f4d4eb7334..3c14896a87 100644--- a/packages/share-theme/src/page/child_links.css+++ b/packages/share-theme/src/page/child_links.css@@ -19,130 +19,122 @@color: var(--right-pane-heading-color);}-#childLinks ul {+#childLinks > ul {margin: 0;padding: 0;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);+#childLinks li {+ position: relative;+ display: grid;+ grid-template-columns: auto 1fr;+ align-items: center;+ column-gap: 8px;}-.child-link-text {- display: flex;- flex-direction: column;- min-width: 0;- line-height: 1.3;+#childLinks li > .tn-icon {+ font-size: 1.2em;}.child-link-title {font-weight: 500;- color: var(--text-link);+ color: var(--text-primary);overflow-wrap: break-word;}+.child-link-title:hover {+ text-decoration: none;+}++/* The title's link covers the whole entry; the links to the child's own children sit above it. */+.child-link-title::after {+ content: "";+ position: absolute;+ inset: 0;+}++.child-link-children a {+ position: relative;+}+.child-link-excerpt,-.child-link-count {- margin-top: 0.25rem;- font-size: 0.9em;- color: var(--muted-text-color);+.child-link-children {+ grid-column: 1 / -1;+ margin: 0;+ font-size: 0.8rem;+}++.child-link-children {+ display: flex;+ flex-direction: column;+ align-items: flex-start;}-.child-link-count {- font-size: 0.8em;+.child-link-children a {+ display: inline-block;+ max-width: 100%;+ overflow: hidden;+ text-overflow: ellipsis;+ white-space: nowrap;}-#childLinks.list ul {+#childLinks.list > ul {display: flex;flex-direction: column;gap: 2px;}-#childLinks.list a {+#childLinks.list li {padding: 0.5rem 0.75rem;border-radius: 6px;transition: background-color 150ms ease-in;}-#childLinks.list a:hover {+#childLinks.list li:hover {background: var(--right-pane-item-hover-background);}-#childLinks.grid ul {+#childLinks.list li > .tn-icon,+#childLinks.list .child-link-excerpt {+ color: var(--muted-text-color);+}++#childLinks.list :is(.child-link-excerpt, .child-link-children) {+ grid-column: 2;+ margin-top: 0.25rem;+}++#childLinks.grid > ul {display: flex;flex-wrap: wrap;gap: 10px;}#childLinks.grid li {- display: flex;flex: 1 0 300px;-}--#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;+ padding: 0 1rem 1rem;border-radius: 8px;background: var(--card-background-color);transition: background-color 200ms ease-out;}-#childLinks.grid a:hover {+#childLinks.grid li:hover {background-color: var(--card-background-hover-color);}-#childLinks.grid a:active {+#childLinks.grid li:not(:has(.child-link-children a:active)):active {transform: scale(0.98);}-#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;+ padding-block: 0.5rem;}-#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 {+#childLinks.grid :is(.child-link-excerpt, .child-link-children) {padding-top: 0.5rem;- color: inherit;-}--#childLinks.grid .child-link-count {- margin-top: 0.25rem;}@media (max-width: 48em) {diff --git a/packages/share-theme/src/templates/page.ejs b/packages/share-theme/src/templates/page.ejsindex 11bb4cd8dd..04c7d30bd0 100644--- a/packages/share-theme/src/templates/page.ejs+++ b/packages/share-theme/src/templates/page.ejs@@ -92,18 +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"<% } %>>- <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>+ <span class="tn-icon <%= child.icon %>" aria-hidden="true"></span>+ <a class="child-link-title type-<%= child.type %> no-arrow" href="<%= child.href %>"<% if (child.isExternal) { %> target="_blank" rel="noopener noreferrer"<% } %>><%= child.title %></a>+ <% if (child.excerpt) { %>+ <p class="child-link-excerpt"><%= child.excerpt %></p>+ <% } %>+ <% if (child.children.length) { %>+ <div class="child-link-children ck-content">+ <% for (const grandchild of child.children) { %>+ <a class="reference-link" href="<%= grandchild.href %>"<% if (grandchild.isExternal) { %> target="_blank" rel="noopener noreferrer"<% } %>><span class="tn-icon <%= grandchild.icon %>" aria-hidden="true"></span><%= grandchild.title %></a><% } %>- <% if (child.childCount) { %>- <span class="child-link-count"><%= t("share_theme.subpage-count", { count: child.childCount }) %></span>- <% } %>- </span>- </a>+ </div>+ <% } %></li><% } %></ul>diff --git a/packages/trilium-core/src/share/content_renderer.spec.ts b/packages/trilium-core/src/share/content_renderer.spec.tsindex 85e13e56e6..d6321f9826 100644--- a/packages/trilium-core/src/share/content_renderer.spec.ts+++ b/packages/trilium-core/src/share/content_renderer.spec.ts@@ -465,7 +465,7 @@ describe("content_renderer", () => {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", () => {+ it("describes each subpage by its icon and the start of its text, or else its own subpages", () => {const shareRootNote = buildShareNote({id: shareRoot.SHARE_ROOT_NOTE_ID,children: [{@@ -477,7 +477,15 @@ describe("content_renderer", () => {"title": "Readable","#iconClass": "bx bx-rocket","content": "<h2>Heading</h2><p>First & <b>bold</b>.</p><p>Second.</p>",- "children": [ { id: "grandchild", content: "" } ]+ "children": [ { id: "hiddenByExcerpt", content: "" } ]+ },+ {+ id: "folder",+ content: "",+ children: [+ { id: "first", title: "First", content: "" },+ { "id": "second", "title": "Second", "content": "", "#iconClass": "bx bx-star" }+ ]},{ id: "locked", content: "<p>Secret</p>", isProtected: true },{ "id": "guarded", "content": "<p>Guarded</p>", "#shareCredentials": "u:p" },@@ -489,19 +497,25 @@ describe("content_renderer", () => {const page = parse(String(renderNoteContent(shareRootNote.getChildNotes()[0],(note) => note.getCredentials().length === 0)));- const items = page.querySelectorAll("#childLinks li").map((item) => ({+ const items = page.querySelectorAll("#childLinks > ul > 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+ children: item.querySelectorAll(".child-link-children a.reference-link").map((link) =>+ [ link.getAttribute("href"), link.text, link.querySelector(".tn-icon")?.classList.contains("bx-star") ])}));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 }+ { icon: true, excerpt: "First & bold. Second.", children: [] },+ {+ icon: false,+ excerpt: undefined,+ children: [ [ "./first", "First", false ], [ "./second", "Second", true ] ]+ },+ { icon: false, excerpt: undefined, children: [] },+ { icon: false, excerpt: undefined, children: [] },+ { icon: false, excerpt: undefined, children: [] },+ { icon: false, excerpt: undefined, children: [] }]);});