From 4b2739bff0401e94a4122c2a058daa73edb10b0e Mon Sep 17 00:00:00 2001 From: pengzhanbo Date: Fri, 30 Aug 2024 19:39:40 +0800 Subject: [PATCH] perf(theme): optimize auto frontmatter --- theme/src/node/autoFrontmatter/generator.ts | 5 +++-- theme/src/node/theme.ts | 12 +++--------- theme/src/shared/options/plugins.ts | 6 ------ 3 files changed, 6 insertions(+), 17 deletions(-) diff --git a/theme/src/node/autoFrontmatter/generator.ts b/theme/src/node/autoFrontmatter/generator.ts index ef4d2bf4..b9969122 100644 --- a/theme/src/node/autoFrontmatter/generator.ts +++ b/theme/src/node/autoFrontmatter/generator.ts @@ -12,6 +12,7 @@ import type { AutoFrontmatterObject, PlumeThemeLocaleOptions, } from '../../shared/index.js' +import { getThemeConfig } from '../loadConfig/index.js' import { readMarkdown, readMarkdownList } from './readFile.js' import { resolveOptions } from './resolveOptions.js' @@ -69,7 +70,7 @@ export async function generateAutoFrontmatter(app: App) { ) } -export async function watchAutoFrontmatter(app: App, watchers: any[], enable?: () => boolean) { +export async function watchAutoFrontmatter(app: App, watchers: any[]) { if (!generate) return @@ -80,7 +81,7 @@ export async function watchAutoFrontmatter(app: App, watchers: any[], enable?: ( }) watcher.on('add', async (relativePath) => { - const enabled = enable ? enable() : true + const enabled = getThemeConfig().autoFrontmatter !== false if (!generate!.globFilter(relativePath) || !enabled) return const file = await readMarkdown(app.dir.source(), relativePath) diff --git a/theme/src/node/theme.ts b/theme/src/node/theme.ts index 2dba7e69..ab97fc6a 100644 --- a/theme/src/node/theme.ts +++ b/theme/src/node/theme.ts @@ -17,10 +17,8 @@ export function plumeTheme(options: PlumeThemeOptions = {}): Theme { initConfigLoader(app, localeOptions, { configFile, onChange: ({ localeOptions, autoFrontmatter }) => { - autoFrontmatter ??= pluginOptions.frontmatter - if (autoFrontmatter !== false) { + if (autoFrontmatter !== false) initAutoFrontmatter(localeOptions, autoFrontmatter) - } }, }) @@ -43,7 +41,7 @@ export function plumeTheme(options: PlumeThemeOptions = {}): Theme { extendsMarkdown: async (_, app) => { const { autoFrontmatter } = await waitForConfigLoaded() - if ((autoFrontmatter ?? pluginOptions.frontmatter) !== false) { + if (autoFrontmatter !== false) { await generateAutoFrontmatter(app) // wait for autoFrontmatter generated // i/o performance @@ -73,11 +71,7 @@ export function plumeTheme(options: PlumeThemeOptions = {}): Theme { await prepareThemeData(app, localeOptions) await prepareData(app) }) - watchAutoFrontmatter(app, watchers, () => { - const autoFrontmatter = getThemeConfig().autoFrontmatter - ?? pluginOptions.frontmatter - return autoFrontmatter !== false - }) + watchAutoFrontmatter(app, watchers) watchPrepare(app, watchers) }, } diff --git a/theme/src/shared/options/plugins.ts b/theme/src/shared/options/plugins.ts index a50abc2e..639a15d4 100644 --- a/theme/src/shared/options/plugins.ts +++ b/theme/src/shared/options/plugins.ts @@ -6,7 +6,6 @@ import type { MarkdownEnhancePluginOptions } from 'vuepress-plugin-md-enhance' import type { ReadingTimePluginOptions } from '@vuepress/plugin-reading-time' import type { MarkdownPowerPluginOptions } from 'vuepress-plugin-md-power' import type { WatermarkPluginOptions } from '@vuepress/plugin-watermark' -import type { AutoFrontmatter } from '../auto-frontmatter.js' export interface PlumeThemePluginOptions { /** @@ -62,11 +61,6 @@ export interface PlumeThemePluginOptions { */ baiduTongji?: false | { key: string } - /** - * @deprecated 使用 `autoFrontmatter` 代替 - */ - frontmatter?: Omit - /** * 阅读时间、字数统计 */