Skip to content

Commit 8f9412c

Browse files
committed
enhance path redirection logic and improve table sorting initialization
1 parent da96b78 commit 8f9412c

1 file changed

Lines changed: 42 additions & 5 deletions

File tree

‎docs/.vitepress/theme/index.ts‎

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import DefaultTheme from 'vitepress/theme'
22
import type { Theme } from 'vitepress'
33
import { h, onMounted, watch, nextTick } from 'vue'
4-
import { useRoute } from 'vitepress'
4+
import { useRoute, useRouter } from 'vitepress'
55
import GitHubStars from './components/GitHubStars.vue'
66
import TypePHPSnippet from './components/TypePHPSnippet.vue'
77
import './custom.css'
@@ -43,6 +43,37 @@ function enableTableSorting() {
4343
})
4444
}
4545

46+
function checkAndRedirect(path: string): boolean {
47+
if (typeof window === 'undefined') return false
48+
49+
if (path.startsWith('/docs/blog/')) {
50+
const target = path.replace('/docs/blog/', '/blog/')
51+
window.location.replace(target + window.location.search + window.location.hash)
52+
return true
53+
}
54+
55+
const docsSections = [
56+
'/getting-started',
57+
'/core-concepts',
58+
'/supported-types',
59+
'/generics',
60+
'/testing',
61+
'/advanced',
62+
'/production',
63+
'/troubleshooting',
64+
]
65+
66+
for (const section of docsSections) {
67+
if (path.startsWith(section) && !path.startsWith('/docs/')) {
68+
const target = '/docs' + path + window.location.search + window.location.hash
69+
window.location.replace(target)
70+
return true
71+
}
72+
}
73+
74+
return false
75+
}
76+
4677
export default {
4778
extends: DefaultTheme,
4879
Layout() {
@@ -57,6 +88,10 @@ export default {
5788
const route = useRoute()
5889

5990
onMounted(() => {
91+
if (checkAndRedirect(window.location.pathname)) {
92+
return
93+
}
94+
6095
enableTableSorting()
6196

6297
if (typeof window !== 'undefined' && 'serviceWorker' in navigator) {
@@ -70,10 +105,12 @@ export default {
70105

71106
watch(
72107
() => route.path,
73-
() => {
74-
nextTick(() => {
75-
enableTableSorting()
76-
})
108+
(newPath) => {
109+
if (!checkAndRedirect(newPath)) {
110+
nextTick(() => {
111+
enableTableSorting()
112+
})
113+
}
77114
}
78115
)
79116
}

0 commit comments

Comments
 (0)