Skip to content

Commit db1c343

Browse files
userquinbrc-dd
andauthored
feat(theme): sort multiple sidebars (#1552)
Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
1 parent 701b876 commit db1c343

File tree

6 files changed

+180
-8
lines changed

6 files changed

+180
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import { describe, test, expect } from 'vitest'
2+
import { getSidebar } from 'client/theme-default/support/sidebar'
3+
4+
describe('client/theme-default/support/sidebar', () => {
5+
const root = [
6+
{
7+
text: 'A',
8+
collapsible: true,
9+
items: [
10+
{
11+
text: 'A',
12+
link: ''
13+
}
14+
]
15+
},
16+
{
17+
text: 'B',
18+
items: [
19+
{
20+
text: 'B',
21+
link: ''
22+
}
23+
]
24+
}
25+
]
26+
const another = [
27+
{
28+
text: 'C',
29+
items: [
30+
{
31+
text: 'C',
32+
link: ''
33+
}
34+
]
35+
}
36+
]
37+
describe('normal sidebar sort', () => {
38+
const normalSidebar = {
39+
'/': root,
40+
'/multi-sidebar/': another
41+
}
42+
test('gets / sidebar', () => {
43+
expect(getSidebar(normalSidebar, '/')).toBe(root)
44+
})
45+
test('gets /multi-sidebar/ sidebar', () => {
46+
expect(getSidebar(normalSidebar, '/multi-sidebar/')).toBe(another)
47+
})
48+
test('gets / sidebar again', () => {
49+
expect(getSidebar(normalSidebar, '/some-entry.html')).toBe(root)
50+
})
51+
})
52+
describe('reversed sidebar sort', () => {
53+
const reversedSidebar = {
54+
'/multi-sidebar/': another,
55+
'/': root
56+
}
57+
test('gets / sidebar', () => {
58+
expect(getSidebar(reversedSidebar, '/')).toBe(root)
59+
})
60+
test('gets /multi-sidebar/ sidebar', () => {
61+
expect(getSidebar(reversedSidebar, '/multi-sidebar/')).toBe(another)
62+
})
63+
test('gets / sidebar again', () => {
64+
expect(getSidebar(reversedSidebar, '/some-entry.html')).toBe(root)
65+
})
66+
})
67+
describe('nested sidebar sort', () => {
68+
const nested = [
69+
{
70+
text: 'D',
71+
items: [
72+
{
73+
text: 'D',
74+
link: ''
75+
}
76+
]
77+
}
78+
]
79+
const nestedSidebar = {
80+
'/': root,
81+
'/multi-sidebar/': another,
82+
'/multi-sidebar/nested/': nested
83+
}
84+
test('gets / sidebar', () => {
85+
expect(getSidebar(nestedSidebar, '/')).toBe(root)
86+
})
87+
test('gets /multi-sidebar/ sidebar', () => {
88+
expect(getSidebar(nestedSidebar, '/multi-sidebar/')).toBe(another)
89+
})
90+
test('gets /multi-sidebar/nested/ sidebar', () => {
91+
expect(getSidebar(nestedSidebar, '/multi-sidebar/nested/')).toBe(nested)
92+
})
93+
test('gets / sidebar again', () => {
94+
expect(getSidebar(nestedSidebar, '/some-entry.html')).toBe(root)
95+
})
96+
})
97+
})

examples/configured/.vitepress/config.js

+24
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,30 @@ export default defineConfig({
2424
link: '/static-data/data'
2525
}
2626
]
27+
},
28+
{
29+
text: 'Multi Sidebar Test',
30+
items: [
31+
{
32+
text: 'Test Page',
33+
link: '/multi-sidebar/'
34+
}
35+
]
36+
}
37+
],
38+
'/multi-sidebar/': [
39+
{
40+
text: 'Multi Sidebar',
41+
items: [
42+
{
43+
text: 'Test Page',
44+
link: '/multi-sidebar/'
45+
},
46+
{
47+
text: 'Back',
48+
link: '/'
49+
}
50+
]
2751
}
2852
]
2953
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { expect, test } from 'vitest'
2+
import { page, vitePressTestUrl, waitForLayout } from '~utils'
3+
4+
describe('test multi sidebar sort root', () => {
5+
beforeAll(async () => {
6+
await page.goto(
7+
vitePressTestUrl + '/frontmatter/multiple-levels-outline.html'
8+
)
9+
await waitForLayout()
10+
})
11+
12+
test('using / sidebar', async () => {
13+
const sidebarLocator = await page.locator(
14+
'.VPSidebarGroup .title .title-text'
15+
)
16+
17+
const sidebarContent = await sidebarLocator.allTextContents()
18+
expect(sidebarContent).toEqual([
19+
'Frontmatter',
20+
'Static Data',
21+
'Multi Sidebar Test'
22+
])
23+
})
24+
})
25+
26+
describe('test multi sidebar sort other', () => {
27+
beforeAll(async () => {
28+
await page.goto(vitePressTestUrl + '/multi-sidebar/index.html')
29+
await waitForLayout()
30+
})
31+
32+
test('using /multi-sidebar/ sidebar', async () => {
33+
const sidebarLocator = await page.locator(
34+
'.VPSidebarGroup .title .title-text'
35+
)
36+
37+
const sidebarContent = await sidebarLocator.allTextContents()
38+
expect(sidebarContent).toEqual(['Multi Sidebar'])
39+
})
40+
})

examples/configured/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Full Configured VitePress Example
22

3-
WIP
3+
WIP
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: Multi Sidebar Test
3+
---
4+
5+
# Multi Sidebar Test

src/client/theme-default/support/sidebar.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,22 @@ export function getSidebar(
1515
return sidebar
1616
}
1717

18+
if (sidebar == null) {
19+
return []
20+
}
21+
1822
path = ensureStartingSlash(path)
1923

20-
for (const dir in sidebar) {
21-
// make sure the multi sidebar key starts with slash too
22-
if (path.startsWith(ensureStartingSlash(dir))) {
23-
return sidebar[dir]
24-
}
25-
}
24+
const dir = Object.keys(sidebar)
25+
.sort((a, b) => {
26+
return b.split('/').length - a.split('/').length
27+
})
28+
.find((dir) => {
29+
// make sure the multi sidebar key starts with slash too
30+
return path.startsWith(ensureStartingSlash(dir))
31+
})
2632

27-
return []
33+
return dir ? sidebar[dir] : []
2834
}
2935

3036
export function getFlatSideBarLinks(sidebar: DefaultTheme.SidebarGroup[]) {

0 commit comments

Comments
 (0)