fix(share): footnotes not working

This commit is contained in:
Elian Doran 2025-01-07 14:43:12 +02:00
parent d6dedb1611
commit 5ffd8bc790
No known key found for this signature in database

View File

@ -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;