Skip to content

Commit

Permalink
perf(theme): 优化 frontmatter 自动生成
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo committed Jul 9, 2024
1 parent 766285e commit 8714d76
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 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 @@ -69,7 +69,7 @@ export async function generateAFrontmatter(app: App) {
)
}

export async function watchAutoFrontmatter(app: App, watchers: any[]) {
export async function watchAutoFrontmatter(app: App, watchers: any[], enable?: () => boolean) {
if (!generate)
return

Expand All @@ -80,7 +80,8 @@ export async function watchAutoFrontmatter(app: App, watchers: any[]) {
})

watcher.on('add', async (relativePath) => {
if (!generate!.globFilter(relativePath))
const enabled = enable ? enable() : true
if (!generate!.globFilter(relativePath) || !enabled)
return
const file = await readMarkdown(app.dir.source(), relativePath)
await generator(file)
Expand Down
6 changes: 3 additions & 3 deletions theme/src/node/loadConfig/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@ export async function initConfigLoader(
defaultConfig: ThemeConfig,
{ configFile, onChange }: InitConfigLoaderOptions = {},
) {
configFile = await findConfigPath(app, configFile)

const { encrypt, autoFrontmatter, ...localeOptions } = defaultConfig
loader = {
configFile,
dependencies: [],
load: () => compiler(configFile),
load: () => compiler(loader!.configFile),
loaded: false,
watcher: null,
changeEvents: [],
Expand All @@ -60,6 +58,8 @@ export async function initConfigLoader(
},
}

loader.configFile = await findConfigPath(app, configFile)

onChange && loader.changeEvents.push(onChange)

const { config, dependencies = [] } = await loader.load()
Expand Down
8 changes: 4 additions & 4 deletions theme/src/node/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ export function plumeTheme(options: PlumeThemeOptions = {}): Theme {
onWatched: (app, watchers) => {
watchConfigFile(app, watchers)
watchPrepare(app, watchers)
const autoFrontmatter = getResolvedThemeConfig().autoFrontmatter ?? pluginOptions.frontmatter
if (autoFrontmatter !== false) {
watchAutoFrontmatter(app, watchers)
}
watchAutoFrontmatter(app, watchers, () => {
const autoFrontmatter = getResolvedThemeConfig().autoFrontmatter ?? pluginOptions.frontmatter
return autoFrontmatter !== false
})
},

extendsPage: async (page) => {
Expand Down

0 comments on commit 8714d76

Please sign in to comment.