From 156e5aef27ff467d74a770f2b0889d79b2c21bc2 Mon Sep 17 00:00:00 2001 From: pengzhanbo Date: Fri, 15 Mar 2024 02:39:58 +0800 Subject: [PATCH] =?UTF-8?q?perf(theme):=20=E4=BC=98=E5=8C=96notes=E9=93=BE?= =?UTF-8?q?=E6=8E=A5=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- theme/src/node/autoFrontmatter.ts | 35 ++++++++++++++------ theme/src/node/resolveNotesList.ts | 51 ++++++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 11 deletions(-) diff --git a/theme/src/node/autoFrontmatter.ts b/theme/src/node/autoFrontmatter.ts index cb4c4183..a585bdd6 100644 --- a/theme/src/node/autoFrontmatter.ts +++ b/theme/src/node/autoFrontmatter.ts @@ -13,7 +13,7 @@ import type { PlumeThemePluginOptions, } from '../shared/index.js' import { getCurrentDirname, getPackage, nanoid, pathJoin } from './utils.js' -import { resolveNotesList } from './resolveNotesList.js' +import { resolveLinkBySidebar, resolveNotesList } from './resolveNotesList.js' import { resolveLocaleOptions } from './resolveLocaleOptions.js' export default function autoFrontmatter( @@ -28,7 +28,11 @@ export default function autoFrontmatter( const avatar = resolveLocaleOptions(localeOption, 'avatar') const notesList = resolveNotesList(localeOption) const localesNotesDirs = notesList - .map(notes => notes.dir?.replace(/^\//, '')) + .map(({ notes, dir }) => { + const _dir = dir?.replace(/^\//, '') + return notes.map(note => pathJoin(_dir, note.dir || '')) + }) + .flat() .filter(Boolean) const baseFrontmatter: FrontmatterObject = { @@ -81,7 +85,7 @@ export default function autoFrontmatter( localesNotesDirs.length ? { // note 首页链接 - include: localesNotesDirs.map(dir => pathJoin(dir, '**/{readme,README,index}.md')), + include: localesNotesDirs.map(dir => pathJoin(dir, '/{readme,README,index}.md')), frontmatter: { title(title: string, { filepath }) { if (title) @@ -117,7 +121,13 @@ export default function autoFrontmatter( title(title: string, { filepath }) { if (title) return title - const basename = path.basename(filepath, '.md') + + const note = findNote(filepath) + + let basename = path.basename(filepath, '.md') + if (note?.sidebar === 'auto') + basename = basename.replace(/^\d+\./, '') + return basename }, ...baseFrontmatter, @@ -127,15 +137,22 @@ export default function autoFrontmatter( if (data.friends) return const locale = resolveLocale(filepath) - const note = findNote(filepath) const notes = notesByLocale(locale) - return pathJoin( + const note = findNote(filepath) + const args: string[] = [ locale, notes?.link || '', note?.link || getCurrentDirname(note?.dir, filepath), - nanoid(), - '/', - ) + ] + const sidebar = note?.sidebar + + if (sidebar && sidebar !== 'auto') { + const res = resolveLinkBySidebar(sidebar, pathJoin(notes?.dir || '', note?.dir || '')) + const file = pathJoin('/', path.relative(sourceDir, filepath)) + res[file] && args.push(res[file]) + } + + return pathJoin(...args, nanoid(), '/') }, }, } diff --git a/theme/src/node/resolveNotesList.ts b/theme/src/node/resolveNotesList.ts index fa348af0..d72fc585 100644 --- a/theme/src/node/resolveNotesList.ts +++ b/theme/src/node/resolveNotesList.ts @@ -1,6 +1,7 @@ -import type { NotesDataOptions } from '@vuepress-plume/plugin-notes-data' +import type { NotesDataOptions, NotesSidebar } from '@vuepress-plume/plugin-notes-data' import type { PlumeThemeLocaleOptions } from '../shared/index.js' import { resolveLocaleOptions } from './resolveLocaleOptions.js' +import { pathJoin } from './utils.js' export function resolveNotesList(options: PlumeThemeLocaleOptions) { const locales = options.locales || {} @@ -8,8 +9,54 @@ export function resolveNotesList(options: PlumeThemeLocaleOptions) { for (const locale of Object.keys(locales)) { const notes = resolveLocaleOptions(options, 'notes', locale, false) - notes && notesList.push(notes) + if (notes) { + if (!notes.dir.includes(locale)) + notes.dir = pathJoin(locale, notes.dir) + + notesList.push(notes) + } } return notesList } + +export function resolveLinkBySidebar( + sidebar: NotesSidebar, + prefix: string, +) { + const res: Record = {} + + for (const item of sidebar) { + if (typeof item !== 'string') { + const { dir = '', link = '/', items, text = '' } = item + SidebarLink(items, link, text, pathJoin(prefix, dir), res) + } + } + return res +} + +function SidebarLink(items: NotesSidebar | undefined, link: string, text: string, dir = '', res: Record = {}) { + if (!items) { + res[pathJoin(dir, `${text}.md`)] = link + return + } + + for (const item of items) { + if (typeof item === 'string') { + if (!link) + continue + if (item) { + res[pathJoin(dir, `${item}.md`)] = link + } + else { + res[pathJoin(dir, 'README.md')] = link + res[pathJoin(dir, 'index.md')] = link + res[pathJoin(dir, 'readme.md')] = link + } + } + else { + const { dir: subDir = '', link: subLink = '/', items: subItems, text: subText = '' } = item + SidebarLink(subItems, pathJoin(link, subLink), subText, pathJoin(dir, subDir), res) + } + } +}