Skip to content

Commit 494f0ad

Browse files
committed
docs: add extendsPageOptions hook
1 parent 9534989 commit 494f0ad

File tree

5 files changed

+65
-3
lines changed

5 files changed

+65
-3
lines changed
Loading

docs/guide/advanced/architecture.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ The above figure shows the core process of VuePress Node App and the hooks of [P
2222

2323
- In the **init** stage:
2424
- Theme and plugins will be loaded. That means all the plugins should be used before initialization.
25-
- Page files will be loaded. As we are using markdown-it to parse the markdown file, the [extendsMarkdown](../../reference/plugin-api.md#extendsmarkdown) hook will be processed before that.
25+
- As we are using markdown-it to parse the markdown file, the [extendsMarkdown](../../reference/plugin-api.md#extendsmarkdown) hook will be processed before loading page files.
26+
- Page files will be loaded, and [extendsPageOptions](../../reference/plugin-api.md#extendspageoptions) hook will be processed to create pages.
2627
- In the **prepare** stage:
2728
- Temp files will be generated, so all hooks related to client files will be processed here.
2829
- In the **dev / build** stage:

docs/reference/plugin-api.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Plugins should be used before initialization. The basic options will be handled
99
The following hooks will be processed when initializing app:
1010

1111
- [extendsMarkdown](#extendsmarkdown)
12+
- [extendsPageOptions](#extendspageoptions)
1213
- [onInitialized](#oninitialized)
1314

1415
The following hooks will be processed when preparing files:
@@ -168,6 +169,35 @@ module.exports = {
168169
}
169170
```
170171

172+
### extendsPageOptions
173+
174+
- Type: `(filePath: string, app: App) => PageOptions | Promise<PageOptions>`
175+
176+
- Details:
177+
178+
Page options extension.
179+
180+
This hook accepts a function that will receive the relative file path of the page. The returned object will be merged into page options, which will be used to create the page.
181+
182+
- Example:
183+
184+
Set permalink pattern for pages in `_posts` directory:
185+
186+
```js
187+
module.exports = {
188+
extendsPageOptions: (filePath) => {
189+
if (filePath.startsWith('_posts/')) {
190+
return {
191+
frontmatter: {
192+
permalinkPattern: '/:year/:month/:day/:slug.html',
193+
},
194+
}
195+
}
196+
return {}
197+
},
198+
}
199+
```
200+
171201
### extendsPageData
172202

173203
- Type: `(page: Page, app: App) => Record<string, any> | Promise<Record<string, any>>`
@@ -189,7 +219,7 @@ module.exports = {
189219
}
190220
```
191221

192-
In client component:
222+
In client component:
193223

194224
```js
195225
import { usePageData } from '@vuepress/client'

docs/zh/guide/advanced/architecture.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222

2323
-**init** 阶段:
2424
- 主题和插件会被加载。这意味着插件需要在初始化之前使用。
25-
- 页面文件会被加载。由于我们要使用 markdown-it 来解析 Markdown 文件,因此 [extendsMarkdown](../../reference/plugin-api.md#extendsmarkdown) 会在这之前调用。
25+
- 由于我们要使用 markdown-it 来解析 Markdown 文件,因此 [extendsMarkdown](../../reference/plugin-api.md#extendsmarkdown) 会在加载页面文件之前调用。
26+
- 页面文件会被加载,因此 [extendsPageOptions](../../reference/plugin-api.md#extendspageoptions) Hook 会被调用,用以创建页面。
2627
-**prepare** 阶段:
2728
- 临时文件会被生成,因此所有和客户端文件相关的 Hooks 会在此处调用。
2829
-**dev / build** 阶段:

docs/zh/reference/plugin-api.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
下列 Hooks 会在初始化 App 时处理:
1010

1111
- [extendsMarkdown](#extendsmarkdown)
12+
- [extendsPageOptions](#extendspageoptions)
1213
- [onInitialized](#oninitialized)
1314

1415
下列 Hooks 会在准备文件时处理:
@@ -168,6 +169,35 @@ module.exports = {
168169
}
169170
```
170171

172+
### extendsPageOptions
173+
174+
- 类型: `(filePath: string, app: App) => PageOptions | Promise<PageOptions>`
175+
176+
- 详情:
177+
178+
页面配置项扩展。
179+
180+
该 Hook 接收一个函数,在参数中会收到页面文件的相对路径。返回的对象会被合并到页面配置项中,用以创建页面。
181+
182+
- 示例:
183+
184+
`_posts` 目录下的页面设置永久链接 Pattern :
185+
186+
```js
187+
module.exports = {
188+
extendsPageOptions: (filePath) => {
189+
if (filePath.startsWith('_posts/')) {
190+
return {
191+
frontmatter: {
192+
permalinkPattern: '/:year/:month/:day/:slug.html',
193+
},
194+
}
195+
}
196+
return {}
197+
},
198+
}
199+
```
200+
171201
### extendsPageData
172202

173203
- 类型: `(page: Page, app: App) => Record<string, any> | Promise<Record<string, any>>`

0 commit comments

Comments
 (0)