mirror of https://github.com/vuejs/vitepress
parent
af4b015864
commit
50798bdb76
@ -1,10 +1,75 @@
|
||||
import { computed } from 'vue'
|
||||
import { resolveSiteDataByRoute } from '/@shared/config'
|
||||
import { siteDataRef } from './siteData'
|
||||
import { SiteData } from '../../../../types/shared'
|
||||
import { useRoute } from '../router'
|
||||
import { siteDataRef } from './siteData'
|
||||
|
||||
const inBrowser = typeof window !== 'undefined'
|
||||
|
||||
export function useSiteDataByRoute(route = useRoute()) {
|
||||
return computed(() => {
|
||||
return resolveSiteDataByRoute(siteDataRef.value, route.path)
|
||||
})
|
||||
}
|
||||
|
||||
function findMatchRoot(route: string, roots: string[]) {
|
||||
// first match to the routes with the most deep level.
|
||||
roots.sort((a, b) => {
|
||||
const levelDelta = b.split('/').length - a.split('/').length
|
||||
if (levelDelta !== 0) {
|
||||
return levelDelta
|
||||
} else {
|
||||
return b.length - a.length
|
||||
}
|
||||
})
|
||||
|
||||
for (const r of roots) {
|
||||
if (route.startsWith(r)) return r
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
function resolveLocales<T>(
|
||||
locales: Record<string, T>,
|
||||
route: string
|
||||
): T | undefined {
|
||||
const localeRoot = findMatchRoot(route, Object.keys(locales))
|
||||
return localeRoot ? locales[localeRoot] : undefined
|
||||
}
|
||||
|
||||
// this merges the locales data to the main data by the route
|
||||
export function resolveSiteDataByRoute(siteData: SiteData, route: string) {
|
||||
route = cleanRoute(siteData, route)
|
||||
|
||||
const localeData = resolveLocales(siteData.locales || {}, route) || {}
|
||||
const localeThemeConfig =
|
||||
resolveLocales<any>(
|
||||
(siteData.themeConfig && siteData.themeConfig.locales) || {},
|
||||
route
|
||||
) || {}
|
||||
|
||||
return {
|
||||
...siteData,
|
||||
...localeData,
|
||||
themeConfig: {
|
||||
...siteData.themeConfig,
|
||||
...localeThemeConfig,
|
||||
// clean the locales to reduce the bundle size
|
||||
locales: {}
|
||||
},
|
||||
locales: {}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up the route by removing the `base` path if it's set in config.
|
||||
*/
|
||||
function cleanRoute(siteData: SiteData, route: string): string {
|
||||
if (!inBrowser) {
|
||||
return route
|
||||
}
|
||||
|
||||
const base = siteData.base
|
||||
const baseWithoutSuffix = base.endsWith('/') ? base.slice(0, -1) : base
|
||||
|
||||
return route.slice(baseWithoutSuffix.length)
|
||||
}
|
||||
|
@ -1,10 +1,14 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "../../dist",
|
||||
"baseUrl": ".",
|
||||
"outDir": "../../dist/node",
|
||||
"module": "commonjs",
|
||||
"lib": ["ESNext", "DOM"],
|
||||
"sourceMap": true
|
||||
},
|
||||
"include": [".", "../shared", "../../types/shared.d.ts"]
|
||||
"include": [
|
||||
".",
|
||||
"../../types/shared.d.ts"
|
||||
]
|
||||
}
|
||||
|
@ -1,66 +0,0 @@
|
||||
import { SiteData } from '../../types/shared'
|
||||
|
||||
const inBrowser = typeof window !== 'undefined'
|
||||
|
||||
function findMatchRoot(route: string, roots: string[]) {
|
||||
// first match to the routes with the most deep level.
|
||||
roots.sort((a, b) => {
|
||||
const levelDelta = b.split('/').length - a.split('/').length
|
||||
if (levelDelta !== 0) {
|
||||
return levelDelta
|
||||
} else {
|
||||
return b.length - a.length
|
||||
}
|
||||
})
|
||||
|
||||
for (const r of roots) {
|
||||
if (route.startsWith(r)) return r
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
function resolveLocales<T>(
|
||||
locales: Record<string, T>,
|
||||
route: string
|
||||
): T | undefined {
|
||||
const localeRoot = findMatchRoot(route, Object.keys(locales))
|
||||
return localeRoot ? locales[localeRoot] : undefined
|
||||
}
|
||||
|
||||
// this merges the locales data to the main data by the route
|
||||
export function resolveSiteDataByRoute(siteData: SiteData, route: string) {
|
||||
route = cleanRoute(siteData, route)
|
||||
|
||||
const localeData = resolveLocales(siteData.locales || {}, route) || {}
|
||||
const localeThemeConfig =
|
||||
resolveLocales<any>(
|
||||
(siteData.themeConfig && siteData.themeConfig.locales) || {},
|
||||
route
|
||||
) || {}
|
||||
|
||||
return {
|
||||
...siteData,
|
||||
...localeData,
|
||||
themeConfig: {
|
||||
...siteData.themeConfig,
|
||||
...localeThemeConfig,
|
||||
// clean the locales to reduce the bundle size
|
||||
locales: {}
|
||||
},
|
||||
locales: {}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up the route by removing the `base` path if it's set in config.
|
||||
*/
|
||||
function cleanRoute(siteData: SiteData, route: string): string {
|
||||
if (!inBrowser) {
|
||||
return route
|
||||
}
|
||||
|
||||
const base = siteData.base
|
||||
const baseWithoutSuffix = base.endsWith('/') ? base.slice(0, -1) : base
|
||||
|
||||
return route.slice(baseWithoutSuffix.length)
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"baseUrl": ".",
|
||||
"outDir": "../../dist/client/shared",
|
||||
"module": "esnext",
|
||||
"lib": ["ESNext", "DOM"],
|
||||
},
|
||||
"include": [
|
||||
".",
|
||||
"../../types/shared.d.ts",
|
||||
]
|
||||
}
|
Loading…
Reference in new issue