@@ -55,41 +55,96 @@ if (typeof window !== 'undefined') {
5555// 获取 VitePress 默认布局组件
5656const DefaultLayout = DefaultTheme .Layout
5757
58+ // 将文档标题更新逻辑提取为独立函数,便于维护和测试
59+ const updateDocTitle = () => {
60+ const base = site .value ?.base || ' /'
61+ const path = route .path .replace (new RegExp (` ^${base } ` ), ' /' )
62+ const cfg = themeConfig .value || {}
63+
64+ // next-sdk / tiny-vue: 从对应 sidebar 的 guide 中寻找匹配项
65+ if (path .includes (' /next-sdk/' ) || path .includes (' /tiny-vue/' )) {
66+ let sidebarConfig: any [] = []
67+ if (path .includes (' /next-sdk/' )) {
68+ sidebarConfig = cfg .sidebar ?.[' /next-sdk/guide/' ] || []
69+ } else if (path .includes (' /tiny-vue/' )) {
70+ sidebarConfig = cfg .sidebar ?.[' /tiny-vue/guide/' ] || []
71+ }
72+
73+ if (! sidebarConfig || ! sidebarConfig .length ) {
74+ docTitle .value = ' 指南'
75+ return
76+ }
77+
78+ const pathIndex = sidebarConfig .findIndex ((child : any ) =>
79+ child .items ?.some ((item : any ) => item ?.link && path .includes (item .link ))
80+ )
81+
82+ docTitle .value = sidebarConfig [pathIndex >= 0 ? pathIndex : 0 ]?.text || ' 指南'
83+ return
84+ }
85+
86+ // tiny-robot: 从 nav 中匹配 activeMatch
87+ if (path .includes (' /tiny-robot/' )) {
88+ const navConfig = cfg .nav || []
89+ if (! navConfig .length ) {
90+ docTitle .value = ' '
91+ return
92+ }
93+ const match = navConfig .find ((item : any ) => item ?.activeMatch && path .includes (item .activeMatch ))
94+ docTitle .value = match ?.text || ' '
95+ return
96+ }
97+
98+ // tiny-engine: 需要先找到对应的 engineNav,然后查 sidebar 的二级或三级项
99+ if (path .includes (' /tiny-engine/' )) {
100+ const engineNavConfig = cfg .engineNav || []
101+ if (! engineNavConfig .length ) {
102+ docTitle .value = ' '
103+ return
104+ }
105+
106+ const activeNav = engineNavConfig .find ((item : any ) => item ?.activeMatch && path .includes (item .activeMatch ))
107+ const engineSidebarConfig = cfg .sidebar ?.[` /tiny-engine${activeNav ?.activeMatch } ` ] || []
108+ if (! engineSidebarConfig .length ) {
109+ docTitle .value = ' '
110+ return
111+ }
112+
113+ let enginePathkey = 0
114+ let engineDeepPathkey: number | null = null
115+
116+ engineSidebarConfig .forEach ((child : any , key : number ) => {
117+ child .items ?.forEach ((item : any , deepKey : number ) => {
118+ if (item ?.items ?.length ) {
119+ const foundDeep = item .items .find ((deepItem : any ) => deepItem ?.link && path .includes (deepItem .link ))
120+ if (foundDeep ) {
121+ enginePathkey = key
122+ engineDeepPathkey = deepKey
123+ }
124+ } else if (item ?.link && path .includes (item .link )) {
125+ enginePathkey = key
126+ }
127+ })
128+ })
129+
130+ if (engineDeepPathkey !== null ) {
131+ docTitle .value = engineSidebarConfig [enginePathkey ].items ?.[engineDeepPathkey ]?.text || ' '
132+ } else {
133+ docTitle .value = engineSidebarConfig [enginePathkey ]?.text || ' '
134+ }
135+
136+ return
137+ }
138+
139+ // 默认情况:清空标题
140+ docTitle .value = ' '
141+ }
142+
143+ // 监听路由变更,初始化并同步标题
58144watch (
59145 () => route .path ,
60- () => {
61- const path = route .path .replace (new RegExp (` ^${site .value .base } ` ), ' /' )
62- if (path .includes (' /next-sdk/' )){
63- let pathkey = 0 ;
64- const sidebarConfig = themeConfig .value .sidebar [' /next-sdk/guide/' ];
65- if (! sidebarConfig ) {
66- docTitle .value = ' 指南'
67- return ;
68- };
69- sidebarConfig .forEach ((child ,key ) => {
70- child .items .forEach (item => {
71- if (path .includes (item .link )){
72- pathkey = key
73- }
74- });
75- });
76- docTitle .value = sidebarConfig [pathkey ]?.text || ' 指南'
77- }else if (path .includes (' /tiny-robot/' )){
78- const navConfig = themeConfig .value .nav ;
79- if (! navConfig ) {
80- docTitle .value = ' '
81- return ;
82- };
83- navConfig .forEach (item => {
84- if (path .includes (item .activeMatch )){
85- docTitle .value = item .text
86- }
87- });
88- }else {
89- docTitle .value = ' '
90- }
91- },
92- { deep: true ,immediate:true },
146+ updateDocTitle ,
147+ { deep: true , immediate: true }
93148)
94149 </script >
95150
0 commit comments