Skip to content

Commit 1d5d431

Browse files
authored
feat:resolve the missing parent title in the document title (#51)
* feat:resolve the missing parent title in the document title * feat:modify tinyvue to use document routing
1 parent 18068d5 commit 1d5d431

7 files changed

Lines changed: 96 additions & 38 deletions

File tree

.vitepress/config.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ export default defineConfig({
224224
{ text: '引入组件', link: 'import-components' },
225225
{ text: '国际化', link: 'i18n' },
226226
{ text: '主题配置', link: 'theme' },
227-
{ text: '暗黑模式', link: 'theme-dark' },
227+
{ text: '深色模式', link: 'theme-dark' },
228228
{ text: '智能化', link: 'mcp' },
229229
{ text: '表单校验配置', link: 'form-valid' },
230230
{ text: '常见问题', link: 'faq' }

.vitepress/theme/Layout.vue

Lines changed: 88 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -55,41 +55,96 @@ if (typeof window !== 'undefined') {
5555
// 获取 VitePress 默认布局组件
5656
const 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+
// 监听路由变更,初始化并同步标题
58144
watch(
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

.vitepress/theme/components/CustomHeader.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ const navigationTabs = computed(() => {
321321
activeProductTab.value === "tiny-vue" &&
322322
route.path.includes("/tiny-vue/")
323323
) {
324-
return [{ key: "guide", name: "使用文档", link: "/tiny-vue/guide/" }];
324+
return [{ key: "guide", name: "使用文档", link: "/tiny-vue/guide/introduce" }];
325325
} else {
326326
let configNav: configNavItem[] = [];
327327
if (

.vitepress/theme/style.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,9 @@ info{
537537
.vp-doc .custom-block.tip::before {
538538
content: url("../../public/images/icon-success.svg");
539539
}
540+
.vp-doc :not(pre) > code{
541+
padding:0 6px;
542+
}
540543

541544
/* 暗色模式下全局样式 */
542545
.dark h1{

next-sdk

tiny-engine

Submodule tiny-engine updated 201 files

tiny-vue

Submodule tiny-vue updated 582 files

0 commit comments

Comments
 (0)