Skip to content

Build your personal knowledge base with Trilium Notes

feat(share): key the static export's note maps by their request

Elian Doranf3fa99a5518a
diff --git a/packages/commons/src/lib/share_hosting.spec.ts b/packages/commons/src/lib/share_hosting.spec.ts
index b468a5e404..1aefb5ea7d 100644
--- a/packages/commons/src/lib/share_hosting.spec.ts
+++ b/packages/commons/src/lib/share_hosting.spec.ts
@@ -1,6 +1,21 @@
import { describe, expect, it } from "vitest";
-import { getShareThemeGroupFiles, resolveShareThemeGroups } from "./share_hosting.js";
+import {
+ getNoteMapDataKey, getShareThemeGroupFiles, resolveShareThemeGroups
+} from "./share_hosting.js";
+
+describe("getNoteMapDataKey", () => {
+ it("keys a note map request by its root, kind and filters in any order", () => {
+ const key = getNoteMapDataKey("root", "link",
+ { excludeRelations: [ "b", "a" ], includeRelations: [ "c d" ] });
+
+ expect(key).toBe("root/link?exclude=a&exclude=b&include=c+d");
+ expect(getNoteMapDataKey("root", "link",
+ { excludeRelations: [ "a", "b" ], includeRelations: [ "c d" ] })).toBe(key);
+ expect(getNoteMapDataKey("root", "tree", { excludeRelations: [], includeRelations: [] }))
+ .toBe("root/tree?");
+ });
+});
describe("share theme groups", () => {
const manifest = {
diff --git a/packages/commons/src/lib/share_hosting.ts b/packages/commons/src/lib/share_hosting.ts
index 394dcd5cda..bc3d54a8cd 100644
--- a/packages/commons/src/lib/share_hosting.ts
+++ b/packages/commons/src/lib/share_hosting.ts
@@ -36,6 +36,22 @@ export function resolveShareThemeGroups(
return [ ...resolved ];
}
+/**
+ * Returns the key of a note map request, the same for the same root, kind and relation filters in
+ * any order, under which the static export indexes the map it computed for that request.
+ */
+export function getNoteMapDataKey(
+ mapRootNoteId: string,
+ mapType: "tree" | "link",
+ filters: { excludeRelations: string[]; includeRelations: string[] }
+) {
+ const params = new URLSearchParams([
+ ...[ ...filters.excludeRelations ].sort().map((name) => [ "exclude", name ]),
+ ...[ ...filters.includeRelations ].sort().map((name) => [ "include", name ])
+ ]);
+ return `${mapRootNoteId}/${mapType}?${params}`;
+}
+
/** 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);
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
index d2ce51701c..32d1d09376 100644
--- a/packages/trilium-core/src/services/export/zip/share_data.spec.ts
+++ b/packages/trilium-core/src/services/export/zip/share_data.spec.ts
@@ -30,6 +30,9 @@ describe("buildShareData", () => {
content: JSON.stringify({ notes: [ { noteId: "event" }, { noteId: "outside" } ] }) },
{ id: "map", type: "noteMap", content: "", "#mapRootNoteId": "calendar",
"#mapExcludeRelation": "template", "#mapIncludeRelation": "related" },
+ { id: "twinMap", type: "noteMap", content: "", "#mapIncludeRelation": "related",
+ "#mapExcludeRelation": "template", "#mapRootNoteId": "calendar" },
+ { id: "plainMap", type: "noteMap", content: "", "#mapRootNoteId": "calendar" },
{ id: "hoistedMap", type: "noteMap", content: "", "#mapRootNoteId": "hoisted" },
{ id: "parentMap", type: "noteMap", content: "" },
{ id: "render", type: "render", content: "", "~renderNote": "script" },
@@ -63,13 +66,17 @@ describe("buildShareData", () => {
"data/blobs/notes/image.json",
"data/blobs/notes/map.json",
"data/blobs/notes/parentMap.json",
+ "data/blobs/notes/plainMap.json",
"data/blobs/notes/relations.json",
"data/blobs/notes/render.json",
+ "data/blobs/notes/twinMap.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/note-map/0.json",
+ "data/note-map/1.json",
+ "data/note-map/2.json",
+ "data/note-map/3.json",
+ "data/note-map/4.json",
+ "data/note-map/5.json",
"data/relation-map/relations.json",
"data/rows.json",
"data/script/script.json"
@@ -79,7 +86,7 @@ describe("buildShareData", () => {
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"
+ "plainMap", "relations", "render", "script", "siteRoot", "twinMap"
]);
expect(rows.links.siteRoot).toBe("");
expect(rows.links.event).toBe("pages/event.html");
@@ -96,7 +103,11 @@ describe("buildShareData", () => {
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(Object.keys(rows.noteMaps).sort()).toEqual([
+ "calendar/link?", "calendar/link?exclude=template&include=related", "calendar/tree?",
+ "calendar/tree?exclude=template&include=related", "siteRoot/link?", "siteRoot/tree?"
+ ]);
+ const treeMap = read(rows.noteMaps["calendar/tree?exclude=template&include=related"]);
expect(treeMap.notes.map(([ noteId ]: [ string ]) => noteId).sort())
.toEqual([ "calendar", "event" ]);
expect(read("data/script/script.json").script).toBe("run()");
diff --git a/packages/trilium-core/src/services/export/zip/share_data.ts b/packages/trilium-core/src/services/export/zip/share_data.ts
index 62cc8cfbb5..9432856a2f 100644
--- a/packages/trilium-core/src/services/export/zip/share_data.ts
+++ b/packages/trilium-core/src/services/export/zip/share_data.ts
@@ -1,5 +1,5 @@
import {
- getImageAttachmentTitle, SHARE_HOSTED_NOTE_TYPES, SHARE_HOSTED_VIEW_TYPES
+ getImageAttachmentTitle, getNoteMapDataKey, SHARE_HOSTED_NOTE_TYPES, SHARE_HOSTED_VIEW_TYPES
} from "@triliumnext/commons";
import mimeTypes from "mime-types";
@@ -22,12 +22,13 @@ export interface ShareDataPaths {
* 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/rows.json`: the rows of every note of the export, with its attachments, the path of
+ * each note's page, of each attachment's file and of each note's image, and the file of each
+ * note map by its {@link getNoteMapDataKey} key;
* - `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/note-map/<index>.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.
@@ -59,10 +60,6 @@ export function buildShareData(root: BNote, paths: ShareDataPaths) {
}
}
- 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()) {
@@ -73,9 +70,10 @@ export function buildShareData(root: BNote, paths: ShareDataPaths) {
}
}
+ const noteMaps: Record<string, string> = {};
for (const note of notes) {
if (note.type === "noteMap") {
- writeNoteMaps(note, files, isVisible);
+ writeNoteMaps(note, files, noteMaps, isVisible);
} else if (note.type === "relationMap") {
writeJson(`data/relation-map/${note.noteId}.json`,
buildVisibleRelationMap(note.noteId, note.getContent(), isVisible));
@@ -89,6 +87,10 @@ export function buildShareData(root: BNote, paths: ShareDataPaths) {
}
}
}
+
+ const rows = buildFrocaRows(notes, (note) => isVisible(note.noteId),
+ (note) => paths.getNotePath(note.noteId) ?? "");
+ writeJson("data/rows.json", { ...rows, attachments, attachmentPaths, images, noteMaps });
return files;
}
@@ -150,11 +152,14 @@ function writeImage(
/**
* 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.
+ * roots it: at `#mapRootNoteId`, else at the note's parent. Each goes into `noteMaps` by its
+ * request's key, so maps of one root with other relation filters keep their own file, and a request
+ * already written is not written again.
*/
function writeNoteMaps(
note: BNote,
files: Map<string, string | Uint8Array>,
+ noteMaps: Record<string, string>,
isVisible: IsVisibleNote
) {
const mapRootNoteId = note.getLabelValue("mapRootNoteId")
@@ -169,7 +174,13 @@ function writeNoteMaps(
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)));
+ const key = getNoteMapDataKey(mapRoot.noteId, mapType, filters);
+ if (noteMaps[key]) {
+ continue;
+ }
+
+ const path = `data/note-map/${Object.keys(noteMaps).length}.json`;
+ noteMaps[key] = path;
+ files.set(path, JSON.stringify(buildVisibleNoteMap(mapRoot, mapType, filters, isVisible)));
}
}