docs: add `transformHtml` and `buildEnd` hooks (#1317)

Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
pull/1319/merge
Joaquín Sánchez 2 years ago committed by GitHub
parent 5378a49613
commit 1aa3456125
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -246,3 +246,59 @@ export default {
cleanUrls: 'with-subfolders'
}
```
## Build Hooks
VitePress build hooks allow you to add new functionality and behaviors to your website:
- Sitemap
- Search Indexing
- PWA
### transformHtml
- Type: `( code: string, id: string, ctx: TransformContext ) => Awaitable<string | void>`
`transformHtml` is a build hook to transform the content of each page before saving to disk (SSG).
::: warning
Modifying the html content may cause hydration problems in runtime.
:::
```ts
import { defineConfig } from 'vitepress'
export default defineConfig({
/* other vitepress options */
async transformHtml(code, id, context) {
}
})
```
```ts
interface TransformContext {
siteConfig: SiteConfig
siteData: SiteData
pageData: PageData
title: string
description: string
head: HeadConfig[]
content: string
}
```
### buildEnd
- Type: `(siteConfig: SiteConfig) => Awaitable<void>`
`buildEnd` is a build CLI hook, it will run after build (SSG) finish but before VitePress CLI process exits.
```ts
import { defineConfig } from 'vitepress'
export default defineConfig({
/* other vitepress options */
async buildEnd(siteConfig) {
}
})
```

@ -99,18 +99,20 @@ export interface UserConfig<ThemeConfig = any> {
transformHtml?: (
code: string,
id: string,
ctx: {
siteConfig: SiteConfig
siteData: SiteData
pageData: PageData
title: string
description: string
head: HeadConfig[]
content: string
}
ctx: TransformContext
) => Awaitable<string | void>
}
export interface TransformContext {
siteConfig: SiteConfig
siteData: SiteData
pageData: PageData
title: string
description: string
head: HeadConfig[]
content: string
}
export type RawConfigExports<ThemeConfig = any> =
| Awaitable<UserConfig<ThemeConfig>>
| (() => Awaitable<UserConfig<ThemeConfig>>)

Loading…
Cancel
Save