fix(plugin-search): incorrect local search results (#479)
This commit is contained in:
parent
4f998a16c3
commit
a1c2a9b700
@ -101,7 +101,7 @@ const mark = computedAsync(async () => {
|
||||
return markRaw(new Mark(resultsEl.value))
|
||||
}, null)
|
||||
|
||||
const cache = new LRUCache<string, Map<string, string>>(16) // 16 files
|
||||
const cache = new LRUCache<string, Map<string, string>>(64) // 64 files
|
||||
|
||||
debouncedWatch(
|
||||
() => [searchIndex.value, filterText.value] as const,
|
||||
@ -121,12 +121,8 @@ debouncedWatch(
|
||||
|
||||
// Search
|
||||
results.value = index
|
||||
.search(filterTextValue)
|
||||
// .slice(0, 16)
|
||||
.map((r) => {
|
||||
r.titles = r.titles?.filter(Boolean) || []
|
||||
return r
|
||||
}) as (SearchResult & Result)[]
|
||||
.search(filterTextValue) as (SearchResult & Result)[]
|
||||
// .slice(0, 16)
|
||||
enableNoResults.value = true
|
||||
|
||||
const terms = new Set<string>()
|
||||
@ -696,6 +692,7 @@ svg {
|
||||
padding: 0 2px;
|
||||
color: var(--vp-mini-search-highlight-text);
|
||||
background-color: var(--vp-mini-search-highlight-bg);
|
||||
background-image: none;
|
||||
border-radius: 2px;
|
||||
}
|
||||
|
||||
|
||||
@ -131,7 +131,8 @@ async function indexFile(page: Page, options: SearchIndexOptions['searchOptions'
|
||||
const index = getIndexByLocale(locale, options)
|
||||
const cache = getIndexCache(fileId)
|
||||
// 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)
|
||||
|
||||
if (cache && cache.length)
|
||||
@ -147,7 +148,7 @@ async function indexFile(page: Page, options: SearchIndexOptions['searchOptions'
|
||||
id,
|
||||
text,
|
||||
title: titles.at(-1)!,
|
||||
titles: [page.frontmatter.title || page.title, ...titles.slice(0, -1)],
|
||||
titles: titles.slice(0, -1),
|
||||
}
|
||||
index.add(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
|
||||
const headingRegex = /<h(\d*).*?>(<a.*? href="#.*?".*?>.*?<\/a>)<\/h\1>/gi
|
||||
// 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
|
||||
@ -175,13 +176,16 @@ function* splitPageIntoSections(html: string) {
|
||||
const content = result[i + 2]
|
||||
if (!title || !content)
|
||||
continue
|
||||
const titles = parentTitles.slice(0, level)
|
||||
titles[level] = title
|
||||
yield { anchor, titles, text: getSearchableText(content) }
|
||||
|
||||
if (level === 0)
|
||||
parentTitles = [title]
|
||||
else
|
||||
parentTitles[level] = title
|
||||
|
||||
let titles = parentTitles.slice(0, level)
|
||||
titles[level] = title
|
||||
titles = titles.filter(Boolean)
|
||||
yield { anchor, titles, text: getSearchableText(content) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user