Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(build): provide siteConfig in transformPageData context #2163

Merged
merged 3 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions docs/reference/site-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -552,21 +552,31 @@ export default {

### transformPageData

- Type: `(pageData: PageData) => Awaitable<Partial<PageData> | { [key: string]: any } | void>`
- Type: `(pageData: PageData, ctx: TransformPageContext) => Awaitable<Partial<PageData> | { [key: string]: any } | void>`

`transformPageData` is a hook to transform the `pageData` of each page. You can directly mutate `pageData` or return changed values which will be merged into PageData.

::: warning
Don't mutate anything inside the `ctx`.
:::

```ts
export default {
async transformPageData(pageData) {
async transformPageData(pageData, { siteConfig }) {
pageData.contributors = await getPageContributors(pageData.relativePath)
}

// or return data to be merged
async transformPageData(pageData) {
async transformPageData(pageData, { siteConfig }) {
return {
contributors: await getPageContributors(pageData.relativePath)
}
}
}
```

```ts
interface TransformPageContext {
siteConfig: SiteConfig
}
```
7 changes: 6 additions & 1 deletion src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,15 @@ export interface UserConfig<ThemeConfig = any>
* PageData transform hook: runs when rendering markdown to vue
*/
transformPageData?: (
pageData: PageData
pageData: PageData,
ctx: TransformPageContext
) => Awaitable<Partial<PageData> | { [key: string]: any } | void>
}

export interface TransformPageContext {
siteConfig: SiteConfig
}

export interface TransformContext {
page: string
siteConfig: SiteConfig
Expand Down
4 changes: 3 additions & 1 deletion src/node/markdownToVue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ export async function createMarkdownToVueRenderFn(
}

if (siteConfig?.transformPageData) {
const dataToMerge = await siteConfig.transformPageData(pageData)
const dataToMerge = await siteConfig.transformPageData(pageData, {
siteConfig
})
if (dataToMerge) {
pageData = {
...pageData,
Expand Down