Skip to content

Build your personal knowledge base with Trilium Notes

fix(text): address automated review on include-note rendering

Elian Doran8cb8bda7a994
diff --git a/apps/client/src/services/content_renderer_text.spec.ts b/apps/client/src/services/content_renderer_text.spec.ts
index 7a966ad66c..bddde7b18f 100644
--- a/apps/client/src/services/content_renderer_text.spec.ts
+++ b/apps/client/src/services/content_renderer_text.spec.ts
@@ -298,6 +298,27 @@ describe("Nested include notes (single-level display vs recursive print)", () =>
expect(contentEl.querySelector("a.reference-link")).toBeNull();
});
+ it("on print, expands a note shared across sibling branches in each branch (not a false cycle)", async () => {
+ // Diamond: A includes B and C; both B and C include D. D is not a cycle, so under recursive
+ // expansion it must render in both branches (the ancestor path is tracked per-branch).
+ buildNote({ id: "dagD", title: "Note D", content: "<p>D body</p>" });
+ buildNote({ id: "dagB", title: "Note B", content: `<p>B body</p><section class="include-note" data-note-id="dagD" data-box-size="medium">&nbsp;</section>` });
+ buildNote({ id: "dagC", title: "Note C", content: `<p>C body</p><section class="include-note" data-note-id="dagD" data-box-size="medium">&nbsp;</section>` });
+ const noteA = buildNote({
+ id: "dagA",
+ title: "Note A",
+ content: trimIndentation`
+ <section class="include-note" data-note-id="dagB" data-box-size="medium">&nbsp;</section>
+ <section class="include-note" data-note-id="dagC" data-box-size="medium">&nbsp;</section>
+ `
+ });
+ const contentEl = document.createElement("div");
+ await renderText(noteA, $(contentEl), { expandNestedIncludes: true });
+
+ expect((contentEl.textContent?.match(/D body/g) ?? []).length).toBe(2);
+ expect(contentEl.querySelector("a.reference-link")).toBeNull();
+ });
+
it("renders a note's own includes as reference links when includesAsReferenceLinks is set", async () => {
// This mirrors how an already-included note (e.g. the editor include widget) is rendered:
// its content shows, but its own includes degrade to reference links.
diff --git a/apps/client/src/services/content_renderer_text.ts b/apps/client/src/services/content_renderer_text.ts
index 0af66524d1..3b43b184c5 100644
--- a/apps/client/src/services/content_renderer_text.ts
+++ b/apps/client/src/services/content_renderer_text.ts
@@ -40,7 +40,7 @@ export async function postProcessRichContent(note: FNote | FAttachment, $rendere
} else if (options.includesAsReferenceLinks) {
// This note is itself an included note in display mode: stop after the first level by
// degrading its own includes to reference links instead of expanding them.
- await replaceIncludesWithReferenceLinks($renderedContent[0]);
+ replaceIncludesWithReferenceLinks($renderedContent[0]);
} else {
await renderIncludedNotes($renderedContent[0], seenNoteIds, options.expandNestedIncludes ?? false);
}
@@ -106,30 +106,32 @@ async function renderIncludedNotes(contentEl: HTMLElement, seenNoteIds: Set<stri
// On display only the first level is expanded: the included note is rendered with its own
// includes degraded to reference links. Printing/export keeps expanding recursively.
+ // Clone seenNoteIds per descent so it tracks the current ancestor path only — sibling
+ // branches must not pollute each other (a note included in two sub-trees is not a cycle).
const renderedContent = (await content_renderer.getRenderedContent(note, expandNested
- ? { seenNoteIds, expandNestedIncludes: true }
- : { seenNoteIds, includesAsReferenceLinks: true }
+ ? { seenNoteIds: new Set(seenNoteIds), expandNestedIncludes: true }
+ : { seenNoteIds: new Set(seenNoteIds), includesAsReferenceLinks: true }
)).$renderedContent;
includeNoteEl.replaceChildren(...renderedContent);
}
}
/**
- * Replace each `section.include-note` in the given content with a reference link to the included
- * note, without expanding it. Used on display to stop inclusion after the first level so a note's
- * transitive include graph is not rendered inline.
+ * Replace each `section.include-note` in the given content with a bare reference link to the
+ * included note, without expanding it. Used on display to stop inclusion after the first level so a
+ * note's transitive include graph is not rendered inline. The link's title, icon and colour are
+ * filled in by the reference-link post-processing pass in `postProcessRichContent` (the same pass
+ * that resolves reference links authored in the note), which also batch-prefetches the note.
*/
-async function replaceIncludesWithReferenceLinks(contentEl: HTMLElement) {
+function replaceIncludesWithReferenceLinks(contentEl: HTMLElement) {
for (const includeNoteEl of contentEl.querySelectorAll("section.include-note")) {
const noteId = includeNoteEl.getAttribute("data-note-id");
if (!noteId) continue;
- const $referenceLink = await link.createLink(noteId, {
- referenceLink: true,
- showNoteIcon: true,
- showTooltip: false
- });
- includeNoteEl.replaceWith(...$referenceLink);
+ const referenceLink = document.createElement("a");
+ referenceLink.className = "reference-link";
+ referenceLink.setAttribute("href", `#root/${noteId}`);
+ includeNoteEl.replaceWith(referenceLink);
}
}
diff --git a/apps/server/src/share/content_renderer.spec.ts b/apps/server/src/share/content_renderer.spec.ts
index 1e8381606e..b1b6339637 100644
--- a/apps/server/src/share/content_renderer.spec.ts
+++ b/apps/server/src/share/content_renderer.spec.ts
@@ -96,6 +96,21 @@ describe("content_renderer", () => {
expect(result.content).not.toContain("reference-link");
});
+ it("expands a note shared across sibling branches in each branch when exporting (not a false cycle)", () => {
+ buildShareNote({ id: "dagD", title: "Note D", content: "<p>D body</p>" });
+ buildShareNote({ id: "dagB", content: `<p>B body</p><section class="include-note" data-note-id="dagD" data-box-size="medium">&nbsp;</section>` });
+ buildShareNote({ id: "dagC", content: `<p>C body</p><section class="include-note" data-note-id="dagD" data-box-size="medium">&nbsp;</section>` });
+ const noteA = buildShareNote({
+ id: "dagA",
+ content: `<section class="include-note" data-note-id="dagB" data-box-size="medium">&nbsp;</section><section class="include-note" data-note-id="dagC" data-box-size="medium">&nbsp;</section>`
+ });
+ const result = getContent(noteA, { expandNestedIncludes: true });
+ if (typeof result.content !== "string") throw new Error("expected string content");
+ // Diamond A→{B,C}→D: D is not a cycle, so it expands in both branches.
+ expect((result.content.match(/D body/g) ?? []).length).toBe(2);
+ expect(result.content).not.toContain("reference-link");
+ });
+
it("does not loop on a circular include chain when expanding recursively", () => {
buildShareNote({
id: "cycB",
diff --git a/apps/server/src/share/content_renderer.ts b/apps/server/src/share/content_renderer.ts
index 72989a6355..33ee378bc1 100644
--- a/apps/server/src/share/content_renderer.ts
+++ b/apps/server/src/share/content_renderer.ts
@@ -398,9 +398,10 @@ function renderText(result: Result, note: SNote | BNote, options: ShareRenderOpt
}
// Process include notes. The share view renders only the first level of inclusion; static export
- // (expandNestedIncludes) keeps expanding recursively. A shared seenNoteIds set guards the
- // recursive path against cycles that would otherwise loop forever.
- const seenNoteIds = options.seenNoteIds ?? new Set<string>();
+ // (expandNestedIncludes) keeps expanding recursively. seenNoteIds tracks the current ancestor
+ // path (cloned per descent below) so the recursive path can break cycles without treating a note
+ // included in two sibling sub-trees as circular.
+ const seenNoteIds = new Set(options.seenNoteIds);
seenNoteIds.add(note.noteId);
for (const includeNoteEl of document.querySelectorAll("section.include-note")) {
const noteId = includeNoteEl.getAttribute("data-note-id");
@@ -412,13 +413,13 @@ function renderText(result: Result, note: SNote | BNote, options: ShareRenderOpt
// Deeper-than-first-level includes (and any cycle in the recursive path) degrade to a
// reference link that the link-processing passes below resolve to the shared note.
if (options.includesAsReferenceLinks || seenNoteIds.has(noteId)) {
- includeNoteEl.replaceWith(...parse(`<a class="reference-link" href="#root/${noteId}">${escapeHtml(includedNote.title)}</a>`, parseOpts).childNodes);
+ includeNoteEl.replaceWith(...parse(`<a class="reference-link" href="#root/${escapeHtml(noteId)}">${escapeHtml(includedNote.title)}</a>`, parseOpts).childNodes);
continue;
}
const includedResult = getContent(includedNote, options.expandNestedIncludes
- ? { expandNestedIncludes: true, seenNoteIds }
- : { includesAsReferenceLinks: true, seenNoteIds });
+ ? { expandNestedIncludes: true, seenNoteIds: new Set(seenNoteIds) }
+ : { includesAsReferenceLinks: true, seenNoteIds: new Set(seenNoteIds) });
if (typeof includedResult.content !== "string") continue;
const includedDocument = parse(includedResult.content, parseOpts).childNodes;