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): hide the children of a #subtreeHidden note from the shared tree
Elian Dorand7cdee8b4b79
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 fa529335f0..3e737407b2 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,6 +23,11 @@ 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>Notes that hide their children from the tree with <code>#subtreeHidden</code> (such+ as a <a class="reference-link" href="#root/_help_CtBQqbwXDx1w">Kanban Board</a>)+ show a badge counting them in place of listing them. The page of one of+ those children shows only that child below its parent, and the previous+ and next buttons skip them.</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 and thestart of its text (or its <code>#shareDescription</code>), or, for a subpage@@ -138,8 +143,8 @@ class="image">the visitor switches it. Until it is drawn, and in browsers with JavaScriptdisabled, the image saved when the note was last edited is shown instead.</li><li>The source of the diagram can be expanded below it.</li>- <li>The diagram can be zoomed with the <span class="tn-icon bx bx-minus-circle"></span> and <span- class="tn-icon bx bx-plus-circle"></span> buttons in its bottom corner,+ <li>The diagram can be zoomed with the <span class="tn-icon bx bx-minus-circle"></span> and <span+ class="tn-icon bx bx-plus-circle"></span> buttons in its bottom corner,between 50% and 1000% of its width on the page. Clicking the percentagebetween them, or double-clicking the diagram, shows it whole again. Oncezoomed in, the diagram can be panned by dragging it, and on touch screensdiff --git a/apps/server/src/assets/translations/en/server.json b/apps/server/src/assets/translations/en/server.jsonindex 214986ac0f..89772ccf2a 100644--- a/apps/server/src/assets/translations/en/server.json+++ b/apps/server/src/assets/translations/en/server.json@@ -460,6 +460,8 @@"on-this-page": "On This Page","expand": "Expand","subpages-of": "Subpages of {{- title}}",+ "hidden-subpages_one": "{{count}} subpage that is hidden from the tree",+ "hidden-subpages_other": "{{count}} subpages that are hidden from the tree","toggle-navigation": "Toggle Navigation","toggle-toc": "Toggle Table of Contents","logo-alt": "Logo",diff --git a/docs/User Guide/User Guide/Advanced Usage/Sharing.md b/docs/User Guide/User Guide/Advanced Usage/Sharing.mdindex 70f83aa106..ef8f0ad381 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).+* Notes that hide their children from the tree with `#subtreeHidden` (such as a <a class="reference-link" href="../Collections/Kanban%20Board.md">Kanban Board</a>) show a badge counting them in place of listing them. The page of one of those children shows only that child below its parent, and the previous and next buttons skip them.* 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.diff --git a/packages/share-theme/src/model/page.spec.ts b/packages/share-theme/src/model/page.spec.tsindex 6088e58e29..9c5e23c2c5 100644--- a/packages/share-theme/src/model/page.spec.ts+++ b/packages/share-theme/src/model/page.spec.ts@@ -257,6 +257,29 @@ describe("getPrevNextLinks", () => {expect(links(elsewhere)).toStrictEqual([ null, null ]);});+ it("leaves the children of a note hiding its subtree out of the order, and links none of them", () => {+ // board ─┬─ card1 ── detail+ // └─ card2+ const boardSite = fakeNote({ noteId: "boardSite" });+ const intro = addChild(boardSite, fakeNote({ noteId: "intro" }));+ const board = addChild(boardSite, fakeNote({ noteId: "board" }));+ const card1 = addChild(board, fakeNote({ noteId: "card1" }));+ const detail = addChild(card1, fakeNote({ noteId: "detail" }));+ addChild(board, fakeNote({ noteId: "card2" }));+ const outro = addChild(boardSite, fakeNote({ noteId: "outro" }));+ const isSubtreeHidden = (note: ShareNote) => note.noteId === "board";+ const boardLinks = (note: ShareNote) => {+ const { previous, next } = getPrevNextLinks(note, boardSite, isSubtreeHidden);+ return [ previous?.title ?? null, next?.title ?? null ];+ };++ expect(boardLinks(intro)).toStrictEqual([ "boardSite", "board" ]);+ expect(boardLinks(board)).toStrictEqual([ "intro", "outro" ]);+ expect(boardLinks(outro)).toStrictEqual([ "board", null ]);+ expect(boardLinks(card1)).toStrictEqual([ null, null ]);+ expect(boardLinks(detail)).toStrictEqual([ null, null ]);+ });+it("reads the parents of each note outside the site once, however many paths lead to it", () => {// Sixteen layers of two clones each, every clone a child of both clones of the layer above,// make 2^16 paths from `page` up to the share root outside the site.@@ -365,6 +388,28 @@ describe("getNavigationTree", () => {expect(outline(tree)).toStrictEqual([ [ "a", [ "a1" ] ], [ "b", [ "b1" ] ] ]);});+ it("counts the children of a note hiding its subtree in place of listing them, but the one shown", () => {+ // board ─┬─ card1 ── detail+ // └─ card2+ const boardSite = fakeNote({ noteId: "boardSite" });+ const board = addChild(boardSite, fakeNote({ noteId: "board" }));+ const card1 = addChild(board, fakeNote({ noteId: "card1" }));+ const detail = addChild(card1, fakeNote({ noteId: "detail" }));+ addChild(board, fakeNote({ noteId: "card2" }));+ const options = { sanitizeUrl: (url: string) => url, isSubtreeHidden: (note: ShareNote) => note.noteId === "board" };++ const onBoard = getNavigationTree(boardSite, board, [], options);+ expect(outline(onBoard)).toStrictEqual([ "board*+" ]);+ expect(onBoard[0].hiddenChildCount).toBe(2);++ const onDetail = getNavigationTree(boardSite, detail, [ "card1", "board" ], options);+ expect(outline(onDetail)).toStrictEqual([ [ "board+", [ [ "card1+", [ "detail*+" ] ] ] ] ]);+ expect(onDetail[0].hiddenChildCount).toBe(2);+ expect(onDetail[0].children[0].hiddenChildCount).toBe(0);+ expect(getNavigationTree(boardSite, card1, [ "board" ], { sanitizeUrl: (url: string) => url })[0]+ .hiddenChildCount).toBe(0);+ });+it("tells whether the page has an entry, which the site root and a hidden note do not", () => {const options = { sanitizeUrl: (url: string) => url };diff --git a/packages/share-theme/src/model/page.ts b/packages/share-theme/src/model/page.tsindex 56fade090a..0198574c42 100644--- a/packages/share-theme/src/model/page.ts+++ b/packages/share-theme/src/model/page.ts@@ -48,6 +48,8 @@ export interface NavigationItem extends ShareLink {isActive: boolean;/** Whether the entry is the page being shown or one of its ancestors. */isExpanded: boolean;+ /** How many children the note keeps out of the tree with `#subtreeHidden`, or 0. */+ hiddenChildCount: number;children: NavigationItem[];}@@ -472,6 +474,8 @@ export interface NavigationTreeOptions {sanitizeUrl: (url: string) => string;/** The prefixes of the icon packs available to the page, for the notes' icons. */iconPackPrefixes?: string[];+ /** Whether the note keeps its children out of the tree; none does by default. */+ isSubtreeHidden?: (note: ShareNote) => boolean;}/**@@ -479,6 +483,9 @@ export interface NavigationTreeOptions {* another as in the note tree. The entries of `activeNote` and of the notes in `ancestorIds` are* expanded. Of a cloned `activeNote`, only the entry below exactly the notes in `ancestorIds` is* the active one; their order and whether they include `siteRoot` do not matter.+ *+ * A note whose subtree is hidden lists none of its children, except the one leading to+ * `activeNote`, so the page being shown keeps its place in the tree.*/export function getNavigationTree(siteRoot: ShareNote, activeNote: ShareNote, ancestorIds: string[], options: NavigationTreeOptions@@ -488,17 +495,22 @@ export function getNavigationTree(const isActive = (note: ShareNote, path: string[]) => note.noteId === activeNote.noteId&& path.length === activeAncestorIds.size&& path.every((noteId) => activeAncestorIds.has(noteId));- const toItem = (note: ShareNote, path: string[]): NavigationItem => ({- ...getShareLink(note, options.sanitizeUrl),- noteId: note.noteId,- title: note.title,- type: note.type,- icon: note.getIcon(options.iconPackPrefixes),- isActive: isActive(note, path),- isExpanded: expandedIds.has(note.noteId),- children: note.getVisibleChildNotes()- .map((child) => toItem(child, [ ...path, note.noteId ]))- });+ const toItem = (note: ShareNote, path: string[]): NavigationItem => {+ const children = note.getVisibleChildNotes();+ const isHidden = children.length > 0 && !!options.isSubtreeHidden?.(note);+ return {+ ...getShareLink(note, options.sanitizeUrl),+ noteId: note.noteId,+ title: note.title,+ type: note.type,+ icon: note.getIcon(options.iconPackPrefixes),+ isActive: isActive(note, path),+ isExpanded: expandedIds.has(note.noteId),+ hiddenChildCount: isHidden ? children.length : 0,+ children: (isHidden ? children.filter((child) => expandedIds.has(child.noteId)) : children)+ .map((child) => toItem(child, [ ...path, note.noteId ]))+ };+ };return siteRoot.getVisibleChildNotes().map((note) => toItem(note, []));}@@ -527,18 +539,36 @@ export function getSiteAncestorIds(note: ShareNote, siteRoot: ShareNote) {/*** Returns the pages before and after `note` when the site starting at `siteRoot` is read in tree* order: a page, then its children, then its next sibling. A note in several places follows the- * first parent inside the site. A note hidden from the tree has neither link.+ * first parent inside the site. A note hidden from the tree has neither link. `isSubtreeHidden` names+ * the notes whose children are left out of that order; a page below one of them has neither link.*/-export function getPrevNextLinks(note: ShareNote, siteRoot: ShareNote) {- const previous = getPreviousPage(note, siteRoot);- const next = getNextPage(note, siteRoot);+export function getPrevNextLinks(+ note: ShareNote, siteRoot: ShareNote, isSubtreeHidden: (note: ShareNote) => boolean = () => false+) {+ if (isInHiddenSubtree(note, siteRoot, isSubtreeHidden)) {+ return { previous: null, next: null };+ }+ const getTreeChildren = (parent: ShareNote) => (isSubtreeHidden(parent) ? [] : parent.getVisibleChildNotes());+ const previous = getPreviousPage(note, siteRoot, getTreeChildren);+ const next = getNextPage(note, siteRoot, getTreeChildren);return {previous: previous && toPageLink(previous),next: next && toPageLink(next)};}-function getPreviousPage(note: ShareNote, siteRoot: ShareNote) {+/** Whether a note between `note` and `siteRoot` keeps its children out of the tree. */+function isInHiddenSubtree(note: ShareNote, siteRoot: ShareNote, isSubtreeHidden: (note: ShareNote) => boolean) {+ for (let position = getSitePosition(note, siteRoot); position;+ position = getSitePosition(position.parent, siteRoot)) {+ if (isSubtreeHidden(position.parent)) {+ return true;+ }+ }+ return false;+}++function getPreviousPage(note: ShareNote, siteRoot: ShareNote, getTreeChildren: (note: ShareNote) => ShareNote[]) {const position = getSitePosition(note, siteRoot);if (!position) {return null;@@ -548,19 +578,19 @@ function getPreviousPage(note: ShareNote, siteRoot: ShareNote) {}let previous = position.siblings[position.index - 1];- for (let children = previous.getVisibleChildNotes(); children.length;- children = previous.getVisibleChildNotes()) {+ for (let children = getTreeChildren(previous); children.length;+ children = getTreeChildren(previous)) {previous = children[children.length - 1];}return previous;}-function getNextPage(note: ShareNote, siteRoot: ShareNote) {+function getNextPage(note: ShareNote, siteRoot: ShareNote, getTreeChildren: (note: ShareNote) => ShareNote[]) {const notePosition = getSitePosition(note, siteRoot);if (!notePosition && note.noteId !== siteRoot.noteId) {return null;}- const firstChild = note.getVisibleChildNotes()[0];+ const firstChild = getTreeChildren(note)[0];if (firstChild) {return firstChild;}diff --git a/packages/share-theme/src/page/navigation.css b/packages/share-theme/src/page/navigation.cssindex 18fb472dc5..a49428899b 100644--- a/packages/share-theme/src/page/navigation.css+++ b/packages/share-theme/src/page/navigation.css@@ -111,6 +111,18 @@padding-inline-start: 4px;}+#menu .tree-item-hidden-count {+ flex-shrink: 0;+ align-self: center;+ line-height: 1.5;+ margin-inline-end: 8px;+ padding: 0.1em 0.6em;+ border-radius: 0.5em;+ background: var(--background-highlight);+ color: var(--text-menu);+ font-size: 0.7rem;+}+#menu a {display: flex;flex: 1;diff --git a/packages/share-theme/src/templates/tree_item.ejs b/packages/share-theme/src/templates/tree_item.ejsindex 2f1ff9e2b2..2a12fc1c70 100644--- a/packages/share-theme/src/templates/tree_item.ejs+++ b/packages/share-theme/src/templates/tree_item.ejs@@ -6,6 +6,7 @@<a class="type-<%= item.type %> no-arrow<%= item.isActive ? " active" : "" %>" href="<%= item.href %>"<% if (item.isExternal) { %> target="_blank" rel="noopener noreferrer"<% } %>><span class="tn-icon <%= item.icon %>" aria-hidden="true"></span><span class="tree-item-title"><%= item.title %></span></a>+ <% if (item.hiddenChildCount) { %><span class="tree-item-hidden-count" title="<%= t("share_theme.hidden-subpages", { count: item.hiddenChildCount }) %>"><%= item.hiddenChildCount %></span><% } %></div><% if (item.isActive && headings.length > 1) { %><nav class="tree-toc" aria-label="<%= t("share_theme.on-this-page") %>">diff --git a/packages/trilium-core/src/share/content_renderer.spec.ts b/packages/trilium-core/src/share/content_renderer.spec.tsindex 4731bc7667..cc61c7f8c8 100644--- a/packages/trilium-core/src/share/content_renderer.spec.ts+++ b/packages/trilium-core/src/share/content_renderer.spec.ts@@ -1740,6 +1740,39 @@ describe("content_renderer pages", () => {expect(textPage.querySelector("#content > h1#title") === null).toBe(false);});+ it("counts a board's cards in the tree in place of listing them, its template hiding them", () => {+ buildNote({ id: "_template_testBoard", title: "Board template", "#subtreeHidden": "" });+ const board = buildSitePage({+ id: "hidingBoard",+ title: "Board",+ content: "<p>Board</p>",+ "~template": "_template_testBoard",+ children: [+ { id: "boardCard1", title: "Card 1", content: "<p>1</p>" },+ { id: "boardCard2", title: "Card 2", content: "<p>2</p>" }+ ]+ });+ const page = parse(String(renderNoteContent(board)));+ const entry = page.querySelector("#menu li[data-note-id=hidingBoard]");+ expect(entry?.querySelector(".tree-item-hidden-count")?.textContent).toBe("2");+ expect(entry?.querySelector(".tree-item-hidden-count")?.getAttribute("title"))+ .toBe("2 subpages that are hidden from the tree");+ expect(entry?.querySelector("li") === null).toBe(true);++ const card = parse(String(renderNoteContent(shaca.getNote("boardCard1"))));+ expect(card.querySelectorAll("#menu li[data-note-id=hidingBoard] li").map((item) =>+ item.getAttribute("data-note-id"))).toEqual([ "boardCard1" ]);++ const shown = buildSitePage({+ id: "showingBoard", content: "<p>Board</p>", "#subtreeHidden": "false",+ "~template": "_template_testBoard",+ children: [ { id: "shownCard", title: "Card", content: "<p>Card</p>" } ]+ });+ const shownPage = parse(String(renderNoteContent(shown)));+ expect(shownPage.querySelector("#menu .tree-item-hidden-count") === null).toBe(true);+ expect(shownPage.querySelector("#menu li[data-note-id=shownCard]") === null).toBe(false);+ });+it("leaves content that is not text as it is, and empty text as empty", () => {const binary = Buffer.from("<p>x</p>");for (const type of [ "text", "mermaid", "code" ]) {diff --git a/packages/trilium-core/src/share/content_renderer.ts b/packages/trilium-core/src/share/content_renderer.tsindex 5ccf847b85..c91e2f7e46 100644--- a/packages/trilium-core/src/share/content_renderer.ts+++ b/packages/trilium-core/src/share/content_renderer.ts@@ -10,7 +10,7 @@ import { getLanguage, highlight, highlightAuto, syncMimeTypes } from "@triliumneimport {getChildLinks, getChildLinksLayout, getContentClasses, getHtmlSnippets, getLastUpdated, getNavigationTree, getPageHead, getPageLanguages,getPrevNextLinks, getShareLink, getSiteAncestorIds, getSiteLogo, getTableOfContents, hasActiveItem,- type NavigationItem, type PageHeading+ type NavigationItem, type PageHeading, type ShareNote} from "@triliumnext/share-theme/model/page";import ejs from "ejs";import escapeHtml from "escape-html";@@ -262,7 +262,8 @@ function renderNoteContentInternal(note: SNote | BNote, renderArgs: RenderArgs)});const navigation = getNavigationTree(siteRoot, note, renderArgs.ancestors, {sanitizeUrl: sanitize.sanitizeUrl,- iconPackPrefixes: renderArgs.iconPackSupportedPrefixes+ iconPackPrefixes: renderArgs.iconPackSupportedPrefixes,+ isSubtreeHidden});const childLinks = isFullHeight ? [] : getChildLinks(note, {sanitizeUrl: sanitize.sanitizeUrl,@@ -287,7 +288,7 @@ function renderNoteContentInternal(note: SNote | BNote, renderArgs: RenderArgs)head: getPageHead(note, siteRoot),snippets: getHtmlSnippets(note),logo,- prevNext: isFullHeight ? { previous: null, next: null } : getPrevNextLinks(note, siteRoot),+ prevNext: isFullHeight ? { previous: null, next: null } : getPrevNextLinks(note, siteRoot, isSubtreeHidden),navigation,childLinks,childLinksLayout: getChildLinksLayout(note),@@ -919,6 +920,20 @@ function getViewType(note: SNote) {return null;}+/**+ * Returns whether the note keeps its children out of the tree with `#subtreeHidden`, which a board+ * has from its built-in template. Shaca holds only shared notes, so becca answers for the template.+ */+function isSubtreeHidden(note: ShareNote) {+ const shared = note as SNote | BNote;+ if (shared instanceof BNote || shared.hasLabel("subtreeHidden")) {+ return shared.isLabelTruthy("subtreeHidden");+ }++ return shared.getOwnedRelations("template").some((relation) => relation.value.startsWith("_template_")+ && !!becca.getNote(relation.value)?.isLabelTruthy("subtreeHidden"));+}+/*** Renders a collection the share theme shows with the app's own view, through its note list: an* element to mount it into, beside the notes and the display options it starts from.