perf(theme): 优化notes链接生成
This commit is contained in:
parent
2ba25c94c7
commit
156e5aef27
@ -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(), '/')
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@ -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<string, string> = {}
|
||||
|
||||
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<string, string> = {}) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user