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(reference links): show [missing note] instead of nothing for a non-note href
Elian Dorand80be3ae45e1
diff --git a/apps/client/src/services/link.spec.ts b/apps/client/src/services/link.spec.tsindex c8a7c10c0a..4ffadeb15a 100644--- a/apps/client/src/services/link.spec.ts+++ b/apps/client/src/services/link.spec.ts@@ -619,6 +619,20 @@ describe("loadReferenceLinkTitle", () => {warn.mockRestore();});+ it("gives an href with no note id the same [missing note] the title resolvers do", async () => {+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});+ // Exactly what the editing downcast hands over: an empty <span> that this call is the only+ // thing ever to fill. An href that is not a hash note URL — an attachment image URL like+ // this one, an external link, imported HTML — must still leave something in it, or the+ // reference link renders as a blank widget while the stored HTML says "[missing note]".+ const $el = $("<span>");++ await linkService.loadReferenceLinkTitle($el, "api/attachments/bc1EIIdlPLKV/image/favicon.ico");++ expect($el.text()).toBe("[missing note]");+ warn.mockRestore();+ });+it("sets text, color class, bookmark and icon for a resolved note", async () => {const note = buildNote({ title: "Loaded", "#color": "red", "#iconClass": "bx bx-star" });const $a = $("<a>").attr("href", `#root/${note.noteId}?bookmark=Sec`);diff --git a/apps/client/src/services/link.ts b/apps/client/src/services/link.tsindex 24cac03e2d..0cc966afc8 100644--- a/apps/client/src/services/link.ts+++ b/apps/client/src/services/link.ts@@ -447,11 +447,16 @@ async function loadReferenceLinkTitle($el: JQuery<HTMLElement>, href: string | nconst { noteId, viewScope } = parseNavigationStateFromUrl(href);if (!noteId) {+ // Warned about but not returned on. The editing downcast creates an empty <span> and this+ // call is the only thing that ever fills it, so bailing here left the widget rendering as+ // nothing at all while the stored HTML — which resolves its title through+ // getReferenceLinkTitleSync instead — said "[missing note]". An href that is not a hash+ // note URL is ordinary enough to reach: an attachment image URL, an external link, or+ // imported HTML carrying an <a class="reference-link">.console.warn("Missing note ID.");- return;}- const note = await froca.getNote(noteId, true);+ const note = noteId ? await froca.getNote(noteId, true) : null;if (note) {$el.addClass(note.getColorClass());@@ -467,8 +472,8 @@ async function loadReferenceLinkTitle($el: JQuery<HTMLElement>, href: string | n));}- if (note) {- const icon = await getLinkIcon(noteId, viewScope.viewMode);+ if (noteId && note) {+ const icon = await getLinkIcon(noteId, viewScope?.viewMode);if (icon) {$el.prepend($("<span>").addClass(icon));