Skip to content

Build your personal knowledge base with Trilium Notes

feat(share): host app views on the pages of the static export

Elian Dorane19e4942b923
diff --git a/apps/server/src/services/export/zip/share_theme.spec.ts b/apps/server/src/services/export/zip/share_theme.spec.ts
index 7ca8782d0f..6faa576fbb 100644
--- a/apps/server/src/services/export/zip/share_theme.spec.ts
+++ b/apps/server/src/services/export/zip/share_theme.spec.ts
@@ -2,7 +2,8 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
const MANIFEST = vi.hoisted(() => ({
files: [ "scripts.js", "scripts.css" ],
- lazy: { mermaid: [ "mermaid.core-a.js" ] }
+ lazy: { mermaid: [ "mermaid.core-a.js", "../assets/worker-b.js" ] },
+ requires: {}
}));
const mockFs = vi.hoisted(() => ({
@@ -50,6 +51,26 @@ describe("createShareThemeExportProvider", () => {
expect(decode(files.get("assets/mermaid.core-a.js")))
.toBe("content of /client-build/src/mermaid.core-a.js");
+ expect(decode(files.get("assets/worker-b.js")))
+ .toBe("content of /client-build/assets/worker-b.js");
+ });
+
+ it("adds the app's catalogues an app view reads, leaving out one the locale lacks", () => {
+ mockFs.existsSync.mockImplementation((path: string) => !path.endsWith("entry.json"));
+ const calendar = {
+ type: "book",
+ getLabelValue: () => "calendar",
+ getChildNotes: () => [],
+ isContentAvailable: () => true,
+ getContent: () => ""
+ };
+ const { files } = createAssets("<p>No diagrams.</p>", calendar);
+
+ expect(decode(files.get("assets/translations/en/translation.json")))
+ .toBe("content of /client/translations/en/translation.json");
+ expect(files.has("assets/translations/en/entry.json")).toBe(false);
+ const withoutViews = createAssets("<p>No diagrams.</p>");
+ expect(withoutViews.files.has("assets/translations/en/translation.json")).toBe(false);
});
it("fails without a client build to read the share theme from", () => {
@@ -61,10 +82,13 @@ describe("createShareThemeExportProvider", () => {
const MERMAID_BLOCK = `<pre><code class="language-mermaid">graph TD;</code></pre>`;
-function createAssets(content: string) {
+function createAssets(content: string, ...others: object[]) {
const note = {
getSubtree: () => ({
- notes: [ { type: "text", isContentAvailable: () => true, getContent: () => content } ]
+ notes: [
+ { type: "text", isContentAvailable: () => true, getContent: () => content },
+ ...others
+ ]
})
};
const provider = createShareThemeExportProvider({ branch: { getNote: () => note } } as never);
diff --git a/apps/server/src/services/export/zip/share_theme.ts b/apps/server/src/services/export/zip/share_theme.ts
index 65b05db8df..35ea965733 100644
--- a/apps/server/src/services/export/zip/share_theme.ts
+++ b/apps/server/src/services/export/zip/share_theme.ts
@@ -2,18 +2,19 @@ import type { ShareThemeManifest } from "@triliumnext/commons";
import type { ZipExportProviderData } from "@triliumnext/core";
import ShareThemeExportProvider, {
getShareThemeExportFiles,
+ getShareThemeTranslationFiles,
type ShareThemeExportAssets
} from "@triliumnext/core/src/services/export/zip/share_theme.js";
import { existsSync, readFileSync } from "fs";
-import { join } from "path";
+import { basename, join } from "path";
import { getClientDir, getShareThemeAssetDir } from "../../../routes/assets";
import { registerShareProvider } from "../../../share/share_provider.js";
import { RESOURCE_DIR } from "../../resource_dir";
/**
- * Builds a share-theme export over the files the client build lists in its share theme manifest
- * and the client's fonts, all read from disk.
+ * Builds a share-theme export over the files the client build lists in its share theme manifest,
+ * the client's catalogues the app views read and the client's fonts, all read from disk.
*/
export function createShareThemeExportProvider(data: ZipExportProviderData) {
registerShareProvider();
@@ -29,8 +30,15 @@ export function createShareThemeExportProvider(data: ZipExportProviderData) {
const files = new Map<string, Uint8Array>([
[ "icon-color.svg", readFileSync(join(RESOURCE_DIR, "images", "icon-color.svg")) ]
]);
- for (const file of getShareThemeExportFiles(manifest, data.branch.getNote())) {
- files.set(`assets/${file}`, readFileSync(join(assetDir, file)));
+ const note = data.branch.getNote();
+ for (const file of getShareThemeExportFiles(manifest, note)) {
+ files.set(`assets/${basename(file)}`, readFileSync(join(assetDir, file)));
+ }
+ for (const file of getShareThemeTranslationFiles(note)) {
+ const path = join(getClientDir(), "translations", file);
+ if (existsSync(path)) {
+ files.set(`assets/translations/${file}`, readFileSync(path));
+ }
}
const assets: ShareThemeExportAssets = {
diff --git a/apps/standalone/src/lightweight/zip_export_provider_factory.spec.ts b/apps/standalone/src/lightweight/zip_export_provider_factory.spec.ts
index d6a8387df4..58ff82a500 100644
--- a/apps/standalone/src/lightweight/zip_export_provider_factory.spec.ts
+++ b/apps/standalone/src/lightweight/zip_export_provider_factory.spec.ts
@@ -9,10 +9,13 @@ vi.mock("@triliumnext/ckeditor5/src/theme/admonitions.css?raw", () => ({ default
vi.mock("@triliumnext/ckeditor5/src/theme/ck-content.css?raw", () => ({ default: ".content {}" }));
vi.mock("@triliumnext/ckeditor5/src/theme/multicolumn.css?raw", () => ({ default: ".columns {}" }));
-function makeData(content = "<p>No diagrams.</p>"): ZipExportProviderData {
+function makeData(content = "<p>No diagrams.</p>", ...others: object[]): ZipExportProviderData {
const note = {
getSubtree: () => ({
- notes: [ { type: "text", isContentAvailable: () => true, getContent: () => content } ]
+ notes: [
+ { type: "text", isContentAvailable: () => true, getContent: () => content },
+ ...others
+ ]
})
};
return {
@@ -73,6 +76,36 @@ describe("standaloneZipExportProviderFactory", () => {
const { files } = (provider as unknown as WithAssets).assets;
expect(new TextDecoder().decode(files.get("assets/mermaid.core-a.js") as Uint8Array))
.toBe("content of /src/mermaid.core-a.js");
+ expect(new TextDecoder().decode(files.get("assets/worker-b.js") as Uint8Array))
+ .toBe("content of /src/../assets/worker-b.js");
+ });
+
+ it("adds the app's catalogues an app view reads, leaving out one the build lacks", async () => {
+ vi.stubGlobal("fetch", vi.fn(async (url: string) => {
+ if (url.endsWith("entry.json")) {
+ return new Response("", { status: 404 });
+ }
+ return new Response(url.endsWith("share_theme.json")
+ ? JSON.stringify(MANIFEST)
+ : `content of ${url}`);
+ }));
+ const calendar = {
+ type: "book",
+ getLabelValue: () => "calendar",
+ getChildNotes: () => [],
+ isContentAvailable: () => true,
+ getContent: () => ""
+ };
+
+ const provider = await standaloneZipExportProviderFactory("share",
+ makeData(undefined, calendar));
+
+ type WithAssets = { assets: { files: Map<string, string | Uint8Array> } };
+ const { files } = (provider as unknown as WithAssets).assets;
+ const catalogue = files.get("assets/translations/en/translation.json") as Uint8Array;
+ expect(new TextDecoder().decode(catalogue))
+ .toBe("content of /translations/en/translation.json");
+ expect(files.has("assets/translations/en/entry.json")).toBe(false);
});
it("fails the share-theme export when the development server serves no manifest", async () => {
@@ -100,7 +133,8 @@ describe("standaloneZipExportProviderFactory", () => {
const MANIFEST = {
files: [ "scripts.js", "scripts.css" ],
- lazy: { mermaid: [ "mermaid.core-a.js" ] }
+ lazy: { mermaid: [ "mermaid.core-a.js", "../assets/worker-b.js" ] },
+ requires: {}
};
/** Serves {@link MANIFEST} and, for every other file, its path. */
diff --git a/apps/standalone/src/lightweight/zip_export_provider_factory.ts b/apps/standalone/src/lightweight/zip_export_provider_factory.ts
index 10f7b2551e..5d041ac198 100644
--- a/apps/standalone/src/lightweight/zip_export_provider_factory.ts
+++ b/apps/standalone/src/lightweight/zip_export_provider_factory.ts
@@ -32,8 +32,11 @@ export async function standaloneZipExportProviderFactory(format: ExportFormat, d
import("./share_provider.js"),
loadShareThemeManifest()
]);
- const files = shareTheme.getShareThemeExportFiles(manifest, data.branch.getNote());
- const assets = await loadShareThemeExportAssets(files);
+ const note = data.branch.getNote();
+ const assets = await loadShareThemeExportAssets(
+ shareTheme.getShareThemeExportFiles(manifest, note),
+ shareTheme.getShareThemeTranslationFiles(note)
+ );
registerShareProvider();
return new shareTheme.default(data, assets);
}
@@ -57,25 +60,38 @@ async function loadShareThemeManifest(): Promise<ShareThemeManifest> {
}
/**
- * Fetches the share theme's `files` from `src/` and the built-in icon fonts from `share/assets`,
- * where the build places them for the share pages. The export reads them synchronously, so they
- * are all loaded before it starts.
+ * Fetches the share theme's `files` from `src/`, or `assets/` for those the manifest lists as
+ * `../assets/<file>`, the app's catalogues `translationFiles` from
+ * `translations/` and the built-in icon fonts from `share/assets`, where the build places them
+ * for the app and the share pages. The export reads them synchronously, so they are all loaded
+ * before it starts. A catalogue the locale lacks is left out.
*/
-async function loadShareThemeExportAssets(themeFiles: string[]): Promise<ShareThemeExportAssets> {
+async function loadShareThemeExportAssets(
+ themeFiles: string[],
+ translationFiles: string[]
+): Promise<ShareThemeExportAssets> {
const { default: iconColorSvg } =
await import("../../../server/src/assets/images/icon-color.svg?raw");
const fontFiles = icon_packs.getIconPacks()
.filter((iconPack) => iconPack.builtin)
.map((iconPack) => `${iconPack.fontAttachmentId}.${icon_packs.MIME_TO_EXTENSION_MAPPINGS[iconPack.fontMime]}`);
- const [ themeContents, fontContents ] = await Promise.all([
+ const [ themeContents, translationContents, fontContents ] = await Promise.all([
Promise.all(themeFiles.map((file) => fetchAsset(`/src/${file}`))),
+ Promise.all(translationFiles.map((file) =>
+ fetchAsset(`/translations/${file}`).catch(() => null))),
Promise.all(fontFiles.map((file) => fetchAsset(`/share/assets/fonts/${file}`)))
]);
const files = new Map<string, string | Uint8Array>([ [ "icon-color.svg", iconColorSvg ] ]);
for (const [ index, file ] of themeFiles.entries()) {
- files.set(`assets/${file}`, themeContents[index]);
+ files.set(`assets/${file.split("/").at(-1) ?? file}`, themeContents[index]);
+ }
+ for (const [ index, file ] of translationFiles.entries()) {
+ const content = translationContents[index];
+ if (content) {
+ files.set(`assets/translations/${file}`, content);
+ }
}
const fonts = new Map(fontFiles.map((file, index) => [ file, fontContents[index] ]));
diff --git a/packages/commons/src/lib/share_hosting.spec.ts b/packages/commons/src/lib/share_hosting.spec.ts
index c99a24fc38..b468a5e404 100644
--- a/packages/commons/src/lib/share_hosting.spec.ts
+++ b/packages/commons/src/lib/share_hosting.spec.ts
@@ -1,12 +1,30 @@
import { describe, expect, it } from "vitest";
-import { resolveShareThemeGroups } from "./share_hosting.js";
+import { getShareThemeGroupFiles, resolveShareThemeGroups } from "./share_hosting.js";
describe("share theme groups", () => {
+ const manifest = {
+ files: [ "scripts.js" ],
+ lazy: {
+ app: [ "app.js", "preact.js" ],
+ "view:geoMap": [ "geomap.js", "preact.js" ],
+ "view:calendar": [ "calendar.js" ],
+ mermaid: [ "mermaid.js" ]
+ },
+ requires: { "view:geoMap": [ "app" ], "view:calendar": [ "app" ], app: [], mermaid: [] }
+ };
+
it("resolves the groups each group requires, transitively and once", () => {
const requires = { a: [ "b" ], b: [ "c", "a" ], c: [] };
expect(resolveShareThemeGroups(requires, [ "a" ]).sort()).toEqual([ "a", "b", "c" ]);
expect(resolveShareThemeGroups(requires, [ "unknown" ])).toEqual([ "unknown" ]);
});
+
+ it("lists the files of the groups and of those they require, each once", () => {
+ expect(getShareThemeGroupFiles(manifest, [ "view:geoMap", "view:calendar" ]).sort())
+ .toEqual([ "app.js", "calendar.js", "geomap.js", "preact.js" ]);
+ expect(getShareThemeGroupFiles(manifest, [])).toEqual([]);
+ expect(getShareThemeGroupFiles(manifest, [ "type:none" ])).toEqual([]);
+ });
});
diff --git a/packages/commons/src/lib/share_hosting.ts b/packages/commons/src/lib/share_hosting.ts
index 62032ddc98..394dcd5cda 100644
--- a/packages/commons/src/lib/share_hosting.ts
+++ b/packages/commons/src/lib/share_hosting.ts
@@ -1,3 +1,5 @@
+import type { ShareThemeManifest } from "./shared_types.js";
+
/**
* The view types of the collections a shared page shows with the app's own view. The app build
* checks that each has a `view:<viewType>` group in the share theme's manifest.
@@ -33,3 +35,9 @@ export function resolveShareThemeGroups(
}
return [ ...resolved ];
}
+
+/** Returns the files of `manifest`'s groups `names` and of the groups they require, each once. */
+export function getShareThemeGroupFiles(manifest: ShareThemeManifest, names: Iterable<string>) {
+ const groups = resolveShareThemeGroups(manifest.requires, names);
+ return [ ...new Set(groups.flatMap((name) => manifest.lazy[name] ?? [])) ];
+}
diff --git a/packages/trilium-core/src/services/export/zip/share_theme.spec.ts b/packages/trilium-core/src/services/export/zip/share_theme.spec.ts
index 2176b1c9ca..5ed7b2b6d6 100644
--- a/packages/trilium-core/src/services/export/zip/share_theme.spec.ts
+++ b/packages/trilium-core/src/services/export/zip/share_theme.spec.ts
@@ -26,10 +26,15 @@ vi.mock("../../../becca/becca.js", () => ({ default: mockBecca }));
const mockLog = { error: vi.fn(), info: vi.fn() };
vi.mock("../../log.js", () => ({ getLog: () => mockLog }));
+const mockOptions = { getOptionOrNull: vi.fn((_name: string): string | null => null) };
+vi.mock("../../options.js", () => ({ default: mockOptions }));
+
const {
default: ShareThemeExportProvider,
hasMermaidDiagrams,
- getShareThemeExportFiles
+ getAppViewGroups,
+ getShareThemeExportFiles,
+ getShareThemeTranslationFiles
} = await import("./share_theme.js");
// --- Test scaffolding -------------------------------------------------------
@@ -201,6 +206,31 @@ describe("ShareThemeExportProvider", () => {
expect(out).toContain('href=""');
});
+ it("links the notes of an app view to their pages, the root to the base path", () => {
+ const note = { noteId: "viewNote", getBestNotePath: () => [ "root", "viewNote" ] };
+ mockBecca.getNote.mockReturnValue(null);
+ const p = makeProvider();
+ (p as any).rootMeta = { noteId: "rootNote1234" };
+ const noteMeta: any = {
+ notePath: [ "root", "rootNote1234", "dir", "viewNote" ],
+ attachments: []
+ };
+
+ const { mock } = mockContentRenderer.renderNoteForExport;
+ const lastCall = () => mock.lastCall as unknown[] | undefined;
+ const getLink = (noteId: string) =>
+ (lastCall()?.[5] as ((id: string) => string | null) | undefined)?.(noteId);
+
+ p.prepareContent("t", "<p>c</p>", noteMeta, note as any, {} as any);
+ expect(getLink("rootNote1234")).toBe("../../");
+ expect(getLink("other")).toBe("url/other");
+
+ (p as any).rootMeta = { noteId: "viewNote" };
+ const rootMeta: any = { notePath: [ "root", "viewNote" ], attachments: [] };
+ p.prepareContent("t", "<p>c</p>", rootMeta, note as any, {} as any);
+ expect(getLink("viewNote")).toBe("./");
+ });
+
it("leaves binary (non-string) content untouched but still indexes the note", () => {
const note = { noteId: "binNote", getBestNotePath: () => ["root", "binNote12345"] };
mockBecca.getNote.mockReturnValue(null);
@@ -349,7 +379,63 @@ describe("hasMermaidDiagrams", () => {
});
});
+describe("getAppViewGroups", () => {
+ it("names the groups of the views, note types and drawn notes the subtree hosts", () => {
+ const site = viewTree({ type: "text", children: [
+ { type: "book", viewType: "calendar" },
+ { type: "book", viewType: "geoMap", isProtected: true },
+ { type: "book", viewType: "dashboard", children: [
+ { type: "code" },
+ { type: "book", viewType: "table" }
+ ] },
+ { type: "book", viewType: "presentation", children: [
+ { type: "text", children: [ { type: "render" } ] }
+ ] },
+ { type: "mermaid" },
+ { type: "book" }
+ ] });
+
+ expect([ ...getAppViewGroups(site) ].sort()).toEqual([
+ "app", "content:book", "content:code", "content:render", "content:text", "scripting",
+ "type:mermaid", "type:render", "view:calendar", "view:dashboard", "view:presentation",
+ "view:table"
+ ]);
+ const withoutViews = viewTree({ type: "text", children: [ { type: "book" } ] });
+ expect(getAppViewGroups(withoutViews).size).toBe(0);
+ });
+});
+
+describe("getShareThemeTranslationFiles", () => {
+ it("lists the catalogues of English and the display language only for app views", () => {
+ const calendar = viewTree({ type: "book", viewType: "calendar" });
+
+ expect(getShareThemeTranslationFiles(viewTree({ type: "text" }))).toEqual([]);
+ expect(getShareThemeTranslationFiles(calendar))
+ .toEqual([ "en/translation.json", "en/entry.json" ]);
+ mockOptions.getOptionOrNull.mockReturnValueOnce("ro");
+ expect(getShareThemeTranslationFiles(calendar)).toEqual([
+ "en/translation.json", "en/entry.json", "ro/translation.json", "ro/entry.json"
+ ]);
+ });
+});
+
describe("getShareThemeExportFiles", () => {
+ it("adds the files of the app views the subtree hosts, with the groups they require", () => {
+ const manifest = {
+ files: [ "scripts.js" ],
+ lazy: {
+ app: [ "app.js" ],
+ "view:calendar": [ "calendar.js" ],
+ "view:table": [ "table.js" ]
+ },
+ requires: { "view:calendar": [ "app" ] }
+ };
+
+ const calendar = viewTree({ type: "book", viewType: "calendar" });
+ expect(getShareThemeExportFiles(manifest, calendar).sort())
+ .toEqual([ "app.js", "calendar.js", "scripts.js" ]);
+ });
+
const manifest = {
files: [ "scripts.js", "scripts.css" ],
lazy: { mermaid: [ "mermaid.core-a.js" ] },
@@ -369,6 +455,31 @@ describe("getShareThemeExportFiles", () => {
});
});
+interface FakeViewNote {
+ type: string;
+ viewType?: string;
+ isProtected?: boolean;
+ children?: FakeViewNote[];
+}
+
+/** A note with the children of `definition`, read for the app views it hosts. */
+function viewTree(definition: FakeViewNote): any {
+ const children = (definition.children ?? []).map(viewTree);
+ const note = {
+ type: definition.type,
+ mime: "text/html",
+ isProtected: !!definition.isProtected,
+ isContentAvailable: () => true,
+ getContent: () => "",
+ getLabelValue: (name: string) => (name === "viewType" ? definition.viewType ?? null : null),
+ getChildNotes: () => children,
+ getSubtree: () => ({
+ notes: [ note, ...children.flatMap((child) => child.getSubtree().notes) ]
+ })
+ };
+ return note;
+}
+
type FakeNote = { type: string; mime?: string; available?: boolean; content: string };
/** A note whose subtree holds `notes`. */
diff --git a/packages/trilium-core/src/services/export/zip/share_theme.ts b/packages/trilium-core/src/services/export/zip/share_theme.ts
index 0b3c50c18d..8b8f4006da 100644
--- a/packages/trilium-core/src/services/export/zip/share_theme.ts
+++ b/packages/trilium-core/src/services/export/zip/share_theme.ts
@@ -1,4 +1,7 @@
-import type { ShareThemeManifest } from "@triliumnext/commons";
+import {
+ getShareThemeGroupFiles, SHARE_HOSTED_NOTE_TYPES, SHARE_HOSTED_VIEW_TYPES,
+ type ShareThemeManifest
+} from "@triliumnext/commons";
import ejs from "ejs";
import { convert as convertToText } from "html-to-text";
import { t } from "i18next";
@@ -10,6 +13,7 @@ import type { ExportFormat, NoteMeta, NoteMetaFile } from "../../../meta.js";
import { readShareTemplate, renderNoteForExport } from "../../../share/index.js";
import * as iconPackService from "../../icon_packs.js";
import { getLog } from "../../log.js";
+import options from "../../options.js";
import { ZipExportProvider, type ZipExportProviderData } from "./abstract_provider.js";
/** The static files a share-theme export copies into the archive, read by each platform its own way. */
@@ -75,7 +79,12 @@ export default class ShareThemeExportProvider extends ZipExportProvider {
}) : "";
// TODO: This will probably never match, but should it be exclude from running on code/jsFrontend notes?
- content = renderNoteForExport(note, branch, basePath, noteMeta.notePath.slice(0, -1), this.iconPacks);
+ const getLink = (noteId: string) => (noteId === this.rootMeta?.noteId
+ ? basePath || "./"
+ : this.getNoteTargetUrl(noteId, noteMeta));
+ const ancestors = noteMeta.notePath.slice(0, -1);
+ content = renderNoteForExport(note, branch, basePath, ancestors, this.iconPacks,
+ getLink);
if (typeof content === "string") {
// Rewrite attachment download links
content = content.replace(/href="api\/attachments\/([a-zA-Z0-9_]+)\/download"/g, (match, attachmentId) => {
@@ -184,11 +193,82 @@ export default class ShareThemeExportProvider extends ZipExportProvider {
/**
* Returns the files of `manifest` the export of `note` copies into `assets/`: those every page can
- * load, and mermaid's only when {@link hasMermaidDiagrams} finds a diagram.
+ * load, those of the app views the pages host, by {@link getAppViewGroups}, and mermaid's only
+ * when {@link hasMermaidDiagrams} finds a diagram.
*/
export function getShareThemeExportFiles(manifest: ShareThemeManifest, note: BNote) {
- const mermaidFiles = hasMermaidDiagrams(note) ? manifest.lazy.mermaid ?? [] : [];
- return [ ...manifest.files, ...mermaidFiles ];
+ const groups = [ ...getAppViewGroups(note) ];
+ if (hasMermaidDiagrams(note)) {
+ groups.push("mermaid");
+ }
+ return [ ...manifest.files, ...getShareThemeGroupFiles(manifest, groups) ];
+}
+
+/**
+ * Returns the app's catalogues the app views of the export of `note` read, relative to the app's
+ * `translations/`: those of the display language and of English, which they fall back to. The
+ * export copies them into `assets/translations/`; a locale without one of them leaves it out.
+ */
+export function getShareThemeTranslationFiles(note: BNote) {
+ if (!getAppViewGroups(note).size) {
+ return [];
+ }
+
+ const locales = new Set([ "en", options.getOptionOrNull("locale") || "en" ]);
+ return [ ...locales ].flatMap((locale) =>
+ [ `${locale}/translation.json`, `${locale}/entry.json` ]);
+}
+
+/**
+ * Returns the groups of the share theme's manifest the pages of the export of `note` load to host
+ * app views: `view:<viewType>` for each collection and `type:<noteType>` for each note type with
+ * one, `content:<noteType>` for the notes a dashboard or a presentation draws and `view:` for a
+ * collection among them, `app` beside any of them and `scripting` for a render note.
+ */
+export function getAppViewGroups(note: BNote) {
+ const groups = new Set<string>();
+ for (const subtreeNote of note.getSubtree().notes) {
+ if (subtreeNote.isProtected) {
+ continue;
+ }
+
+ const viewType = getViewTypeOf(subtreeNote);
+ if (viewType && SHARE_HOSTED_VIEW_TYPES.includes(viewType)) {
+ groups.add(`view:${viewType}`);
+ if (CONTENT_VIEW_TYPES.includes(viewType)) {
+ for (const drawn of getDrawnNotes(subtreeNote)) {
+ groups.add(`content:${drawn.type}`);
+ const drawnViewType = getViewTypeOf(drawn);
+ if (drawnViewType) {
+ groups.add(`view:${drawnViewType}`);
+ }
+ }
+ }
+ } else if (SHARE_HOSTED_NOTE_TYPES.includes(subtreeNote.type)) {
+ groups.add(`type:${subtreeNote.type}`);
+ }
+ }
+
+ if (groups.size) {
+ groups.add("app");
+ }
+ if (groups.has("type:render") || groups.has("content:render")) {
+ groups.add("scripting");
+ }
+ return groups;
+}
+
+/** The view types of the collections that draw the content of their notes. */
+const CONTENT_VIEW_TYPES = [ "dashboard", "presentation" ];
+
+/** The view type of a collection, the grid when it names none, or `null` for another note. */
+function getViewTypeOf(note: BNote) {
+ return note.type === "book" ? note.getLabelValue("viewType") || "grid" : null;
+}
+
+/** The notes a dashboard or a presentation draws: its children and, as vertical slides, theirs. */
+function getDrawnNotes(collection: BNote) {
+ return collection.getChildNotes().flatMap((child) => [ child, ...child.getChildNotes() ]);
}
/**
diff --git a/packages/trilium-core/src/share/content_renderer.spec.ts b/packages/trilium-core/src/share/content_renderer.spec.ts
index 251a985831..a959647cdd 100644
--- a/packages/trilium-core/src/share/content_renderer.spec.ts
+++ b/packages/trilium-core/src/share/content_renderer.spec.ts
@@ -1921,6 +1921,45 @@ describe("content_renderer pages", () => {
expect(renderNoteForExport(script, branch, "../", [], []))
.toBe(`console.log("Protected note cannot be exported.");`);
});
+
+ it("hosts an exported collection with the app's view, linking the exported notes", () => {
+ const site = buildNote({
+ id: "hostSite",
+ title: "Site",
+ content: "",
+ children: [ {
+ id: "hostCalendar",
+ title: "Calendar",
+ type: "book",
+ content: "",
+ "#viewType": "calendar",
+ children: [
+ { id: "hostEvent", title: "Event", content: "" },
+ { id: "hostLeftOut", title: "Left out", content: "" }
+ ]
+ } ]
+ });
+ const [ calendar ] = site.getChildNotes();
+ const branch = calendar.getParentBranches()[0];
+ const getLink = (noteId: string) =>
+ (noteId === "hostLeftOut" ? null : `../pages/${noteId}.html`);
+
+ const page = parse(String(renderNoteForExport(calendar, branch, "../", [], [], getLink)));
+ const payload = JSON.parse(page.querySelector("#content .share-froca")?.text ?? "{}");
+ expect(page.querySelector("#content .share-collection")?.getAttribute("data-note-id"))
+ .toBe("hostCalendar");
+ expect(payload.links).toStrictEqual({
+ hostCalendar: "../pages/hostCalendar.html",
+ hostEvent: "../pages/hostEvent.html"
+ });
+ expect(payload.branches.map((row: { noteId: string }) => row.noteId))
+ .toStrictEqual([ "hostEvent" ]);
+ expect(payload.assetPath).toBe("../assets");
+
+ // Without the export's links, a page keeps the content rendered for it.
+ const staticPage = parse(String(renderNoteForExport(calendar, branch, "../", [], [])));
+ expect(staticPage.querySelector("#content .share-collection") === null).toBe(true);
+ });
});
let sitePageCount = 0;
diff --git a/packages/trilium-core/src/share/content_renderer.ts b/packages/trilium-core/src/share/content_renderer.ts
index 9332f0eb41..184f3d8abe 100644
--- a/packages/trilium-core/src/share/content_renderer.ts
+++ b/packages/trilium-core/src/share/content_renderer.ts
@@ -28,7 +28,7 @@ import options from "../services/options.js";
import * as sanitize from "../services/sanitizer.js";
import * as task_states from "../services/task_states.js";
import * as utils from "../services/utils/index.js";
-import { buildFrocaPayload } from "./froca_payload.js";
+import { buildFrocaPayload, buildFrocaRows } from "./froca_payload.js";
import { getShareProvider } from "./share_provider.js";
import SAttachment from "./shaca/entities/sattachment.js";
import SBranch from "./shaca/entities/sbranch.js";
@@ -99,7 +99,14 @@ function getSharedSubTreeRoot(note: SNote): Subroot {
return getSharedSubTreeRoot(parentBranch.getParentNote());
}
-export function renderNoteForExport(note: BNote, parentBranch: BBranch, basePath: string, ancestors: string[], iconPacks: iconPackService.ProcessedIconPack[]) {
+export function renderNoteForExport(
+ note: BNote,
+ parentBranch: BBranch,
+ basePath: string,
+ ancestors: string[],
+ iconPacks: iconPackService.ProcessedIconPack[],
+ getLink?: ExportHosting["getLink"]
+) {
// An exported JavaScript note stays a script.
if (note.mime.startsWith("application/javascript")) {
return note.isProtected ? `console.log("Protected note cannot be exported.");` : note.getContent();
@@ -128,6 +135,7 @@ export function renderNoteForExport(note: BNote, parentBranch: BBranch, basePath
faviconUrl: `${basePath}favicon.ico`,
ancestors,
isStatic: true,
+ exportHosting: getLink && { basePath, getLink },
...getIconPackArgs(iconPacks, (p) => `${basePath}assets/icon-pack-${p.prefix.toLowerCase()}.${iconPackService.MIME_TO_EXTENSION_MAPPINGS[p.fontMime]}`)
});
}
@@ -232,6 +240,8 @@ interface RenderArgs {
ancestors: string[];
isStatic: boolean;
canAccessEmbed?: CanAccessEmbed;
+ /** How the page of the static export hosts app views. */
+ exportHosting?: ExportHosting;
faviconUrl: string;
iconPackCss: string;
iconPackSupportedPrefixes: string[];
@@ -250,7 +260,8 @@ function renderNoteContentInternal(note: SNote | BNote, renderArgs: RenderArgs)
// Static export preserves full embed nesting; the live share view renders only the first level.
const { header, content, isEmpty, isAppView, isFullHeight } = getContent(note, {
expandNestedEmbeds: renderArgs.isStatic,
- canAccessEmbed: renderArgs.canAccessEmbed
+ canAccessEmbed: renderArgs.canAccessEmbed,
+ exportHosting: renderArgs.exportHosting
});
const showLoginInShareTheme = options.getOptionBool("showLoginInShareTheme");
const siteRoot = renderArgs.subRoot.note;
@@ -455,6 +466,14 @@ const EXCERPT_SOURCE_LENGTH = 10_000;
*/
export type CanAccessEmbed = (note: SNote) => boolean;
+/** How a page of the static export hosts app views, which link to the export's own pages. */
+export interface ExportHosting {
+ /** The path of the export's root from the page, such as `../`. */
+ basePath: string;
+ /** The URL of a note's page relative to the page, or `null` for a note left out. */
+ getLink(noteId: string): string | null;
+}
+
export interface ShareRenderOptions {
/**
* Keep expanding embeds recursively at every depth. Used for static export, which
@@ -469,6 +488,11 @@ export interface ShareRenderOptions {
seenNoteIds?: Set<string>;
/** See {@link CanAccessEmbed}. When omitted, every embedded note is expanded. */
canAccessEmbed?: CanAccessEmbed;
+ /**
+ * How a page of the static export hosts app views. Without it, a note of becca keeps the
+ * content rendered here.
+ */
+ exportHosting?: ExportHosting;
/**
* The blocks of a text note to render, a `block` link parameter. The rest of the note is left
* out, and a missing block renders as a broken reference.
@@ -499,25 +523,25 @@ export function getContent(note: SNote | BNote, options: ShareRenderOptions = {}
renderCode(result, note.mime);
} else if (note.type === "mermaid") {
renderMermaid(result, note);
- hostNoteView(result, note, options.canAccessEmbed);
+ hostNoteView(result, note, options);
} else if ([ "image", "canvas", "mindMap" ].includes(note.type)) {
renderImage(result, note);
- hostNoteView(result, note, options.canAccessEmbed);
+ hostNoteView(result, note, options);
} else if (note.type === "file") {
renderFile(note, result);
- } else if (note.type === "book" && !(note instanceof BNote)
+ } else if (note.type === "book" && canHostAppView(note, options)
&& SHARE_HOSTED_VIEW_TYPES.includes(getViewType(note) ?? "")) {
- renderCollectionView(result, note, options.canAccessEmbed);
+ renderCollectionView(result, note, options);
} else if (note.type === "book") {
result.isEmpty = true;
} else if (note.type === "webView") {
renderWebView(note, result);
} else if ([ "noteMap", "relationMap", "render" ].includes(note.type)) {
result.content = "";
- hostNoteView(result, note, options.canAccessEmbed);
+ hostNoteView(result, note, options);
} else if (note.type === "spreadsheet") {
renderSpreadsheet(result);
- hostNoteView(result, note, options.canAccessEmbed);
+ hostNoteView(result, note, options);
} else {
result.content = `<p>${t("content_renderer.note-cannot-be-displayed")}</p>`;
}
@@ -929,34 +953,50 @@ function isSubtreeHidden(note: ShareNote) {
* 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.
*/
-function renderCollectionView(result: Result, note: SNote, canAccess: CanAccessEmbed | undefined) {
+function renderCollectionView(result: Result, note: SNote | BNote, options: ShareRenderOptions) {
result.content = "";
- hostInAppView(result, note, "share-collection", canAccess);
+ hostInAppView(result, note, "share-collection", options);
}
/**
- * Has the share theme show a shared note with the app's own widget for its type, keeping the content
- * rendered here for a visitor without scripts. The static export, binary content and an empty note
- * keep the content alone.
+ * Has the share theme show a shared note, or a note of the static export, with the app's own widget
+ * for its type, keeping the content rendered here for a visitor without scripts. Binary content and
+ * an empty note keep the content alone.
*/
-function hostNoteView(result: Result, note: SNote | BNote, canAccess: CanAccessEmbed | undefined) {
- if (!(note instanceof BNote) && typeof result.content === "string" && !result.isEmpty
+function hostNoteView(result: Result, note: SNote | BNote, options: ShareRenderOptions) {
+ if (canHostAppView(note, options) && typeof result.content === "string" && !result.isEmpty
&& SHARE_HOSTED_NOTE_TYPES.includes(note.type)) {
- hostInAppView(result, note, "share-note-view", canAccess);
+ hostInAppView(result, note, "share-note-view", options);
}
}
+/** Whether `note` can have an app view: a shared note, or a note of the static export. */
+function canHostAppView(note: SNote | BNote, options: ShareRenderOptions) {
+ return !(note instanceof BNote) || !!options.exportHosting;
+}
+
/**
* Wraps the content in a `container` the share theme's script mounts an app view into, beside the
* notes and the display options the view starts from. The content stays for a visitor without
* scripts until the view replaces it.
*/
-function hostInAppView(result: Result, note: SNote, container: string, canAccess: CanAccessEmbed | undefined) {
+function hostInAppView(
+ result: Result,
+ note: SNote | BNote,
+ container: string,
+ renderOptions: ShareRenderOptions
+) {
+ const { exportHosting } = renderOptions;
const payload = {
- ...buildFrocaPayload(note, canAccess),
+ ...(note instanceof BNote
+ ? buildExportRows(note, exportHosting)
+ : buildFrocaPayload(note, renderOptions.canAccessEmbed)),
options: Object.fromEntries(SHARED_OPTIONS.map((name) => [ name, options.getOptionOrNull(name) ])),
- // The app's assets, such as its translations, from a page directly below `/share/`.
- assetPath: `../${utils.isDev() ? `${assetUrlFragment}/src` : assetUrlFragment}`,
+ // The app's assets, such as its translations: the export's `assets/`, else those of the
+ // app from a page directly below `/share/`.
+ assetPath: exportHosting
+ ? `${exportHosting.basePath}assets`
+ : `../${utils.isDev() ? `${assetUrlFragment}/src` : assetUrlFragment}`,
// The note the app would show the note below, such as the root of a note map note's map.
parentNoteId: note.getParentBranches()[0]?.parentNoteId ?? null
};
@@ -968,6 +1008,16 @@ function hostInAppView(result: Result, note: SNote, container: string, canAccess
+ `<script type="application/json" class="share-froca">${json}</script>`;
}
+/**
+ * The rows of a note of the static export, of the notes the export holds, each linked to its page
+ * in the export.
+ */
+function buildExportRows(note: BNote, exportHosting: ExportHosting | undefined) {
+ const getLink = (linked: BNote) => exportHosting?.getLink(linked.noteId) ?? null;
+ return buildFrocaRows([ note ], (candidate) => getLink(candidate) !== null,
+ (candidate) => getLink(candidate) ?? "");
+}
+
/**
* The options the app's views read to draw a note as the app does, which a shared page receives. Only
* display options: the rest of the options can hold secrets, such as API keys.
diff --git a/packages/trilium-core/src/share/froca_payload.ts b/packages/trilium-core/src/share/froca_payload.ts
index f85568120a..35a218b6d1 100644
--- a/packages/trilium-core/src/share/froca_payload.ts
+++ b/packages/trilium-core/src/share/froca_payload.ts
@@ -1,9 +1,13 @@
import { getShareLink } from "@triliumnext/share-theme/model/page";
import becca from "../becca/becca.js";
-import type BNote from "../becca/entities/bnote.js";
+import type BAttribute from "../becca/entities/battribute.js";
+import type BBranch from "../becca/entities/bbranch.js";
+import BNote from "../becca/entities/bnote.js";
import * as sanitize from "../services/sanitizer.js";
import type SAttachment from "./shaca/entities/sattachment.js";
+import type SAttribute from "./shaca/entities/sattribute.js";
+import type SBranch from "./shaca/entities/sbranch.js";
import type SNote from "./shaca/entities/snote.js";
/**
@@ -12,11 +16,14 @@ import type SNote from "./shaca/entities/snote.js";
*/
export interface FrocaRows {
notes: ReturnType<typeof getNoteRow>[];
- branches: ReturnType<SNote["parentBranches"][number]["getPojo"]>[];
- attributes: ReturnType<SNote["ownedAttributes"][number]["getPojo"]>[];
+ branches: ReturnType<typeof getBranchRow>[];
+ attributes: ReturnType<typeof getAttributeRow>[];
links: Record<string, string>;
}
+/** A note of the share, or a note of the static export, which renders becca's notes. */
+type PayloadNote = SNote | BNote;
+
/**
* The notes a collection view reads, embedded in its page so the view draws without a request, of
* those that `canAccess` lets the visitor read.
@@ -32,18 +39,27 @@ export function buildFrocaPayload(note: SNote, canAccess?: (note: SNote) => bool
* that is protected or that `canAccess` refuses. A built-in template such as `_template_calendar`
* is read from becca instead, as it lives in the hidden subtree, so the notes inherit its labels.
* `#shareCredentials` is always left out, as it holds the password of the notes.
+ *
+ * The roots are shaca's notes on a shared page and becca's in the static export, whose relations
+ * and children are notes of the same cache. `getLink` gives the URL of each note's page, by
+ * default its shared page.
*/
-export function buildFrocaRows(roots: SNote[], canAccess: (note: SNote) => boolean = () => true): FrocaRows {
- const notes = new Map<string, SNote>();
- const add = (candidate: SNote | null | undefined) => {
- if (candidate && !candidate.isProtected && canAccess(candidate)) {
- notes.set(candidate.noteId, candidate);
+export function buildFrocaRows<T extends PayloadNote>(
+ roots: T[],
+ canAccess: (note: T) => boolean = () => true,
+ getLink: (note: T) => string = (note) => getShareLink(note, sanitize.sanitizeUrl).href
+): FrocaRows {
+ const notes = new Map<string, T>();
+ const add = (candidate: PayloadNote | null | undefined) => {
+ if (candidate && !candidate.isProtected && canAccess(candidate as T)) {
+ notes.set(candidate.noteId, candidate as T);
}
};
for (const root of roots) {
add(root);
- for (const child of root.getVisibleChildNotes()) {
+ const children = root instanceof BNote ? root.getChildNotes() : root.getVisibleChildNotes();
+ for (const child of children) {
add(child);
}
}
@@ -69,17 +85,16 @@ export function buildFrocaRows(roots: SNote[], canAccess: (note: SNote) => boole
for (const template of templates.values()) {
rows.notes.push(getNoteRow(template));
for (const attribute of template.getOwnedAttributes()) {
- const { attributeId, noteId, type, name, value, position, isInheritable } = attribute;
- rows.attributes.push({ attributeId, noteId, type, name, value, position, isInheritable: !!isInheritable });
+ rows.attributes.push(getAttributeRow(attribute));
}
}
for (const included of notes.values()) {
rows.notes.push(getNoteRow(included));
- rows.links[included.noteId] = getShareLink(included, sanitize.sanitizeUrl).href;
+ rows.links[included.noteId] = getLink(included);
for (const branch of included.parentBranches) {
if (notes.has(branch.parentNoteId)) {
- rows.branches.push(branch.getPojo());
+ rows.branches.push(getBranchRow(branch));
}
}
for (const attribute of included.ownedAttributes) {
@@ -87,7 +102,7 @@ export function buildFrocaRows(roots: SNote[], canAccess: (note: SNote) => boole
continue;
}
if (attribute.type === "label" || notes.has(attribute.value) || templates.has(attribute.value)) {
- rows.attributes.push(attribute.getPojo());
+ rows.attributes.push(getAttributeRow(attribute));
}
}
}
@@ -119,6 +134,18 @@ export function getBlobRow(entity: SNote | SAttachment) {
};
}
+function getBranchRow(branch: SBranch | BBranch) {
+ const { branchId, noteId, parentNoteId, notePosition, prefix, isExpanded } = branch.getPojo();
+ return {
+ branchId, noteId, parentNoteId, notePosition, prefix, isExpanded, fromSearchNote: false
+ };
+}
+
+function getAttributeRow(attribute: SAttribute | BAttribute) {
+ const { attributeId, noteId, type, name, value, position, isInheritable } = attribute.getPojo();
+ return { attributeId, noteId, type, name, value, position, isInheritable: !!isInheritable };
+}
+
function getNoteRow(note: SNote | BNote) {
return {
noteId: note.noteId,