@ruan-cat/vitepress-preset-config 2.6.0 → 2.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/config.mjs +145 -5
- package/package.json +2 -1
- package/src/config/index.ts +1 -0
- package/src/config/prompts-nav.ts +241 -0
- package/src/config.mts +5 -2
package/dist/config.mjs
CHANGED
|
@@ -2,7 +2,19 @@
|
|
|
2
2
|
import { defineConfig } from "vitepress";
|
|
3
3
|
import { generateSidebar } from "vitepress-sidebar";
|
|
4
4
|
import { vitepressDemoPlugin } from "vitepress-demo-plugin";
|
|
5
|
-
import { merge as
|
|
5
|
+
import { merge as merge3, isUndefined as isUndefined3, cloneDeep as cloneDeep2 } from "lodash-es";
|
|
6
|
+
|
|
7
|
+
// src/config/page-order-config.ts
|
|
8
|
+
var pageOrderConfig = {
|
|
9
|
+
/** 提示词页面排序 */
|
|
10
|
+
prompts: {
|
|
11
|
+
order: 8e3
|
|
12
|
+
},
|
|
13
|
+
/** 变更日志页面排序 */
|
|
14
|
+
changelog: {
|
|
15
|
+
order: 9e3
|
|
16
|
+
}
|
|
17
|
+
};
|
|
6
18
|
|
|
7
19
|
// src/config/copy-readme.ts
|
|
8
20
|
import fs from "fs";
|
|
@@ -195,9 +207,136 @@ function handleChangeLog(userConfig) {
|
|
|
195
207
|
nav.push({ text: "\u66F4\u65B0\u65E5\u5FD7", link: "/CHANGELOG.md" });
|
|
196
208
|
}
|
|
197
209
|
|
|
210
|
+
// src/config/prompts-nav.ts
|
|
211
|
+
import fs4 from "fs";
|
|
212
|
+
import path5 from "path";
|
|
213
|
+
import consola5 from "consola";
|
|
214
|
+
import { isUndefined as isUndefined2, merge } from "lodash-es";
|
|
215
|
+
import matter from "gray-matter";
|
|
216
|
+
var PROMPTS_INDEX_MD_PATH = "prompts/index.md";
|
|
217
|
+
function getProjectRootFromArgs() {
|
|
218
|
+
const args = process.argv;
|
|
219
|
+
const vitepressIndex = args.findIndex((arg) => arg.includes("vitepress"));
|
|
220
|
+
if (vitepressIndex === -1) {
|
|
221
|
+
return null;
|
|
222
|
+
}
|
|
223
|
+
for (let i = vitepressIndex + 1; i < args.length; i++) {
|
|
224
|
+
const arg = args[i];
|
|
225
|
+
if (arg === "dev" || arg === "build" || arg === "serve" || arg === "preview") {
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
if (arg.startsWith("-")) {
|
|
229
|
+
if (arg.startsWith("--") && !arg.includes("=")) {
|
|
230
|
+
i++;
|
|
231
|
+
}
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
return path5.resolve(process.cwd(), arg);
|
|
235
|
+
}
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
function getVitepressProjectRoot() {
|
|
239
|
+
const projectRootFromArgs = getProjectRootFromArgs();
|
|
240
|
+
if (projectRootFromArgs) {
|
|
241
|
+
const vitepressDir2 = path5.join(projectRootFromArgs, ".vitepress");
|
|
242
|
+
if (fs4.existsSync(vitepressDir2)) {
|
|
243
|
+
consola5.log("\u4ECE\u547D\u4EE4\u884C\u53C2\u6570\u83B7\u53D6\u5230 VitePress \u9879\u76EE\u6839\u76EE\u5F55\uFF1A", projectRootFromArgs);
|
|
244
|
+
return projectRootFromArgs;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
let currentDir = process.cwd();
|
|
248
|
+
const root = path5.parse(currentDir).root;
|
|
249
|
+
while (currentDir !== root) {
|
|
250
|
+
const vitepressDir2 = path5.join(currentDir, ".vitepress");
|
|
251
|
+
if (fs4.existsSync(vitepressDir2)) {
|
|
252
|
+
consola5.log("\u901A\u8FC7\u5411\u4E0A\u67E5\u627E\u83B7\u53D6\u5230 VitePress \u9879\u76EE\u6839\u76EE\u5F55\uFF1A", currentDir);
|
|
253
|
+
return currentDir;
|
|
254
|
+
}
|
|
255
|
+
currentDir = path5.dirname(currentDir);
|
|
256
|
+
}
|
|
257
|
+
const vitepressDir = path5.join(root, ".vitepress");
|
|
258
|
+
if (fs4.existsSync(vitepressDir)) {
|
|
259
|
+
consola5.log("\u5728\u6587\u4EF6\u7CFB\u7EDF\u6839\u76EE\u5F55\u627E\u5230 VitePress \u9879\u76EE\u6839\u76EE\u5F55\uFF1A", root);
|
|
260
|
+
return root;
|
|
261
|
+
}
|
|
262
|
+
const fallbackDir = process.cwd();
|
|
263
|
+
consola5.warn("\u672A\u627E\u5230 .vitepress \u76EE\u5F55\uFF0C\u56DE\u9000\u5230\u5F53\u524D\u5DE5\u4F5C\u76EE\u5F55\uFF1A", fallbackDir);
|
|
264
|
+
return fallbackDir;
|
|
265
|
+
}
|
|
266
|
+
function getVitepressSourceDirectory(userConfig) {
|
|
267
|
+
const projectRoot = getVitepressProjectRoot();
|
|
268
|
+
const srcDir = userConfig.srcDir;
|
|
269
|
+
if (srcDir) {
|
|
270
|
+
const sourceDirectory = path5.resolve(projectRoot, srcDir);
|
|
271
|
+
consola5.log("\u4ECE\u914D\u7F6E\u4E2D\u83B7\u53D6\u5230 srcDir:", srcDir);
|
|
272
|
+
consola5.log("VitePress \u6E90\u76EE\u5F55\u4E3A\uFF1A", sourceDirectory);
|
|
273
|
+
return sourceDirectory;
|
|
274
|
+
}
|
|
275
|
+
consola5.log("\u914D\u7F6E\u4E2D\u672A\u6307\u5B9A srcDir\uFF0C\u6E90\u76EE\u5F55\u7B49\u4E8E\u9879\u76EE\u6839\u76EE\u5F55\uFF1A", projectRoot);
|
|
276
|
+
return projectRoot;
|
|
277
|
+
}
|
|
278
|
+
function hasPromptsIndexMd(userConfig) {
|
|
279
|
+
const sourceDir = getVitepressSourceDirectory(userConfig);
|
|
280
|
+
const res = fs4.existsSync(path5.resolve(sourceDir, PROMPTS_INDEX_MD_PATH));
|
|
281
|
+
if (!res) {
|
|
282
|
+
consola5.log("\u5F53\u524D\u9879\u76EE\u7684vitepress\u6E90\u76EE\u5F55\u4E3A\uFF1A", sourceDir);
|
|
283
|
+
consola5.warn(`\u5F53\u524D\u9879\u76EE\u7684vitepress\u6E90\u76EE\u5F55\u4E0D\u5B58\u5728 ${PROMPTS_INDEX_MD_PATH} \u6587\u4EF6`);
|
|
284
|
+
}
|
|
285
|
+
return res;
|
|
286
|
+
}
|
|
287
|
+
function writeYaml2PromptsIndexMd(userConfig, data) {
|
|
288
|
+
const newData = data ?? pageOrderConfig.prompts;
|
|
289
|
+
const sourceDir = getVitepressSourceDirectory(userConfig);
|
|
290
|
+
const mdPath = path5.resolve(sourceDir, PROMPTS_INDEX_MD_PATH);
|
|
291
|
+
try {
|
|
292
|
+
let fileContent = "";
|
|
293
|
+
let existingData = {};
|
|
294
|
+
if (fs4.existsSync(mdPath)) {
|
|
295
|
+
fileContent = fs4.readFileSync(mdPath, "utf-8");
|
|
296
|
+
const parsed = matter(fileContent);
|
|
297
|
+
if (parsed.data && Object.keys(parsed.data).length > 0) {
|
|
298
|
+
existingData = parsed.data;
|
|
299
|
+
consola5.log("\u68C0\u6D4B\u5230\u5DF2\u6709 YAML frontmatter\uFF0C\u5C06\u8FDB\u884C\u6570\u636E\u5408\u5E76");
|
|
300
|
+
} else {
|
|
301
|
+
consola5.log("\u6587\u4EF6\u5B58\u5728\u4F46\u6CA1\u6709 YAML frontmatter\uFF0C\u5C06\u5199\u5165\u65B0\u7684 frontmatter");
|
|
302
|
+
}
|
|
303
|
+
const mergedData = merge({}, existingData, newData);
|
|
304
|
+
const newContent = matter.stringify(parsed.content, mergedData);
|
|
305
|
+
fs4.writeFileSync(mdPath, newContent, "utf-8");
|
|
306
|
+
consola5.success(`\u5DF2\u5C06YAML\u6570\u636E\u5199\u5165\u5230 ${mdPath}`);
|
|
307
|
+
} else {
|
|
308
|
+
consola5.warn(`\u6587\u4EF6 ${mdPath} \u4E0D\u5B58\u5728\uFF0C\u5C06\u521B\u5EFA\u65B0\u6587\u4EF6`);
|
|
309
|
+
const dir = path5.dirname(mdPath);
|
|
310
|
+
if (!fs4.existsSync(dir)) {
|
|
311
|
+
fs4.mkdirSync(dir, { recursive: true });
|
|
312
|
+
}
|
|
313
|
+
const newContent = matter.stringify("", newData);
|
|
314
|
+
fs4.writeFileSync(mdPath, newContent, "utf-8");
|
|
315
|
+
consola5.success(`\u5DF2\u521B\u5EFA\u6587\u4EF6\u5E76\u5199\u5165YAML\u6570\u636E\u5230 ${mdPath}`);
|
|
316
|
+
}
|
|
317
|
+
} catch (error) {
|
|
318
|
+
consola5.error(`\u5199\u5165 YAML \u6570\u636E\u5230 ${mdPath} \u65F6\u53D1\u751F\u9519\u8BEF:`, error);
|
|
319
|
+
throw error;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
function handlePrompts(userConfig) {
|
|
323
|
+
var _a;
|
|
324
|
+
if (!hasPromptsIndexMd(userConfig)) {
|
|
325
|
+
consola5.warn(` \u672A\u627E\u5230\u63D0\u793A\u8BCD\u7D22\u5F15\u6587\u4EF6\uFF0C\u4E0D\u6DFB\u52A0\u63D0\u793A\u8BCD\u5BFC\u822A\u680F\u3002 `);
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
writeYaml2PromptsIndexMd(userConfig);
|
|
329
|
+
const nav = (_a = userConfig == null ? void 0 : userConfig.themeConfig) == null ? void 0 : _a.nav;
|
|
330
|
+
if (isUndefined2(nav)) {
|
|
331
|
+
consola5.error(` \u5F53\u524D\u7684\u7528\u6237\u914D\u7F6E\u4E3A\uFF1A `, userConfig);
|
|
332
|
+
throw new Error(` nav \u9ED8\u8BA4\u63D0\u4F9B\u7684\u5BFC\u822A\u680F\u914D\u7F6E\u4E3A\u7A7A\u3002\u4E0D\u7B26\u5408\u9ED8\u8BA4\u914D\u7F6E\uFF0C\u8BF7\u68C0\u67E5\u3002 `);
|
|
333
|
+
}
|
|
334
|
+
nav.push({ text: "\u63D0\u793A\u8BCD", link: `/${PROMPTS_INDEX_MD_PATH}` });
|
|
335
|
+
}
|
|
336
|
+
|
|
198
337
|
// src/config/teek.ts
|
|
199
338
|
import { defineTeekConfig } from "vitepress-theme-teek/config";
|
|
200
|
-
import { merge, cloneDeep } from "lodash-es";
|
|
339
|
+
import { merge as merge2, cloneDeep } from "lodash-es";
|
|
201
340
|
var defaultTeekConfig = {
|
|
202
341
|
/**
|
|
203
342
|
* 启用侧边栏展开/折叠触发器
|
|
@@ -260,7 +399,7 @@ var defaultTeekConfig = {
|
|
|
260
399
|
};
|
|
261
400
|
function handleTeekConfig(userConfig, extraConfig) {
|
|
262
401
|
const { teekConfig = defaultTeekConfig } = extraConfig ?? {};
|
|
263
|
-
userConfig.extends = defineTeekConfig(
|
|
402
|
+
userConfig.extends = defineTeekConfig(merge2({}, cloneDeep(defaultTeekConfig), teekConfig));
|
|
264
403
|
}
|
|
265
404
|
|
|
266
405
|
// src/config.mts
|
|
@@ -294,7 +433,7 @@ var defaultSidebarOptions = {
|
|
|
294
433
|
debugPrint: false
|
|
295
434
|
};
|
|
296
435
|
function getMergeSidebarOptions(options) {
|
|
297
|
-
return
|
|
436
|
+
return merge3({}, cloneDeep2(defaultSidebarOptions), isUndefined3(options) ? {} : options);
|
|
298
437
|
}
|
|
299
438
|
function setGenerateSidebar(options) {
|
|
300
439
|
return generateSidebar(getMergeSidebarOptions(options));
|
|
@@ -372,7 +511,8 @@ var defaultUserConfig = {
|
|
|
372
511
|
}
|
|
373
512
|
};
|
|
374
513
|
function setUserConfig(config, extraConfig) {
|
|
375
|
-
const resUserConfig =
|
|
514
|
+
const resUserConfig = merge3({}, cloneDeep2(defaultUserConfig), isUndefined3(config) ? {} : config);
|
|
515
|
+
handlePrompts(resUserConfig);
|
|
376
516
|
handleChangeLog(resUserConfig);
|
|
377
517
|
handlePlugins(resUserConfig, extraConfig);
|
|
378
518
|
handleTeekConfig(resUserConfig, extraConfig);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ruan-cat/vitepress-preset-config",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.7.0",
|
|
4
4
|
"description": "用于给大多数的vitepress项目提供一个预设的配置文件。",
|
|
5
5
|
"homepage": "https://vitepress-preset.ruancat6312.top",
|
|
6
6
|
"types": "./src/config.mts",
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
"@nolebase/vitepress-plugin-git-changelog": "^2.18.2",
|
|
11
11
|
"@shikijs/vitepress-twoslash": "^3.13.0",
|
|
12
12
|
"consola": "^3.4.2",
|
|
13
|
+
"gray-matter": "^4.0.3",
|
|
13
14
|
"js-yaml": "^4.1.0",
|
|
14
15
|
"lodash-es": "^4.17.21",
|
|
15
16
|
"markdown-it-async": "^2.2.0",
|
package/src/config/index.ts
CHANGED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { type UserConfig, type DefaultTheme } from "vitepress";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import consola from "consola";
|
|
5
|
+
import { isUndefined, merge } from "lodash-es";
|
|
6
|
+
import matter from "gray-matter";
|
|
7
|
+
|
|
8
|
+
import { pageOrderConfig } from "./page-order-config";
|
|
9
|
+
|
|
10
|
+
/** 提示词索引文件的相对路径 */
|
|
11
|
+
const PROMPTS_INDEX_MD_PATH = "prompts/index.md" as const;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 从命令行参数中获取 VitePress 项目根目录
|
|
15
|
+
* @description
|
|
16
|
+
* 解析命令行参数,查找 vitepress dev/build 命令后的第一个路径参数
|
|
17
|
+
* @private 目前仅设计为内部使用
|
|
18
|
+
*/
|
|
19
|
+
function getProjectRootFromArgs(): string | null {
|
|
20
|
+
const args = process.argv;
|
|
21
|
+
|
|
22
|
+
// 查找 vitepress 命令的索引
|
|
23
|
+
const vitepressIndex = args.findIndex((arg) => arg.includes("vitepress"));
|
|
24
|
+
|
|
25
|
+
if (vitepressIndex === -1) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// 查找 dev 或 build 命令后的第一个非选项参数(不以 - 或 -- 开头)
|
|
30
|
+
for (let i = vitepressIndex + 1; i < args.length; i++) {
|
|
31
|
+
const arg = args[i];
|
|
32
|
+
|
|
33
|
+
// 跳过命令本身(dev, build等)和选项参数
|
|
34
|
+
if (arg === "dev" || arg === "build" || arg === "serve" || arg === "preview") {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// 跳过选项和选项值
|
|
39
|
+
if (arg.startsWith("-")) {
|
|
40
|
+
// 如果是 --port 这样的选项,也跳过下一个值
|
|
41
|
+
if (arg.startsWith("--") && !arg.includes("=")) {
|
|
42
|
+
i++; // 跳过选项值
|
|
43
|
+
}
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// 找到路径参数,解析为绝对路径
|
|
48
|
+
return path.resolve(process.cwd(), arg);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 获得 vitepress 项目的 `项目根目录 (project root)`
|
|
56
|
+
* @description
|
|
57
|
+
* 检查出当前运行vitepress build 命令时,所使用的项目根目录,并对外返回该目录。
|
|
58
|
+
* @see https://vitepress.dev/zh/guide/routing#root-and-source-directory
|
|
59
|
+
* @private 目前仅设计为内部使用
|
|
60
|
+
*/
|
|
61
|
+
function getVitepressProjectRoot(): string {
|
|
62
|
+
// 首先尝试从命令行参数获取
|
|
63
|
+
const projectRootFromArgs = getProjectRootFromArgs();
|
|
64
|
+
if (projectRootFromArgs) {
|
|
65
|
+
const vitepressDir = path.join(projectRootFromArgs, ".vitepress");
|
|
66
|
+
if (fs.existsSync(vitepressDir)) {
|
|
67
|
+
consola.log("从命令行参数获取到 VitePress 项目根目录:", projectRootFromArgs);
|
|
68
|
+
return projectRootFromArgs;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// 如果命令行参数没有提供或无效,尝试向上查找 .vitepress 目录
|
|
73
|
+
let currentDir = process.cwd();
|
|
74
|
+
const root = path.parse(currentDir).root;
|
|
75
|
+
|
|
76
|
+
while (currentDir !== root) {
|
|
77
|
+
const vitepressDir = path.join(currentDir, ".vitepress");
|
|
78
|
+
if (fs.existsSync(vitepressDir)) {
|
|
79
|
+
consola.log("通过向上查找获取到 VitePress 项目根目录:", currentDir);
|
|
80
|
+
return currentDir;
|
|
81
|
+
}
|
|
82
|
+
currentDir = path.dirname(currentDir);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// 检查根目录本身
|
|
86
|
+
const vitepressDir = path.join(root, ".vitepress");
|
|
87
|
+
if (fs.existsSync(vitepressDir)) {
|
|
88
|
+
consola.log("在文件系统根目录找到 VitePress 项目根目录:", root);
|
|
89
|
+
return root;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// 如果找不到 .vitepress 目录,回退到当前工作目录
|
|
93
|
+
const fallbackDir = process.cwd();
|
|
94
|
+
consola.warn("未找到 .vitepress 目录,回退到当前工作目录:", fallbackDir);
|
|
95
|
+
return fallbackDir;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 获取vitepress项目的源目录
|
|
100
|
+
* @description
|
|
101
|
+
* 获取vitepress项目的源目录 并对外返回该目录。
|
|
102
|
+
*
|
|
103
|
+
* 源目录的计算规则:
|
|
104
|
+
* 1. 如果配置中指定了 srcDir,则源目录 = 项目根目录 + srcDir
|
|
105
|
+
* 2. 如果没有指定 srcDir,则源目录 = 项目根目录
|
|
106
|
+
*
|
|
107
|
+
* @param userConfig - VitePress 用户配置对象
|
|
108
|
+
* @see https://vitepress.dev/zh/guide/routing#source-directory
|
|
109
|
+
* @private 目前仅设计为内部使用
|
|
110
|
+
*/
|
|
111
|
+
function getVitepressSourceDirectory(userConfig: UserConfig<DefaultTheme.Config>): string {
|
|
112
|
+
// 获取项目根目录
|
|
113
|
+
const projectRoot = getVitepressProjectRoot();
|
|
114
|
+
|
|
115
|
+
// 读取配置中的 srcDir(如果有)
|
|
116
|
+
const srcDir = (userConfig as any).srcDir as string | undefined;
|
|
117
|
+
|
|
118
|
+
// 如果配置了 srcDir,将其与项目根目录拼接;否则返回项目根目录
|
|
119
|
+
if (srcDir) {
|
|
120
|
+
const sourceDirectory = path.resolve(projectRoot, srcDir);
|
|
121
|
+
consola.log("从配置中获取到 srcDir:", srcDir);
|
|
122
|
+
consola.log("VitePress 源目录为:", sourceDirectory);
|
|
123
|
+
return sourceDirectory;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// 默认情况下,源目录等于项目根目录
|
|
127
|
+
consola.log("配置中未指定 srcDir,源目录等于项目根目录:", projectRoot);
|
|
128
|
+
return projectRoot;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 检查当前运行的 `源目录` 是否存在 `prompts/index.md` 文件
|
|
133
|
+
* @description
|
|
134
|
+
* 检查当前运行的 `源目录` 是否存在 `prompts/index.md` 文件,并对外返回检查结果。
|
|
135
|
+
* @private 目前仅设计为内部使用·
|
|
136
|
+
*/
|
|
137
|
+
function hasPromptsIndexMd(userConfig: UserConfig<DefaultTheme.Config>) {
|
|
138
|
+
const sourceDir = getVitepressSourceDirectory(userConfig);
|
|
139
|
+
const res = fs.existsSync(path.resolve(sourceDir, PROMPTS_INDEX_MD_PATH));
|
|
140
|
+
if (!res) {
|
|
141
|
+
consola.log("当前项目的vitepress源目录为:", sourceDir);
|
|
142
|
+
consola.warn(`当前项目的vitepress源目录不存在 ${PROMPTS_INDEX_MD_PATH} 文件`);
|
|
143
|
+
}
|
|
144
|
+
return res;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* 将YAML数据写入到提示词索引文件内
|
|
149
|
+
* @description
|
|
150
|
+
* - 读取 markdown 文件
|
|
151
|
+
* - 检查是否已经包含了预先准备好的 yaml 信息
|
|
152
|
+
* - 如果有 yaml 信息,就用提供的数据做数据拓展(使用 lodash merge)
|
|
153
|
+
* - 如果没有 yaml 信息,就写入顶部 yaml 信息
|
|
154
|
+
*
|
|
155
|
+
* @param userConfig - VitePress 用户配置对象
|
|
156
|
+
* @param data - 要写入或合并的 YAML 数据
|
|
157
|
+
* @private 目前仅设计为内部使用
|
|
158
|
+
*/
|
|
159
|
+
function writeYaml2PromptsIndexMd(userConfig: UserConfig<DefaultTheme.Config>, data?: Record<string, any>) {
|
|
160
|
+
// 获取要写入的数据
|
|
161
|
+
const newData = data ?? pageOrderConfig.prompts;
|
|
162
|
+
|
|
163
|
+
// 获取文件路径
|
|
164
|
+
const sourceDir = getVitepressSourceDirectory(userConfig);
|
|
165
|
+
const mdPath = path.resolve(sourceDir, PROMPTS_INDEX_MD_PATH);
|
|
166
|
+
|
|
167
|
+
try {
|
|
168
|
+
// 读取 markdown 文件
|
|
169
|
+
let fileContent = "";
|
|
170
|
+
let existingData: Record<string, any> = {};
|
|
171
|
+
|
|
172
|
+
if (fs.existsSync(mdPath)) {
|
|
173
|
+
fileContent = fs.readFileSync(mdPath, "utf-8");
|
|
174
|
+
|
|
175
|
+
// 使用 gray-matter 解析文件
|
|
176
|
+
const parsed = matter(fileContent);
|
|
177
|
+
|
|
178
|
+
// 如果有 frontmatter,获取已有数据
|
|
179
|
+
if (parsed.data && Object.keys(parsed.data).length > 0) {
|
|
180
|
+
existingData = parsed.data;
|
|
181
|
+
consola.log("检测到已有 YAML frontmatter,将进行数据合并");
|
|
182
|
+
} else {
|
|
183
|
+
consola.log("文件存在但没有 YAML frontmatter,将写入新的 frontmatter");
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// 使用 lodash merge 合并数据
|
|
187
|
+
// merge 会深度合并对象,新数据会覆盖旧数据
|
|
188
|
+
const mergedData = merge({}, existingData, newData);
|
|
189
|
+
|
|
190
|
+
// 使用 gray-matter 的 stringify 方法生成新的文件内容
|
|
191
|
+
const newContent = matter.stringify(parsed.content, mergedData);
|
|
192
|
+
|
|
193
|
+
// 写入文件
|
|
194
|
+
fs.writeFileSync(mdPath, newContent, "utf-8");
|
|
195
|
+
consola.success(`已将YAML数据写入到 ${mdPath}`);
|
|
196
|
+
} else {
|
|
197
|
+
// 文件不存在,创建新文件
|
|
198
|
+
consola.warn(`文件 ${mdPath} 不存在,将创建新文件`);
|
|
199
|
+
|
|
200
|
+
// 确保目录存在
|
|
201
|
+
const dir = path.dirname(mdPath);
|
|
202
|
+
if (!fs.existsSync(dir)) {
|
|
203
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// 创建带有 frontmatter 的空文件
|
|
207
|
+
const newContent = matter.stringify("", newData);
|
|
208
|
+
fs.writeFileSync(mdPath, newContent, "utf-8");
|
|
209
|
+
consola.success(`已创建文件并写入YAML数据到 ${mdPath}`);
|
|
210
|
+
}
|
|
211
|
+
} catch (error) {
|
|
212
|
+
consola.error(`写入 YAML 数据到 ${mdPath} 时发生错误:`, error);
|
|
213
|
+
throw error;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* 处理提示词文件
|
|
219
|
+
* @description
|
|
220
|
+
* - 检查当前文档的运行目录内 是否包含有约定格式的提示词文档。
|
|
221
|
+
* - 如果存在 就给目标文件增加一个顶部yaml信息,做排序使用。
|
|
222
|
+
* - 并且文档的顶部导航栏内,增加一个固定的提示词入口。
|
|
223
|
+
*/
|
|
224
|
+
export function handlePrompts(userConfig: UserConfig<DefaultTheme.Config>) {
|
|
225
|
+
if (!hasPromptsIndexMd(userConfig)) {
|
|
226
|
+
consola.warn(` 未找到提示词索引文件,不添加提示词导航栏。 `);
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// 给提示词索引文件 写入排序值
|
|
231
|
+
writeYaml2PromptsIndexMd(userConfig);
|
|
232
|
+
|
|
233
|
+
const nav = userConfig?.themeConfig?.nav;
|
|
234
|
+
|
|
235
|
+
if (isUndefined(nav)) {
|
|
236
|
+
consola.error(` 当前的用户配置为: `, userConfig);
|
|
237
|
+
throw new Error(` nav 默认提供的导航栏配置为空。不符合默认配置,请检查。 `);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
nav.push({ text: "提示词", link: `/${PROMPTS_INDEX_MD_PATH}` });
|
|
241
|
+
}
|
package/src/config.mts
CHANGED
|
@@ -17,7 +17,7 @@ import { transformerTwoslash } from "@shikijs/vitepress-twoslash";
|
|
|
17
17
|
|
|
18
18
|
import llmstxt, { copyOrDownloadAsMarkdownButtons } from "vitepress-plugin-llms";
|
|
19
19
|
|
|
20
|
-
import { defaultTeekConfig, handleTeekConfig, handlePlugins, handleChangeLog } from "./config/index.ts";
|
|
20
|
+
import { defaultTeekConfig, handleTeekConfig, handlePlugins, handleChangeLog, handlePrompts } from "./config/index.ts";
|
|
21
21
|
|
|
22
22
|
/** @see https://vitepress-ext.leelaa.cn/Mermaid.html#扩展-md-插件 */
|
|
23
23
|
import { MermaidPlugin } from "@leelaa/vitepress-plugin-extended";
|
|
@@ -183,7 +183,10 @@ export function setUserConfig(
|
|
|
183
183
|
/** 最终的用户数据 */
|
|
184
184
|
const resUserConfig = merge({}, cloneDeep(defaultUserConfig), isUndefined(config) ? {} : config);
|
|
185
185
|
|
|
186
|
-
//
|
|
186
|
+
// 处理提示词导航栏
|
|
187
|
+
handlePrompts(resUserConfig);
|
|
188
|
+
|
|
189
|
+
// 处理变更日志导航栏
|
|
187
190
|
handleChangeLog(resUserConfig);
|
|
188
191
|
|
|
189
192
|
// 设置插件
|