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): restore target and rel on external share links
Elian Dorane778527c8f46
diff --git a/apps/server/src/share/content_renderer.spec.ts b/apps/server/src/share/content_renderer.spec.tsindex d3d5e7aa57..42467e2d21 100644--- a/apps/server/src/share/content_renderer.spec.ts+++ b/apps/server/src/share/content_renderer.spec.ts@@ -1,10 +1,13 @@import { trimIndentation } from "@triliumnext/commons";+import { sanitize, utils } from "@triliumnext/core";+import ejs from "ejs";import { parse } from "node-html-parser";import { beforeAll, describe, expect, it, vi } from "vitest";import { buildShareNote, buildShareNotes } from "../test/shaca_mocking.js";-import { getContent, renderCode, renderNoteContent, type Result, shouldSyntaxHighlight } from "./content_renderer.js";+import { getContent, getDefaultTemplatePath, readTemplate, renderCode, renderNoteContent, type Result, shouldSyntaxHighlight } from "./content_renderer.js";import type SNote from "./shaca/entities/snote.js";+import shaca from "./shaca/shaca.js";import shareRoot from "./share_root.js";describe("content_renderer", () => {@@ -637,4 +640,103 @@ describe("content_renderer", () => {expect(Object.keys(anchor?.attributes ?? {}).sort()).toEqual([ "class", "href" ]);});});+ describe("Tree item template", () => {+ it("sets a working target and rel on external tree links only", () => {+ const external = renderTreeItemAnchor({+ "id": "external1",+ "#shareExternal": "https://example.com/page"+ });++ expect(external?.getAttribute("href")).toBe("https://example.com/page");+ expect(external?.getAttribute("target")).toBe("_blank");+ expect(external?.getAttribute("rel")).toBe("noopener noreferrer");+ expect(Object.keys(external?.attributes ?? {}).sort())+ .toEqual([ "class", "href", "rel", "target" ]);++ const internal = renderTreeItemAnchor({ id: "internal1" });++ expect(internal?.getAttribute("href")).toBe("./internal1");+ expect(Object.keys(internal?.attributes ?? {}).sort()).toEqual([ "class", "href" ]);+ });++ function renderTreeItemAnchor(noteDef: Parameters<typeof buildShareNote>[0]) {+ const note = buildShareNote(noteDef);+ const subRootNote = buildShareNote({ id: `subRoot-${noteDef.id}` });++ const html = ejs.render(readTemplate(getDefaultTemplatePath("tree_item")), {+ note,+ activeNote: subRootNote,+ subRoot: { note: subRootNote },+ ancestors: [],+ sanitizeUrl: sanitize.sanitizeUrl,+ iconPackSupportedPrefixes: [],+ t: (key: string) => key+ });++ return parse(html).querySelector("a");+ }+ });+ describe("Subpage list template", () => {+ it("sets a working target and rel on external subpage links only", () => {+ buildShareNote({+ id: "pageParent",+ content: "<p>Parent</p>",+ children: [+ {+ "id": "pageExternal",+ "title": "External",+ "#shareExternal": "https://example.com/page"+ },+ { id: "pageInternal", title: "Internal" }+ ]+ });+ const anchors = renderPageAnchors("pageParent");++ const external = anchors.find((a) => a.textContent === "External");+ expect(external?.getAttribute("href")).toBe("https://example.com/page");+ expect(external?.getAttribute("target")).toBe("_blank");+ expect(external?.getAttribute("rel")).toBe("noopener noreferrer");+ expect(Object.keys(external?.attributes ?? {}).sort())+ .toEqual([ "class", "href", "rel", "target" ]);++ const internal = anchors.find((a) => a.textContent === "Internal");+ expect(internal?.getAttribute("href")).toBe("./pageInternal");+ expect(Object.keys(internal?.attributes ?? {}).sort()).toEqual([ "class", "href" ]);+ });++ function renderPageAnchors(noteId: string) {+ const note = shaca.getNote(noteId);+ const { header, content, isEmpty } = getContent(note);++ const html = ejs.render(readTemplate(getDefaultTemplatePath("page")), {+ note,+ header,+ content,+ isEmpty,+ assetPath: "assets",+ assetUrlFragment: "assets",+ showLoginInShareTheme: false,+ t: (key: string) => key,+ isDev: false,+ utils,+ sanitizeUrl: sanitize.sanitizeUrl,+ subRoot: { note },+ rootNoteId: noteId,+ cssToLoad: [],+ jsToLoad: [],+ logoUrl: "",+ ancestors: [],+ isStatic: false,+ faviconUrl: "",+ iconPackCss: "",+ iconPackSupportedPrefixes: []+ }, {+ includer: (path: string) => ({+ template: readTemplate(getDefaultTemplatePath(path))+ })+ });++ return parse(html).querySelectorAll("#childLinks a");+ }+ });});diff --git a/packages/share-theme/src/templates/page.ejs b/packages/share-theme/src/templates/page.ejsindex b569d31a75..4f6c292f1d 100644--- a/packages/share-theme/src/templates/page.ejs+++ b/packages/share-theme/src/templates/page.ejs@@ -187,10 +187,9 @@ content = content.replaceAll(headingRe, (...match) => {const isExternalLink = childNote.hasLabel("shareExternal") || childNote.hasLabel("shareExternalLink");const rawHref = isExternalLink ? childNote.getLabelValue("shareExternal") ?? childNote.getLabelValue("shareExternalLink") : `./${childNote.shareId}`;const linkHref = isExternalLink ? sanitizeUrl(rawHref ?? "") : rawHref;- const target = isExternalLink ? ` target="_blank" rel="noopener noreferrer"` : "";%><li>- <a class="type-<%= childNote.type %>" href="<%= linkHref %>"<%= target %>><%= childNote.title %></a>+ <a class="type-<%= childNote.type %>" href="<%= linkHref %>"<% if (isExternalLink) { %> target="_blank" rel="noopener noreferrer"<% } %>><%= childNote.title %></a></li><% } %></ul>diff --git a/packages/share-theme/src/templates/tree_item.ejs b/packages/share-theme/src/templates/tree_item.ejsindex 13c6c563de..9345032d8a 100644--- a/packages/share-theme/src/templates/tree_item.ejs+++ b/packages/share-theme/src/templates/tree_item.ejs@@ -8,12 +8,10 @@ if (isExternalLink) {} else if (note.shareId) {linkHref = `./${note.shareId}`;}--const target = isExternalLink ? ` target="_blank" rel="noopener noreferrer"` : "";%><% if (note.noteId !== subRoot.note.noteId) { %>- <a class="<%= linkClass %>" href="<%= linkHref %>"<%= target %>>+ <a class="<%= linkClass %>" href="<%= linkHref %>"<% if (isExternalLink) { %> target="_blank" rel="noopener noreferrer"<% } %>><% if (note.hasVisibleChildren()) { %><button class="collapse-button" aria-label="<%= t("share_theme.expand") %>"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="svg-icon right-triangle"><path d="M3 8L12 17L21 8"></path></svg></button><% } %><span><i class="<%= note.getIcon(iconPackSupportedPrefixes) %>"></i> <%= note.title %></span></a>