diff --git a/theme/src/client/composables/sidebar.ts b/theme/src/client/composables/sidebar.ts index fe2fcaa6..d174df76 100644 --- a/theme/src/client/composables/sidebar.ts +++ b/theme/src/client/composables/sidebar.ts @@ -27,18 +27,24 @@ import { useEncrypt } from './encrypt.js' export type SidebarData = Record export type SidebarDataRef = Ref -export type AutoDirSidebarRef = Ref +export type AutoDirSidebarRef = Ref +export type AutoHomeDataRef = Ref> -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 } } @@ -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 diff --git a/theme/src/client/shim.d.ts b/theme/src/client/shim.d.ts index 0d984747..14ed9404 100644 --- a/theme/src/client/shim.d.ts +++ b/theme/src/client/shim.d.ts @@ -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 [key: string]: Sidebar } export { diff --git a/theme/src/node/prepare/prepareSidebar.ts b/theme/src/node/prepare/prepareSidebar.ts index d94d07aa..19621e68 100644 --- a/theme/src/node/prepare/prepareSidebar.ts +++ b/theme/src/node/prepare/prepareSidebar.ts @@ -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, -): Sidebar { +): { resolved: Sidebar, autoHome: Record } { const autoDirList: string[] = [] const resolved: Sidebar = {} @@ -67,11 +70,16 @@ function getSidebarData( } }) + const autoHome: Record = {} 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[]) .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[] { diff --git a/theme/src/shared/options/bulletin.ts b/theme/src/shared/options/bulletin.ts index fac4160d..cc2791e5 100644 --- a/theme/src/shared/options/bulletin.ts +++ b/theme/src/shared/options/bulletin.ts @@ -26,7 +26,7 @@ export interface BulletinOptions { /** * 公告持续时间 * - * @default 'session' + * @default 'always' * * - `'session'` 表示在会话周期内关闭公告后不再显示,在新的会话周期重新显示,刷新页面不会重新显示 * - `'always'` 表示总是显示,关闭公告后刷新页面会重新显示