From e0dc4fee20e5c4b4d595db6c97a1247003b2ad63 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Tue, 28 Oct 2025 17:14:33 +0200 Subject: [PATCH] fix(share): reference links not working --- apps/server/src/share/content_renderer.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/apps/server/src/share/content_renderer.ts b/apps/server/src/share/content_renderer.ts index 3ab219f26..0e8058ae5 100644 --- a/apps/server/src/share/content_renderer.ts +++ b/apps/server/src/share/content_renderer.ts @@ -15,6 +15,7 @@ import log from "../services/log.js"; import { join } from "path"; import { readFileSync } from "fs"; import { highlightAuto } from "@triliumnext/highlightjs"; +import becca from "../becca/becca.js"; const shareAdjustedAssetPath = isDev ? assetPath : `../${assetPath}`; const templateCache: Map = new Map(); @@ -300,7 +301,10 @@ function renderText(result: Result, note: SNote | BNote) { } if (href?.startsWith("#")) { - handleAttachmentLink(linkEl, href); + const getNote = note instanceof BNote + ? (noteId: string) => becca.getNote(noteId) + : (noteId: string) => shaca.getNote(noteId); + handleAttachmentLink(linkEl, href, getNote); } } @@ -319,7 +323,7 @@ function renderText(result: Result, note: SNote | BNote) { } } -function handleAttachmentLink(linkEl: HTMLElement, href: string) { +function handleAttachmentLink(linkEl: HTMLElement, href: string, getNote: (id: string) => SNote | BNote | null) { const linkRegExp = /attachmentId=([a-zA-Z0-9_]+)/g; let attachmentMatch; if ((attachmentMatch = linkRegExp.exec(href))) { @@ -339,7 +343,7 @@ function handleAttachmentLink(linkEl: HTMLElement, href: string) { const [notePath] = href.split("?"); const notePathSegments = notePath.split("/"); const noteId = notePathSegments[notePathSegments.length - 1]; - const linkedNote = shaca.getNote(noteId); + const linkedNote = getNote(noteId); if (linkedNote) { const isExternalLink = linkedNote.hasLabel("shareExternalLink"); const href = isExternalLink ? linkedNote.getLabelValue("shareExternalLink") : `./${linkedNote.shareId}`; @@ -352,6 +356,7 @@ function handleAttachmentLink(linkEl: HTMLElement, href: string) { } linkEl.classList.add(`type-${linkedNote.type}`); } else { + log.error(`Broken link detected in shared note: unable to find note with ID ${noteId}`); linkEl.removeAttribute("href"); } }