fix(server): infinite loop in note map

This commit is contained in:
Elian Doran 2025-09-29 09:45:16 +03:00
parent f718e87673
commit 58a883797d
No known key found for this signature in database
2 changed files with 12 additions and 6 deletions

View File

@ -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"
}

View File

@ -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) {