mirror of
https://github.com/pengzhanbo/vuepress-theme-plume.git
synced 2026-04-23 10:58:13 +08:00
parent
4591a3e1cc
commit
b98f46a914
@ -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
|
||||
|
||||
3
theme/src/client/shim.d.ts
vendored
3
theme/src/client/shim.d.ts
vendored
@ -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 {
|
||||
|
||||
@ -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[] {
|
||||
|
||||
@ -26,7 +26,7 @@ export interface BulletinOptions {
|
||||
/**
|
||||
* 公告持续时间
|
||||
*
|
||||
* @default 'session'
|
||||
* @default 'always'
|
||||
*
|
||||
* - `'session'` 表示在会话周期内关闭公告后不再显示,在新的会话周期重新显示,刷新页面不会重新显示
|
||||
* - `'always'` 表示总是显示,关闭公告后刷新页面会重新显示
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user