Skip to content

Commit e4bbf55

Browse files
committed
feat: 🚀 支持 permalink 高亮 nav
1 parent 06e2d39 commit e4bbf55

File tree

7 files changed

+65
-15
lines changed

7 files changed

+65
-15
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
## TODO
66

77
- 文章页隐藏分类和标签(位置不够),支持配置项开启
8-
- 归档、目录进入后,导航栏对应的 label 没有高亮,需要转为高亮的 link
98
- F12 打开慢的问题,可能监听器触发了多次,需要优化下
109
- 去掉 dev 启动时终端打印的 scss 语法废弃提示
10+
- cleanUrl 改为 false 来校验下功能
1111
- 首页 top 0 位置时监听 F12,开启壁纸功能
1212
- 归档页添加 commit 图标风格,如:`http://niubin.site/archive.html`

docs/.vitepress/locales/zh.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default defineConfig({
1313
// 开启行号
1414
lineNumbers: true,
1515
image: {
16-
// 默认禁用;设置为 true 可为所有图片启用懒加载
16+
// 默认禁用;设置为 true 可为所有图片启用懒加载
1717
lazyLoading: true,
1818
},
1919
// 更改容器默认值标题

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

+6
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ const isMdFile = (filePath: string) => {
8888
return ["md", "MD"].includes(fileExtension);
8989
};
9090

91+
/**
92+
* 判断数组中是否存在某个元素,支持正则表达式
93+
*
94+
* @param arr 数组
95+
* @param name 元素
96+
*/
9197
const isSome = (arr: Array<string | RegExp>, name: string) => {
9298
return arr.some(item => name.includes(item as string) || (item instanceof RegExp && item.test(name)));
9399
};

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

+6
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ const isMdFile = (filePath: string) => {
8585
return ["md", "MD"].includes(fileExtension);
8686
};
8787

88+
/**
89+
* 判断数组中是否存在某个元素,支持正则表达式
90+
*
91+
* @param arr 数组
92+
* @param name 元素
93+
*/
8894
const isSome = (arr: Array<string | RegExp>, name: string) => {
8995
return arr.some(item => item === name || (item instanceof RegExp && item.test(name)));
9096
};

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,20 @@ const isMdFile = (filePath: string) => {
104104
return ["md", "MD"].includes(fileExtension);
105105
};
106106

107+
/**
108+
* 判断数组中是否存在某个元素,支持正则表达式
109+
*
110+
* @param arr 数组
111+
* @param name 元素
112+
*/
107113
const isSome = (arr: Array<string | RegExp>, name: string) => {
108114
return arr.some(item => item === name || (item instanceof RegExp && item.test(name)));
109115
};
110116

111-
export const standardLink = (permalink: string) => {
117+
/**
118+
* 处理 permalink 的格式为 /xxx 或者 /xx/xx,即开头带有 /,结尾不带 /
119+
*/
120+
export const standardLink = (permalink = "") => {
112121
let finalPermalink = permalink;
113122
if (!finalPermalink.startsWith("/")) finalPermalink = "/" + finalPermalink;
114123
if (finalPermalink.endsWith("/")) finalPermalink = finalPermalink.replace(/\/$/, "");

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

+35-12
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,12 @@ export default function VitePluginVitePressPermalink(option: PermalinkOption = {
2929
const pathToPermalink: Record<string, string> = {};
3030
// Key 为 permalink,Value 为 path
3131
const permalinkToPath: Record<string, string> = {};
32-
// 多语言 key 数组
32+
// 多语言 key 数组,排除 root 根目录
3333
const localesKeys = Object.keys(locales || {});
3434

3535
for (const [key, value] of Object.entries(permalinks)) {
36-
let newValue = value;
3736
// 如果设置了多语言,则 permalink 添加语言前缀
38-
const localesKey = localesKeys.find(k => key.startsWith(k));
39-
if (localesKey) newValue = `/${localesKey}${value.startsWith("/") ? value : `/${value}`}`;
37+
let newValue = getLocalePermalink(localesKeys, key, value);
4038

4139
pathToPermalink[key] = newValue;
4240

@@ -49,15 +47,13 @@ export default function VitePluginVitePressPermalink(option: PermalinkOption = {
4947

5048
themeConfig.permalinks = { map: pathToPermalink, inv: permalinkToPath };
5149

52-
// TODO 归档、目录进入后,导航栏对应的 label 没有高亮,需要转为高亮的 link
53-
// themeConfig.nav = themeConfig.nav?.map((n: any) => {
54-
// const link = standardLink(n.link);
55-
// const permalink = permalinkToPath[link];
56-
// if (permalink) n.link = permalink;
57-
// return n;
58-
// });
59-
6050
vitepressConfig = config.vitepress;
51+
52+
if (!localesKeys.length) return setDefaultActiveMatch(themeConfig.nav, permalinkToPath);
53+
54+
localesKeys.forEach(localeKey => {
55+
setDefaultActiveMatch(locales[localeKey].themeConfig?.nav, permalinkToPath);
56+
});
6157
},
6258
configureServer(server: ViteDevServer) {
6359
const {
@@ -86,3 +82,30 @@ export default function VitePluginVitePressPermalink(option: PermalinkOption = {
8682
},
8783
};
8884
}
85+
86+
/**
87+
* 给 permalink 添加多语言前缀
88+
* @param localesKeys 多语言 key 数组,排除 root 根目录
89+
* @param path 文件路径
90+
* @param permalink 永久链接
91+
*/
92+
const getLocalePermalink = (localesKeys: string[] = [], path = "", permalink = "") => {
93+
// 过滤掉 root 根目录
94+
const localesKey = localesKeys.filter(key => key !== "root").find(key => path.startsWith(key));
95+
if (localesKey) return `/${localesKey}${permalink.startsWith("/") ? permalink : `/${permalink}`}`;
96+
97+
return permalink;
98+
};
99+
100+
const setDefaultActiveMatch = (nav: any[] = [], permalinkToPath: Record<string, string>) => {
101+
if (!nav.length) return;
102+
103+
nav.forEach(item => {
104+
const link = standardLink(item.link);
105+
const path = permalinkToPath[link];
106+
107+
// 官方归档 activeMatch 是一个正则表达式字符串
108+
if (path && !item.activeMatch) item.activeMatch = path;
109+
if (item.items) setDefaultActiveMatch(item.items, permalinkToPath);
110+
});
111+
};

plugins/vitepress-plugin-sidebar-resolve/src/helper.ts

+6
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@ const resolveFileName = (
242242
return { index, title, type, name };
243243
};
244244

245+
/**
246+
* 判断数组中是否存在某个元素,支持正则表达式
247+
*
248+
* @param arr 数组
249+
* @param name 元素
250+
*/
245251
const isSome = (arr: Array<string | RegExp>, name: string) => {
246252
return arr.some(item => item === name || (item instanceof RegExp && item.test(name)));
247253
};

0 commit comments

Comments
 (0)