From 9d11f461282995dd88137fa116ceb07b34250d39 Mon Sep 17 00:00:00 2001 From: Mahiru Date: Thu, 1 Oct 2026 21:30:24 +0800 Subject: [PATCH 1/2] fix: include rendered page content in search --- src/.vuepress/config.ts | 13 ++----------- src/.vuepress/search.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 11 deletions(-) create mode 100644 src/.vuepress/search.ts diff --git a/src/.vuepress/config.ts b/src/.vuepress/config.ts index 9528f4870..2504221ec 100644 --- a/src/.vuepress/config.ts +++ b/src/.vuepress/config.ts @@ -1,21 +1,12 @@ import { defineUserConfig } from "vuepress"; import theme from "./theme.js"; -import { searchPlugin } from "@vuepress/plugin-search"; +import { contentSearchPlugin } from "./search.js"; import { shikiPlugin } from "@vuepress/plugin-shiki"; import path from "path"; export default defineUserConfig({ base: "/", plugins: [ - searchPlugin({ - locales: { - "/": { - placeholder: "Search", - }, - "/zh/": { - placeholder: "搜索", - }, - }, - }), + contentSearchPlugin(), shikiPlugin({ theme: "dracula", langs: [ diff --git a/src/.vuepress/search.ts b/src/.vuepress/search.ts new file mode 100644 index 000000000..b920cc8e0 --- /dev/null +++ b/src/.vuepress/search.ts @@ -0,0 +1,28 @@ +import { searchPlugin } from "@vuepress/plugin-search"; +import type { Plugin } from "vuepress"; + +export const contentSearchPlugin = (): Plugin => { + let decodeEntity: (entity: string) => string; + + return { + ...searchPlugin({ + locales: { + "/": { placeholder: "Search" }, + "/zh/": { placeholder: "搜索" }, + }, + // 渲染后的正文包含 @include 内容;去掉标签,再还原代码中的 HTML 实体。 + getExtraFields: (page) => [ + page.contentRendered + .replace(/|<[^>]*>/g, "") + .replace(/&(?:#x[\da-f]+|#\d+|[a-z][\da-z]*);/gi, decodeEntity) + .replace(/\s+/g, " ") + .trim(), + ], + }), + name: "@vuepress/plugin-search", + // Markdown 配置先于搜索索引生成,复用其实体解码,不新增依赖。 + extendsMarkdown(markdown) { + decodeEntity = markdown.utils.unescapeAll; + }, + }; +}; From 6e489d05d9da7b8ff29757962249bd061bc548fa Mon Sep 17 00:00:00 2001 From: Mahiru Date: Thu, 1 Oct 2026 21:34:08 +0800 Subject: [PATCH 2/2] fix: leave heading indexing to the search plugin --- src/.vuepress/search.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/.vuepress/search.ts b/src/.vuepress/search.ts index b920cc8e0..eb642ee4e 100644 --- a/src/.vuepress/search.ts +++ b/src/.vuepress/search.ts @@ -10,9 +10,10 @@ export const contentSearchPlugin = (): Plugin => { "/": { placeholder: "Search" }, "/zh/": { placeholder: "搜索" }, }, - // 渲染后的正文包含 @include 内容;去掉标签,再还原代码中的 HTML 实体。 + // 标题由插件索引;正文包含 @include 内容,去掉标签后还原 HTML 实体。 getExtraFields: (page) => [ page.contentRendered + .replace(/]*>[\s\S]*?<\/h[1-6]>/gi, "") .replace(/|<[^>]*>/g, "") .replace(/&(?:#x[\da-f]+|#\d+|[a-z][\da-z]*);/gi, decodeEntity) .replace(/\s+/g, " ")