Skip to content

Build your personal knowledge base with Trilium Notes

fix(share): link a note with either external link label out from note text too

Elian Doran5b30c4714c3a
diff --git a/packages/trilium-core/src/share/content_renderer.spec.ts b/packages/trilium-core/src/share/content_renderer.spec.ts
index 18d9290ae8..8439f292d7 100644
--- a/packages/trilium-core/src/share/content_renderer.spec.ts
+++ b/packages/trilium-core/src/share/content_renderer.spec.ts
@@ -705,6 +705,34 @@ describe("content_renderer", () => {
expect(externalLink.text).toBe("Ext");
});
+ it("links a note with either external link label out, as the navigation does", () => {
+ buildShareNote({
+ "id": "legacyExt0001",
+ "title": "Legacy",
+ "#shareExternal": "https://example.com/legacy"
+ });
+ buildShareNote({
+ "id": "spacedExt0001",
+ "title": "Spaced",
+ "#shareExternalLink": " ",
+ "#shareExternal": " https://example.com/spaced "
+ });
+ const note = buildShareNote({
+ id: "note",
+ content: `<p><a href="#root/legacyExt0001">Legacy</a>`
+ + ` <a href="#root/spacedExt0001">Spaced</a></p>`
+ });
+
+ const links = parse(String(getContent(note).content)).querySelectorAll("a");
+
+ expect(links.map((link) => link.getAttribute("href")))
+ .toStrictEqual([ "https://example.com/legacy", "https://example.com/spaced" ]);
+ for (const link of links) {
+ expect(link.getAttribute("target")).toBe("_blank");
+ expect(link.getAttribute("rel")).toBe("noopener noreferrer");
+ }
+ });
+
it("replaces a reference link to a missing attachment with its text", () => {
buildShareNote({ id: "attachOwner01", title: "Owner" });
const href = "#root/attachOwner01?viewMode=attachments&amp;attachmentId=missing01";
diff --git a/packages/trilium-core/src/share/content_renderer.ts b/packages/trilium-core/src/share/content_renderer.ts
index 136360b051..78b6cb51e8 100644
--- a/packages/trilium-core/src/share/content_renderer.ts
+++ b/packages/trilium-core/src/share/content_renderer.ts
@@ -598,13 +598,11 @@ function handleAttachmentLink(linkEl: HTMLElement, href: string, getNote: GetNot
const noteId = getNoteIdFromLink(href);
const linkedNote = getNote(noteId);
if (linkedNote) {
- const isExternalLink = linkedNote.hasLabel("shareExternalLink");
- const rawHref = linkedNote.getLabelValue("shareExternalLink") ?? "";
- const href = isExternalLink ? sanitize.sanitizeUrl(rawHref) : `./${linkedNote.shareId}`;
- if (href) {
- linkEl.setAttribute("href", href);
+ const link = getShareLink(linkedNote, sanitize.sanitizeUrl);
+ if (link.href) {
+ linkEl.setAttribute("href", link.href);
}
- if (isExternalLink) {
+ if (link.isExternal) {
linkEl.setAttribute("target", "_blank");
linkEl.setAttribute("rel", "noopener noreferrer");
}