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
fix(help-meta): resolve cloned help notes to their primary occurrence
Elian Doran9c3cb245575e
diff --git a/apps/edit-docs/src/help_meta_generator.spec.ts b/apps/edit-docs/src/help_meta_generator.spec.tsindex a81598547b..5078b2164d 100644--- a/apps/edit-docs/src/help_meta_generator.spec.ts+++ b/apps/edit-docs/src/help_meta_generator.spec.ts@@ -1,3 +1,4 @@+import type { HiddenSubtreeItem } from "@triliumnext/commons";import type { NoteMeta } from "@triliumnext/core";import { describe, expect, it } from "vitest";@@ -36,6 +37,52 @@ describe("Help meta generation (server)", () => {expect(icon?.value).toBe("bx bx-star");});+ it("resolves a cloned note's iconClass and docName to the primary occurrence", () => {+ // The same note is placed under two folders: a primary occurrence (isClone:false) carrying+ // a custom icon, and a clone (isClone:true) with no attributes and a ".clone" data file.+ // Both occurrences map to the same noteId, so the runtime hidden-subtree check enforces one+ // set of attributes; if the generator emits different iconClass/docName per occurrence, that+ // check flip-flops them on every run. The clone must therefore inherit the primary's values.+ const primary: NoteMeta = {+ isClone: false, noteId: "nix", title: "Nix flake", type: "text", mime: "text/html",+ attributes: [{ type: "label", name: "iconClass", value: "bx bxl-tux", isInheritable: false, position: 10 }],+ format: "html", dataFileName: "Nix flake.html", children: []+ };+ const clone: NoteMeta = {+ isClone: true, noteId: "nix", title: "Nix flake", type: "text", mime: "text/html",+ format: "html", dataFileName: "Nix flake.clone.html", children: []+ };+ const desktop: NoteMeta = {+ isClone: false, noteId: "desktop", title: "Desktop Installation", type: "text", mime: "text/html",+ attributes: [], format: "html", dirFileName: "Desktop Installation", children: [primary]+ };+ const server: NoteMeta = {+ isClone: false, noteId: "server", title: "Server Installation", type: "text", mime: "text/html",+ attributes: [], format: "html", dirFileName: "Server Installation", children: [clone]+ };+ const root: NoteMeta = {+ isClone: false, noteId: "root", title: "User Guide", type: "text", mime: "text/html",+ attributes: [], format: "html", dirFileName: "User Guide", children: [desktop, server]+ };++ const metaFile = { formatVersion: 2, appVersion: "0.103.0", files: [root] };+ const items = parseNoteMetaFile(metaFile, serverTextNoteHandler);++ const primaryItem = items.find((i) => i.id === "_help_desktop")?.children?.[0];+ const cloneItem = items.find((i) => i.id === "_help_server")?.children?.[0];+ expect(primaryItem?.id).toBe("_help_nix");+ expect(cloneItem?.id).toBe("_help_nix");++ const iconOf = (it?: HiddenSubtreeItem) => it?.attributes?.find((a) => a.name === "iconClass")?.value;+ const docNameOf = (it?: HiddenSubtreeItem) => it?.attributes?.find((a) => a.name === "docName")?.value;++ // The clone inherits the primary's custom icon rather than falling back to "bx bx-file".+ expect(iconOf(primaryItem)).toBe("bx bxl-tux");+ expect(iconOf(cloneItem)).toBe("bx bxl-tux");+ // Both point at the same documentation file (the primary's), not the clone's ".clone" path.+ expect(docNameOf(cloneItem)).toBe(docNameOf(primaryItem));+ });+it("generates docUrl from shareAlias when baseUrl is provided", () => {const meta: NoteMeta = {isClone: false,diff --git a/apps/edit-docs/src/help_meta_generator.ts b/apps/edit-docs/src/help_meta_generator.tsindex 1e40afab8d..a16880e0a4 100644--- a/apps/edit-docs/src/help_meta_generator.ts+++ b/apps/edit-docs/src/help_meta_generator.ts@@ -46,12 +46,25 @@ export function parseNoteMetaFile(noteMetaFile: NoteMetaFile, handleTextNote: Te}const metaRoot = noteMetaFile.files[0];- const parsedMetaRoot = parseNoteMeta(metaRoot, handleTextNote, "/" + (metaRoot.dirFileName ?? ""), baseUrl);+ const docNameRoot = "/" + (metaRoot.dirFileName ?? "");++ // A note cloned into several help locations appears once per location, but the runtime+ // hidden-subtree check enforces a single set of attributes per noteId. The per-occurrence+ // `iconClass`/`docName` would therefore conflict and flip-flop on every check. Index the+ // primary (non-clone) occurrence's values up front so every clone can reuse them.+ const canonicalByNoteId = new Map<string, CanonicalOccurrence>();+ indexPrimaryOccurrences(metaRoot, docNameRoot, canonicalByNoteId);++ const parsedMetaRoot = parseNoteMeta(metaRoot, handleTextNote, docNameRoot, canonicalByNoteId, baseUrl);return parsedMetaRoot?.children ?? [];}-function parseNoteMeta(noteMeta: NoteMeta, handleTextNote: TextNoteHandler, docNameRoot: string, parentUrl?: string): HiddenSubtreeItem | null {- let iconClass: string = "bx bx-file";+interface CanonicalOccurrence {+ iconClass: string;+ docPath?: string;+}++function parseNoteMeta(noteMeta: NoteMeta, handleTextNote: TextNoteHandler, docNameRoot: string, canonicalByNoteId: Map<string, CanonicalOccurrence>, parentUrl?: string): HiddenSubtreeItem | null {const item: HiddenSubtreeItem = {id: `_help_${noteMeta.noteId}`,title: noteMeta.title ?? "",@@ -61,7 +74,6 @@ function parseNoteMeta(noteMeta: NoteMeta, handleTextNote: TextNoteHandler, docN// Handle folder notesif (!noteMeta.dataFileName) {- iconClass = "bx bx-folder";item.type = "book";}@@ -72,11 +84,6 @@ function parseNoteMeta(noteMeta: NoteMeta, handleTextNote: TextNoteHandler, docN// Handle attributesfor (const attribute of noteMeta.attributes ?? []) {- if (attribute.name === "iconClass") {- iconClass = attribute.value;- continue;- }-if (attribute.name === "webViewSrc") {item.attributes?.push({type: "label",@@ -90,9 +97,14 @@ function parseNoteMeta(noteMeta: NoteMeta, handleTextNote: TextNoteHandler, docN}}+ // Clones share the underlying note, so resolve their per-occurrence icon/docName to the+ // primary occurrence's values; otherwise the two occurrences would enforce conflicting ones.+ const canonical = noteMeta.isClone ? canonicalByNoteId.get(noteMeta.noteId ?? "") : undefined;+ const iconClass = canonical?.iconClass ?? computeIconClass(noteMeta);+// Handle text notesif (noteMeta.type === "text" && noteMeta.dataFileName) {- const docPath = `${docNameRoot}/${path.basename(noteMeta.dataFileName, ".html")}`.substring(1);+ const docPath = canonical?.docPath ?? computeDocPath(docNameRoot, noteMeta.dataFileName);if (!handleTextNote(item, docPath, noteUrl)) {return null;}@@ -109,7 +121,7 @@ function parseNoteMeta(noteMeta: NoteMeta, handleTextNote: TextNoteHandler, docNconst children: HiddenSubtreeItem[] = [];for (const childMeta of noteMeta.children) {const newDocNameRoot = noteMeta.dirFileName ? `${docNameRoot}/${noteMeta.dirFileName}` : docNameRoot;- const child = parseNoteMeta(childMeta, handleTextNote, newDocNameRoot, currentUrl);+ const child = parseNoteMeta(childMeta, handleTextNote, newDocNameRoot, canonicalByNoteId, currentUrl);if (child) {children.push(child);}@@ -126,3 +138,33 @@ function parseNoteMeta(noteMeta: NoteMeta, handleTextNote: TextNoteHandler, docNreturn item;}++/** Records the `iconClass`/`docName` of every primary (non-clone) occurrence so clones can reuse them. */+function indexPrimaryOccurrences(noteMeta: NoteMeta, docNameRoot: string, out: Map<string, CanonicalOccurrence>): void {+ if (!noteMeta.isClone && noteMeta.noteId) {+ out.set(noteMeta.noteId, {+ iconClass: computeIconClass(noteMeta),+ docPath: noteMeta.type === "text" && noteMeta.dataFileName ? computeDocPath(docNameRoot, noteMeta.dataFileName) : undefined+ });+ }++ if (noteMeta.children) {+ const childDocNameRoot = noteMeta.dirFileName ? `${docNameRoot}/${noteMeta.dirFileName}` : docNameRoot;+ for (const childMeta of noteMeta.children) {+ indexPrimaryOccurrences(childMeta, childDocNameRoot, out);+ }+ }+}++function computeIconClass(noteMeta: NoteMeta): string {+ // An explicit iconClass wins; otherwise folders default to bx-folder and files to bx-file.+ const explicit = noteMeta.attributes?.find((a) => a.name === "iconClass")?.value;+ if (explicit) {+ return explicit;+ }+ return noteMeta.dataFileName ? "bx bx-file" : "bx bx-folder";+}++function computeDocPath(docNameRoot: string, dataFileName: string): string {+ return `${docNameRoot}/${path.basename(dataFileName, ".html")}`.substring(1);+}