@@ -29,14 +29,12 @@ export default function VitePluginVitePressPermalink(option: PermalinkOption = {
29
29
const pathToPermalink : Record < string , string > = { } ;
30
30
// Key 为 permalink,Value 为 path
31
31
const permalinkToPath : Record < string , string > = { } ;
32
- // 多语言 key 数组
32
+ // 多语言 key 数组,排除 root 根目录
33
33
const localesKeys = Object . keys ( locales || { } ) ;
34
34
35
35
for ( const [ key , value ] of Object . entries ( permalinks ) ) {
36
- let newValue = value ;
37
36
// 如果设置了多语言,则 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 ) ;
40
38
41
39
pathToPermalink [ key ] = newValue ;
42
40
@@ -49,15 +47,13 @@ export default function VitePluginVitePressPermalink(option: PermalinkOption = {
49
47
50
48
themeConfig . permalinks = { map : pathToPermalink , inv : permalinkToPath } ;
51
49
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
-
60
50
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
+ } ) ;
61
57
} ,
62
58
configureServer ( server : ViteDevServer ) {
63
59
const {
@@ -86,3 +82,30 @@ export default function VitePluginVitePressPermalink(option: PermalinkOption = {
86
82
} ,
87
83
} ;
88
84
}
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
+ } ;
0 commit comments