-
Notifications
You must be signed in to change notification settings - Fork 130
/
utils.ts
144 lines (114 loc) Β· 4.04 KB
/
utils.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import type { Route } from 'vitepress'
import type { DefaultTheme } from './config'
export const hashRE = /#.*$/
export const extRE = /(index)?\.(md|html)$/
export const endingSlashRE = /\/$/
export const outboundRE = /^[a-z]+:/i
export function isNullish(value: any): value is null | undefined {
return value === null || value === undefined
}
export function isArray(value: any): value is any[] {
return Array.isArray(value)
}
export function isExternal(path: string): boolean {
return outboundRE.test(path)
}
export function isActive(route: Route, path?: string): boolean {
if (path === undefined)
return false
const routePath = normalize(`/${route.data.relativePath}`)
const pagePath = normalize(path)
return routePath === pagePath
}
export function normalize(path: string): string {
return decodeURI(path).replace(hashRE, '').replace(extRE, '')
}
export function joinUrl(base: string, path: string): string {
const baseEndsWithSlash = base.endsWith('/')
const pathStartsWithSlash = path.startsWith('/')
if (baseEndsWithSlash && pathStartsWithSlash)
return base.slice(0, -1) + path
if (!baseEndsWithSlash && !pathStartsWithSlash)
return `${base}/${path}`
return base + path
}
/**
* get the path without filename (the last segment). for example, if the given
* path is `/guide/getting-started.html`, this method will return `/guide/`.
* Always with a trailing slash.
*/
export function getPathDirName(path: string): string {
const segments = path.split('/')
if (segments[segments.length - 1])
segments.pop()
return ensureEndingSlash(segments.join('/'))
}
export function ensureSlash(path: string): string {
return ensureEndingSlash(ensureStartingSlash(path))
}
export function ensureStartingSlash(path: string): string {
return /^\//.test(path) ? path : `/${path}`
}
export function ensureEndingSlash(path: string): string {
return /(\.html|\/)$/.test(path) ? path : `${path}/`
}
/**
* Remove `.md` or `.html` extension from the given path. It also converts
* `index` to slush.
*/
export function removeExtension(path: string): string {
return path.replace(/(index)?(\.(md|html))?$/, '') || '/'
}
/**
* Sidebar
*/
export function isSideBarConfig(
sidebar: DefaultTheme.SideBarConfig | DefaultTheme.MultiSideBarConfig,
): sidebar is DefaultTheme.SideBarConfig {
return sidebar === false || sidebar === 'auto' || isArray(sidebar)
}
export function isSideBarGroup(
item: DefaultTheme.SideBarItem,
): item is DefaultTheme.SideBarGroup {
return (item as DefaultTheme.SideBarGroup).children !== undefined
}
export function isSideBarEmpty(sidebar?: DefaultTheme.SideBarConfig): boolean {
return isArray(sidebar) ? sidebar.length === 0 : !sidebar
}
/**
* Get the `SideBarConfig` from sidebar option. This method will ensure to get
* correct sidebar config from `MultiSideBarConfig` with various path
* combinations such as matching `guide/` and `/guide/`. If no matching config
* was found, it will return `auto` as a fallback.
*/
export function getSideBarConfig(
sidebar: DefaultTheme.SideBarConfig | DefaultTheme.MultiSideBarConfig,
path: string,
): DefaultTheme.SideBarConfig {
if (isSideBarConfig(sidebar))
return sidebar
path = ensureStartingSlash(path)
for (const dir of Object.keys(sidebar)) {
// make sure the multi sidebar key starts with slash too
if (path.startsWith(ensureStartingSlash(dir)))
return sidebar[dir]
}
return 'auto'
}
/**
* Get flat sidebar links from the sidebar items. This method is useful for
* creating the "next and prev link" feature. It will ignore any items that
* don't have `link` property and removes `.md` or `.html` extension if a
* link contains it.
*/
export function getFlatSideBarLinks(
sidebar: DefaultTheme.SideBarItem[],
): DefaultTheme.SideBarLink[] {
return sidebar.reduce<DefaultTheme.SideBarLink[]>((links, item) => {
if (item.link)
links.push({ text: item.text, link: removeExtension(item.link) })
if (isSideBarGroup(item))
links = [...links, ...getFlatSideBarLinks(item.children || [])]
return links
}, [])
}