feat(next): sidebar is sorted by title (#232)

pull/286/head
Rizumu Ayaka 2023-12-19 22:51:00 +08:00
parent dbf74b6780
commit 2547c0ba82
3 changed files with 54 additions and 29 deletions

View File

@ -1,28 +1,53 @@
import { type DefaultTheme } from 'vitepress'
import { generateSidebar } from 'vitepress-sidebar'
import { SidebarItem, generateSidebar } from 'vitepress-sidebar'
export const sidebar = generateSidebar([
// 大学指南
{
documentRootPath: '/docs',
scanStartPath: 'campus',
resolvePath: '/campus/',
useTitleFromFrontmatter: true,
},
// 贡献指南
{
documentRootPath: '/docs',
scanStartPath: 'contributor-guide',
resolvePath: '/contributor-guide/',
useTitleFromFrontmatter: true,
},
// Fashion
{
documentRootPath: '/docs',
scanStartPath: 'fashion',
resolvePath: '/fashion/',
useTitleFromFrontmatter: true,
export const sidebar = generate()
function generate() {
let sidebar = generateSidebar([
// 大学指南
{
documentRootPath: '/docs',
scanStartPath: 'campus',
resolvePath: '/campus/',
useTitleFromFrontmatter: true,
},
// 贡献指南
{
documentRootPath: '/docs',
scanStartPath: 'contributor-guide',
resolvePath: '/contributor-guide/',
useTitleFromFrontmatter: true,
},
// Fashion
{
documentRootPath: '/docs',
scanStartPath: 'fashion',
resolvePath: '/fashion/',
useTitleFromFrontmatter: true,
}
// 这个 `as` 源于 vitepress-sidebar 的类型定义与实际情况的差异,目前不影响使用,后续 vitepress-sidebar 修复后可以移除。
]) as DefaultTheme.Config['sidebar']
for (const key in sidebar) {
sidebar[key].items.sort(sidebarTitleSorter)
}
return sidebar
}
// 这个 `as` 源于 vitepress-sidebar 的类型定义与实际情况的差异,目前不影响使用,后续 vitepress-sidebar 修复后可以移除。
]) as DefaultTheme.Config['sidebar']
function sidebarTitleSorter(
infoA: SidebarItem,
infoB: SidebarItem
): number {
const textA = infoA.text
const textB = infoB.text
if (textA === undefined || textB === undefined) {
return 0
}
const infoANfc = textA.normalize('NFC');
const infoBNfc = textB.normalize('NFC');
return infoANfc.localeCompare(infoBNfc, 'zh', {
numeric: true,
})
}

View File

@ -26,7 +26,7 @@
"unplugin-vue-components": "^0.26.0",
"vite": "^5.0.5",
"vitepress": "^1.0.0-rc.31",
"vitepress-sidebar": "^1.18.0",
"vitepress-sidebar": "^1.18.5",
"vue": "^3.3.8",
"vuepress": "2.0.0-rc.0",
"vuepress-theme-hope": "2.0.0-rc.0"

View File

@ -50,8 +50,8 @@ devDependencies:
specifier: ^1.0.0-rc.31
version: 1.0.0-rc.31(@algolia/client-search@4.20.0)(postcss@8.4.32)(search-insights@2.11.0)
vitepress-sidebar:
specifier: ^1.18.0
version: 1.18.0
specifier: ^1.18.5
version: 1.18.5
vue:
specifier: ^3.3.8
version: 3.3.8
@ -5905,8 +5905,8 @@ packages:
optionalDependencies:
fsevents: 2.3.3
/vitepress-sidebar@1.18.0:
resolution: {integrity: sha512-Ki9ZpNa0CUNr7kovAbAEJ5v4MEZi+dte8Bz3OxkQcjFqrOGfQGEoGATRgf9m6N741epLLB3PFdaUCN6ZH0zmhg==}
/vitepress-sidebar@1.18.5:
resolution: {integrity: sha512-YksBZrwf6vYpSX+A9ZIa/YjSoiXT0gON2J9va+4FyPYvv31CwufGSjFmobW7IYOYbJEGA7Kl+i5CUo4wVmAvHg==}
engines: {node: '>=18.0.0'}
dependencies:
gray-matter: 4.0.3