Skip to content

Commit

Permalink
feat(search-pro): add filter option, close #3790
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Dec 27, 2023
1 parent 4283b7d commit be6b4e4
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 8 deletions.
7 changes: 7 additions & 0 deletions docs/search-pro/src/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ Performing client search with huge contents could be slow, so under this case yo

:::

### filter

- Type: `(page: Page) => boolean`
- Default: `() => true`

Function used to filter pages.

### sortStrategy

- Type: `"max" | "total"`
Expand Down
2 changes: 2 additions & 0 deletions docs/search-pro/src/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ icon: lightbulb

By default, the plugin will only index headings, article excerpt and custom fields you add. If you want to index all content, you should set `indexContent: true` in the plugin options.

If you want only some of the pages to be indexed, set `filter` options in plugin options to control which pages are indexed, see [Config → Filter](./config.md#filter). You can also set `search: false` in frontmatter to prevent a page from being indexed.

::: warning

When indexing languages that is not word based, like Chinese, Japanese or Korean, you should set `indexOptions` and `indexLocaleOptions` to perform correct word-splitting, see [Customize Index Generation](#customize-index-generation).
Expand Down
7 changes: 7 additions & 0 deletions docs/search-pro/src/zh/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ export default defineUserConfig({

:::

### filter

- 类型: `(page: Page) => boolean`
- 默认值: `() => true`

用于过滤页面的函数。

### sortStrategy

- 类型: `"max" | "total"`
Expand Down
2 changes: 2 additions & 0 deletions docs/search-pro/src/zh/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ icon: lightbulb

默认情况下,插件仅索引标题,文章摘要和你添加的自定义字段。如果你想要索引文章的全部内容,你可以通过设置 `indexContent: true` 来开启。

如果你只想要部分页面被索引,你可以在插件选项中设置 `filter` 选项来控制索引范围,参见 [配置 → filter](./config.md#filter)。你也可以在 Frontmatter 中设置 `search: false` 来阻止页面被索引。

::: warning

当索引不基于单词的语言时,例如中文、日语或韩语,你需要设置 `indexOptions``indexLocaleOptions` 以执行正确的分词,详见[自定义索引生成](#自定义索引生成)
Expand Down
18 changes: 10 additions & 8 deletions packages/search-pro/src/node/generateIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ const CONTENT_INLINE_TAGS =
",",
);

const isExcerptMarker = (node: AnyNode): boolean =>
node.type === "comment" && node.data.trim() === "more";

const $ = load("");

const renderHeader = (node: Element): string =>
Expand Down Expand Up @@ -90,7 +93,6 @@ export const generatePageIndex = (
const header = renderHeader(node);

if (currentContent && shouldIndexContent) {
// add last content
// add last content
((isContentBeforeFirstHeader ? pageIndex : currentSectionIndex!).t ??=
[]).push(currentContent.replace(/\s+/gu, " "));
Expand Down Expand Up @@ -125,12 +127,10 @@ export const generatePageIndex = (
} else if (node.type === "text") {
currentContent += preserveSpace || node.data.trim() ? node.data : "";
} else if (
// we are expecting to stop at excerpt marker
// we are expecting to stop at excerpt marker if content is not indexed
hasExcerpt &&
!indexContent &&
// we got excerpt marker
node.type === "comment" &&
node.data.trim() === "more"
isExcerptMarker(node)
) {
shouldIndexContent = false;
}
Expand Down Expand Up @@ -185,16 +185,18 @@ export const getSearchIndexStore = async (
{
customFields,
indexContent,
filter = (): boolean => true,
indexOptions,
indexLocaleOptions,
}: SearchProOptions,
): Promise<SearchIndexStore> => {
const indexesByLocale: LocaleIndex = {};

app.pages.forEach((page) => {
const indexes = generatePageIndex(page, customFields, indexContent);

(indexesByLocale[page.pathLocale] ??= []).push(...indexes);
if (filter(page) && page.frontmatter["search"] !== false)
(indexesByLocale[page.pathLocale] ??= []).push(
...generatePageIndex(page, customFields, indexContent),
);
});

const searchIndex: SearchIndexStore = {};
Expand Down
10 changes: 10 additions & 0 deletions packages/search-pro/src/node/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,14 @@ export interface SearchProOptions extends DeprecatedSearchProOptions {
* 按语言的创建索引选项
*/
indexLocaleOptions?: Record<string, SearchProIndexOptions>;

/**
* Filter pages to be indexed
*
* 过滤需要索引的页面
*
* @param page Page
* @returns whether the page should be indexed
*/
filter?: (page: Page) => boolean;
}

0 comments on commit be6b4e4

Please sign in to comment.