Skip to content

Commit

Permalink
don't mutate stuff and use the returned value
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Sep 12, 2022
1 parent 86d78ae commit ccafa4b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 42 deletions.
36 changes: 11 additions & 25 deletions docs/config/app-configs.md
Expand Up @@ -257,22 +257,19 @@ VitePress build hooks allow you to add new functionality and behaviors to your w

### transformHead

- Type: `( ctx: TransformContext ) => Awaitable<void>`
- Type: `(ctx: TransformContext) => Awaitable<HeadConfig[]>`

`transformHead` is a build hook to transform the head before rendering each page: it will allow you to add head entries that cannot be statically added to your VitePress config module.
`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
The `head` provided in `TransformContext` is a clone, any headers you want to include on all pages will need to be added on each call.
Don't mutate anything inside the `ctx`.
:::

```ts
import { defineConfig } from 'vitepress'

export default defineConfig({
/* other vitepress options */
export default {
async transformHead(ctx) {
}
})
}
```

```ts
Expand All @@ -283,32 +280,24 @@ interface TransformContext {
title: string
description: string
head: HeadConfig[]
content: string
}
```

### transformHtml

- Type: `( code: string, id: string, ctx: HtmlTransformContext ) => Awaitable<string | void>`
- 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).
`transformHtml` is a build hook to transform the content of each page before saving to disk.

::: warning
Modifying the html content may cause hydration problems in runtime.
:::

```ts
import { defineConfig } from 'vitepress'

export default defineConfig({
/* other vitepress options */
export default {
async transformHtml(code, id, context) {
}
})
```

```ts
interface HtmlTransformContext extends TransformContext {
content: string
}
```

Expand All @@ -319,11 +308,8 @@ interface HtmlTransformContext extends TransformContext {
`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 */
export default {
async buildEnd(siteConfig) {
}
})
}
```
23 changes: 11 additions & 12 deletions src/node/build/render.ts
Expand Up @@ -106,23 +106,22 @@ export async function renderPage(
const title: string = createTitle(siteData, pageData)
const description: string = pageData.description || siteData.description

let useHead = siteData.head
if (config.transformHead) {
// make a copy of the head
useHead = Array.from(siteData.head)
await config.transformHead({
const headBeforeTransform = mergeHead(
siteData.head,
filterOutHeadDescription(pageData.frontmatter.head)
)

const head = mergeHead(
headBeforeTransform,
(await config.transformHead?.({
siteConfig: config,
siteData,
pageData,
title,
description,
head: useHead
})
}

const head = mergeHead(
useHead,
filterOutHeadDescription(pageData.frontmatter.head)
head: headBeforeTransform,
content
})) || []
)

let inlinedScript = ''
Expand Down
7 changes: 2 additions & 5 deletions src/node/config.ts
Expand Up @@ -98,15 +98,15 @@ export interface UserConfig<ThemeConfig = any> {
*
* This build hook will allow you to modify the head adding new entries that cannot be statically added.
*/
transformHead?: (ctx: TransformContext) => Awaitable<void>
transformHead?: (ctx: TransformContext) => Awaitable<HeadConfig[]>

/**
* HTML transform hook: runs before writing HTML to dist.
*/
transformHtml?: (
code: string,
id: string,
ctx: HtmlTransformContext
ctx: TransformContext
) => Awaitable<string | void>
}

Expand All @@ -117,9 +117,6 @@ export interface TransformContext {
title: string
description: string
head: HeadConfig[]
}

export interface HtmlTransformContext extends TransformContext {
content: string
}

Expand Down

0 comments on commit ccafa4b

Please sign in to comment.