Skip to content

Commit

Permalink
feat(build): provide siteConfig in transformPageData context (#2163)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnCampionJr authored and brc-dd committed Mar 29, 2023
1 parent 4c23003 commit 3714741
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
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

0 comments on commit 3714741

Please sign in to comment.