From 15d9265b21969b493268d3cc55012ecfb554306c Mon Sep 17 00:00:00 2001 From: pengzhanbo Date: Wed, 27 Dec 2023 01:09:42 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E8=B0=83=E6=95=B4=E4=B8=BB=E9=A2=98?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- theme/src/node/autoFrontmatter.ts | 8 ++++---- theme/src/node/defaultOptions.ts | 30 +++++++++++++++++++++++------- theme/src/node/plugins.ts | 7 +++++-- theme/src/node/setupPages.ts | 2 +- theme/src/shared/options/locale.ts | 10 ++-------- 5 files changed, 35 insertions(+), 22 deletions(-) diff --git a/theme/src/node/autoFrontmatter.ts b/theme/src/node/autoFrontmatter.ts index 513f17f2..e65c68e7 100644 --- a/theme/src/node/autoFrontmatter.ts +++ b/theme/src/node/autoFrontmatter.ts @@ -1,4 +1,4 @@ -import { createRequire } from 'node:module' +import fs from 'node:fs' import path from 'node:path' import type { App } from '@vuepress/core' import { resolveLocalePath } from '@vuepress/shared' @@ -16,11 +16,11 @@ import type { } from '../shared/index.js' const nanoid = customAlphabet('0123456789abcdefghijklmnopqrstuvwxyz', 8) -const require = createRequire(process.cwd()) const getPackage = () => { let pkg = {} as any try { - pkg = require('package.json') || {} + const content = fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf-8') + pkg = JSON.parse(content) } catch {} return pkg } @@ -92,7 +92,7 @@ export default function autoFrontmatter( return dirList.length > 0 ? dirList[dirList.length - 1] : '' } return { - include: options.frontmatter?.include ?? ['**/*.{md,MD}'], + include: options.frontmatter?.include ?? ['**/*.md'], exclude: options.frontmatter?.exclude ?? ['.vuepress/**/*', 'node_modules'], frontmatter: options.frontmatter?.frontmatter ?? [ localesNotesDirs.length diff --git a/theme/src/node/defaultOptions.ts b/theme/src/node/defaultOptions.ts index 345da96d..9bc80787 100644 --- a/theme/src/node/defaultOptions.ts +++ b/theme/src/node/defaultOptions.ts @@ -3,22 +3,38 @@ import type { PlumeThemeLocaleOptions } from '../shared/index.js' export const defaultLocaleOption: Partial = { article: '/article', notes: { link: '/note', dir: 'notes', notes: [] }, - blog: { - include: ['**/*.md'], - exclude: ['.vuepress/', 'node_modules/', '{README,index}.md'], - link: '/blog/', - }, footer: { message: - 'Power by Vuepress & vuepress-theme-plume', + 'Power by vuepress & vuepress-theme-plume', }, appearance: true, + // page meta + editLink: true, + editLinkText: 'Edit this page', + lastUpdated: true, + lastUpdatedText: 'Last Updated', + contributors: true, + contributorsText: 'Contributors', } export const mergeLocaleOptions = (options: PlumeThemeLocaleOptions) => { if (!options.locales) { options.locales = {} } + if (options.notes) { + options.notes = { + ...defaultLocaleOption.notes, + ...(options.notes ?? {}), + } + } + + if (options.footer) { + options.footer = { + ...defaultLocaleOption.footer, + ...options.footer, + } + } + const { locales, ...otherOptions } = options if (!locales['/']) { @@ -30,7 +46,7 @@ export const mergeLocaleOptions = (options: PlumeThemeLocaleOptions) => { }) Object.assign(locales['/'], { - ...defaultLocaleOption, + ...{ selectLanguageName: 'English' }, ...JSON.parse(JSON.stringify(otherOptions)), ...locales['/'], }) diff --git a/theme/src/node/plugins.ts b/theme/src/node/plugins.ts index 9e7a1dac..2e35112d 100644 --- a/theme/src/node/plugins.ts +++ b/theme/src/node/plugins.ts @@ -51,6 +51,7 @@ export const setupPlugins = ( return [ palettePlugin({ preset: 'sass' }), + themeDataPlugin({ themeData: { ...localeOptions, @@ -60,12 +61,14 @@ export const setupPlugins = ( } as any, }), autoFrontmatterPlugin(autoFrontmatter(app, options, localeOptions)), + blogDataPlugin({ - include: localeOptions.blog?.include, + include: localeOptions.blog?.include ?? ['**/*.md'], exclude: [ '**/{README,readme,index}.md', '.vuepress/', - ...(localeOptions.blog?.exclude || []), + 'node_modules/', + ...(localeOptions.blog?.exclude ?? []), ...localeNotesDirs, ].filter(Boolean), sortBy: 'createTime', diff --git a/theme/src/node/setupPages.ts b/theme/src/node/setupPages.ts index 3bf01528..e20bfa0f 100644 --- a/theme/src/node/setupPages.ts +++ b/theme/src/node/setupPages.ts @@ -21,7 +21,7 @@ export async function setupPage( const defaultBlog = localeOption.blog const link = blog?.link ? blog.link - : normalizePath(path.join('/', locale, defaultBlog?.link || '')) + : normalizePath(path.join('/', locale, defaultBlog?.link || '/blog/')) const blogPage = await createPage(app, { path: link, frontmatter: { diff --git a/theme/src/shared/options/locale.ts b/theme/src/shared/options/locale.ts index e543477b..8cb9426c 100644 --- a/theme/src/shared/options/locale.ts +++ b/theme/src/shared/options/locale.ts @@ -38,12 +38,6 @@ export type SocialLinkIcon = | { svg: string } export interface PlumeThemeBlog { - /** - * blog 文章读取目录 - * - * @default './' 即 vuepress 配置的 source 目录 - */ - dir?: string /** * blog list link @@ -53,14 +47,14 @@ export interface PlumeThemeBlog { link?: string /** - * 在 `blog.dir` 目录中,通过 glob string 配置包含文件 + * 通过 glob string 配置包含文件 * * @default - ['**\*.md'] */ include?: string[] /** - * 在 `blog.dir` 目录中,通过 glob string 配置排除的文件 + * 通过 glob string 配置排除的文件 * * _README.md 文件一般作为主页或者某个目录下的主页,不应该被读取为 blog文章_ *