Skip to content

Commit 19b7e83

Browse files
committed
feat(core): add extendsPageOptions hook
1 parent 1ce602e commit 19b7e83

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

packages/@vuepress/core/src/app/createAppPages.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
import { globby } from '@vuepress/utils'
22
import { createPage } from '../page'
3-
import type { App, Page } from '../types'
3+
import type { App, Page, PageOptions } from '../types'
44

55
/**
66
* Create pages for vuepress app
77
*/
88
export const createAppPages = async (app: App): Promise<Page[]> => {
9+
// resolve page file paths according to the page patterns
910
const pagePaths = await globby(app.options.pagePatterns, {
1011
cwd: app.dir.source(),
1112
})
1213

13-
// TODO
14-
// may need to limit the max parallel tasks
15-
// or change to serial tasks
14+
// create pages from files
1615
const pages = await Promise.all(
17-
pagePaths.map((filePath) =>
18-
createPage(app, {
16+
pagePaths.map(async (filePath) => {
17+
const pageOptions: PageOptions = { filePath }
18+
19+
// plugin hook: extendsPageOptions
20+
const extendsPageOptions = await app.pluginApi.hooks.extendsPageOptions.process(
1921
filePath,
20-
})
21-
)
22+
app
23+
)
24+
extendsPageOptions.forEach((item) => Object.assign(pageOptions, item))
25+
26+
return createPage(app, pageOptions)
27+
})
2228
)
2329

2430
// if there is no 404 page, add one

packages/@vuepress/core/src/pluginApi/createPluginApiHooks.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const createPluginApiHooks = (): PluginApi['hooks'] => ({
99
onGenerated: createHookQueue('onGenerated'),
1010

1111
// page hooks
12+
extendsPageOptions: createHookQueue('extendsPageOptions'),
1213
extendsPageData: createHookQueue('extendsPageData'),
1314

1415
// markdown hooks

packages/@vuepress/core/src/types/plugin-api/hooks.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Markdown } from '@vuepress/markdown'
22
import type { App } from '../app'
3-
import type { Page } from '../page'
3+
import type { Page, PageOptions } from '../page'
44

55
// util type
66
type PromiseOrNot<T> = Promise<T> | T
@@ -42,6 +42,9 @@ export type ReturnObjectHook = Hook<
4242
export type ExtendsMarkdownHook = Hook<(md: Markdown, app: App) => void>
4343

4444
// page hook
45+
export type ExtendsPageOptionsHook = Hook<
46+
(filePath: string, app: App) => PromiseOrNot<PageOptions>
47+
>
4548
export type ExtendsPageDataHook = Hook<
4649
(page: Page, app: App) => PromiseOrNot<Record<string, any>>
4750
>
@@ -55,6 +58,7 @@ export interface Hooks {
5558
onWatched: LifeCycleHook<[watchers: Closable[], restart: () => Promise<void>]>
5659
onGenerated: LifeCycleHook
5760
extendsMarkdown: ExtendsMarkdownHook
61+
extendsPageOptions: ExtendsPageOptionsHook
5862
extendsPageData: ExtendsPageDataHook
5963
clientAppEnhanceFiles: ClientFilesHook
6064
clientAppRootComponentFiles: ClientFilesHook

0 commit comments

Comments
 (0)