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): ignore blank external link labels
matt wilkie5c554743b47f
diff --git a/apps/server/src/share/content_renderer.spec.ts b/apps/server/src/share/content_renderer.spec.tsindex c849cbc7b5..ce8cbab082 100644--- a/apps/server/src/share/content_renderer.spec.ts+++ b/apps/server/src/share/content_renderer.spec.ts@@ -701,6 +701,23 @@ describe("content_renderer", () => {});expect(twoUrls?.getAttribute("href")).toBe("https://example.com/documented");++ const whitespaceWithLegacy = renderTreeItemAnchor({+ "id": "external6",+ "#shareExternal": "https://example.com/legacy2",+ "#shareExternalLink": " "+ });++ expect(whitespaceWithLegacy?.getAttribute("href")).toBe("https://example.com/legacy2");++ const whitespaceOnly = renderTreeItemAnchor({+ "id": "external7",+ "#shareExternalLink": " "+ });++ expect(whitespaceOnly?.getAttribute("href")).toBe("./external7");+ expect(Object.keys(whitespaceOnly?.attributes ?? {}).sort())+ .toEqual([ "class", "href" ]);});function renderTreeItemAnchor(noteDef: Parameters<typeof buildShareNote>[0]) {@@ -737,7 +754,24 @@ describe("content_renderer", () => {"#shareExternal": "","#shareExternalLink": "https://example.com/other"},- { id: "pageInternal", title: "Internal" }+ { id: "pageInternal", title: "Internal" },+ {+ "id": "pageTwoUrls",+ "title": "Two URLs",+ "#shareExternal": "https://example.com/legacy",+ "#shareExternalLink": "https://example.com/documented"+ },+ {+ "id": "pageWhitespaceWithLegacy",+ "title": "Whitespace with legacy",+ "#shareExternal": "https://example.com/legacy2",+ "#shareExternalLink": " "+ },+ {+ "id": "pageWhitespaceOnly",+ "title": "Whitespace only",+ "#shareExternalLink": " "+ }]});const anchors = renderPageAnchors("pageParent");@@ -756,6 +790,18 @@ describe("content_renderer", () => {const internal = anchors.find((a) => a.textContent === "Internal");expect(internal?.getAttribute("href")).toBe("./pageInternal");expect(Object.keys(internal?.attributes ?? {}).sort()).toEqual([ "class", "href" ]);++ const twoUrls = anchors.find((a) => a.textContent === "Two URLs");+ expect(twoUrls?.getAttribute("href")).toBe("https://example.com/documented");++ const whitespaceWithLegacy = anchors+ .find((a) => a.textContent === "Whitespace with legacy");+ expect(whitespaceWithLegacy?.getAttribute("href")).toBe("https://example.com/legacy2");++ const whitespaceOnly = anchors.find((a) => a.textContent === "Whitespace only");+ expect(whitespaceOnly?.getAttribute("href")).toBe("./pageWhitespaceOnly");+ expect(Object.keys(whitespaceOnly?.attributes ?? {}).sort())+ .toEqual([ "class", "href" ]);});function renderPageAnchors(noteId: string) {diff --git a/packages/share-theme/src/templates/page.ejs b/packages/share-theme/src/templates/page.ejsindex adb0fbf28a..d940f69e04 100644--- a/packages/share-theme/src/templates/page.ejs+++ b/packages/share-theme/src/templates/page.ejs@@ -184,8 +184,9 @@ content = content.replaceAll(headingRe, (...match) => {<%const action = note.type === "book" ? "getChildNotes" : "getVisibleChildNotes";for (const childNote of note[action]()) {- const externalHref = childNote.getLabelValue("shareExternalLink")- || childNote.getLabelValue("shareExternal");+ const externalHref = childNote+ .getLabelValue("shareExternalLink")?.trim()+ || childNote.getLabelValue("shareExternal")?.trim();const isExternalLink = !!externalHref;const linkHref = isExternalLink? sanitizeUrl(externalHref)diff --git a/packages/share-theme/src/templates/tree_item.ejs b/packages/share-theme/src/templates/tree_item.ejsindex 078a45d933..d093ceb0b9 100644--- a/packages/share-theme/src/templates/tree_item.ejs+++ b/packages/share-theme/src/templates/tree_item.ejs@@ -1,6 +1,7 @@<%const linkClass = `type-${note.type}` + (activeNote.noteId === note.noteId ? " active" : "");-const externalHref = note.getLabelValue("shareExternalLink") || note.getLabelValue("shareExternal");+const externalHref = note.getLabelValue("shareExternalLink")?.trim()+ || note.getLabelValue("shareExternal")?.trim();const isExternalLink = !!externalHref;let linkHref;