-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.js
73 lines (61 loc) · 2.18 KB
/
utils.js
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
const path = require('path');
// 获取页面路径(用于侧边栏激活状态判断)
function getPagePath(relativePath) {
// 移除 .md 或 .html 扩展名
const pathWithoutExt = relativePath.replace(/\.(md|html)$/, '');
// 如果是 index 文件,特殊处理
const normalizedPath = pathWithoutExt === 'index' ? '' : pathWithoutExt;
// 转换为 URL 风格的路径(使用正斜杠)
const urlPath = normalizedPath.split(path.sep).join('/');
// 确保路径以 / 开头
return urlPath.startsWith('/') ? urlPath : `/${urlPath}`;
}
// 判断特定功能是否启用
function isFeatureEnabled(config, featureName) {
return config.feature?.[featureName]?.enable === true;
}
function appendHtml(path, config, locale) {
// 如果是字符串,替换 .md 为 .html
if (typeof path === 'string') {
if (isFeatureEnabled(config, 'i18n')) {
path = path.replace(`.${locale}`, '');
}
return path.endsWith('.md')
? path.replace('.md', '.html') // 如果以 .md 结尾,替换为 .html
: path.endsWith('.html') // 如果已经是 .html 结尾,保持不变
? path
: `${path}.html`; // 其他情况,添加 .html
}
// 如果是数组,处理第一个元素
if (Array.isArray(path)) {
path = path[0];
if (isFeatureEnabled(config, 'i18n')) {
path = path.replace(`.${locale}`, '');
}
return appendHtml(path, config, locale);
}
// 如果是对象,处理第一个值
if (typeof path === 'object' && path !== null) {
const firstKey = Object.keys(path)[0];
path = path[firstKey];
if (isFeatureEnabled(config, 'i18n')) {
path = path.replace(`.${locale}`, '');
}
return appendHtml(path, config, locale);
}
return path;
}
// 计算相对于根目录的路径
function getRelativeBasePath(baseDir) {
if (!baseDir) {
return '.';
}
const depth = baseDir.split(path.sep).length;
return Array(depth).fill('..').join('/') || '.';
}
module.exports = {
getPagePath,
isFeatureEnabled,
appendHtml,
getRelativeBasePath
}