Skip to content

Commit 25a51a7

Browse files
committed
feat(print/list): unlink references to notes that are not printed
1 parent f4d6e98 commit 25a51a7

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

apps/client/src/widgets/collections/legacy/ListPrintView.tsx

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import { useFilteredNoteIds } from "./utils";
88

99
interface NotesWithContent {
1010
note: FNote;
11-
content: { __html: string };
11+
contentEl: HTMLElement;
1212
}
1313

1414
export function ListPrintView({ note, noteIds: unfilteredNoteIds, onReady }: ViewModeProps<{}>) {
1515
const noteIds = useFilteredNoteIds(note, unfilteredNoteIds);
16+
const noteIdsSet = new Set<string>();
1617
const [ notesWithContent, setNotesWithContent ] = useState<NotesWithContent[]>();
1718

1819
useLayoutEffect(() => {
@@ -29,9 +30,8 @@ export function ListPrintView({ note, noteIds: unfilteredNoteIds, onReady }: Vie
2930

3031
insertPageTitle(contentEl, note.title);
3132
rewriteHeadings(contentEl, depth);
32-
rewriteLinks(contentEl);
33-
34-
notesWithContent.push({ note, content: { __html: contentEl.innerHTML } });
33+
noteIdsSet.add(note.noteId);
34+
notesWithContent.push({ note, contentEl });
3535

3636
if (note.hasChildren()) {
3737
const imageLinks = note.getRelations("imageLink");
@@ -46,6 +46,12 @@ export function ListPrintView({ note, noteIds: unfilteredNoteIds, onReady }: Vie
4646
for (const note of notes) {
4747
await processNote(note, 1);
4848
}
49+
50+
// After all notes are processed, rewrite links
51+
for (const { contentEl } of notesWithContent) {
52+
rewriteLinks(contentEl, noteIdsSet);
53+
}
54+
4955
setNotesWithContent(notesWithContent);
5056
});
5157
}, [noteIds]);
@@ -61,8 +67,8 @@ export function ListPrintView({ note, noteIds: unfilteredNoteIds, onReady }: Vie
6167
<div class="note-list-container use-tn-links">
6268
<h1>{note.title}</h1>
6369

64-
{notesWithContent?.map(({ note: childNote, content }) => (
65-
<section id={`note-${childNote.noteId}`} class="note" dangerouslySetInnerHTML={content} />
70+
{notesWithContent?.map(({ note: childNote, contentEl }) => (
71+
<section id={`note-${childNote.noteId}`} class="note" dangerouslySetInnerHTML={{ __html: contentEl.innerHTML }} />
6672
))}
6773
</div>
6874
</div>
@@ -86,13 +92,21 @@ function rewriteHeadings(contentEl: HTMLElement, depth: number) {
8692
}
8793
}
8894

89-
function rewriteLinks(contentEl: HTMLElement) {
95+
function rewriteLinks(contentEl: HTMLElement, noteIdsSet: Set<string>) {
9096
const linkEls = contentEl.querySelectorAll("a");
9197
for (const linkEl of linkEls) {
9298
const href = linkEl.getAttribute("href");
9399
if (href && href.startsWith("#root/")) {
94100
const noteId = href.split("/").at(-1);
95-
linkEl.setAttribute("href", `#note-${noteId}`);
101+
102+
if (noteId && noteIdsSet.has(noteId)) {
103+
linkEl.setAttribute("href", `#note-${noteId}`);
104+
} else {
105+
// Link to note not in the print view, remove link but keep text
106+
const spanEl = document.createElement("span");
107+
spanEl.innerHTML = linkEl.innerHTML;
108+
linkEl.replaceWith(spanEl);
109+
}
96110
}
97111
}
98112
}

0 commit comments

Comments
 (0)