-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path_utils.ts
More file actions
120 lines (107 loc) · 3.02 KB
/
_utils.ts
File metadata and controls
120 lines (107 loc) · 3.02 KB
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
import type {
NormalizedSidebarGroup,
SidebarDivider,
SidebarItem,
SidebarSectionHeader,
} from '@rspress/shared'
function removeIndex(link: string) {
if (link.endsWith('/index')) {
return link.slice(0, -5)
}
return link
}
export const isSidebarDivider = (
item:
| NormalizedSidebarGroup
| SidebarItem
| SidebarDivider
| SidebarSectionHeader,
): item is SidebarDivider => {
return 'dividerType' in item
}
export const isSidebarSectionHeader = (
item:
| NormalizedSidebarGroup
| SidebarItem
| SidebarDivider
| SidebarSectionHeader,
): item is SidebarSectionHeader => {
return 'sectionHeaderText' in item
}
export const isSingleFile = (
item:
| SidebarItem
| SidebarSectionHeader
| SidebarDivider
| NormalizedSidebarGroup,
): item is SidebarItem | (NormalizedSidebarGroup & { link: string }) =>
!('items' in item) && 'link' in item
/**
* @zh_CN 如果 index 在 sidebar items 中, 则返回所有平级 item, 如果 index 在 dir 上, 则返回 items
* @example
*/
export function findItemByRoutePath(
items: (
| SidebarItem
| SidebarSectionHeader
| NormalizedSidebarGroup
| SidebarDivider
)[],
routePath: string,
): (SidebarItem | NormalizedSidebarGroup)[] {
function isRoutePathMatch(
item:
| SidebarItem
| SidebarSectionHeader
| NormalizedSidebarGroup
| SidebarDivider,
) {
if (isSidebarDivider(item) || isSidebarSectionHeader(item)) {
return false
}
const removeIndexUrl = removeIndex(item.link || '/')
const removeBackSlashedRoutePath = routePath.replace(/\/$/, '')
return (
removeIndexUrl === routePath ||
removeIndexUrl === removeBackSlashedRoutePath
)
}
const matchRoutePathItemIndex = items.findIndex((item) => {
return isRoutePathMatch(item)
})
if (matchRoutePathItemIndex === -1) {
return items
.map((item) => {
if (!('items' in item)) {
return []
}
return findItemByRoutePath(item.items, routePath)
})
.flat()
}
const matchRoutePathItem = items[matchRoutePathItemIndex] as
| SidebarItem
| NormalizedSidebarGroup
const isArray = (i: unknown): i is Array<unknown> =>
Array.isArray(i) && i.length >= 1
// 1. if isDir(item) return item.items
if ('items' in matchRoutePathItem && isArray(matchRoutePathItem.items)) {
// 2. if isDir(item) && item.items is all files, style is different
if (matchRoutePathItem.items.every((item) => !('items' in item))) {
return [matchRoutePathItem]
}
return matchRoutePathItem.items.filter(
(item) => !isSidebarDivider(item),
) as (SidebarItem | NormalizedSidebarGroup)[]
}
// 3. if matchRoutePathItem is a item, return other items in same level (/api/index.md is in the child sidebar)
const result = [...items]
if (!('items' in matchRoutePathItem)) {
result.splice(matchRoutePathItemIndex, 1)
}
const res = result.filter((item) => !isSidebarDivider(item)) as (
| SidebarItem
| NormalizedSidebarGroup
)[]
return res
}