Skip to content

Commit

Permalink
feat: transformPageData hook (#1492)
Browse files Browse the repository at this point in the history
Co-authored-by: Divyansh Singh <[email protected]>
  • Loading branch information
rigor789 and brc-dd authored Oct 18, 2022
1 parent d404753 commit afeb06f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
22 changes: 22 additions & 0 deletions docs/config/app-configs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>`
Expand Down
11 changes: 10 additions & 1 deletion src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -138,6 +145,7 @@ export interface SiteConfig<ThemeConfig = any>
| 'buildEnd'
| 'transformHead'
| 'transformHtml'
| 'transformPageData'
> {
root: string
srcDir: string
Expand Down Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions src/node/markdownToVue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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$/, '')))
Expand Down Expand Up @@ -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),
Expand All @@ -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) ?? [],
Expand Down
3 changes: 2 additions & 1 deletion src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ export async function createVitePressPlugin(
config.command === 'build',
config.base,
lastUpdated,
cleanUrls
cleanUrls,
siteConfig
)
},

Expand Down

0 comments on commit afeb06f

Please sign in to comment.