Skip to content

Commit

Permalink
perf(theme): optimize auto frontmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo committed Aug 30, 2024
1 parent 0af1391 commit 4b2739b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 17 deletions.
5 changes: 3 additions & 2 deletions theme/src/node/autoFrontmatter/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand Down
12 changes: 3 additions & 9 deletions theme/src/node/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
},
})

Expand All @@ -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
Expand Down Expand Up @@ -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)
},
}
Expand Down
6 changes: 0 additions & 6 deletions theme/src/shared/options/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -62,11 +61,6 @@ export interface PlumeThemePluginOptions {
*/
baiduTongji?: false | { key: string }

/**
* @deprecated 使用 `autoFrontmatter` 代替
*/
frontmatter?: Omit<AutoFrontmatter, 'frontmatter'>

/**
* 阅读时间、字数统计
*/
Expand Down

0 comments on commit 4b2739b

Please sign in to comment.