Skip to content

Build your personal knowledge base with Trilium Notes

fix(share): keep links to a note's own anchors

Elian Doran5b4f50ccce43
diff --git a/packages/trilium-core/src/share/content_renderer.spec.ts b/packages/trilium-core/src/share/content_renderer.spec.ts
index 6f6f98a49f..7e53456bf4 100644
--- a/packages/trilium-core/src/share/content_renderer.spec.ts
+++ b/packages/trilium-core/src/share/content_renderer.spec.ts
@@ -1969,6 +1969,16 @@ describe("content_renderer pages", () => {
expect(document.querySelector("code")?.classList.contains("hljs")).toBe(false);
});
+ it("keeps links to a bookmark of the note itself", () => {
+ const result = getContent(buildShareNote({
+ content: `<h3><a id="schema-note"></a>Note</h3><p>See <a href="#schema-note">Note</a>.</p>`
+ }));
+
+ const document = parse(String(result.content));
+ expect(document.querySelector("#schema-note")?.tagName).toBe("A");
+ expect(document.querySelector("p a")?.getAttribute("href")).toBe("#schema-note");
+ });
+
it("detects the language of a code block naming none, unless it is too long", async () => {
await ensureShareHighlighting();
const long = Array.from({ length: 600 }, (_, index) => `x = ${index}`).join("\n");
diff --git a/packages/trilium-core/src/share/content_renderer.ts b/packages/trilium-core/src/share/content_renderer.ts
index 6957b54cac..abe405561a 100644
--- a/packages/trilium-core/src/share/content_renderer.ts
+++ b/packages/trilium-core/src/share/content_renderer.ts
@@ -675,8 +675,8 @@ function renderText(result: Result, note: SNote | BNote, options: ShareRenderOpt
for (const linkEl of document.querySelectorAll("a")) {
const href = linkEl.getAttribute("href");
- // Preserve footnotes.
- if (href?.startsWith("#fn")) {
+ // Preserve in-page anchors (footnotes, bookmarks): a link to a note starts with `#root`.
+ if (href?.startsWith("#") && !href.startsWith("#root")) {
continue;
}