From 3b596c9311fd5fe552d6c53e9b27841932a26587 Mon Sep 17 00:00:00 2001 From: Jacky Zhao Date: Fri, 2 Feb 2024 00:19:19 -0800 Subject: [PATCH] fix: flatmap children when highlighting rich preview to avoid body --- quartz/components/scripts/search.inline.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/quartz/components/scripts/search.inline.ts b/quartz/components/scripts/search.inline.ts index 17f3e1ba8..57067d1e3 100644 --- a/quartz/components/scripts/search.inline.ts +++ b/quartz/components/scripts/search.inline.ts @@ -81,10 +81,10 @@ function highlight(searchTerm: string, text: string, trim?: boolean) { }` } -function highlightHTML(searchTerm: string, innerHTML: string) { +function highlightHTML(searchTerm: string, el: HTMLElement) { const p = new DOMParser() const tokenizedTerms = tokenizeTerm(searchTerm) - const html = p.parseFromString(innerHTML, "text/html") + const html = p.parseFromString(el.innerHTML, "text/html") const createHighlightSpan = (text: string) => { const span = document.createElement("span") @@ -389,7 +389,7 @@ document.addEventListener("nav", async (e: CustomEventMap["nav"]) => { previewInner = document.createElement("div") previewInner.classList.add("preview-inner") const innerDiv = await fetchContent(slug).then((contents) => - contents.map((el) => highlightHTML(currentSearchTerm, el.innerHTML)), + contents.flatMap((el) => [...highlightHTML(currentSearchTerm, el as HTMLElement).children]), ) previewInner.append(...innerDiv) preview.replaceChildren(previewInner)