diff --git a/apps/client/src/widgets/collections/legacy/ListPrintView.tsx b/apps/client/src/widgets/collections/legacy/ListPrintView.tsx index d4fa36133..a2b9d5319 100644 --- a/apps/client/src/widgets/collections/legacy/ListPrintView.tsx +++ b/apps/client/src/widgets/collections/legacy/ListPrintView.tsx @@ -8,11 +8,12 @@ import { useFilteredNoteIds } from "./utils"; interface NotesWithContent { note: FNote; - content: { __html: string }; + contentEl: HTMLElement; } export function ListPrintView({ note, noteIds: unfilteredNoteIds, onReady }: ViewModeProps<{}>) { const noteIds = useFilteredNoteIds(note, unfilteredNoteIds); + const noteIdsSet = new Set(); const [ notesWithContent, setNotesWithContent ] = useState(); useLayoutEffect(() => { @@ -29,9 +30,8 @@ export function ListPrintView({ note, noteIds: unfilteredNoteIds, onReady }: Vie insertPageTitle(contentEl, note.title); rewriteHeadings(contentEl, depth); - rewriteLinks(contentEl); - - notesWithContent.push({ note, content: { __html: contentEl.innerHTML } }); + noteIdsSet.add(note.noteId); + notesWithContent.push({ note, contentEl }); if (note.hasChildren()) { const imageLinks = note.getRelations("imageLink"); @@ -46,6 +46,12 @@ export function ListPrintView({ note, noteIds: unfilteredNoteIds, onReady }: Vie for (const note of notes) { await processNote(note, 1); } + + // After all notes are processed, rewrite links + for (const { contentEl } of notesWithContent) { + rewriteLinks(contentEl, noteIdsSet); + } + setNotesWithContent(notesWithContent); }); }, [noteIds]); @@ -61,8 +67,8 @@ export function ListPrintView({ note, noteIds: unfilteredNoteIds, onReady }: Vie @@ -86,13 +92,21 @@ function rewriteHeadings(contentEl: HTMLElement, depth: number) { } } -function rewriteLinks(contentEl: HTMLElement) { +function rewriteLinks(contentEl: HTMLElement, noteIdsSet: Set) { const linkEls = contentEl.querySelectorAll("a"); for (const linkEl of linkEls) { const href = linkEl.getAttribute("href"); if (href && href.startsWith("#root/")) { const noteId = href.split("/").at(-1); - linkEl.setAttribute("href", `#note-${noteId}`); + + if (noteId && noteIdsSet.has(noteId)) { + linkEl.setAttribute("href", `#note-${noteId}`); + } else { + // Link to note not in the print view, remove link but keep text + const spanEl = document.createElement("span"); + spanEl.innerHTML = linkEl.innerHTML; + linkEl.replaceWith(spanEl); + } } } }