feat: add `transformHead` hook (#1323)

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

1
.gitattributes vendored

@ -0,0 +1 @@
* text=auto eol=lf

@ -255,24 +255,21 @@ VitePress build hooks allow you to add new functionality and behaviors to your w
- Search Indexing - Search Indexing
- PWA - PWA
### transformHtml ### transformHead
- Type: `( code: string, id: string, ctx: TransformContext ) => Awaitable<string | void>` - Type: `(ctx: TransformContext) => Awaitable<HeadConfig[]>`
`transformHtml` is a build hook to transform the content of each page before saving to disk (SSG). `transformHead` is a build hook to transform the head before generating each page. It will allow you to add head entries that cannot be statically added to your VitePress config. You only need to return extra entries, they will be merged automatically with the existing ones.
::: warning ::: warning
Modifying the html content may cause hydration problems in runtime. Don't mutate anything inside the `ctx`.
::: :::
```ts ```ts
import { defineConfig } from 'vitepress' export default {
async transformHead(ctx) {
export default defineConfig({
/* other vitepress options */
async transformHtml(code, id, context) {
} }
}) }
``` ```
```ts ```ts
@ -287,6 +284,23 @@ interface TransformContext {
} }
``` ```
### 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.
::: warning
Don't mutate anything inside the `ctx`. Also, modifying the html content may cause hydration problems in runtime.
:::
```ts
export default {
async transformHtml(code, id, context) {
}
}
```
### buildEnd ### buildEnd
- Type: `(siteConfig: SiteConfig) => Awaitable<void>` - Type: `(siteConfig: SiteConfig) => Awaitable<void>`
@ -294,11 +308,8 @@ interface TransformContext {
`buildEnd` is a build CLI hook, it will run after build (SSG) finish but before VitePress CLI process exits. `buildEnd` is a build CLI hook, it will run after build (SSG) finish but before VitePress CLI process exits.
```ts ```ts
import { defineConfig } from 'vitepress' export default {
export default defineConfig({
/* other vitepress options */
async buildEnd(siteConfig) { async buildEnd(siteConfig) {
} }
}) }
``` ```

@ -106,11 +106,24 @@ export async function renderPage(
const title: string = createTitle(siteData, pageData) const title: string = createTitle(siteData, pageData)
const description: string = pageData.description || siteData.description const description: string = pageData.description || siteData.description
const head = mergeHead( const headBeforeTransform = mergeHead(
siteData.head, siteData.head,
filterOutHeadDescription(pageData.frontmatter.head) filterOutHeadDescription(pageData.frontmatter.head)
) )
const head = mergeHead(
headBeforeTransform,
(await config.transformHead?.({
siteConfig: config,
siteData,
pageData,
title,
description,
head: headBeforeTransform,
content
})) || []
)
let inlinedScript = '' let inlinedScript = ''
if (config.mpa && result) { if (config.mpa && result) {
const matchingChunk = result.output.find( const matchingChunk = result.output.find(

@ -93,6 +93,13 @@ export interface UserConfig<ThemeConfig = any> {
*/ */
buildEnd?: (siteConfig: SiteConfig) => Awaitable<void> buildEnd?: (siteConfig: SiteConfig) => Awaitable<void>
/**
* Head transform hook: runs before writing HTML to dist.
*
* This build hook will allow you to modify the head adding new entries that cannot be statically added.
*/
transformHead?: (ctx: TransformContext) => Awaitable<HeadConfig[]>
/** /**
* HTML transform hook: runs before writing HTML to dist. * HTML transform hook: runs before writing HTML to dist.
*/ */
@ -129,6 +136,7 @@ export interface SiteConfig<ThemeConfig = any>
| 'ignoreDeadLinks' | 'ignoreDeadLinks'
| 'cleanUrls' | 'cleanUrls'
| 'buildEnd' | 'buildEnd'
| 'transformHead'
| 'transformHtml' | 'transformHtml'
> { > {
root: string root: string
@ -215,6 +223,7 @@ export async function resolveConfig(
ignoreDeadLinks: userConfig.ignoreDeadLinks, ignoreDeadLinks: userConfig.ignoreDeadLinks,
cleanUrls: userConfig.cleanUrls || 'disabled', cleanUrls: userConfig.cleanUrls || 'disabled',
buildEnd: userConfig.buildEnd, buildEnd: userConfig.buildEnd,
transformHead: userConfig.transformHead,
transformHtml: userConfig.transformHtml transformHtml: userConfig.transformHtml
} }

Loading…
Cancel
Save