fix(plugin-search): incorrect local search results (#479)

This commit is contained in:
pengzhanbo 2025-02-23 01:49:51 +08:00 committed by GitHub
parent 4f998a16c3
commit a1c2a9b700
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 13 deletions

View File

@ -101,7 +101,7 @@ const mark = computedAsync(async () => {
return markRaw(new Mark(resultsEl.value)) return markRaw(new Mark(resultsEl.value))
}, null) }, null)
const cache = new LRUCache<string, Map<string, string>>(16) // 16 files const cache = new LRUCache<string, Map<string, string>>(64) // 64 files
debouncedWatch( debouncedWatch(
() => [searchIndex.value, filterText.value] as const, () => [searchIndex.value, filterText.value] as const,
@ -121,12 +121,8 @@ debouncedWatch(
// Search // Search
results.value = index results.value = index
.search(filterTextValue) .search(filterTextValue) as (SearchResult & Result)[]
// .slice(0, 16) // .slice(0, 16)
.map((r) => {
r.titles = r.titles?.filter(Boolean) || []
return r
}) as (SearchResult & Result)[]
enableNoResults.value = true enableNoResults.value = true
const terms = new Set<string>() const terms = new Set<string>()
@ -696,6 +692,7 @@ svg {
padding: 0 2px; padding: 0 2px;
color: var(--vp-mini-search-highlight-text); color: var(--vp-mini-search-highlight-text);
background-color: var(--vp-mini-search-highlight-bg); background-color: var(--vp-mini-search-highlight-bg);
background-image: none;
border-radius: 2px; border-radius: 2px;
} }

View File

@ -131,7 +131,8 @@ async function indexFile(page: Page, options: SearchIndexOptions['searchOptions'
const index = getIndexByLocale(locale, options) const index = getIndexByLocale(locale, options)
const cache = getIndexCache(fileId) const cache = getIndexCache(fileId)
// retrieve file and split into "sections" // retrieve file and split into "sections"
const html = page.contentRendered const html = `<h1><a href="#"><span>${page.frontmatter.title || page.title}</span></a></h1>
${page.contentRendered}`
const sections = splitPageIntoSections(html) const sections = splitPageIntoSections(html)
if (cache && cache.length) if (cache && cache.length)
@ -147,7 +148,7 @@ async function indexFile(page: Page, options: SearchIndexOptions['searchOptions'
id, id,
text, text,
title: titles.at(-1)!, title: titles.at(-1)!,
titles: [page.frontmatter.title || page.title, ...titles.slice(0, -1)], titles: titles.slice(0, -1),
} }
index.add(item) index.add(item)
cache.push(item) cache.push(item)
@ -157,7 +158,7 @@ async function indexFile(page: Page, options: SearchIndexOptions['searchOptions'
// eslint-disable-next-line regexp/no-super-linear-backtracking // eslint-disable-next-line regexp/no-super-linear-backtracking
const headingRegex = /<h(\d*).*?>(<a.*? href="#.*?".*?>.*?<\/a>)<\/h\1>/gi const headingRegex = /<h(\d*).*?>(<a.*? href="#.*?".*?>.*?<\/a>)<\/h\1>/gi
// eslint-disable-next-line regexp/no-super-linear-backtracking // eslint-disable-next-line regexp/no-super-linear-backtracking
const headingContentRegex = /<a.*? href="#(.*?)".*?>(.*?)<\/a>/i const headingContentRegex = /<a.*? href="#(.*?)".*?><span>(.*?)<\/span><\/a>/i
/** /**
* Splits HTML into sections based on headings * Splits HTML into sections based on headings
@ -175,13 +176,16 @@ function* splitPageIntoSections(html: string) {
const content = result[i + 2] const content = result[i + 2]
if (!title || !content) if (!title || !content)
continue continue
const titles = parentTitles.slice(0, level)
titles[level] = title
yield { anchor, titles, text: getSearchableText(content) }
if (level === 0) if (level === 0)
parentTitles = [title] parentTitles = [title]
else else
parentTitles[level] = title parentTitles[level] = title
let titles = parentTitles.slice(0, level)
titles[level] = title
titles = titles.filter(Boolean)
yield { anchor, titles, text: getSearchableText(content) }
} }
} }