Skip to content

Build your personal knowledge base with Trilium Notes

fix(server): infinite loop in note map

Elian Doran58a883797d35
diff --git a/apps/server/src/routes/api/note_map.spec.ts b/apps/server/src/routes/api/note_map.spec.ts
index c3696fc3a1..0ab5d1d2d7 100644
--- a/apps/server/src/routes/api/note_map.spec.ts
+++ b/apps/server/src/routes/api/note_map.spec.ts
@@ -57,7 +57,7 @@ describe("Note map service", () => {
{
excerpts: [
trimIndentation`\
- <div class="ck-content backlink-excerpt"><head></head><body><p>
+ <div class="ck-content backlink-excerpt"><p>
The quick brownie
</p>
<p>
@@ -68,14 +68,14 @@ describe("Note map service", () => {
<figure class="image">
${" "}
</figure>
- </body></div>`
+ </div>`
],
noteId: "first",
},
{
excerpts: [
trimIndentation`\
- <div class="ck-content backlink-excerpt"><head></head><body><p>
+ <div class="ck-content backlink-excerpt"><p>
<a class="reference-link backlink-link" href="#root/dUtgloZIckax">
Backlink text
</a>
@@ -90,7 +90,7 @@ describe("Note map service", () => {
Second
</a>
</p>
- </body></div>`
+ </div>`
],
noteId: "second"
}
diff --git a/apps/server/src/routes/api/note_map.ts b/apps/server/src/routes/api/note_map.ts
index 6547ad6b93..84161cacc6 100644
--- a/apps/server/src/routes/api/note_map.ts
+++ b/apps/server/src/routes/api/note_map.ts
@@ -243,8 +243,8 @@ function updateDescendantCountMapForSearch(noteIdToDescendantCountMap: Record<st
function removeImages(document: HTMLElement) {
const images = document.getElementsByTagName("img");
- while (images && images.length > 0) {
- images[0]?.parentNode?.removeChild(images[0]);
+ for (const image of images) {
+ image.remove();
}
}
@@ -257,9 +257,13 @@ export function findExcerpts(sourceNote: BNote, referencedNoteId: string) {
const excerpts: string[] = [];
+ console.log("Removing images")
removeImages(document);
+ console.log("Querying links");
+
for (const linkEl of document.querySelectorAll("a")) {
+ console.log("Got ", linkEl.innerHTML);
const href = linkEl.getAttribute("href");
if (!href || !href.endsWith(referencedNoteId)) {
@@ -271,6 +275,7 @@ export function findExcerpts(sourceNote: BNote, referencedNoteId: string) {
let centerEl: HTMLElement = linkEl;
while (centerEl.tagName !== "BODY" && centerEl.parentNode && (centerEl.parentNode?.textContent?.length || 0) <= EXCERPT_CHAR_LIMIT) {
+ console.log("Got ", centerEl.tagName, centerEl.parentNode);
centerEl = centerEl.parentNode;
}
@@ -366,6 +371,7 @@ function getBacklinks(req: Request): BacklinksResponse {
let backlinksWithExcerptCount = 0;
return getFilteredBacklinks(note).map((backlink) => {
+ console.log("Processing ", backlink);
const sourceNote = backlink.note;
if (sourceNote.type !== "text" || backlinksWithExcerptCount > 50) {