forked from hazel-group/hazel-group.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goat.config.ts
executable file
·115 lines (103 loc) · 3.81 KB
/
goat.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import fs from "fs/promises";
import path from "path";
import { fileURLToPath } from 'url';
const curFilename = fileURLToPath(import.meta.url);
const curDirname = path.dirname(curFilename);
const starlightPath = path.join(curDirname, 'node_modules/@astrojs/starlight');
// 获取docs目录下的一级文件夹
let docsFile = await fs.readdir('src/content/docs');
docsFile = docsFile.filter((item) => item !== '.DS_Store')
/**
* @description: 替换 utils/route-data.ts
* 传递 categories 参数
*/
const replaceRouteData = async () => {
const originFile = path.join(starlightPath, "/utils/route-data.ts");
const originContent = await fs.readFile(originFile);
const replacedContent = originContent.toString().replace(
/const sidebar = getSidebar.*?;\n/,
'const sidebar = getSidebar(url.pathname, locale, props.categories);\n'
);
await fs.writeFile(originFile, replacedContent);
}
/**
* @description: 替换 utils/navigation.ts
*
*/
const replaceNavigation = async () => {
/**
* 获取当前页面的 sidebar, 左侧菜单动态加载
* 根据页面路由获取sidebar
*/
const originFile = path.join(starlightPath, "/utils/navigation.ts");
const originContent = await fs.readFile(originFile);
const sideBarRegex = /export function getSidebar\(pathname\: string\, locale\: string \| undefined\).+\n(.+)/;
const sideBarContent = originContent.toString().replace(
sideBarRegex,
`export function getSidebar(pathname: string, locale: string | undefined, categories: any): SidebarEntry[] {
const routes = getLocaleRoutes(locale);
const versionRegex = /\\/docs\\/(${docsFile.join('|')})\\//;
const match = versionRegex.exec(pathname);
const version = match ? match[1] : 'latest';
if(categories && categories[version]){
return categories[version].map((group) => configItemToEntry(group, pathname, locale, routes));
}\n`
);
/**
* 核心的 sidebar Link链接构建
*/
/**
* /v2/en/quickstart/quick-start-docker.html => /docs/v2/quickstart/quick-start-docker.html
* /v2/zh-cn/quickstart/quick-start-kubernetes.html => /docs/v2/quickstart/quick-start-docker.html
*/
const sideBarLinkRegex = /href = formatPath\(href\).+\n.+\}/;
const sideBarLinkContent = sideBarContent.replace(
sideBarLinkRegex,
`href = formatPath(href);
const regex = /\\/(${docsFile.join('|')})\\/(en|zh-cn)/;
href = href.replace(regex, '/docs/$1');
}`
);
/**
* 核心的 localeDir链接构建
*/
/**
* /v2/en/quickstart/quick-start-docker.html => /docs/v2/quickstart/quick-start-docker.html
* /v2/zh-cn/quickstart/quick-start-kubernetes.html => /docs/v2/quickstart/quick-start-docker.html
*/
const localeDirRegex = /const localeDir = locale \? locale \+ \'\/\' \+ directory \: directory\;/;
const localeDirContent = sideBarLinkContent.replace(
localeDirRegex,
`const regex = /(${docsFile.join('|')})\\/(en|zh-cn)/;
const localeDir = locale ? locale + '/' + directory.replace(regex, "$1/"+locale) : directory;`
);
await fs.writeFile(originFile, localeDirContent);
}
/**
* @description: 替换 index.astro
* 1. 动态替换核心路由能力
* 2. 动态集成siderBar能力
*/
const replaceIndexAstro = async () => {
const originFile = path.join(starlightPath, "index.astro");
const replacedContent = await fs.readFile('./template/index.startlight.tpl');
const updateContent =replacedContent.toString().replaceAll(
'DOCSREGEX',
`${docsFile.join('|')}`
)
await fs.writeFile(originFile, updateContent);
}
/**
* @description: 替换 404.astro
*/
const replace404Astro = async () => {
const originFile = path.join(starlightPath, "404.astro");
const replacedContent = await fs.readFile('./template/404.startlight.tpl');
await fs.writeFile(originFile, replacedContent.toString());
}
export default async () => {
await replaceRouteData();
await replaceNavigation();
await replaceIndexAstro();
await replace404Astro();
}