forked from nuxt-themes/docus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseDocus.ts
88 lines (75 loc) · 2.57 KB
/
useDocus.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
export const useDocus = () => {
const docus = computed(() => useAppConfig()?.docus || {})
const { navPageFromPath, navDirFromPath, navKeyFromPath } = useContentHelpers()
const { navigation, page } = useContent()
const route = useRoute()
/**
* Returns fallbacked values for `main`, `header`, `aside` and `footer`.
*
* It allows to configure `app.config` per page/_dir.
*/
const config = computed(
() => {
const titleTemplate = docus?.value?.titleTemplate || navKeyFromPath(page?.value?._path, 'titleTemplate', navigation.value || []) || `%s · ${docus?.value?.title || 'Docus'}`
const main = docus?.value?.main || {}
const header = docus?.value?.header || {}
const aside = docus?.value?.aside || {}
const footer = docus?.value?.footer || {}
return {
// Raw appConfig
...docus.value,
// Merged attributes
titleTemplate,
main: {
...main,
...navKeyFromPath(route.path, 'main', navigation.value || []),
...page.value?.main
} as typeof main,
header: {
...header,
...navKeyFromPath(route.path, 'header', navigation.value || []),
...page.value?.header
} as typeof header,
aside: {
...aside,
...navKeyFromPath(route.path, 'aside', navigation.value || []),
...page.value?.aside
} as typeof aside,
footer: {
...footer,
...navKeyFromPath(route.path, 'footer', navigation.value || []),
...page.value?.footer
} as typeof footer
}
}
)
/**
* Current aside tree.
*/
const tree = computed(() => {
let nav = navigation.value || []
const _path = route.path
const level = config?.value?.aside?.level || 0
const filtered = config?.value?.aside?.exclude || []
if (level) {
// Filter if `aside.level` is enabled in docus configuration
const path = _path.split('/')
// Get level
const leveledPath = path.splice(0, 1 + level).join('/')
nav = navDirFromPath(leveledPath, nav) || []
if (!Array.isArray(nav)) { nav = [nav] }
}
// No navigation found; try to resolve page url
if (nav.length === 0) {
nav = navPageFromPath(page.value?._path || '/', navigation.value || [])
if (!nav) { return [] }
if (!Array.isArray(nav)) { nav = [nav] }
}
// Filtered using `aside.exclude` in docus configuration
return nav.filter((item: any) => {
if (filtered.includes(item._path)) { return false }
return true
})
})
return { tree, config }
}