diff --git a/src/share/content_renderer.ts b/src/share/content_renderer.ts index fecf479b8..31845d6ee 100644 --- a/src/share/content_renderer.ts +++ b/src/share/content_renderer.ts @@ -70,43 +70,13 @@ function renderText(result: Result, note: SNote) { for (const linkEl of document.querySelectorAll("a")) { const href = linkEl.getAttribute("href"); - if (!href?.startsWith("#")) { + // Preserve footnotes. + if (href?.startsWith("#fn")) { continue; } - const linkRegExp = /attachmentId=([a-zA-Z0-9_]+)/g; - let attachmentMatch - if (attachmentMatch = linkRegExp.exec(href)) { - const attachmentId = attachmentMatch[1]; - const attachment = shaca.getAttachment(attachmentId); - - if (attachment) { - linkEl.setAttribute("href", `api/attachments/${attachmentId}/download`); - linkEl.classList.add(`attachment-link`); - linkEl.classList.add(`role-${attachment.role}`); - linkEl.innerText = attachment.title; - } else { - linkEl.removeAttribute("href"); - } - } else { - const [notePath] = href.split('?'); - const notePathSegments = notePath.split("/"); - const noteId = notePathSegments[notePathSegments.length - 1]; - const linkedNote = shaca.getNote(noteId); - if (linkedNote) { - const isExternalLink = linkedNote.hasLabel("shareExternalLink"); - const href = isExternalLink ? linkedNote.getLabelValue("shareExternalLink") : `./${linkedNote.shareId}`; - if (href) { - linkEl.setAttribute("href", href); - } - if (isExternalLink) { - linkEl.setAttribute("target", "_blank"); - linkEl.setAttribute("rel", "noopener noreferrer"); - } - linkEl.classList.add(`type-${linkedNote.type}`); - } else { - linkEl.removeAttribute("href"); - } + if (href?.startsWith("#")) { + handleAttachmentLink(linkEl, href); } } @@ -131,6 +101,43 @@ document.addEventListener("DOMContentLoaded", function() { } } +function handleAttachmentLink(linkEl: HTMLAnchorElement, href: string) { + const linkRegExp = /attachmentId=([a-zA-Z0-9_]+)/g; + let attachmentMatch + if (attachmentMatch = linkRegExp.exec(href)) { + const attachmentId = attachmentMatch[1]; + const attachment = shaca.getAttachment(attachmentId); + + if (attachment) { + linkEl.setAttribute("href", `api/attachments/${attachmentId}/download`); + linkEl.classList.add(`attachment-link`); + linkEl.classList.add(`role-${attachment.role}`); + linkEl.innerText = attachment.title; + } else { + linkEl.removeAttribute("href"); + } + } else { + const [notePath] = href.split('?'); + const notePathSegments = notePath.split("/"); + const noteId = notePathSegments[notePathSegments.length - 1]; + const linkedNote = shaca.getNote(noteId); + if (linkedNote) { + const isExternalLink = linkedNote.hasLabel("shareExternalLink"); + const href = isExternalLink ? linkedNote.getLabelValue("shareExternalLink") : `./${linkedNote.shareId}`; + if (href) { + linkEl.setAttribute("href", href); + } + if (isExternalLink) { + linkEl.setAttribute("target", "_blank"); + linkEl.setAttribute("rel", "noopener noreferrer"); + } + linkEl.classList.add(`type-${linkedNote.type}`); + } else { + linkEl.removeAttribute("href"); + } + } +} + function renderCode(result: Result) { if (typeof result.content !== "string" || !result.content?.trim()) { result.isEmpty = true;