Skip to content

Build your personal knowledge base with Trilium Notes

feat(core): serve in-app help content from the virtual note provider

Elian Dorancd27d63666e0
diff --git a/apps/server/spec/in_app_help.spec.ts b/apps/server/spec/in_app_help.spec.ts
new file mode 100644
index 0000000000..6834d7961f
--- /dev/null
+++ b/apps/server/spec/in_app_help.spec.ts
@@ -0,0 +1,65 @@
+import type { HelpMetaItem } from "@triliumnext/commons";
+import becca from "@triliumnext/core/src/becca/becca.js";
+import { load } from "@triliumnext/core/src/becca/becca_loader.js";
+import blobService from "@triliumnext/core/src/services/blob.js";
+import { getContext } from "@triliumnext/core/src/services/context.js";
+import { initInAppHelp } from "@triliumnext/core/src/services/in_app_help.js";
+import { beforeAll, describe, expect, it } from "vitest";
+
+import NodejsInAppHelpProvider from "../src/in_app_help_provider.js";
+
+/**
+ * Exercises the in-app help against the artifacts that actually ship — the tree and content
+ * `edit-docs` writes into `src/assets/help`. The unit tests around the provider and the injection
+ * use stubs; this is the check that the real files still add up to a working help subtree.
+ */
+describe("in-app help (shipped artifacts)", () => {
+ const provider = new NodejsInAppHelpProvider();
+ const meta = provider.getHelpHiddenSubtreeData() as HelpMetaItem[];
+
+ beforeAll(() => {
+ initInAppHelp(provider);
+ getContext().init(() => load());
+ });
+
+ it("injects every page of the shipped tree under _help", () => {
+ expect(meta.length).toBeGreaterThan(0);
+
+ for (const item of flatten(meta)) {
+ const note = becca.getNote(item.id);
+ expect(note, `help note ${item.id} ("${item.title}") is missing from becca`).toBeTruthy();
+ expect(note?.isVirtual).toBe(true);
+ expect(note?.title).toBe(item.title);
+ }
+ });
+
+ it("serves the content of every page that ships one, through becca and the blob route", () => {
+ const pages = flatten(meta).filter((item) => item.source);
+ expect(pages.length).toBeGreaterThan(0);
+
+ for (const page of pages) {
+ const content = String(becca.getNoteOrThrow(page.id).getContent());
+ expect(content, `help page ${page.id} ("${page.title}") has no content`).not.toBe("");
+
+ const pojo = blobService.getBlobPojo("notes", page.id);
+ expect(pojo.content).toBe(content);
+ expect(pojo.isStubbed).toBe(false);
+ }
+ });
+
+ it("makes every page read-only", () => {
+ for (const item of flatten(meta)) {
+ expect(becca.getNoteOrThrow(item.id).isLabelTruthy("readOnly")).toBe(true);
+ }
+ });
+});
+
+function flatten(items: HelpMetaItem[], out: HelpMetaItem[] = []): HelpMetaItem[] {
+ for (const item of items) {
+ out.push(item);
+ if (item.children) {
+ flatten(item.children, out);
+ }
+ }
+ return out;
+}
diff --git a/apps/server/src/in_app_help_provider.ts b/apps/server/src/in_app_help_provider.ts
index 978e893fa1..b2d1dadcff 100644
--- a/apps/server/src/in_app_help_provider.ts
+++ b/apps/server/src/in_app_help_provider.ts
@@ -1,21 +1,38 @@
-import type { HiddenSubtreeItem } from "@triliumnext/commons";
+import type { HelpBundle, HiddenSubtreeItem } from "@triliumnext/commons";
import { InAppHelpProvider } from "@triliumnext/core";
import fs from "fs";
import path from "path";
import { RESOURCE_DIR } from "./services/resource_dir.js";
+/**
+ * Serves the in-app help from the two artifacts `edit-docs` builds out of `docs/User Guide`:
+ * the note tree and the rendered content of its pages.
+ */
export default class NodejsInAppHelpProvider extends InAppHelpProvider {
+ /** The guide only changes with the application, so both files are read once per process. */
+ private helpContent: HelpBundle | null = null;
+
getHelpHiddenSubtreeData(): HiddenSubtreeItem[] {
- // Written by edit-docs from the User Guide's markdown export; see apps/edit-docs.
- const metaFilePath = path.join(RESOURCE_DIR, "help", "help_meta.json");
+ return readHelpFile<HiddenSubtreeItem[]>("help_meta.json") ?? [];
+ }
- try {
- return JSON.parse(fs.readFileSync(metaFilePath).toString("utf-8"));
- } catch (e) {
- console.warn(e);
- return [];
+ getHelpContent(): HelpBundle {
+ if (!this.helpContent) {
+ this.helpContent = readHelpFile<HelpBundle>("help_content.json") ?? {};
}
+ return this.helpContent;
+ }
+}
+
+function readHelpFile<T>(fileName: string): T | null {
+ const filePath = path.join(RESOURCE_DIR, "help", fileName);
+
+ try {
+ return JSON.parse(fs.readFileSync(filePath).toString("utf-8")) as T;
+ } catch (e) {
+ console.warn(e);
+ return null;
}
}
diff --git a/apps/standalone/src/lightweight/in_app_help_provider.ts b/apps/standalone/src/lightweight/in_app_help_provider.ts
index 69e62d605f..208aef1f6f 100644
--- a/apps/standalone/src/lightweight/in_app_help_provider.ts
+++ b/apps/standalone/src/lightweight/in_app_help_provider.ts
@@ -1,4 +1,4 @@
-import type { HiddenSubtreeItem } from "@triliumnext/commons";
+import type { HelpBundle, HiddenSubtreeItem } from "@triliumnext/commons";
import { InAppHelpProvider } from "@triliumnext/core";
import helpMeta from "../assets/help_meta.json";
@@ -12,4 +12,11 @@ export default class StandaloneInAppHelpProvider extends InAppHelpProvider {
getHelpHiddenSubtreeData(): HiddenSubtreeItem[] {
return helpMeta as HiddenSubtreeItem[];
}
+
+ getHelpContent(): HelpBundle {
+ // Standalone's help pages are web views onto the online documentation, so they hold no
+ // content of their own. They move to the bundled content once this build consumes the
+ // same artifacts the server does.
+ return {};
+ }
}
diff --git a/packages/trilium-core/src/services/blob.ts b/packages/trilium-core/src/services/blob.ts
index acbface40a..cd636aeb77 100644
--- a/packages/trilium-core/src/services/blob.ts
+++ b/packages/trilium-core/src/services/blob.ts
@@ -4,7 +4,9 @@ import { NotFoundError } from "../errors";
import protectedSessionService from "./protected_session.js";
import { getSql } from "./sql/index.js";
import { decodeUtf8 } from "./utils/binary.js";
-import { hash, isStringNote } from "./utils/index.js";
+import dateUtils from "./utils/date.js";
+import { hash, hashedBlobId, isStringNote } from "./utils/index.js";
+import { getVirtualNoteContent } from "./virtual_notes.js";
function getBlobPojo(entityName: string, entityId: string, opts?: { preview: boolean }) {
// TODO: Unused opts.
@@ -13,6 +15,10 @@ function getBlobPojo(entityName: string, entityId: string, opts?: { preview: boo
throw new NotFoundError(`Entity ${entityName} '${entityId}' was not found.`);
}
+ if (entity.isVirtual) {
+ return virtualBlobPojo(entityId);
+ }
+
const blob = becca.getBlob(entity);
if (!blob) {
throw new NotFoundError(`Blob ${entity.blobId} for ${entityName} '${entityId}' was not found.`);
@@ -35,6 +41,30 @@ function getBlobPojo(entityName: string, entityId: string, opts?: { preview: boo
return { ...pojo, isStubbed };
}
+/**
+ * Produces a blob POJO for a virtual note. Virtual notes have no `blobs` row — their content comes
+ * from the provider that injected them (see `services/virtual_notes.ts`) — so the POJO is derived
+ * on the fly. The blobId is hashed from the content exactly as the persisted path does, so the
+ * client caches and change-detects it the same way, and an empty page still yields
+ * {@link EMPTY_BLOB_ID} rather than looking like a withheld sync stub.
+ */
+function virtualBlobPojo(noteId: string) {
+ const content = getVirtualNoteContent(noteId);
+ const isString = typeof content === "string";
+
+ return {
+ blobId: hashedBlobId(content),
+ // Binary content follows the persisted path, which hands the caller null and lets it fetch
+ // the bytes through the dedicated route instead.
+ content: isString ? content : null,
+ contentLength: content.length,
+ textRepresentation: null,
+ dateModified: dateUtils.localNowDateTime(),
+ utcDateModified: dateUtils.utcNowDateTime(),
+ isStubbed: false
+ };
+}
+
/**
* Produces a blob POJO for a soft-deleted (not-yet-erased) note. The normal {@link getBlobPojo}
* path resolves the note through Becca, which never contains deleted notes — so this reads the
diff --git a/packages/trilium-core/src/services/in_app_help.spec.ts b/packages/trilium-core/src/services/in_app_help.spec.ts
index d6430dc9ec..8f1dbe00df 100644
--- a/packages/trilium-core/src/services/in_app_help.spec.ts
+++ b/packages/trilium-core/src/services/in_app_help.spec.ts
@@ -1,21 +1,26 @@
import { describe, expect, it } from "vitest";
-import type { HiddenSubtreeItem } from "@triliumnext/commons";
+import { EMPTY_BLOB_ID, type HelpBundle, type HiddenSubtreeItem } from "@triliumnext/commons";
import becca from "../becca/becca.js";
import { load } from "../becca/becca_loader.js";
+import blobService from "./blob.js";
import { getContext } from "./context.js";
import { getHelpHiddenSubtreeData, InAppHelpProvider, initInAppHelp } from "./in_app_help.js";
import { getVirtualNoteProvider } from "./virtual_notes.js";
/** Minimal concrete provider standing in for the platform-specific implementations. */
class TestHelpProvider extends InAppHelpProvider {
- constructor(private data: HiddenSubtreeItem[]) {
+ constructor(private data: HiddenSubtreeItem[], private content: HelpBundle = {}) {
super();
}
getHelpHiddenSubtreeData(): HiddenSubtreeItem[] {
return this.data;
}
+
+ getHelpContent(): HelpBundle {
+ return this.content;
+ }
}
function reloadBecca() {
@@ -56,6 +61,43 @@ describe("in_app_help", () => {
expect(stub.getLabelValue("iconClass")).toBe("bx bx-file");
});
+ it("serves page content from the provider, through becca and the blob route", () => {
+ const data: HiddenSubtreeItem[] = [
+ { id: "_helpPage", title: "Page", type: "text" },
+ { id: "_helpBlank", title: "Blank", type: "text" }
+ ];
+ initInAppHelp(new TestHelpProvider(data, { _helpPage: "<p>Rendered</p>" }));
+ reloadBecca();
+
+ expect(becca.getNoteOrThrow("_helpPage").getContent()).toBe("<p>Rendered</p>");
+ // A page the provider has no entry for reads as empty rather than failing.
+ expect(becca.getNoteOrThrow("_helpBlank").getContent()).toBe("");
+
+ // Virtual notes have no blobs row, so the route derives the POJO from the provider. The
+ // blobId is content-derived, as for persisted notes, so the client change-detects it the
+ // same way — and empty content yields the shared empty-blob id rather than looking like a
+ // blob the sync server withheld.
+ const page = blobService.getBlobPojo("notes", "_helpPage");
+ expect(page.content).toBe("<p>Rendered</p>");
+ expect(page.contentLength).toBe("<p>Rendered</p>".length);
+ expect(page.isStubbed).toBe(false);
+
+ const blank = blobService.getBlobPojo("notes", "_helpBlank");
+ expect(blank.blobId).toBe(EMPTY_BLOB_ID);
+ expect(blank.isStubbed).toBe(false);
+ expect(page.blobId).not.toBe(blank.blobId);
+ });
+
+ it("makes every help page read-only through an inheritable label on the root", () => {
+ initInAppHelp(new TestHelpProvider([{ id: "_helpPage", title: "Page", type: "text" }]));
+ reloadBecca();
+
+ // Owned by the root, inherited by the pages: the guide is never edited in place, and the
+ // application must not offer an editor for it.
+ expect(becca.getNoteOrThrow("_help").getOwnedAttribute("label", "readOnly")).toBeTruthy();
+ expect(becca.getNoteOrThrow("_helpPage").isLabelTruthy("readOnly")).toBe(true);
+ });
+
it("unregisters the help provider when none is supplied", () => {
initInAppHelp(undefined as unknown as InAppHelpProvider);
expect(getHelpHiddenSubtreeData()).toEqual([]);
diff --git a/packages/trilium-core/src/services/in_app_help.ts b/packages/trilium-core/src/services/in_app_help.ts
index 0f66ec0f3f..99a3755161 100644
--- a/packages/trilium-core/src/services/in_app_help.ts
+++ b/packages/trilium-core/src/services/in_app_help.ts
@@ -1,20 +1,26 @@
-import type { HiddenSubtreeItem } from "@triliumnext/commons";
+import type { HelpBundle, HiddenSubtreeItem } from "@triliumnext/commons";
import { t } from "i18next";
import { registerVirtualNoteProvider, unregisterVirtualNoteProvider, type VirtualSubtreeItem } from "./virtual_notes.js";
/**
- * Platform-specific source of the in-app help (User Guide) tree definition — the server reads
- * it from `RESOURCE_DIR/doc_notes`, the standalone build from a bundled JSON asset.
+ * Platform-specific source of the in-app help (User Guide), both its tree and the rendered content
+ * of its pages. Both are produced by `edit-docs` from the markdown under `docs/User Guide`; the
+ * server reads them from `RESOURCE_DIR/help`.
*
* The help notes themselves are **virtual notes**: they exist only in becca, injected under
* `_hidden` by the `_help` virtual note provider registered in {@link initInAppHelp}. They are
- * never persisted or synced — their structure is owned by the application, not the user — and
- * their HTML content is fetched by the client directly from the bundled assets (see the
- * client's `doc_renderer`), so the provider supplies no content.
+ * never persisted or synced — their structure and content are owned by the application, not the
+ * user.
*/
export abstract class InAppHelpProvider {
abstract getHelpHiddenSubtreeData(): HiddenSubtreeItem[];
+
+ /**
+ * Rendered content of the help pages, keyed by note ID. Called on every page read, so
+ * implementations that load it from disk should cache it.
+ */
+ abstract getHelpContent(): HelpBundle;
}
let provider: InAppHelpProvider | null = null;
@@ -41,11 +47,20 @@ export function initInAppHelp(p: InAppHelpProvider) {
type: "book",
icon: "bx-help-circle",
isExpanded: true,
+ // Inherited by every page: the guide is application-owned, so it opens in the
+ // reading view rather than an editor. Writes are refused by the entity layer
+ // anyway; this keeps the application from offering them in the first place.
+ attributes: [
+ { type: "label", name: "readOnly", value: "", isInheritable: true }
+ ],
// HiddenSubtreeItem is structurally a VirtualSubtreeItem (its enforce*
// fields are simply ignored here).
children: getHelpHiddenSubtreeData()
}
];
+ },
+ getContent(noteId: string) {
+ return provider?.getHelpContent()[noteId];
}
});
}