feat(print/list): unlink references to notes that are not printed

This commit is contained in:
Elian Doran 2025-11-20 21:20:24 +02:00
parent f4d6e98d61
commit 25a51a71a0
No known key found for this signature in database

View File

@ -8,11 +8,12 @@ import { useFilteredNoteIds } from "./utils";
interface NotesWithContent { interface NotesWithContent {
note: FNote; note: FNote;
content: { __html: string }; contentEl: HTMLElement;
} }
export function ListPrintView({ note, noteIds: unfilteredNoteIds, onReady }: ViewModeProps<{}>) { export function ListPrintView({ note, noteIds: unfilteredNoteIds, onReady }: ViewModeProps<{}>) {
const noteIds = useFilteredNoteIds(note, unfilteredNoteIds); const noteIds = useFilteredNoteIds(note, unfilteredNoteIds);
const noteIdsSet = new Set<string>();
const [ notesWithContent, setNotesWithContent ] = useState<NotesWithContent[]>(); const [ notesWithContent, setNotesWithContent ] = useState<NotesWithContent[]>();
useLayoutEffect(() => { useLayoutEffect(() => {
@ -29,9 +30,8 @@ export function ListPrintView({ note, noteIds: unfilteredNoteIds, onReady }: Vie
insertPageTitle(contentEl, note.title); insertPageTitle(contentEl, note.title);
rewriteHeadings(contentEl, depth); rewriteHeadings(contentEl, depth);
rewriteLinks(contentEl); noteIdsSet.add(note.noteId);
notesWithContent.push({ note, contentEl });
notesWithContent.push({ note, content: { __html: contentEl.innerHTML } });
if (note.hasChildren()) { if (note.hasChildren()) {
const imageLinks = note.getRelations("imageLink"); const imageLinks = note.getRelations("imageLink");
@ -46,6 +46,12 @@ export function ListPrintView({ note, noteIds: unfilteredNoteIds, onReady }: Vie
for (const note of notes) { for (const note of notes) {
await processNote(note, 1); await processNote(note, 1);
} }
// After all notes are processed, rewrite links
for (const { contentEl } of notesWithContent) {
rewriteLinks(contentEl, noteIdsSet);
}
setNotesWithContent(notesWithContent); setNotesWithContent(notesWithContent);
}); });
}, [noteIds]); }, [noteIds]);
@ -61,8 +67,8 @@ export function ListPrintView({ note, noteIds: unfilteredNoteIds, onReady }: Vie
<div class="note-list-container use-tn-links"> <div class="note-list-container use-tn-links">
<h1>{note.title}</h1> <h1>{note.title}</h1>
{notesWithContent?.map(({ note: childNote, content }) => ( {notesWithContent?.map(({ note: childNote, contentEl }) => (
<section id={`note-${childNote.noteId}`} class="note" dangerouslySetInnerHTML={content} /> <section id={`note-${childNote.noteId}`} class="note" dangerouslySetInnerHTML={{ __html: contentEl.innerHTML }} />
))} ))}
</div> </div>
</div> </div>
@ -86,13 +92,21 @@ function rewriteHeadings(contentEl: HTMLElement, depth: number) {
} }
} }
function rewriteLinks(contentEl: HTMLElement) { function rewriteLinks(contentEl: HTMLElement, noteIdsSet: Set<string>) {
const linkEls = contentEl.querySelectorAll("a"); const linkEls = contentEl.querySelectorAll("a");
for (const linkEl of linkEls) { for (const linkEl of linkEls) {
const href = linkEl.getAttribute("href"); const href = linkEl.getAttribute("href");
if (href && href.startsWith("#root/")) { if (href && href.startsWith("#root/")) {
const noteId = href.split("/").at(-1); const noteId = href.split("/").at(-1);
if (noteId && noteIdsSet.has(noteId)) {
linkEl.setAttribute("href", `#note-${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);
}
} }
} }
} }