fix(theme): incorrect auto generate sidebar, close #307 (#308)

This commit is contained in:
pengzhanbo 2024-10-24 00:55:01 +08:00 committed by GitHub
parent 4591a3e1cc
commit b98f46a914
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 45 additions and 19 deletions

View File

@ -27,18 +27,24 @@ import { useEncrypt } from './encrypt.js'
export type SidebarData = Record<string, Sidebar>
export type SidebarDataRef = Ref<SidebarData>
export type AutoDirSidebarRef = Ref<SidebarItem[]>
export type AutoDirSidebarRef = Ref<SidebarItem[] | {
link: string
items: SidebarItem[]
}>
export type AutoHomeDataRef = Ref<Record<string, string>>
const { __auto__, ...items } = sidebarRaw
const { __auto__, __home__, ...items } = sidebarRaw
const sidebarData: SidebarDataRef = ref(items)
const autoDirSidebar: AutoDirSidebarRef = ref(__auto__)
const autoHomeData: AutoHomeDataRef = ref(__home__)
if (__VUEPRESS_DEV__ && (import.meta.webpackHot || import.meta.hot)) {
__VUE_HMR_RUNTIME__.updateSidebar = (data: SidebarData) => {
const { __auto__, ...items } = data
const { __auto__, __home__, ...items } = data
sidebarData.value = items
autoDirSidebar.value = __auto__ as SidebarItem[]
autoHomeData.value = __home__ as Record<string, string>
}
}
@ -127,7 +133,10 @@ export function getSidebar(routePath: string, routeLocal: string): ResolvedSideb
return []
}
function resolveSidebarItems(sidebarItems: (string | SidebarItem)[], _prefix = ''): ResolvedSidebarItem[] {
function resolveSidebarItems(
sidebarItems: (string | SidebarItem)[],
_prefix = '',
): ResolvedSidebarItem[] {
const resolved: ResolvedSidebarItem[] = []
sidebarItems.forEach((item) => {
if (isString(item)) {
@ -144,6 +153,11 @@ function resolveSidebarItems(sidebarItems: (string | SidebarItem)[], _prefix = '
const nextPrefix = normalizePrefix(_prefix, prefix || dir)
if (items === 'auto') {
navLink.items = autoDirSidebar.value[nextPrefix]
if (!navLink.link && autoHomeData.value[nextPrefix]) {
navLink.link = normalizeLink(autoHomeData.value[nextPrefix])
const nav = resolveNavLink(navLink.link)
navLink.icon = nav.icon
}
}
else {
navLink.items = items?.length

View File

@ -36,7 +36,8 @@ declare module '@internal/sidebar' {
import type { Sidebar, SidebarItem } from '../shared/index.js'
const sidebar: {
__auto__: SidebarItem[]
__auto__: SidebarItem[] | { link: string, items: SidebarItem[] }
__home__: Record<string, string>
[key: string]: Sidebar
}
export {

View File

@ -18,7 +18,10 @@ import { logger, normalizeLink, resolveContent, writeTemp } from '../utils/index
export async function prepareSidebar(app: App, localeOptions: PlumeThemeLocaleOptions) {
const start = performance.now()
const sidebar = getAllSidebar(localeOptions)
sidebar.__auto__ = getSidebarData(app, sidebar)
const { resolved, autoHome } = getSidebarData(app, sidebar)
sidebar.__auto__ = resolved
sidebar.__home__ = autoHome as any
await writeTemp(app, 'internal/sidebar.js', resolveContent(app, { name: 'sidebar', content: sidebar }))
if (app.env.isDebug) {
@ -29,7 +32,7 @@ export async function prepareSidebar(app: App, localeOptions: PlumeThemeLocaleOp
function getSidebarData(
app: App,
locales: Record<string, Sidebar>,
): Sidebar {
): { resolved: Sidebar, autoHome: Record<string, string> } {
const autoDirList: string[] = []
const resolved: Sidebar = {}
@ -67,11 +70,16 @@ function getSidebarData(
}
})
const autoHome: Record<string, string> = {}
autoDirList.forEach((localePath) => {
resolved[localePath] = getAutoDirSidebar(app, localePath)
const { link, sidebar } = getAutoDirSidebar(app, localePath)
resolved[localePath] = sidebar
if (link) {
autoHome[localePath] = link
}
})
return resolved
return { resolved, autoHome }
}
const MD_RE = /\.md$/
@ -85,7 +93,7 @@ function resolveTitle(dirname: string) {
function getAutoDirSidebar(
app: App,
localePath: string,
): SidebarItem[] {
): { link: string, sidebar: SidebarItem[] } {
const locale = removeLeadingSlash(localePath)
let pages = (app.pages as Page<PlumeThemePageData>[])
.filter(page => page.data.filePathRelative?.startsWith(locale))
@ -110,7 +118,8 @@ function getAutoDirSidebar(
const RE_INDEX = ['index.md', 'README.md', 'readme.md']
const result: ResolvedSidebarItem[] = []
const sidebar: ResolvedSidebarItem[] = []
let rootLink = ''
for (const page of pages) {
const { data, title, path, frontmatter } = page
const paths = (data.filePathRelative || '')
@ -119,7 +128,7 @@ function getAutoDirSidebar(
let index = 0
let dir: string
let items = result
let items = sidebar
let parent: ResolvedSidebarItem | undefined
// eslint-disable-next-line no-cond-assign
while ((dir = paths[index])) {
@ -131,13 +140,15 @@ function getAutoDirSidebar(
if (!isHome) {
items.push(current)
}
else if (!parent) {
items.unshift(current)
}
}
if (dir.endsWith('.md')) {
if (isHome && parent) {
parent.link = path
if (isHome) {
if (parent) {
parent.link = path
}
else {
rootLink = path
}
}
else {
current.link = path
@ -155,7 +166,7 @@ function getAutoDirSidebar(
index++
}
}
return result
return { link: rootLink, sidebar }
}
function findAutoDirList(sidebar: (string | SidebarItem)[], prefix = ''): string[] {

View File

@ -26,7 +26,7 @@ export interface BulletinOptions {
/**
*
*
* @default 'session'
* @default 'always'
*
* - `'session'`
* - `'always'`