Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(share): keep a heading's own ID on shared pages
Elian Doran802a6bb132a7
diff --git a/packages/trilium-core/src/services/utils/index.spec.ts b/packages/trilium-core/src/services/utils/index.spec.tsindex 1c218e4726..4c8ed966b6 100644--- a/packages/trilium-core/src/services/utils/index.spec.ts+++ b/packages/trilium-core/src/services/utils/index.spec.ts@@ -852,6 +852,10 @@ describe("#slugifyHeadings", () => {expect(utils.slugifyHeadings(["Notes", "Notes 1", "Notes"])).toStrictEqual(["notes", "notes-1", "notes-2"]);});+ it("skips the reserved slugs", () => {+ expect(utils.slugifyHeadings(["Notes", "Notes"], ["notes", "notes-1"])).toStrictEqual(["notes-2", "notes-3"]);+ });+it("strips HTML tags before slugifying", () => {expect(utils.slugifyHeadings(["<b>Bold</b> Heading"])).toStrictEqual(["bold-heading"]);});diff --git a/packages/trilium-core/src/services/utils/index.ts b/packages/trilium-core/src/services/utils/index.tsindex ccf12b01d3..349a092166 100644--- a/packages/trilium-core/src/services/utils/index.ts+++ b/packages/trilium-core/src/services/utils/index.ts@@ -517,9 +517,10 @@ export function stripTags(text: string) {** This keeps anchor IDs and their table-of-contents links unique on shared* pages, so clicking a duplicate heading in the ToC jumps to the right one.+ * No slug is one of `reserved`, the IDs the page already uses.*/-export function slugifyHeadings(titles: string[]): string[] {- const used = new Set<string>();+export function slugifyHeadings(titles: string[], reserved: Iterable<string> = []): string[] {+ const used = new Set(reserved);return titles.map((title) => {const base = slugify(stripTags(title));let slug = base;diff --git a/packages/trilium-core/src/share/content_renderer.spec.ts b/packages/trilium-core/src/share/content_renderer.spec.tsindex 72b0cd2e55..dfe285d42a 100644--- a/packages/trilium-core/src/share/content_renderer.spec.ts+++ b/packages/trilium-core/src/share/content_renderer.spec.ts@@ -1122,6 +1122,23 @@ describe("content_renderer", () => {expect(parse(content).querySelectorAll(".toc-anchor")).toHaveLength(4);});+ it("keeps a heading's own ID and gives no other heading an ID already on the page", () => {+ const { content, headings } = prepare(trimIndentation`+ <h2 id="footnote-label" class="sr-only">Footnotes</h2>+ <p><a id="footnotes">Bookmark</a> <sup><a aria-describedby="footnote-label">1</a></sup></p>+ <h2>Footnotes</h2>+ `);++ expect(headings).toStrictEqual([+ { level: 2, text: "Footnotes", slug: "footnote-label" },+ { level: 2, text: "Footnotes", slug: "footnotes-1" }+ ]);+ expect(content).toContain(+ `<h2 id="footnote-label" class="sr-only">Footnotes`+ + `<a class="toc-anchor" href="#footnote-label"`);+ expect(content).toContain(`<h2 id="footnotes-1">Footnotes<a class="toc-anchor" href="#footnotes-1"`);+ });+it("gives an image without alt text the generic one and lazy loading, keeping its own", () => {const { content } = prepare(trimIndentation`<p><img src="a.png"></p>diff --git a/packages/trilium-core/src/share/content_renderer.ts b/packages/trilium-core/src/share/content_renderer.tsindex 0a36adfdd1..170187564c 100644--- a/packages/trilium-core/src/share/content_renderer.ts+++ b/packages/trilium-core/src/share/content_renderer.ts@@ -287,10 +287,11 @@ function renderNoteContentInternal(note: SNote | BNote, renderArgs: RenderArgs)}/**- * Prepares the content of a share page for the default template. Every heading gets an ID made from- * its text that is unique on the page, and a link to itself labeled `headingLinkLabel`; the- * headings are returned in document order. An image without `alt` gets `imageAlt`, and one without- * `loading` loads lazily. HTML without headings or images comes back as it is.+ * Prepares the content of a share page for the default template. A heading without an ID gets one+ * made from its text that is unique on the page. Every heading gets a link to its ID labeled+ * `headingLinkLabel`; the headings are returned in document order. An image without `alt` gets+ * `imageAlt`, and one without `loading` loads lazily. HTML without headings or images comes back as+ * it is.*/export function preparePageContent(html: string,@@ -311,13 +312,16 @@ export function preparePageContent(}const elements = document.querySelectorAll("h1, h2, h3, h4, h5, h6");- const slugs = utils.slugifyHeadings(elements.map((element) => element.innerHTML));- const headings = elements.map((element, index) => {- const slug = slugs[index];+ const unnamed = elements.filter((element) => !element.id);+ const slugs = utils.slugifyHeadings(unnamed.map((element) => element.innerHTML),+ document.querySelectorAll("[id]").map((element) => element.id));+ let slugIndex = 0;+ const headings = elements.map((element) => {+ const slug = element.id || slugs[slugIndex++];const text = element.text.replace(/\s+/g, " ").trim();const heading = { level: Number(element.tagName.slice(1)), text, slug };element.setAttribute("id", slug);- element.insertAdjacentHTML("beforeend", `<a class="toc-anchor" href="#${slug}"`+ element.insertAdjacentHTML("beforeend", `<a class="toc-anchor" href="#${escapeHtml(slug)}"`+ ` aria-label="${escapeHtml(options.headingLinkLabel)}">`+ `<span class="tn-icon bx bx-link" aria-hidden="true"></span></a>`);return heading;