feat: transformPageData hook (#1492)

Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
pull/1527/head
Igor Randjelovic 2 years ago committed by GitHub
parent d404753005
commit afeb06f17c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -318,6 +318,28 @@ export default {
}
```
### transformPageData
- Type: `(pageData: PageData) => 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.
```ts
export default {
async transformPageData(pageData) {
pageData.contributors = await getPageContributors(pageData.relativePath)
}
// or return data to be merged
async transformPageData(pageData) {
return {
contributors: await getPageContributors(pageData.relativePath)
}
}
}
```
### buildEnd
- Type: `(siteConfig: SiteConfig) => Awaitable<void>`

@ -108,6 +108,13 @@ export interface UserConfig<ThemeConfig = any> {
id: string,
ctx: TransformContext
) => Awaitable<string | void>
/**
* PageData transform hook: runs when rendering markdown to vue
*/
transformPageData?: (
pageData: PageData
) => Awaitable<Partial<PageData> | { [key: string]: any } | void>
}
export interface TransformContext {
@ -138,6 +145,7 @@ export interface SiteConfig<ThemeConfig = any>
| 'buildEnd'
| 'transformHead'
| 'transformHtml'
| 'transformPageData'
> {
root: string
srcDir: string
@ -224,7 +232,8 @@ export async function resolveConfig(
cleanUrls: userConfig.cleanUrls || 'disabled',
buildEnd: userConfig.buildEnd,
transformHead: userConfig.transformHead,
transformHtml: userConfig.transformHtml
transformHtml: userConfig.transformHtml,
transformPageData: userConfig.transformPageData
}
return config

@ -3,6 +3,7 @@ import path from 'path'
import c from 'picocolors'
import LRUCache from 'lru-cache'
import { resolveTitleFromToken } from '@mdit-vue/shared'
import { SiteConfig } from './config'
import { PageData, HeadConfig, EXTERNAL_URL_RE, CleanUrlsMode } from './shared'
import { slash } from './utils/slash'
import { getGitTimestamp } from './utils/getGitTimestamp'
@ -37,7 +38,8 @@ export async function createMarkdownToVueRenderFn(
isBuild = false,
base = '/',
includeLastUpdatedData = false,
cleanUrls: CleanUrlsMode = 'disabled'
cleanUrls: CleanUrlsMode = 'disabled',
siteConfig: SiteConfig | null = null
) {
const md = await createMarkdownRenderer(srcDir, options, base)
pages = pages.map((p) => slash(p.replace(/\.md$/, '')))
@ -131,7 +133,7 @@ export async function createMarkdownToVueRenderFn(
}
}
const pageData: PageData = {
let pageData: PageData = {
title: inferTitle(md, frontmatter, title),
titleTemplate: frontmatter.titleTemplate as any,
description: inferDescription(frontmatter),
@ -144,6 +146,16 @@ export async function createMarkdownToVueRenderFn(
pageData.lastUpdated = await getGitTimestamp(file)
}
if (siteConfig?.transformPageData) {
const dataToMerge = await siteConfig.transformPageData(pageData)
if (dataToMerge) {
pageData = {
...pageData,
...dataToMerge
}
}
}
const vueSrc = [
...injectPageDataCode(
sfcBlocks?.scripts.map((item) => item.content) ?? [],

@ -95,7 +95,8 @@ export async function createVitePressPlugin(
config.command === 'build',
config.base,
lastUpdated,
cleanUrls
cleanUrls,
siteConfig
)
},

Loading…
Cancel
Save