Skip to content

Build your personal knowledge base with Trilium Notes

test(edit-docs): add tests for standalone

Elian Doran0027ede245ee
diff --git a/apps/edit-docs/package.json b/apps/edit-docs/package.json
index 87d1339f8b..3742f07929 100644
--- a/apps/edit-docs/package.json
+++ b/apps/edit-docs/package.json
@@ -9,7 +9,6 @@
},
"devDependencies": {
"@triliumnext/client": "workspace:*",
- "@triliumnext/commons": "workspace:*",
"@triliumnext/core": "workspace:*",
"@triliumnext/desktop": "workspace:*",
"copy-webpack-plugin": "14.0.0",
diff --git a/apps/edit-docs/src/edit-docs.ts b/apps/edit-docs/src/edit-docs.ts
index 7ea44fe7b0..1fe8a3c2c1 100644
--- a/apps/edit-docs/src/edit-docs.ts
+++ b/apps/edit-docs/src/edit-docs.ts
@@ -1,6 +1,5 @@
import debounce from "@triliumnext/client/src/services/debounce.js";
import type { AdvancedExportOptions, ExportFormat } from "@triliumnext/core";
-import type { HiddenSubtreeItem } from "@triliumnext/commons";
import NodejsInAppHelpProvider from "@triliumnext/server/src/in_app_help_provider.js";
import cls from "@triliumnext/server/src/services/cls.js";
import type NoteMeta from "@triliumnext/server/src/services/meta/note_meta.js";
@@ -251,7 +250,7 @@ async function cleanUpMeta(outputPath: string, minify: boolean) {
await fs.writeFile(metaPath, JSON.stringify(subtree));
// Generate standalone variant: doc notes with docUrl become webView notes with webViewSrc
- const standaloneSubtree = transformForStandalone(subtree);
+ const standaloneSubtree = NodejsInAppHelpProvider.transformForStandalone(subtree);
const standaloneMetaPath = path.join(outputPath, "!!!meta.standalone.json");
await fs.writeFile(standaloneMetaPath, JSON.stringify(standaloneSubtree));
} else {
@@ -260,52 +259,6 @@ async function cleanUpMeta(outputPath: string, minify: boolean) {
}
-/**
- * Transforms the help subtree for standalone mode: converts `doc` notes that have a `docUrl`
- * into `webView` notes with `webViewSrc`, removing the `docName` attribute.
- * Notes of type `doc` without a `docUrl` are excluded since their content isn't available offline.
- */
-function transformForStandalone(items: HiddenSubtreeItem[]): HiddenSubtreeItem[] {
- const result: HiddenSubtreeItem[] = [];
-
- for (const item of items) {
- const transformed = transformItemForStandalone(item);
- if (transformed) {
- result.push(transformed);
- }
- }
-
- return result;
-}
-
-function transformItemForStandalone(item: HiddenSubtreeItem): HiddenSubtreeItem | null {
- const docUrl = item.attributes?.find(a => a.name === "docUrl")?.value;
- const hasDocName = item.attributes?.some(a => a.name === "docName");
-
- // If it's a doc note with content but no online URL, skip it
- if (item.type === "doc" && hasDocName && !docUrl) {
- return null;
- }
-
- const newItem: HiddenSubtreeItem = { ...item };
-
- // Convert doc notes with docUrl to webView notes
- if (item.type === "doc" && hasDocName && docUrl) {
- newItem.type = "webView";
- newItem.enforceAttributes = true;
- newItem.attributes = (item.attributes ?? [])
- .filter(a => a.name !== "docName" && a.name !== "docUrl")
- .concat({ type: "label", name: "webViewSrc", value: docUrl });
- }
-
- // Recursively transform children
- if (item.children) {
- newItem.children = transformForStandalone(item.children);
- }
-
- return newItem;
-}
-
async function registerHandlers() {
const { events } = await import("@triliumnext/core");
const { erase: eraseService } = await import("@triliumnext/core");
diff --git a/apps/edit-docs/tsconfig.app.json b/apps/edit-docs/tsconfig.app.json
index 877f081880..ab40ca633c 100644
--- a/apps/edit-docs/tsconfig.app.json
+++ b/apps/edit-docs/tsconfig.app.json
@@ -23,9 +23,6 @@
"eslint.config.mjs"
],
"references": [
- {
- "path": "../../packages/commons/tsconfig.lib.json"
- },
{
"path": "../../packages/trilium-core/tsconfig.lib.json"
},
diff --git a/apps/server/src/in_app_help_provider.ts b/apps/server/src/in_app_help_provider.ts
index 6214432818..440b8ff96e 100644
--- a/apps/server/src/in_app_help_provider.ts
+++ b/apps/server/src/in_app_help_provider.ts
@@ -125,6 +125,50 @@ export default class NodejsInAppHelpProvider implements InAppHelpProvider {
return noteMeta.attributes?.find((a) => a.type === "label" && a.name === "shareAlias")?.value;
}
+ /**
+ * Transforms the help subtree for standalone mode: converts `doc` notes that have a `docUrl`
+ * into `webView` notes with `webViewSrc`, removing the `docName` attribute.
+ * Notes of type `doc` without a `docUrl` are excluded since their content isn't available offline.
+ */
+ static transformForStandalone(items: HiddenSubtreeItem[]): HiddenSubtreeItem[] {
+ const result: HiddenSubtreeItem[] = [];
+ for (const item of items) {
+ const transformed = NodejsInAppHelpProvider.transformItemForStandalone(item);
+ if (transformed) {
+ result.push(transformed);
+ }
+ }
+ return result;
+ }
+
+ private static transformItemForStandalone(item: HiddenSubtreeItem): HiddenSubtreeItem | null {
+ const docUrl = item.attributes?.find(a => a.name === "docUrl")?.value;
+ const hasDocName = item.attributes?.some(a => a.name === "docName");
+
+ // If it's a doc note with content but no online URL, skip it
+ if (item.type === "doc" && hasDocName && !docUrl) {
+ return null;
+ }
+
+ const newItem: HiddenSubtreeItem = { ...item };
+
+ // Convert doc notes with docUrl to webView notes
+ if (item.type === "doc" && hasDocName && docUrl) {
+ newItem.type = "webView";
+ newItem.enforceAttributes = true;
+ newItem.attributes = (item.attributes ?? [])
+ .filter(a => a.name !== "docName" && a.name !== "docUrl")
+ .concat({ type: "label", name: "webViewSrc", value: docUrl });
+ }
+
+ // Recursively transform children
+ if (item.children) {
+ newItem.children = NodejsInAppHelpProvider.transformForStandalone(item.children);
+ }
+
+ return newItem;
+ }
+
/**
* Iterates recursively through the help subtree that the user has and compares it against the definition
* to remove any notes that are no longer present in the latest version of the help.
diff --git a/apps/server/src/services/in_app_help.spec.ts b/apps/server/src/services/in_app_help.spec.ts
index b063d3aaa6..20f6be6419 100644
--- a/apps/server/src/services/in_app_help.spec.ts
+++ b/apps/server/src/services/in_app_help.spec.ts
@@ -1,3 +1,4 @@
+import type { HiddenSubtreeItem } from "@triliumnext/commons";
import { describe, expect, it } from "vitest";
import NodejsInAppHelpProvider from "../in_app_help_provider.js";
@@ -188,3 +189,103 @@ describe("In-app help", () => {
expect(item).toBeFalsy();
});
});
+
+describe("transformForStandalone", () => {
+ it("converts doc notes with docUrl to webView notes", () => {
+ const items: HiddenSubtreeItem[] = [{
+ id: "_help_abc123",
+ title: "Test Note",
+ type: "doc",
+ attributes: [
+ { type: "label", name: "docName", value: "User Guide/Test Note" },
+ { type: "label", name: "docUrl", value: "https://docs.triliumnotes.org/test-note" },
+ { type: "label", name: "iconClass", value: "bx bx-file" }
+ ]
+ }];
+
+ const result = NodejsInAppHelpProvider.transformForStandalone(items);
+
+ expect(result).toHaveLength(1);
+ expect(result[0].type).toBe("webView");
+ expect(result[0].enforceAttributes).toBe(true);
+ expect(result[0].attributes).toContainEqual({
+ type: "label", name: "webViewSrc", value: "https://docs.triliumnotes.org/test-note"
+ });
+ expect(result[0].attributes?.find(a => a.name === "docName")).toBeUndefined();
+ expect(result[0].attributes?.find(a => a.name === "docUrl")).toBeUndefined();
+ expect(result[0].attributes?.find(a => a.name === "iconClass")).toBeDefined();
+ });
+
+ it("excludes doc notes without docUrl", () => {
+ const items: HiddenSubtreeItem[] = [{
+ id: "_help_abc123",
+ title: "Offline Only Note",
+ type: "doc",
+ attributes: [
+ { type: "label", name: "docName", value: "User Guide/Offline" },
+ { type: "label", name: "iconClass", value: "bx bx-file" }
+ ]
+ }];
+
+ const result = NodejsInAppHelpProvider.transformForStandalone(items);
+ expect(result).toHaveLength(0);
+ });
+
+ it("preserves book and webView notes unchanged", () => {
+ const items: HiddenSubtreeItem[] = [
+ {
+ id: "_help_book1",
+ title: "Section",
+ type: "book",
+ attributes: [{ type: "label", name: "iconClass", value: "bx bx-folder" }]
+ },
+ {
+ id: "_help_wv1",
+ title: "API Docs",
+ type: "webView",
+ attributes: [{ type: "label", name: "webViewSrc", value: "/api/docs" }]
+ }
+ ];
+
+ const result = NodejsInAppHelpProvider.transformForStandalone(items);
+
+ expect(result).toHaveLength(2);
+ expect(result[0].type).toBe("book");
+ expect(result[1].type).toBe("webView");
+ });
+
+ it("recursively transforms children", () => {
+ const items: HiddenSubtreeItem[] = [{
+ id: "_help_parent",
+ title: "Parent",
+ type: "book",
+ attributes: [{ type: "label", name: "iconClass", value: "bx bx-folder" }],
+ children: [
+ {
+ id: "_help_child1",
+ title: "Child With URL",
+ type: "doc",
+ attributes: [
+ { type: "label", name: "docName", value: "User Guide/Child" },
+ { type: "label", name: "docUrl", value: "https://docs.triliumnotes.org/child" }
+ ]
+ },
+ {
+ id: "_help_child2",
+ title: "Child Without URL",
+ type: "doc",
+ attributes: [
+ { type: "label", name: "docName", value: "User Guide/Child2" }
+ ]
+ }
+ ]
+ }];
+
+ const result = NodejsInAppHelpProvider.transformForStandalone(items);
+
+ expect(result).toHaveLength(1);
+ expect(result[0].children).toHaveLength(1);
+ expect(result[0].children![0].type).toBe("webView");
+ expect(result[0].children![0].id).toBe("_help_child1");
+ });
+});