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 reference links to notes with a shareAlias clickable (closes #8942)
matt wilkie39cea09671a7
diff --git a/apps/server/src/share/content_renderer.spec.ts b/apps/server/src/share/content_renderer.spec.tsindex 04a4744b7a..2000f53992 100644--- a/apps/server/src/share/content_renderer.spec.ts+++ b/apps/server/src/share/content_renderer.spec.ts@@ -366,6 +366,53 @@ describe("content_renderer", () => {</p>`);});++ it("keeps a reference link whose target has a shareAlias or shareExternalLink", () => {+ buildShareNote({ id: "aliasTarget01", title: "Linux", "#shareAlias": "linux" });+ buildShareNote({+ id: "extTarget0001",+ title: "Ext",+ "#shareExternalLink": "https://example.com/some/page"+ });+ const note = buildShareNote({+ id: "note",+ content: trimIndentation`\+ <p>+ <a class="reference-link" href="#root/zaIItd4TM5Ly/aliasTarget01">+ Old title+ </a>+ <a class="reference-link" href="#root/extTarget0001?viewMode=source">+ Old+ </a>+ </p>+ `+ });+ const result = getContent(note);+ const links = parse(String(result.content)).querySelectorAll("a.reference-link");+ expect(links).toHaveLength(2);++ const [ aliasLink, externalLink ] = links;+ expect(aliasLink.getAttribute("href")).toBe("./linux");+ expect(aliasLink.classList.contains("type-text")).toBe(true);+ expect(aliasLink.querySelector("span.tn-icon")).not.toBeNull();+ expect(aliasLink.text).toBe("Linux");++ expect(externalLink.getAttribute("href")).toBe("https://example.com/some/page");+ expect(externalLink.getAttribute("target")).toBe("_blank");+ expect(externalLink.getAttribute("rel")).toBe("noopener noreferrer");+ expect(externalLink.text).toBe("Ext");+ });++ it("replaces a reference link to a missing attachment with its text", () => {+ buildShareNote({ id: "attachOwner01", title: "Owner" });+ const href = "#root/attachOwner01?viewMode=attachments&attachmentId=missing01";+ const note = buildShareNote({+ id: "note",+ content: `<p><a class="reference-link" href="${href}">clip.mp4</a></p>`+ });+ const result = getContent(note);+ expect(result.content).toStrictEqual("<p>clip.mp4</p>");+ });});});diff --git a/apps/server/src/share/content_renderer.ts b/apps/server/src/share/content_renderer.tsindex 725322bc6e..0b097c9209 100644--- a/apps/server/src/share/content_renderer.ts+++ b/apps/server/src/share/content_renderer.ts@@ -532,7 +532,7 @@ function renderText(result: Result, note: SNote | BNote, options: ShareRenderOpt}if (linkEl.classList.contains("reference-link")) {- cleanUpReferenceLinks(linkEl, getNote);+ cleanUpReferenceLinks(linkEl, href ?? "", getNote);}}@@ -580,9 +580,7 @@ function handleAttachmentLink(linkEl: HTMLElement, href: string, getNote: GetNotgetLog().error(`Broken attachment link detected in shared note: unable to find attachment with ID ${attachmentId}`);}} else {- const [notePath] = href.split("?");- const notePathSegments = notePath.split("/");- const noteId = notePathSegments[notePathSegments.length - 1];+ const noteId = getNoteIdFromLink(href);const linkedNote = getNote(noteId);if (linkedNote) {const isExternalLink = linkedNote.hasLabel("shareExternalLink");@@ -607,10 +605,10 @@ function handleAttachmentLink(linkEl: HTMLElement, href: string, getNote: GetNot* Processes reference links to ensure that they are up to date. More specifically, reference links contain in their HTML source code the note title at the time of the linking. It can be changed in the mean-time or the note can become protected, which leaks information.** @param linkEl the <a> element to process.+ * @param href the `href` stored in the note; `handleAttachmentLink()` rewrites the attribute.*/-function cleanUpReferenceLinks(linkEl: HTMLElement, getNote: GetNoteFunction) {+function cleanUpReferenceLinks(linkEl: HTMLElement, href: string, getNote: GetNoteFunction) {// Note: this method is basically a reimplementation of getReferenceLinkTitleSync from the link service of the client.- const href = linkEl.getAttribute("href") ?? "";// Handle attachment reference linksif (linkEl.classList.contains("attachment-link")) {@@ -619,7 +617,8 @@ function cleanUpReferenceLinks(linkEl: HTMLElement, getNote: GetNoteFunction) {return;}- const noteId = href.split("/").at(-1);+ // `handleAttachmentLink()` removes the `href` of a link whose target is missing.+ const noteId = linkEl.hasAttribute("href") ? getNoteIdFromLink(href) : "";const note = noteId ? getNote(noteId) : undefined;if (!note) {// If a note is not found, simply replace it with a text.@@ -631,6 +630,13 @@ function cleanUpReferenceLinks(linkEl: HTMLElement, getNote: GetNoteFunction) {}}+/** Returns the ID of the note a link such as `#root/abc/def?viewMode=source` points to. */+function getNoteIdFromLink(href: string) {+ const [notePath] = href.split("?");+ const notePathSegments = notePath.split("/");+ return notePathSegments[notePathSegments.length - 1];+}+/*** Renders a markdown code note by converting the markdown source to HTML* using the shared {@link renderMarkdownToHtml} pipeline.