From 3714741b409f4e5f8df4cc42c7b59b065c8cc6f6 Mon Sep 17 00:00:00 2001 From: John Campion Jr Date: Wed, 29 Mar 2023 11:42:56 -0400 Subject: [PATCH] feat(build): provide `siteConfig` in `transformPageData` context (#2163) --- docs/reference/site-config.md | 16 +++++++++++++--- src/node/config.ts | 7 ++++++- src/node/markdownToVue.ts | 4 +++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/docs/reference/site-config.md b/docs/reference/site-config.md index 7c3fa19021a2..9c294608171b 100644 --- a/docs/reference/site-config.md +++ b/docs/reference/site-config.md @@ -552,21 +552,31 @@ export default { ### transformPageData -- Type: `(pageData: PageData) => Awaitable | { [key: string]: any } | void>` +- Type: `(pageData: PageData, ctx: TransformPageContext) => Awaitable | { [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 +} +``` diff --git a/src/node/config.ts b/src/node/config.ts index 63b8e9ecd397..6085ef709a6f 100644 --- a/src/node/config.ts +++ b/src/node/config.ts @@ -145,10 +145,15 @@ export interface UserConfig * PageData transform hook: runs when rendering markdown to vue */ transformPageData?: ( - pageData: PageData + pageData: PageData, + ctx: TransformPageContext ) => Awaitable | { [key: string]: any } | void> } +export interface TransformPageContext { + siteConfig: SiteConfig +} + export interface TransformContext { page: string siteConfig: SiteConfig diff --git a/src/node/markdownToVue.ts b/src/node/markdownToVue.ts index 420a4bf696b3..5e17c63a91b2 100644 --- a/src/node/markdownToVue.ts +++ b/src/node/markdownToVue.ts @@ -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,