Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions src/.vuepress/config.ts
Original file line number Diff line number Diff line change
@@ -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: [
Expand Down
29 changes: 29 additions & 0 deletions src/.vuepress/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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(/<h[1-6]\b[^>]*>[\s\S]*?<\/h[1-6]>/gi, "")
.replace(/<!--[\s\S]*?-->|<[^>]*>/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;
},
};
};
Loading