Skip to content

Commit

Permalink
fix: cannot search for 1-level heading content (umijs#1828)
Browse files Browse the repository at this point in the history
* fix: 修复一级内容搜索失效缺陷,页面级搜索结果冗余问题

* feat: searchWorker.min.js

---------

Co-authored-by: Bill <[email protected]>
  • Loading branch information
Zhou-Bill and Zhou-Bill authored Aug 13, 2023
1 parent effdf6f commit d114525
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion compiled/_internal/searchWorker.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 14 additions & 6 deletions src/client/theme-api/useSiteSearch/searchWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function generateSearchMetadata(
const metadata: ISearchMetadata = [];
// generate demos mapping by route.id
const demosMapping = Object.values(demos).reduce<
Record<string, typeof demos[0][]>
Record<string, (typeof demos)[0][]>
>((acc, demo) => {
if (demo.asset) {
acc[demo.routeId] ??= [];
Expand Down Expand Up @@ -119,11 +119,17 @@ function generateSearchMetadata(
const tocSections = routeMeta.toc.reduce<ISearchMetadata[0]['sections']>(
(acc, toc, i) => {
// exclude demo id, to avoid duplicate
if (!demoIds.includes(toc.id) && toc.depth > 1) {
if (!demoIds.includes(toc.id) && toc.depth >= 1) {
// 1 level is the pageTitle, 2 level is the pageTitle with toc title
let rawTitle = generateRouteTitle(routeMeta.frontmatter);
if (toc.depth !== 1) {
rawTitle = `${rawTitle} - ${toc.title}`;
}

acc.push(
createMetadataSection(
toc.title,
`${generateRouteTitle(routeMeta.frontmatter)} - ${toc.title}`,
rawTitle,
`${routeAbsPath}#${toc.id}`,
routeMeta.texts,
i,
Expand Down Expand Up @@ -191,7 +197,6 @@ function generateSearchMetadata(
});
}
});

return metadata;
}

Expand Down Expand Up @@ -279,8 +284,11 @@ function generateSearchResult(metadata: ISearchMetadata, keywordsStr: string) {
}
}

// find matched keywords in section title
if (matchReg.test(sec.rawTitle)) {
/**
* find matched keywords in section title
* if the section title is matched but the page title is not,
*/
if (matchReg.test(sec.rawTitle) && !matchReg.test(data.title)) {
const [highlightTitleTexts, titleMatchMapping] = generateHighlightTexts(
sec.title,
keywords,
Expand Down

0 comments on commit d114525

Please sign in to comment.