Skip to content

Build your personal knowledge base with Trilium Notes

feat(share): compute the data of the static export's app views

Elian Doranca7cf406c937
diff --git a/packages/trilium-core/src/routes/api/note_map.ts b/packages/trilium-core/src/routes/api/note_map.ts
index d3338b6828..cdb6362ed9 100644
--- a/packages/trilium-core/src/routes/api/note_map.ts
+++ b/packages/trilium-core/src/routes/api/note_map.ts
@@ -6,7 +6,7 @@ import type { Request } from "../../http_interface";
import { findExcerpts, findLlmChatExcerpts, findMindMapExcerpts } from "../../services/backlink_excerpts";
-interface TreeLink {
+export interface TreeLink {
sourceNoteId: string;
targetNoteId: string;
}
diff --git a/packages/trilium-core/src/services/export/zip/share_data.spec.ts b/packages/trilium-core/src/services/export/zip/share_data.spec.ts
new file mode 100644
index 0000000000..d2ce51701c
--- /dev/null
+++ b/packages/trilium-core/src/services/export/zip/share_data.spec.ts
@@ -0,0 +1,122 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+import becca from "../../../becca/becca.js";
+import { buildNote } from "../../../test/becca_easy_mocking.js";
+import scriptService from "../../script.js";
+import { getSql } from "../../sql/index.js";
+import { buildShareData, isHostedNote } from "./share_data.js";
+
+describe("buildShareData", () => {
+ beforeEach(() => {
+ becca.reset();
+ });
+
+ it("writes the rows, blobs, images, maps and scripts the app views of the export read", () => {
+ buildNote({ id: "outside", title: "Outside" });
+ const root = buildNote({
+ id: "siteRoot",
+ children: [
+ { id: "calendar", type: "book", content: "", "#viewType": "calendar", children: [
+ { id: "event", content: "<p>Event</p>", "~related": "outside" }
+ ] },
+ { id: "canvas", type: "canvas", content: "{}", attachments: [
+ {
+ id: "canvasSvg", title: "canvas-export.svg", role: "image",
+ mime: "image/svg+xml"
+ }
+ ] },
+ { id: "image", type: "image", mime: "image/png" },
+ { id: "relations", type: "relationMap",
+ content: JSON.stringify({ notes: [ { noteId: "event" }, { noteId: "outside" } ] }) },
+ { id: "map", type: "noteMap", content: "", "#mapRootNoteId": "calendar",
+ "#mapExcludeRelation": "template", "#mapIncludeRelation": "related" },
+ { id: "hoistedMap", type: "noteMap", content: "", "#mapRootNoteId": "hoisted" },
+ { id: "parentMap", type: "noteMap", content: "" },
+ { id: "render", type: "render", content: "", "~renderNote": "script" },
+ { id: "script", type: "code", mime: "application/javascript;env=frontend" },
+ { id: "plain", content: "<p>Plain</p>" },
+ { id: "locked" }
+ ]
+ });
+ becca.getNoteOrThrow("locked").isProtected = true;
+ const image = becca.getNoteOrThrow("image");
+ image.getContent = () => new Uint8Array([ 1, 2 ]);
+ const [ svg ] = becca.getNoteOrThrow("canvas").getAttachments();
+ svg.getContent = () => new TextEncoder().encode("<svg/>");
+ const bundle = vi.spyOn(scriptService, "getScriptBundleForFrontend")
+ .mockReturnValue({ script: "run()", html: "", allNoteIds: [ "script" ] } as never);
+ // The fixtures are in becca alone, so the query for the placed notes finds each.
+ const placed = vi.spyOn(getSql(), "getColumn")
+ .mockImplementation((_query, params) => params as never);
+
+ const files = buildShareData(root, {
+ getNotePath: (noteId) => (noteId === "siteRoot" ? "" : `pages/${noteId}.html`),
+ getAttachmentPath: (attachmentId) => `pages/${attachmentId}.svg`
+ });
+
+ expect([ ...files.keys() ].sort()).toEqual([
+ "data/blobs/attachments/canvasSvg.json",
+ "data/blobs/notes/calendar.json",
+ "data/blobs/notes/canvas.json",
+ "data/blobs/notes/event.json",
+ "data/blobs/notes/hoistedMap.json",
+ "data/blobs/notes/image.json",
+ "data/blobs/notes/map.json",
+ "data/blobs/notes/parentMap.json",
+ "data/blobs/notes/relations.json",
+ "data/blobs/notes/render.json",
+ "data/images/image.png",
+ "data/note-map/calendar-link.json",
+ "data/note-map/calendar-tree.json",
+ "data/note-map/siteRoot-link.json",
+ "data/note-map/siteRoot-tree.json",
+ "data/relation-map/relations.json",
+ "data/rows.json",
+ "data/script/script.json"
+ ]);
+ const read = (name: string) => JSON.parse(String(files.get(name)));
+
+ const rows = read("data/rows.json");
+ expect(rows.notes.map((note: { noteId: string }) => note.noteId).sort()).toEqual([
+ "calendar", "canvas", "event", "hoistedMap", "image", "map", "parentMap", "plain",
+ "relations", "render", "script", "siteRoot"
+ ]);
+ expect(rows.links.siteRoot).toBe("");
+ expect(rows.links.event).toBe("pages/event.html");
+ expect(rows.attributes.some((attribute: { name: string }) => attribute.name === "related"))
+ .toBe(false);
+ expect(rows.attachments.canvas[0])
+ .toMatchObject({ attachmentId: "canvasSvg", ownerId: "canvas" });
+ expect(rows.attachmentPaths).toEqual({ canvasSvg: "pages/canvasSvg.svg" });
+ expect(rows.images)
+ .toEqual({ image: "data/images/image.png", canvas: "pages/canvasSvg.svg" });
+
+ expect(read("data/blobs/notes/event.json").content).toBe("<p>Event</p>");
+ expect(read("data/blobs/attachments/canvasSvg.json").content).toBe("<svg/>");
+ expect(files.get("data/images/image.png")).toEqual(new Uint8Array([ 1, 2 ]));
+ expect(Object.keys(read("data/relation-map/relations.json").noteTitles))
+ .toEqual([ "event" ]);
+ const treeMap = read("data/note-map/calendar-tree.json");
+ expect(treeMap.notes.map(([ noteId ]: [ string ]) => noteId).sort())
+ .toEqual([ "calendar", "event" ]);
+ expect(read("data/script/script.json").script).toBe("run()");
+
+ bundle.mockReturnValue({ script: "", html: "", allNoteIds: [ "script", "outside" ] } as never);
+ const withOutsideModule = buildShareData(root, {
+ getNotePath: (noteId) => (noteId === "outside" ? null : noteId),
+ getAttachmentPath: () => null
+ });
+ expect(withOutsideModule.has("data/script/script.json")).toBe(false);
+ expect(JSON.parse(String(withOutsideModule.get("data/rows.json"))).images)
+ .toEqual({ image: "data/images/image.png" });
+ bundle.mockRestore();
+ placed.mockRestore();
+ });
+
+ it("tells the notes a page hosts an app view for", () => {
+ expect(isHostedNote(buildNote({ type: "book", "#viewType": "geoMap" }))).toBe(true);
+ expect(isHostedNote(buildNote({ type: "book" }))).toBe(false);
+ expect(isHostedNote(buildNote({ type: "mindMap" }))).toBe(true);
+ expect(isHostedNote(buildNote({ type: "text" }))).toBe(false);
+ });
+});
diff --git a/packages/trilium-core/src/services/export/zip/share_data.ts b/packages/trilium-core/src/services/export/zip/share_data.ts
new file mode 100644
index 0000000000..62cc8cfbb5
--- /dev/null
+++ b/packages/trilium-core/src/services/export/zip/share_data.ts
@@ -0,0 +1,175 @@
+import {
+ getImageAttachmentTitle, SHARE_HOSTED_NOTE_TYPES, SHARE_HOSTED_VIEW_TYPES
+} from "@triliumnext/commons";
+import mimeTypes from "mime-types";
+
+import becca from "../../../becca/becca.js";
+import type BNote from "../../../becca/entities/bnote.js";
+import {
+ buildFrocaRows, buildVisibleNoteMap, buildVisibleRelationMap, buildVisibleScriptBundle,
+ getAttachmentRow, getBlobRow, type IsVisibleNote
+} from "../../../share/froca_payload.js";
+
+/** Where the export of a subtree writes its notes, as paths relative to the export's root. */
+export interface ShareDataPaths {
+ /** The path of a note's page, `""` for the root's, or `null` for a note left out. */
+ getNotePath(noteId: string): string | null;
+ /** The path of an attachment's file, or `null` for one left out. */
+ getAttachmentPath(attachmentId: string): string | null;
+}
+
+/**
+ * Builds the files under `data/` the app views of the export of `root` read in place of the
+ * share's API, keyed by their path in the archive:
+ *
+ * - `data/rows.json`: the rows of every note of the export, with its attachments and the path of
+ * each note's page, of each attachment's file and of each note's image;
+ * - `data/blobs/{notes,attachments}/<id>.json`: the content of the notes the views read and of
+ * their attachments;
+ * - `data/images/<noteId>.<extension>`: the image of an image note, whose page takes its file;
+ * - `data/note-map/<mapRootNoteId>-{tree,link}.json`, `data/relation-map/<noteId>.json` and
+ * `data/script/<noteId>.json`: what a note map, a relation map and a render note draw.
+ *
+ * Only notes the export holds and that are not protected appear, as on a shared page.
+ */
+export function buildShareData(root: BNote, paths: ShareDataPaths) {
+ const files = new Map<string, string | Uint8Array>();
+ const writeJson = (name: string, data: unknown) => files.set(name, JSON.stringify(data));
+ const notes = root.getSubtree().notes
+ .filter((note) => !note.isProtected && paths.getNotePath(note.noteId) !== null);
+ const included = new Set(notes.map((note) => note.noteId));
+ const isVisible = (noteId: string) => included.has(noteId);
+
+ const attachments: Record<string, ReturnType<typeof getAttachmentRow>[]> = {};
+ const attachmentPaths: Record<string, string> = {};
+ const images: Record<string, string> = {};
+ for (const note of notes) {
+ const owned = note.getAttachments();
+ attachments[note.noteId] = owned.map(getAttachmentRow);
+ for (const attachment of owned) {
+ const path = paths.getAttachmentPath(attachment.attachmentId ?? "");
+ if (path !== null) {
+ attachmentPaths[attachment.attachmentId ?? ""] = path;
+ }
+ }
+
+ const imagePath = writeImage(note, files, attachmentPaths);
+ if (imagePath) {
+ images[note.noteId] = imagePath;
+ }
+ }
+
+ const rows = buildFrocaRows(notes, (note) => isVisible(note.noteId),
+ (note) => paths.getNotePath(note.noteId) ?? "");
+ writeJson("data/rows.json", { ...rows, attachments, attachmentPaths, images });
+
+ for (const note of getNotesReadByViews(notes)) {
+ writeJson(`data/blobs/notes/${note.noteId}.json`, getBlobRow(note));
+ for (const attachment of note.getAttachments()) {
+ if (attachment.hasStringContent()) {
+ writeJson(`data/blobs/attachments/${attachment.attachmentId}.json`,
+ getBlobRow(attachment));
+ }
+ }
+ }
+
+ for (const note of notes) {
+ if (note.type === "noteMap") {
+ writeNoteMaps(note, files, isVisible);
+ } else if (note.type === "relationMap") {
+ writeJson(`data/relation-map/${note.noteId}.json`,
+ buildVisibleRelationMap(note.noteId, note.getContent(), isVisible));
+ }
+
+ const scripts = note.type === "render" ? note.getRelations("renderNote") : [];
+ for (const script of scripts.flatMap((relation) => relation.targetNote ?? [])) {
+ const bundle = buildVisibleScriptBundle(script, isVisible);
+ if (bundle) {
+ writeJson(`data/script/${script.noteId}.json`, bundle);
+ }
+ }
+ }
+ return files;
+}
+
+/** The view type of a collection, the grid when it names none, or `null` for another note. */
+export function getViewTypeOf(note: BNote) {
+ return note.type === "book" ? note.getLabelValue("viewType") || "grid" : null;
+}
+
+/** Whether a page of the export hosts an app view for `note`. */
+export function isHostedNote(note: BNote) {
+ const viewType = getViewTypeOf(note);
+ return viewType
+ ? SHARE_HOSTED_VIEW_TYPES.includes(viewType)
+ : SHARE_HOSTED_NOTE_TYPES.includes(note.type);
+}
+
+/**
+ * The notes whose content the app views of `notes` read: each hosted note, its children (a
+ * collection's cards, a dashboard's widgets, a presentation's slides) and theirs (vertical
+ * slides), of `notes` alone.
+ */
+function getNotesReadByViews(notes: BNote[]) {
+ const byId = new Map(notes.map((note) => [ note.noteId, note ]));
+ const read = new Map<string, BNote>();
+ for (const hosted of notes.filter(isHostedNote)) {
+ const children = hosted.getChildNotes();
+ const grandchildren = children.flatMap((child) => child.getChildNotes());
+ for (const note of [ hosted, ...children, ...grandchildren ]) {
+ const exported = byId.get(note.noteId);
+ if (exported) {
+ read.set(exported.noteId, exported);
+ }
+ }
+ }
+ return [ ...read.values() ];
+}
+
+/**
+ * Writes the image of an image note under `data/images/` and returns its path; for a note whose
+ * image is an attachment, such as a canvas's `canvas-export.svg`, returns that attachment's path.
+ */
+function writeImage(
+ note: BNote,
+ files: Map<string, string | Uint8Array>,
+ attachmentPaths: Record<string, string>
+) {
+ if (note.type === "image") {
+ const path = `data/images/${note.noteId}.${mimeTypes.extension(note.mime) || "dat"}`;
+ files.set(path, note.getContent());
+ return path;
+ }
+
+ const title = getImageAttachmentTitle(note.type);
+ const attachment = title
+ ? note.getAttachments().find((candidate) => candidate.title === title)
+ : undefined;
+ return attachment ? attachmentPaths[attachment.attachmentId ?? ""] : undefined;
+}
+
+/**
+ * Writes both maps of a note map note, which a reader can switch between, rooted where the app
+ * roots it: at `#mapRootNoteId`, else at the note's parent.
+ */
+function writeNoteMaps(
+ note: BNote,
+ files: Map<string, string | Uint8Array>,
+ isVisible: IsVisibleNote
+) {
+ const mapRootNoteId = note.getLabelValue("mapRootNoteId")
+ || note.getParentBranches()[0]?.parentNoteId;
+ const mapRoot = mapRootNoteId ? becca.getNote(mapRootNoteId) : null;
+ if (!mapRoot) {
+ return;
+ }
+
+ const filters = {
+ excludeRelations: note.getLabels("mapExcludeRelation").map((label) => label.value),
+ includeRelations: note.getLabels("mapIncludeRelation").map((label) => label.value)
+ };
+ for (const mapType of [ "tree", "link" ] as const) {
+ files.set(`data/note-map/${mapRoot.noteId}-${mapType}.json`,
+ JSON.stringify(buildVisibleNoteMap(mapRoot, mapType, filters, isVisible)));
+ }
+}
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 5ed7b2b6d6..1447b0220f 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
@@ -29,6 +29,13 @@ vi.mock("../../log.js", () => ({ getLog: () => mockLog }));
const mockOptions = { getOptionOrNull: vi.fn((_name: string): string | null => null) };
vi.mock("../../options.js", () => ({ default: mockOptions }));
+const buildShareData = vi.hoisted(() => vi.fn((..._args: unknown[]) =>
+ new Map([ [ "data/rows.json", "{}" ] ])));
+vi.mock("./share_data.js", async (importOriginal) => ({
+ ...await importOriginal<typeof import("./share_data.js")>(),
+ buildShareData
+}));
+
const {
default: ShareThemeExportProvider,
hasMermaidDiagrams,
@@ -324,6 +331,37 @@ describe("ShareThemeExportProvider", () => {
expect(appendCalls.map((c) => c.options.name)).toContain("404.html");
});
+ it("writes the data of the app views, with the paths of the notes and attachments", () => {
+ const p = makeProvider();
+ (p as any).indexMeta = null;
+ (p as any).getNoteTargetUrl.mockImplementation((id: string) => `dir/${id}.html`);
+ const rootMeta: any = {
+ noteId: "rootNote",
+ title: "Root",
+ children: [
+ { noteId: "child", attachments: [
+ { attachmentId: "att", dataFileName: "child_a b.svg" },
+ { dataFileName: "untracked.svg" }
+ ] },
+ { noteId: "clone", isClone: true, attachments: [
+ { attachmentId: "cloneAtt", dataFileName: "clone.svg" }
+ ] }
+ ]
+ };
+
+ p.afterDone(rootMeta);
+
+ expect(findAppend("data/rows.json").data).toBe("{}");
+ const paths = buildShareData.mock.lastCall?.[1] as {
+ getNotePath(noteId: string): string | null;
+ getAttachmentPath(attachmentId: string): string | null;
+ } | undefined;
+ expect(paths?.getNotePath("rootNote")).toBe("");
+ expect(paths?.getNotePath("child")).toBe("dir/child.html");
+ expect(paths?.getAttachmentPath("att")).toBe("dir/child_a%20b.svg");
+ expect(paths?.getAttachmentPath("cloneAtt")).toBeNull();
+ });
+
it("logs an error and skips a font when its data cannot be found", () => {
const p = makeProvider();
(p as any).indexMeta = null;
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 8b8f4006da..223a705295 100644
--- a/packages/trilium-core/src/services/export/zip/share_theme.ts
+++ b/packages/trilium-core/src/services/export/zip/share_theme.ts
@@ -1,7 +1,4 @@
-import {
- getShareThemeGroupFiles, SHARE_HOSTED_NOTE_TYPES, SHARE_HOSTED_VIEW_TYPES,
- type ShareThemeManifest
-} from "@triliumnext/commons";
+import { getShareThemeGroupFiles, type ShareThemeManifest } from "@triliumnext/commons";
import ejs from "ejs";
import { convert as convertToText } from "html-to-text";
import { t } from "i18next";
@@ -15,6 +12,7 @@ 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";
+import { buildShareData, getViewTypeOf, isHostedNote } from "./share_data.js";
/** The static files a share-theme export copies into the archive, read by each platform its own way. */
export interface ShareThemeExportAssets {
@@ -125,6 +123,7 @@ export default class ShareThemeExportProvider extends ZipExportProvider {
this.#saveAssets();
this.#saveIndex(rootMeta);
this.#save404();
+ this.#saveData(rootMeta);
// Search index
for (const item of this.searchIndex.values()) {
@@ -184,6 +183,34 @@ export default class ShareThemeExportProvider extends ZipExportProvider {
}
}
+ #saveData(rootMeta: NoteMeta) {
+ const attachmentPaths = new Map<string, string>();
+ const collect = (noteMeta: NoteMeta) => {
+ const notePath = noteMeta.isClone || !noteMeta.noteId
+ ? null : this.getNoteTargetUrl(noteMeta.noteId, rootMeta);
+ const directory = notePath?.slice(0, notePath.lastIndexOf("/") + 1);
+ for (const attachment of notePath === null ? [] : noteMeta.attachments ?? []) {
+ if (attachment.attachmentId) {
+ attachmentPaths.set(attachment.attachmentId,
+ `${directory}${encodeURIComponent(attachment.dataFileName)}`);
+ }
+ }
+ for (const child of noteMeta.children ?? []) {
+ collect(child);
+ }
+ };
+ collect(rootMeta);
+
+ const data = buildShareData(this.branch.getNote(), {
+ getNotePath: (noteId) => (noteId === rootMeta.noteId
+ ? "" : this.getNoteTargetUrl(noteId, rootMeta)),
+ getAttachmentPath: (attachmentId) => attachmentPaths.get(attachmentId) ?? null
+ });
+ for (const [ name, content ] of data) {
+ this.archive.append(content, { name });
+ }
+ }
+
#save404() {
const content = ejs.render(readShareTemplate("404"), { t });
this.archive.append(content, { name: "404.html" });
@@ -232,8 +259,12 @@ export function getAppViewGroups(note: BNote) {
continue;
}
+ if (!isHostedNote(subtreeNote)) {
+ continue;
+ }
+
const viewType = getViewTypeOf(subtreeNote);
- if (viewType && SHARE_HOSTED_VIEW_TYPES.includes(viewType)) {
+ if (viewType) {
groups.add(`view:${viewType}`);
if (CONTENT_VIEW_TYPES.includes(viewType)) {
for (const drawn of getDrawnNotes(subtreeNote)) {
@@ -244,7 +275,7 @@ export function getAppViewGroups(note: BNote) {
}
}
}
- } else if (SHARE_HOSTED_NOTE_TYPES.includes(subtreeNote.type)) {
+ } else {
groups.add(`type:${subtreeNote.type}`);
}
}
@@ -261,11 +292,6 @@ export function getAppViewGroups(note: BNote) {
/** 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/froca_payload.spec.ts b/packages/trilium-core/src/share/froca_payload.spec.ts
new file mode 100644
index 0000000000..54853fe9d4
--- /dev/null
+++ b/packages/trilium-core/src/share/froca_payload.spec.ts
@@ -0,0 +1,39 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+import becca from "../becca/becca.js";
+import scriptService from "../services/script.js";
+import { buildNote } from "../test/becca_easy_mocking.js";
+import { buildVisibleRelationMap, buildVisibleScriptBundle } from "./froca_payload.js";
+
+describe("hosted view data", () => {
+ beforeEach(() => {
+ becca.reset();
+ });
+
+ it("builds no script bundle for a backend script or one that cannot be read", () => {
+ const bundle = vi.spyOn(scriptService, "getScriptBundleForFrontend");
+ const backend = buildNote({ type: "code", mime: "application/javascript;env=backend" });
+ const frontend = buildNote({ type: "code", mime: "application/javascript;env=frontend" });
+
+ expect(buildVisibleScriptBundle(backend, () => true)).toBeUndefined();
+ expect(bundle).not.toHaveBeenCalled();
+ bundle.mockImplementationOnce(() => {
+ throw new Error("Protected module");
+ });
+ expect(buildVisibleScriptBundle(frontend, () => true)).toBeUndefined();
+ bundle.mockReturnValueOnce(undefined);
+ expect(buildVisibleScriptBundle(frontend, () => true)).toBeUndefined();
+ bundle.mockReturnValueOnce({ script: "run()", html: "" } as never);
+ expect(buildVisibleScriptBundle(frontend, () => false)).toMatchObject({ script: "run()" });
+ bundle.mockRestore();
+ });
+
+ it("places no note on a relation map whose content is not its JSON", () => {
+ const map = buildNote({ type: "relationMap" });
+
+ expect(buildVisibleRelationMap(map.noteId, "{", () => true).noteTitles).toEqual({});
+ expect(buildVisibleRelationMap(map.noteId, new Uint8Array(), () => true).noteTitles)
+ .toEqual({});
+ expect(buildVisibleRelationMap(map.noteId, "{}", () => true).noteTitles).toEqual({});
+ });
+});
diff --git a/packages/trilium-core/src/share/froca_payload.ts b/packages/trilium-core/src/share/froca_payload.ts
index 35a218b6d1..8f3cca1abf 100644
--- a/packages/trilium-core/src/share/froca_payload.ts
+++ b/packages/trilium-core/src/share/froca_payload.ts
@@ -1,10 +1,15 @@
import { getShareLink } from "@triliumnext/share-theme/model/page";
import becca from "../becca/becca.js";
+import type BAttachment from "../becca/entities/battachment.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 { buildLinkMap, buildTreeMap } from "../routes/api/note_map.js";
+import { buildRelationMap } from "../routes/api/relation-map.js";
import * as sanitize from "../services/sanitizer.js";
+import { decodeUtf8 } from "../services/utils/binary.js";
+import scriptService from "../services/script.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";
@@ -110,21 +115,28 @@ export function buildFrocaRows<T extends PayloadNote>(
}
/** An attachment as the row `froca` reads attachments from. */
-export function getAttachmentRow(attachment: SAttachment) {
- const pojo = attachment.getPojo();
+export function getAttachmentRow(attachment: SAttachment | BAttachment) {
+ const { attachmentId, role, mime, title, blobId, utcDateModified } = attachment.getPojo();
return {
- ...pojo,
+ attachmentId,
+ role,
+ mime,
+ title,
+ blobId,
+ utcDateModified,
ownerId: attachment.ownerId,
- dateModified: pojo.utcDateModified,
+ dateModified: utcDateModified,
utcDateScheduledForErasureSince: null,
contentLength: 0
};
}
/** The content of a note or an attachment as the blob row `froca.getBlob()` reads. */
-export function getBlobRow(entity: SNote | SAttachment) {
- const content = entity.hasStringContent() ? String(entity.getContent()) : null;
- const utcDateModified = "noteId" in entity ? entity.utcDateModified : entity.getPojo().utcDateModified;
+export function getBlobRow(entity: SNote | BNote | SAttachment | BAttachment) {
+ const raw = entity.hasStringContent() ? entity.getContent() : null;
+ const content = raw instanceof Uint8Array ? decodeUtf8(raw) : raw ?? null;
+ const utcDateModified = ("ownerId" in entity
+ ? entity.getPojo().utcDateModified : entity.utcDateModified) ?? "";
return {
blobId: utcDateModified,
content,
@@ -134,6 +146,73 @@ export function getBlobRow(entity: SNote | SAttachment) {
};
}
+/** Whether a note can appear in a hosted view's data, by its ID. */
+export type IsVisibleNote = (noteId: string) => boolean;
+
+/**
+ * The note map of `mapRoot` the app draws, as a tree or as its relations, with only the notes
+ * `isVisible` accepts and the links between them.
+ */
+export function buildVisibleNoteMap(
+ mapRoot: BNote,
+ mapType: "tree" | "link",
+ filters: { excludeRelations: string[]; includeRelations: string[] },
+ isVisible: IsVisibleNote
+) {
+ const map = mapType === "tree" ? buildTreeMap(mapRoot) : buildLinkMap(mapRoot, filters);
+ const notes = map.notes.filter(([ noteId ]) => isVisible(noteId));
+ const noteIds = new Set(notes.map(([ noteId ]) => noteId));
+ return {
+ notes,
+ links: map.links.filter((link) => noteIds.has(link.sourceNoteId) && noteIds.has(link.targetNoteId)),
+ noteIdToDescendantCountMap: Object.fromEntries(Object.entries(map.noteIdToDescendantCountMap)
+ .filter(([ noteId ]) => noteIds.has(noteId)))
+ };
+}
+
+/**
+ * The relations the relation map note `mapNoteId` draws between the notes its `content` places
+ * on the map, of which only those `isVisible` accepts.
+ */
+export function buildVisibleRelationMap(
+ mapNoteId: string,
+ content: string | Uint8Array | null | undefined,
+ isVisible: IsVisibleNote
+) {
+ const noteIds = readPlacedNoteIds(content).filter(isVisible);
+ return buildRelationMap(becca.getNote(mapNoteId), noteIds);
+}
+
+/**
+ * The frontend bundle of the script note `note`, which a render note runs, or `undefined` when
+ * `note` is a backend script, a module is protected, or `isVisible` refuses a note the bundle is
+ * built from, as the bundle carries the source of each.
+ */
+export function buildVisibleScriptBundle(note: BNote, isVisible: IsVisibleNote) {
+ if (note.isJavaScript() && note.getScriptEnv() === "backend") {
+ return undefined;
+ }
+
+ let bundle;
+ try {
+ bundle = scriptService.getScriptBundleForFrontend(note);
+ } catch {
+ // A protected module cannot be read.
+ return undefined;
+ }
+ return bundle && (bundle.allNoteIds ?? []).every(isVisible) ? bundle : undefined;
+}
+
+/** Returns the IDs of the notes a relation map's content places on the map. */
+function readPlacedNoteIds(content: string | Uint8Array | null | undefined) {
+ try {
+ const data = JSON.parse(typeof content === "string" ? content : "") as { notes?: { noteId?: unknown }[] };
+ return (data.notes ?? []).map((entry) => entry.noteId).filter((noteId) => typeof noteId === "string");
+ } catch {
+ return [];
+ }
+}
+
function getBranchRow(branch: SBranch | BBranch) {
const { branchId, noteId, parentNoteId, notePosition, prefix, isExpanded } = branch.getPojo();
return {
diff --git a/packages/trilium-core/src/share/handlers.ts b/packages/trilium-core/src/share/handlers.ts
index e05cbb76ba..44deacbf64 100644
--- a/packages/trilium-core/src/share/handlers.ts
+++ b/packages/trilium-core/src/share/handlers.ts
@@ -3,17 +3,17 @@ import ejs from "ejs";
import { t } from "i18next";
import becca from "../becca/becca.js";
-import { buildLinkMap, buildTreeMap } from "../routes/api/note_map.js";
-import { buildRelationMap } from "../routes/api/relation-map.js";
import attributeService from "../services/attributes.js";
import { getCrypto } from "../services/encryption/crypto.js";
-import scriptService from "../services/script.js";
import searchService from "../services/search/services/search.js";
import SearchContext from "../services/search/search_context.js";
import { decodeBase64, decodeUtf8, encodeUtf8 } from "../services/utils/binary.js";
import * as utils from "../services/utils/index.js";
import { readShareTemplate, renderNoteContent } from "./content_renderer.js";
-import { buildFrocaRows, getAttachmentRow, getBlobRow } from "./froca_payload.js";
+import {
+ buildFrocaRows, buildVisibleNoteMap, buildVisibleRelationMap, buildVisibleScriptBundle,
+ getAttachmentRow, getBlobRow
+} from "./froca_payload.js";
import { SHARE_ROUTE_PATHS, type ShareRoutePath } from "./route_paths.js";
import { isShareReady } from "./share_provider.js";
import type SAttachment from "./shaca/entities/sattachment.js";
@@ -209,24 +209,10 @@ function getNoteMap(req: ShareRequest): ShareReply {
}
const toNames = (value: string | string[] | undefined) => [ value ?? [] ].flat();
- const map = mapType === "tree"
- ? buildTreeMap(mapRoot)
- : buildLinkMap(mapRoot, {
- excludeRelations: toNames(req.query.excludeRelation),
- includeRelations: toNames(req.query.includeRelation)
- });
-
- const notes = map.notes.filter(([ noteId ]) => {
- const note = shaca.getNote(noteId);
- return !!note && !note.isProtected && hasCredentialAccess(note, req);
- });
- const noteIds = new Set(notes.map(([ noteId ]) => noteId));
- return jsonReply(200, {
- notes,
- links: map.links.filter((link) => noteIds.has(link.sourceNoteId) && noteIds.has(link.targetNoteId)),
- noteIdToDescendantCountMap: Object.fromEntries(Object.entries(map.noteIdToDescendantCountMap)
- .filter(([ noteId ]) => noteIds.has(noteId)))
- });
+ return jsonReply(200, buildVisibleNoteMap(mapRoot, mapType, {
+ excludeRelations: toNames(req.query.excludeRelation),
+ includeRelations: toNames(req.query.includeRelation)
+ }, (noteId) => isReadable(noteId, req)));
}
/**
@@ -235,13 +221,8 @@ function getNoteMap(req: ShareRequest): ShareReply {
*/
function getRelationMap(req: ShareRequest): ShareReply {
const mapNote = checkNoteContentAccess(req.params.noteId ?? "", req);
- const placedNoteIds = readPlacedNoteIds(mapNote.getContent());
- const noteIds = placedNoteIds.filter((noteId) => {
- const note = shaca.getNote(noteId);
- return !!note && !note.isProtected && hasCredentialAccess(note, req);
- });
-
- return jsonReply(200, buildRelationMap(becca.getNote(mapNote.noteId), noteIds));
+ return jsonReply(200, buildVisibleRelationMap(mapNote.noteId, mapNote.getContent(),
+ (noteId) => isReadable(noteId, req)));
}
/**
@@ -252,38 +233,16 @@ function getRelationMap(req: ShareRequest): ShareReply {
function getScriptBundle(req: ShareRequest): ShareReply {
const scriptNote = checkNoteContentAccess(req.params.noteId ?? "", req);
const note = becca.getNote(scriptNote.noteId);
- const notFound = jsonReply(404, { message: `Note '${scriptNote.noteId}' has no script to run.` });
- if (!note || (note.isJavaScript() && note.getScriptEnv() === "backend")) {
- return notFound;
- }
-
- let bundle;
- try {
- bundle = scriptService.getScriptBundleForFrontend(note);
- } catch {
- // A protected module cannot be read.
- return notFound;
- }
-
- const isReadable = (noteId: string) => {
- const module = shaca.getNote(noteId);
- return !!module && !module.isProtected && hasCredentialAccess(module, req);
- };
- if (!bundle || !(bundle.allNoteIds ?? []).every(isReadable)) {
- return notFound;
- }
-
- return jsonReply(200, bundle);
+ const bundle = note && buildVisibleScriptBundle(note, (noteId) => isReadable(noteId, req));
+ return bundle
+ ? jsonReply(200, bundle)
+ : jsonReply(404, { message: `Note '${scriptNote.noteId}' has no script to run.` });
}
-/** Returns the IDs of the notes a relation map's content places on the map. */
-function readPlacedNoteIds(content: string | Uint8Array | null | undefined) {
- try {
- const data = JSON.parse(typeof content === "string" ? content : "") as { notes?: { noteId?: unknown }[] };
- return (data.notes ?? []).map((entry) => entry.noteId).filter((noteId) => typeof noteId === "string");
- } catch {
- return [];
- }
+/** Whether the shared note `noteId` is one the caller can read. */
+function isReadable(noteId: string, req: ShareRequest) {
+ const note = shaca.getNote(noteId);
+ return !!note && !note.isProtected && hasCredentialAccess(note, req);
}
function getNoteAttachments(req: ShareRequest): ShareReply {