Skip to content

Commit b496fdd

Browse files
committed
feat: 🚀 post 相关数据兼容多语言配置,优化插件代码,修复部分插件逻辑问题
1 parent 65fc2bf commit b496fdd

File tree

27 files changed

+272
-153
lines changed

27 files changed

+272
-153
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@
44

55
## TODO
66

7-
- 适配多语言功能:PostList、ArchivesPage、TopArticle
87
- 首页 top 0 位置时监听 F12,开启壁纸功能
98
- 归档页添加 commit 图标风格,如:`http://niubin.site/archive.html`

docs/.vitepress/cache/deps/_metadata.json

+33-15
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,83 @@
11
{
2-
"hash": "f011a082",
3-
"configHash": "564efbc5",
2+
"hash": "c07c1623",
3+
"configHash": "8e3d59cc",
44
"lockfileHash": "d290dc87",
5-
"browserHash": "05f7dce4",
5+
"browserHash": "e4f9db73",
66
"optimized": {
77
"vue": {
88
"src": "../../../../node_modules/.pnpm/[email protected][email protected]/node_modules/vue/dist/vue.runtime.esm-bundler.js",
99
"file": "vue.js",
10-
"fileHash": "64a1207b",
10+
"fileHash": "4211d27f",
1111
"needsInterop": false
1212
},
1313
"vitepress > @vue/devtools-api": {
1414
"src": "../../../../node_modules/.pnpm/@[email protected]/node_modules/@vue/devtools-api/dist/index.js",
1515
"file": "vitepress___@vue_devtools-api.js",
16-
"fileHash": "ece2227c",
16+
"fileHash": "12f6ddea",
1717
"needsInterop": false
1818
},
1919
"vitepress > @vueuse/core": {
2020
"src": "../../../../node_modules/.pnpm/@[email protected][email protected]/node_modules/@vueuse/core/index.mjs",
2121
"file": "vitepress___@vueuse_core.js",
22-
"fileHash": "11e2d4d5",
22+
"fileHash": "005987ea",
23+
"needsInterop": false
24+
},
25+
"vitepress > @vueuse/integrations/useFocusTrap": {
26+
"src": "../../../../node_modules/.pnpm/@[email protected][email protected][email protected][email protected]/node_modules/@vueuse/integrations/useFocusTrap.mjs",
27+
"file": "vitepress___@vueuse_integrations_useFocusTrap.js",
28+
"fileHash": "db7c0779",
29+
"needsInterop": false
30+
},
31+
"vitepress > mark.js/src/vanilla.js": {
32+
"src": "../../../../node_modules/.pnpm/[email protected]/node_modules/mark.js/src/vanilla.js",
33+
"file": "vitepress___mark__js_src_vanilla__js.js",
34+
"fileHash": "eb786ec2",
35+
"needsInterop": false
36+
},
37+
"vitepress > minisearch": {
38+
"src": "../../../../node_modules/.pnpm/[email protected]/node_modules/minisearch/dist/es/index.js",
39+
"file": "vitepress___minisearch.js",
40+
"fileHash": "bcf86016",
2341
"needsInterop": false
2442
},
2543
"@giscus/vue": {
2644
"src": "../../../../node_modules/.pnpm/@[email protected][email protected][email protected]_/node_modules/@giscus/vue/dist/index.js",
2745
"file": "@giscus_vue.js",
28-
"fileHash": "df07806c",
46+
"fileHash": "f2e2d570",
2947
"needsInterop": false
3048
},
3149
"element-plus": {
3250
"src": "../../../../node_modules/.pnpm/[email protected][email protected][email protected]_/node_modules/element-plus/es/index.mjs",
3351
"file": "element-plus.js",
34-
"fileHash": "4c706785",
52+
"fileHash": "cfab3a06",
3553
"needsInterop": false
3654
},
3755
"@element-plus/icons-vue": {
3856
"src": "../../../../node_modules/.pnpm/@[email protected][email protected][email protected]_/node_modules/@element-plus/icons-vue/dist/index.js",
3957
"file": "@element-plus_icons-vue.js",
40-
"fileHash": "f1dcbd8d",
58+
"fileHash": "56e92671",
4159
"needsInterop": false
4260
},
4361
"@waline/client": {
4462
"src": "../../../../node_modules/.pnpm/@[email protected][email protected]/node_modules/@waline/client/dist/slim.js",
4563
"file": "@waline_client.js",
46-
"fileHash": "d03214e8",
64+
"fileHash": "0a058985",
4765
"needsInterop": false
4866
}
4967
},
5068
"chunks": {
51-
"chunk-6QBDH6GL": {
52-
"file": "chunk-6QBDH6GL.js"
69+
"giscus-Ci9LqPcC-PTJE42XD": {
70+
"file": "giscus-Ci9LqPcC-PTJE42XD.js"
5371
},
5472
"chunk-QY2276BV": {
5573
"file": "chunk-QY2276BV.js"
5674
},
75+
"chunk-6QBDH6GL": {
76+
"file": "chunk-6QBDH6GL.js"
77+
},
5778
"chunk-3ZOFCUNA": {
5879
"file": "chunk-3ZOFCUNA.js"
5980
},
60-
"giscus-Ci9LqPcC-PTJE42XD": {
61-
"file": "giscus-Ci9LqPcC-PTJE42XD.js"
62-
},
6381
"chunk-EQCVQC35": {
6482
"file": "chunk-EQCVQC35.js"
6583
}

docs/.vitepress/locales/shared.ts

-3
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,6 @@ const tkConfig = themeConfig({
8282
link: "https://www.youngkbt.cn/?contact=true",
8383
},
8484
],
85-
post: {
86-
showBaseInfo: ["home"],
87-
},
8885
comment: {
8986
provider: "giscus",
9087
options: {

plugins/vitepress-plugin-catalogue/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { join } from "node:path";
55

66
export * from "./types";
77

8-
export default function VitePluginVitePressCatalogue(option: CatalogueOption = {}): Plugin {
8+
export default function VitePluginVitePressCatalogue(option: CatalogueOption = {}): Plugin & { name: string } {
99
return {
1010
name: "vite-plugin-vitepress-catalogue",
1111
config(config: any) {

plugins/vitepress-plugin-doc-analysis/src/helper.ts

+28-9
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,31 @@ export const log = (message: string, type = "yellow") => {
1010
// 默认忽略的文件夹列表
1111
export const DEFAULT_IGNORE_DIR = ["scripts", "components", "assets", ".vitepress", "node_modules", "public"];
1212

13-
export default (option: DocAnalysisOption = {}) => {
13+
/**
14+
* 扫描所有的 md 文件
15+
* @param option 配置项
16+
* @param prefix 指定前缀,在生成 relativePath 的时候会自动加上前缀
17+
*/
18+
export default (option: DocAnalysisOption = {}, prefix = ""): FileInfo[] => {
1419
const { base = process.cwd() } = option;
15-
return readFileList(base, option);
20+
// 开头不允许有 /
21+
prefix = prefix.replace(/^\//, "");
22+
// 结尾必须有 /
23+
prefix = prefix.endsWith("/") ? prefix : `${prefix}/`;
24+
25+
return readFileList(base, option, [], prefix);
1626
};
1727

1828
/**
1929
* 获取所有的 md 文档
2030
*/
21-
export function readFileList(root: string, option: DocAnalysisOption, fileList: FileInfo[] = []) {
22-
const { ignoreList = [] } = option;
31+
export function readFileList(
32+
root: string,
33+
option: DocAnalysisOption,
34+
fileList: FileInfo[] = [],
35+
prefix = ""
36+
): FileInfo[] {
37+
const { base = process.cwd(), ignoreList = [], ignoreIndexMd } = option;
2338
const ignoreListAll = [...DEFAULT_IGNORE_DIR, ...ignoreList];
2439

2540
const secondDirOrFilenames = readdirSync(root);
@@ -31,18 +46,22 @@ export function readFileList(root: string, option: DocAnalysisOption, fileList:
3146

3247
if (statSync(filePath).isDirectory()) {
3348
// 是文件夹目录
34-
readFileList(filePath, option, fileList);
49+
readFileList(filePath, option, fileList, prefix);
3550
} else {
3651
// 是文件
37-
if (!isMdFile(dirOrFilename)) return;
52+
if (!isMdFile(dirOrFilename)) return [];
53+
if (ignoreIndexMd && ["index.md", "index.MD"].includes(dirOrFilename)) return [];
54+
// 根目录的 index.md(首页文档)不扫描
55+
if (filePath === resolve(base, "index.md")) return [];
56+
3857
// 确保路径是绝对路径
39-
const workingDir = resolve(option.base || process.cwd());
58+
const workingDir = resolve(base);
4059
const absoluteFilePath = resolve(filePath);
4160
// 计算相对路径
4261
const relativePath = relative(workingDir, absoluteFilePath).replace(/\\/g, "/");
4362
let type = extname(dirOrFilename);
4463

45-
if (type === ".md") fileList.push({ filePath, relativePath });
64+
if (type === ".md") fileList.push({ filePath, relativePath: prefix + relativePath });
4665
}
4766
});
4867
return fileList;
@@ -59,5 +78,5 @@ const isMdFile = (filePath: string) => {
5978
};
6079

6180
const isSome = (arr: Array<string | RegExp>, name: string) => {
62-
return arr.some(item => name.includes(item as string) || (item instanceof RegExp && item.test(name)));
81+
return arr.some(item => item === name || (item instanceof RegExp && item.test(name)));
6382
};
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,53 @@
11
import type { Plugin } from "vite";
2-
import { DocAnalysisOption } from "./types";
2+
import { DocAnalysisOption, FileInfo } from "./types";
33
import { join } from "node:path";
44
import readFileList from "./helper";
55
import { getLastCommitTime, getEachFileWords, getTotalFileWords, getLastUpdateTime } from "./util";
66

77
export * from "./types";
88

9-
export default function VitePluginVitePressDocAnalysis(option: DocAnalysisOption = {}): Plugin {
9+
export default function VitePluginVitePressDocAnalysis(option: DocAnalysisOption = {}): Plugin & { name: string } {
1010
return {
1111
name: "vitepress-plugin-doc-analysis",
1212
async config(config: any) {
1313
const {
14-
site: { themeConfig },
14+
site: { themeConfig = {}, locales = {} },
1515
srcDir,
1616
} = config.vitepress;
1717

1818
option.base = option.base ? join(process.cwd(), option.base) : srcDir;
1919

20-
const fileList = readFileList(option);
21-
const filePathList = fileList.map(item => item.filePath);
20+
// 多语言 key 数组
21+
const localesKeys = Object.keys(locales).filter(key => key !== "root");
22+
// 如果不是多语言,则不需要处理多语言的文档分析
23+
if (!localesKeys.length) return doDocAnalysisThenSet(themeConfig, readFileList(option), option);
2224

23-
const totalFileWords = getTotalFileWords(filePathList);
24-
const eachFileWords = getEachFileWords(fileList, option.cn, option.en);
25+
// 多语言处理,针对每个语言的目录进行单独的扫描(除了 root)
26+
localesKeys.forEach(localesKey => {
27+
const fileList = readFileList({ ...option, base: `${option.base}/${localesKey}` }, localesKey);
28+
doDocAnalysisThenSet(locales[localesKey].themeConfig, fileList, option);
29+
});
2530

26-
themeConfig.docAnalysisInfo = {
27-
fileList,
28-
totalFileWords,
29-
eachFileWords,
30-
lastCommitTime: (await getLastCommitTime()) || getLastUpdateTime(filePathList),
31-
};
31+
// 对 root 根目录的文档进行单独的分析,且不扫描其他语言目录
32+
const rootFileList = readFileList({ ...option, ignoreList: [...(option.ignoreList || []), ...localesKeys] });
33+
doDocAnalysisThenSet(locales["root"].themeConfig, rootFileList, option);
3234
},
3335
};
3436
}
37+
38+
const doDocAnalysisThenSet = async (themeConfig: any, fileList: FileInfo[], option: DocAnalysisOption) => {
39+
const filePathList = fileList.map(item => item.filePath);
40+
41+
const totalFileWords = getTotalFileWords(filePathList);
42+
const eachFileWords = getEachFileWords(fileList, option.cn, option.en);
43+
const lastCommitTime = (await getLastCommitTime()) || getLastUpdateTime(filePathList);
44+
45+
// 防止 themeConfig 为 undefined
46+
themeConfig = themeConfig || {};
47+
themeConfig.docAnalysisInfo = {
48+
fileList,
49+
totalFileWords,
50+
eachFileWords,
51+
lastCommitTime,
52+
};
53+
};

plugins/vitepress-plugin-doc-analysis/src/types.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@ export interface DocAnalysisOption {
66
*/
77
ignoreList?: Array<RegExp | string>;
88
/**
9-
* 文章所在的目录,基于 package.json 所在目录
9+
* 文章所在的目录,基于 package.json 所在目录,开头不需要有 /
10+
* @default 'vitepress 的 srcDir 配置项'
1011
*/
1112
base?: string;
13+
/**
14+
* 是否忽略每个目录下的 index.md 文件
15+
*
16+
* @default false
17+
*/
18+
ignoreIndexMd?: boolean;
1219
/**
1320
* 1 分钟内阅读的中文字数
1421
* @default 300

plugins/vitepress-plugin-doc-analysis/src/util.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import { exec } from "child_process";
1010
export function getTotalFileWords(filePathList: string[]) {
1111
let wordCount = 0;
1212
filePathList.forEach((item: any) => {
13-
const content = readFileSync(item, "utf8");
13+
const fileContent = readFileSync(item, "utf8");
14+
const { content } = matter(fileContent, {});
15+
// 使用 content 而不是 fileContent 是因为 fileContent 有 frontmatter,影响字数
1416
let len = getCounter(content);
1517
wordCount += len[0] + len[1];
1618
});
@@ -26,7 +28,9 @@ export function getEachFileWords(fileList: FileInfo[], cn: number = 300, en: num
2628
const filePathListWords: FileWords[] = [];
2729

2830
fileList.forEach(item => {
29-
const content = readFileSync(item.filePath, "utf8");
31+
const fileContent = readFileSync(item.filePath, "utf8");
32+
const { data, content } = matter(fileContent, {});
33+
// 使用 content 而不是 fileContent 是因为 fileContent 有 frontmatter,影响字数
3034
let len = getCounter(content);
3135
// 计算预计的阅读时间
3236
let readingTime = getReadTime(len, cn, en);
@@ -35,7 +39,6 @@ export function getEachFileWords(fileList: FileInfo[], cn: number = 300, en: num
3539

3640
if (wordsCount >= 1000) wordsCount = Math.round(wordsCount / 100) / 10 + "k";
3741

38-
const { data } = matter(content, {});
3942
filePathListWords.push({ fileInfo: item, wordsCount, readingTime, frontmatter: data });
4043
});
4144

plugins/vitepress-plugin-md-h1/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Plugin } from "vite";
33
import matter from "gray-matter";
44
import { basename } from "node:path";
55

6-
export default function VitePluginVitePressMdH1(): Plugin {
6+
export default function VitePluginVitePressMdH1(): Plugin & { name: string } {
77
return {
88
name: "vite-plugin-vitepress-md-h1",
99
transform: (code, id) => {

plugins/vitepress-plugin-permalink/src/helper.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,5 @@ const isMdFile = (filePath: string) => {
109109
};
110110

111111
const isSome = (arr: Array<string | RegExp>, name: string) => {
112-
return arr.some(item => name.includes(item as string) || (item instanceof RegExp && item.test(name)));
112+
return arr.some(item => item === name || (item instanceof RegExp && item.test(name)));
113113
};

plugins/vitepress-plugin-permalink/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const log = (message: string, type = "yellow") => {
1010
console.log((chalk as any)[type](message));
1111
};
1212

13-
export default function VitePluginVitePressPermalink(option: PermalinkOption = {}): Plugin {
13+
export default function VitePluginVitePressPermalink(option: PermalinkOption = {}): Plugin & { name: string } {
1414
let vitepressConfig: any = {};
1515

1616
return {

plugins/vitepress-plugin-permalink/src/types.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export interface PermalinkOption {
66
*/
77
ignoreList?: Array<RegExp | string>;
88
/**
9-
* 文章所在的目录,基于 package.json 所在目录
9+
* 文章所在的目录,基于 package.json 所在目录,开头不需要有 /
10+
* @default 'vitepress 的 srcDir 配置项'
1011
*/
1112
base?: string;
1213
}

0 commit comments

Comments
 (0)