mirror of https://github.com/vuejs/vitepress
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
723 lines
18 KiB
723 lines
18 KiB
2 years ago
|
---
|
||
|
outline: deep
|
||
|
---
|
||
|
|
||
|
# Site Config
|
||
|
|
||
|
Site config is where you can define the global settings of the site. App config options define settings that apply to every VitePress site, regardless of what theme it is using. For example, the base directory or the title of the site.
|
||
|
|
||
2 years ago
|
## Overview
|
||
|
|
||
|
### Config Resolution
|
||
2 years ago
|
|
||
1 year ago
|
The config file is always resolved from `<root>/.vitepress/config.[ext]`, where `<root>` is your VitePress [project root](../guide/routing#root-and-source-directory), and `[ext]` is one of the supported file extensions. TypeScript is supported out of the box. Supported extensions include `.js`, `.ts`, `.mjs`, and `.mts`.
|
||
3 years ago
|
|
||
2 years ago
|
It is recommended to use ES modules syntax in config files. The config file should default export an object:
|
||
3 years ago
|
|
||
|
```ts
|
||
|
export default {
|
||
2 years ago
|
// app level config options
|
||
3 years ago
|
lang: 'en-US',
|
||
|
title: 'VitePress',
|
||
|
description: 'Vite & Vue powered static site generator.',
|
||
|
...
|
||
|
}
|
||
|
```
|
||
|
|
||
11 months ago
|
:::details Dynamic (Async) Config
|
||
|
|
||
|
If you need to dynamically generate the config, you can also default export a function. For example:
|
||
|
|
||
|
```ts
|
||
|
import { defineConfig } from 'vitepress'
|
||
|
|
||
10 months ago
|
export default async () => {
|
||
10 months ago
|
const posts = await (await fetch('https://my-cms.com/blog-posts')).json()
|
||
11 months ago
|
|
||
10 months ago
|
return defineConfig({
|
||
11 months ago
|
// app level config options
|
||
|
lang: 'en-US',
|
||
|
title: 'VitePress',
|
||
|
description: 'Vite & Vue powered static site generator.',
|
||
|
|
||
|
// theme level config options
|
||
|
themeConfig: {
|
||
|
sidebar: [
|
||
|
...posts.map((post) => ({
|
||
|
text: post.name,
|
||
|
link: `/posts/${post.name}`
|
||
|
}))
|
||
|
]
|
||
|
}
|
||
10 months ago
|
})
|
||
|
}
|
||
11 months ago
|
```
|
||
|
|
||
|
You can also use top-level `await`. For example:
|
||
|
|
||
|
```ts
|
||
|
import { defineConfig } from 'vitepress'
|
||
|
|
||
|
const posts = await (await fetch('https://my-cms.com/blog-posts')).json()
|
||
|
|
||
|
export default defineConfig({
|
||
|
// app level config options
|
||
|
lang: 'en-US',
|
||
|
title: 'VitePress',
|
||
|
description: 'Vite & Vue powered static site generator.',
|
||
|
|
||
|
// theme level config options
|
||
|
themeConfig: {
|
||
|
sidebar: [
|
||
|
...posts.map((post) => ({
|
||
|
text: post.name,
|
||
|
link: `/posts/${post.name}`
|
||
|
}))
|
||
|
]
|
||
|
}
|
||
|
})
|
||
|
```
|
||
|
|
||
|
:::
|
||
|
|
||
2 years ago
|
### Config Intellisense
|
||
2 years ago
|
|
||
2 years ago
|
Using the `defineConfig` helper will provide TypeScript-powered intellisense for config options. Assuming your IDE supports it, this should work in both JavaScript and TypeScript.
|
||
2 years ago
|
|
||
|
```js
|
||
|
import { defineConfig } from 'vitepress'
|
||
|
|
||
|
export default defineConfig({
|
||
|
// ...
|
||
|
})
|
||
|
```
|
||
|
|
||
2 years ago
|
### Typed Theme Config
|
||
2 years ago
|
|
||
2 years ago
|
By default, `defineConfig` helper expects the theme config type from default theme:
|
||
2 years ago
|
|
||
|
```ts
|
||
|
import { defineConfig } from 'vitepress'
|
||
|
|
||
|
export default defineConfig({
|
||
|
themeConfig: {
|
||
|
// Type is `DefaultTheme.Config`
|
||
|
}
|
||
|
})
|
||
|
```
|
||
|
|
||
|
If you use a custom theme and want type checks for the theme config, you'll need to use `defineConfigWithTheme` instead, and pass the config type for your custom theme via a generic argument:
|
||
|
|
||
|
```ts
|
||
|
import { defineConfigWithTheme } from 'vitepress'
|
||
2 years ago
|
import type { ThemeConfig } from 'your-theme'
|
||
2 years ago
|
|
||
|
export default defineConfigWithTheme<ThemeConfig>({
|
||
|
themeConfig: {
|
||
|
// Type is `ThemeConfig`
|
||
|
}
|
||
|
})
|
||
|
```
|
||
|
|
||
2 years ago
|
### Vite, Vue & Markdown Config
|
||
|
|
||
|
- **Vite**
|
||
|
|
||
2 years ago
|
You can configure the underlying Vite instance using the [vite](#vite) option in your VitePress config. No need to create a separate Vite config file.
|
||
2 years ago
|
|
||
|
- **Vue**
|
||
|
|
||
2 years ago
|
VitePress already includes the official Vue plugin for Vite ([@vitejs/plugin-vue](https://github.com/vitejs/vite-plugin-vue)). You can configure its options using the [vue](#vue) option in your VitePress config.
|
||
2 years ago
|
|
||
|
- **Markdown**
|
||
|
|
||
2 years ago
|
You can configure the underlying [Markdown-It](https://github.com/markdown-it/markdown-it) instance using the [markdown](#markdown) option in your VitePress config.
|
||
2 years ago
|
|
||
2 years ago
|
## Site Metadata
|
||
2 years ago
|
|
||
2 years ago
|
### title
|
||
2 years ago
|
|
||
2 years ago
|
- Type: `string`
|
||
|
- Default: `VitePress`
|
||
2 years ago
|
- Can be overridden per page via [frontmatter](./frontmatter-config#title)
|
||
|
|
||
|
Title for the site. When using the default theme, this will be displayed in the nav bar.
|
||
2 years ago
|
|
||
2 years ago
|
It will also be used as the default suffix for all individual page titles, unless [`titleTemplate`](#titletemplate) is defined. An individual page's final title will be the text content of its first `<h1>` header, combined with the global `title` as the suffix. For example with the following config and page content:
|
||
2 years ago
|
|
||
2 years ago
|
```ts
|
||
|
export default {
|
||
2 years ago
|
title: 'My Awesome Site'
|
||
2 years ago
|
}
|
||
|
```
|
||
2 years ago
|
|
||
2 years ago
|
```md
|
||
|
# Hello
|
||
|
```
|
||
|
|
||
|
The title of the page will be `Hello | My Awesome Site`.
|
||
2 years ago
|
|
||
2 years ago
|
### titleTemplate
|
||
|
|
||
|
- Type: `string | boolean`
|
||
2 years ago
|
- Can be overridden per page via [frontmatter](./frontmatter-config#titletemplate)
|
||
2 years ago
|
|
||
2 years ago
|
Allows customizing each page's title suffix or the entire title. For example:
|
||
2 years ago
|
|
||
|
```ts
|
||
|
export default {
|
||
2 years ago
|
title: 'My Awesome Site',
|
||
|
titleTemplate: 'Custom Suffix'
|
||
2 years ago
|
}
|
||
|
```
|
||
2 years ago
|
|
||
2 years ago
|
```md
|
||
|
# Hello
|
||
|
```
|
||
|
|
||
|
The title of the page will be `Hello | Custom Suffix`.
|
||
2 years ago
|
|
||
2 years ago
|
To completely customize how the title should be rendered, you can use the `:title` symbol in `titleTemplate`:
|
||
2 years ago
|
|
||
|
```ts
|
||
|
export default {
|
||
2 years ago
|
titleTemplate: ':title - Custom Suffix'
|
||
2 years ago
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
Here `:title` will be replaced with the text inferred from the page's first `<h1>` header. The title of the previous example page will be `Hello - Custom Suffix`.
|
||
|
|
||
|
The option can be set to `false` to disable title suffixes.
|
||
|
|
||
2 years ago
|
### description
|
||
|
|
||
|
- Type: `string`
|
||
|
- Default: `A VitePress site`
|
||
2 years ago
|
- Can be overridden per page via [frontmatter](./frontmatter-config#description)
|
||
2 years ago
|
|
||
|
Description for the site. This will render as a `<meta>` tag in the page HTML.
|
||
|
|
||
|
```ts
|
||
|
export default {
|
||
|
description: 'A VitePress site'
|
||
2 years ago
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
### head
|
||
3 years ago
|
|
||
2 years ago
|
- Type: `HeadConfig[]`
|
||
|
- Default: `[]`
|
||
|
- Can be appended per page via [frontmatter](./frontmatter-config#head)
|
||
3 years ago
|
|
||
2 years ago
|
Additional elements to render in the `<head>` tag in the page HTML. The user-added tags are rendered before the closing `head` tag, after VitePress tags.
|
||
3 years ago
|
|
||
1 year ago
|
```ts
|
||
|
type HeadConfig =
|
||
|
| [string, Record<string, string>]
|
||
|
| [string, Record<string, string>, string]
|
||
|
```
|
||
|
|
||
|
#### Example: Adding a favicon
|
||
|
|
||
|
```ts
|
||
|
export default {
|
||
|
head: [['link', { rel: 'icon', href: '/favicon.ico' }]]
|
||
|
} // put favicon.ico in public directory, if base is set, use /base/favicon.ico
|
||
|
|
||
|
/* Would render:
|
||
|
<link rel="icon" href="/favicon.ico">
|
||
|
*/
|
||
|
```
|
||
|
|
||
|
#### Example: Adding Google Fonts
|
||
|
|
||
3 years ago
|
```ts
|
||
|
export default {
|
||
2 years ago
|
head: [
|
||
1 year ago
|
[
|
||
|
'link',
|
||
|
{ rel: 'preconnect', href: 'https://fonts.googleapis.com' }
|
||
|
],
|
||
2 years ago
|
[
|
||
|
'link',
|
||
|
{ rel: 'preconnect', href: 'https://fonts.gstatic.com', crossorigin: '' }
|
||
2 years ago
|
],
|
||
1 year ago
|
[
|
||
|
'link',
|
||
|
{ href: 'https://fonts.googleapis.com/css2?family=Roboto&display=swap', rel: 'stylesheet' }
|
||
|
]
|
||
|
]
|
||
|
}
|
||
2 years ago
|
|
||
1 year ago
|
/* Would render:
|
||
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||
|
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
|
||
|
*/
|
||
|
```
|
||
|
|
||
|
#### Example: Registering a service worker
|
||
|
|
||
|
```ts
|
||
|
export default {
|
||
|
head: [
|
||
2 years ago
|
[
|
||
|
'script',
|
||
|
{ id: 'register-sw' },
|
||
|
`;(() => {
|
||
|
if ('serviceWorker' in navigator) {
|
||
|
navigator.serviceWorker.register('/sw.js')
|
||
|
}
|
||
|
})()`
|
||
2 years ago
|
]
|
||
|
]
|
||
3 years ago
|
}
|
||
1 year ago
|
|
||
|
/* Would render:
|
||
|
<script id="register-sw">
|
||
|
;(() => {
|
||
|
if ('serviceWorker' in navigator) {
|
||
|
navigator.serviceWorker.register('/sw.js')
|
||
|
}
|
||
|
})()
|
||
|
</script>
|
||
|
*/
|
||
3 years ago
|
```
|
||
|
|
||
1 year ago
|
#### Example: Using Google Analytics
|
||
|
|
||
2 years ago
|
```ts
|
||
1 year ago
|
export default {
|
||
|
head: [
|
||
|
[
|
||
|
'script',
|
||
|
{ async: '', src: 'https://www.googletagmanager.com/gtag/js?id=TAG_ID' }
|
||
|
],
|
||
|
[
|
||
|
'script',
|
||
|
{},
|
||
|
`window.dataLayer = window.dataLayer || [];
|
||
|
function gtag(){dataLayer.push(arguments);}
|
||
|
gtag('js', new Date());
|
||
|
gtag('config', 'TAG_ID');`
|
||
|
]
|
||
|
]
|
||
|
}
|
||
|
|
||
|
/* Would render:
|
||
|
<script async src="https://www.googletagmanager.com/gtag/js?id=TAG_ID"></script>
|
||
|
<script>
|
||
|
window.dataLayer = window.dataLayer || [];
|
||
|
function gtag(){dataLayer.push(arguments);}
|
||
|
gtag('js', new Date());
|
||
|
gtag('config', 'TAG_ID');
|
||
|
</script>
|
||
|
*/
|
||
2 years ago
|
```
|
||
|
|
||
2 years ago
|
### lang
|
||
3 years ago
|
|
||
|
- Type: `string`
|
||
2 years ago
|
- Default: `en-US`
|
||
3 years ago
|
|
||
2 years ago
|
The lang attribute for the site. This will render as a `<html lang="en-US">` tag in the page HTML.
|
||
3 years ago
|
|
||
|
```ts
|
||
|
export default {
|
||
2 years ago
|
lang: 'en-US'
|
||
3 years ago
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
### base
|
||
2 years ago
|
|
||
2 years ago
|
- Type: `string`
|
||
|
- Default: `/`
|
||
2 years ago
|
|
||
2 years ago
|
The base URL the site will be deployed at. You will need to set this if you plan to deploy your site under a sub path, for example, GitHub pages. If you plan to deploy your site to `https://foo.github.io/bar/`, then you should set base to `'/bar/'`. It should always start and end with a slash.
|
||
|
|
||
|
The base is automatically prepended to all the URLs that start with / in other options, so you only need to specify it once.
|
||
2 years ago
|
|
||
|
```ts
|
||
|
export default {
|
||
2 years ago
|
base: '/base/'
|
||
2 years ago
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
## Routing
|
||
3 years ago
|
|
||
2 years ago
|
### cleanUrls
|
||
|
|
||
|
- Type: `boolean`
|
||
2 years ago
|
- Default: `false`
|
||
3 years ago
|
|
||
2 years ago
|
When set to `true`, VitePress will remove the trailing `.html` from URLs. Also see [Generating Clean URL](../guide/routing#generating-clean-url).
|
||
3 years ago
|
|
||
2 years ago
|
::: warning Server Support Required
|
||
|
Enabling this may require additional configuration on your hosting platform. For it to work, your server must be able to serve `/foo.html` when visiting `/foo` **without a redirect**.
|
||
2 years ago
|
:::
|
||
3 years ago
|
|
||
2 years ago
|
### rewrites
|
||
3 years ago
|
|
||
2 years ago
|
- Type: `Record<string, string>`
|
||
|
|
||
1 year ago
|
Defines custom directory <-> URL mappings. See [Routing: Route Rewrites](../guide/routing#route-rewrites) for more details.
|
||
3 years ago
|
|
||
|
```ts
|
||
|
export default {
|
||
2 years ago
|
rewrites: {
|
||
|
'source/:page': 'destination/:page'
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
## Build
|
||
2 years ago
|
|
||
2 years ago
|
### srcDir
|
||
2 years ago
|
|
||
2 years ago
|
- Type: `string`
|
||
|
- Default: `.`
|
||
2 years ago
|
|
||
2 years ago
|
The directory where your markdown pages are stored, relative to project root. Also see [Root and Source Directory](../guide/routing#root-and-source-directory).
|
||
2 years ago
|
|
||
2 years ago
|
```ts
|
||
|
export default {
|
||
|
srcDir: './src'
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### srcExclude
|
||
2 years ago
|
|
||
2 years ago
|
- Type: `string`
|
||
|
- Default: `undefined`
|
||
|
|
||
|
A [glob pattern](https://github.com/mrmlnc/fast-glob#pattern-syntax) for matching markdown files that should be excluded as source content.
|
||
2 years ago
|
|
||
|
```ts
|
||
|
export default {
|
||
2 years ago
|
srcExclude: ['**/README.md', '**/TODO.md']
|
||
3 years ago
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
### outDir
|
||
3 years ago
|
|
||
2 years ago
|
- Type: `string`
|
||
|
- Default: `./.vitepress/dist`
|
||
|
|
||
2 years ago
|
The build output location for the site, relative to [project root](../guide/routing#root-and-source-directory).
|
||
2 years ago
|
|
||
|
```ts
|
||
|
export default {
|
||
|
outDir: '../public'
|
||
|
}
|
||
|
```
|
||
|
|
||
1 year ago
|
### assetsDir
|
||
|
|
||
|
- Type: `string`
|
||
|
- Default: `assets`
|
||
|
|
||
1 year ago
|
Specify the directory to nest generated assets under. The path should be inside [`outDir`](#outdir) and is resolved relative to it.
|
||
1 year ago
|
|
||
|
```ts
|
||
|
export default {
|
||
|
assetsDir: 'static'
|
||
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
### cacheDir
|
||
|
|
||
|
- Type: `string`
|
||
|
- Default: `./.vitepress/cache`
|
||
|
|
||
2 years ago
|
The directory for cache files, relative to [project root](../guide/routing#root-and-source-directory). See also: [cacheDir](https://vitejs.dev/config/shared-options.html#cachedir).
|
||
2 years ago
|
|
||
|
```ts
|
||
|
export default {
|
||
|
cacheDir: './.vitepress/.vite'
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### ignoreDeadLinks
|
||
|
|
||
2 years ago
|
- Type: `boolean | 'localhostLinks' | (string | RegExp | ((link: string) => boolean))[]`
|
||
2 years ago
|
- Default: `false`
|
||
3 years ago
|
|
||
2 years ago
|
When set to `true`, VitePress will not fail builds due to dead links.
|
||
|
|
||
|
When set to `'localhostLinks'`, the build will fail on dead links, but won't check `localhost` links.
|
||
3 years ago
|
|
||
|
```ts
|
||
|
export default {
|
||
2 years ago
|
ignoreDeadLinks: true
|
||
3 years ago
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
It can also be an array of exact url string, regex patterns, or custom filter functions.
|
||
2 years ago
|
|
||
|
```ts
|
||
|
export default {
|
||
|
ignoreDeadLinks: [
|
||
|
// ignore exact url "/playground"
|
||
|
'/playground',
|
||
|
// ignore all localhost links
|
||
|
/^https?:\/\/localhost/,
|
||
|
// ignore all links include "/repl/""
|
||
|
/\/repl\//,
|
||
|
// custom function, ignore all links include "ignore"
|
||
|
(url) => {
|
||
|
return url.toLowerCase().includes('ignore')
|
||
|
}
|
||
|
]
|
||
|
}
|
||
|
```
|
||
|
|
||
8 months ago
|
### metaChunk <Badge type="warning" text="experimental" />
|
||
|
|
||
|
- Type: `boolean`
|
||
|
- Default: `false`
|
||
|
|
||
|
When set to `true`, extract pages metadata to a separate JavaScript chunk instead of inlining it in the initial HTML. This makes each page's HTML payload smaller and makes the pages metadata cacheable, thus reducing server bandwidth when you have many pages in the site.
|
||
|
|
||
2 years ago
|
### mpa <Badge type="warning" text="experimental" />
|
||
|
|
||
|
- Type: `boolean`
|
||
|
- Default: `false`
|
||
|
|
||
2 years ago
|
When set to `true`, the production app will be built in [MPA Mode](../guide/mpa-mode). MPA mode ships 0kb JavaScript by default, at the cost of disabling client-side navigation and requires explicit opt-in for interactivity.
|
||
2 years ago
|
|
||
2 years ago
|
## Theming
|
||
|
|
||
|
### appearance
|
||
|
|
||
5 months ago
|
- Type: `boolean | 'dark' | 'force-dark' | 'force-auto' | import('@vueuse/core').UseDarkOptions`
|
||
2 years ago
|
- Default: `true`
|
||
|
|
||
|
Whether to enable dark mode (by adding the `.dark` class to the `<html>` element).
|
||
|
|
||
|
- If the option is set to `true`, the default theme will be determined by the user's preferred color scheme.
|
||
|
- If the option is set to `dark`, the theme will be dark by default, unless the user manually toggles it.
|
||
|
- If the option is set to `false`, users will not be able to toggle the theme.
|
||
5 months ago
|
- If the option is set to `'force-dark'`, the theme will always be dark and users will not be able to toggle it.
|
||
|
- If the option is set to `'force-auto'`, the theme will always be determined by the user's preferred color scheme and users will not be able to toggle it.
|
||
2 years ago
|
|
||
|
This option injects an inline script that restores users settings from local storage using the `vitepress-theme-appearance` key. This ensures the `.dark` class is applied before the page is rendered to avoid flickering.
|
||
|
|
||
1 year ago
|
`appearance.initialValue` can only be `'dark' | undefined`. Refs or getters are not supported.
|
||
|
|
||
2 years ago
|
### lastUpdated
|
||
|
|
||
|
- Type: `boolean`
|
||
|
- Default: `false`
|
||
|
|
||
2 years ago
|
Whether to get the last updated timestamp for each page using Git. The timestamp will be included in each page's page data, accessible via [`useData`](./runtime-api#usedata).
|
||
2 years ago
|
|
||
|
When using the default theme, enabling this option will display each page's last updated time. You can customize the text via [`themeConfig.lastUpdatedText`](./default-theme-config#lastupdatedtext) option.
|
||
|
|
||
2 years ago
|
## Customization
|
||
|
|
||
2 years ago
|
### markdown
|
||
3 years ago
|
|
||
|
- Type: `MarkdownOption`
|
||
|
|
||
10 months ago
|
Configure Markdown parser options. VitePress uses [Markdown-it](https://github.com/markdown-it/markdown-it) as the parser, and [Shiki](https://github.com/shikijs/shiki) to highlight language syntax. Inside this option, you may pass various Markdown related options to fit your needs.
|
||
3 years ago
|
|
||
|
```js
|
||
|
export default {
|
||
1 year ago
|
markdown: {...}
|
||
3 years ago
|
}
|
||
|
```
|
||
|
|
||
12 months ago
|
Check the [type declaration and jsdocs](https://github.com/vuejs/vitepress/blob/main/src/node/markdown/markdown.ts) for all the options available.
|
||
3 years ago
|
|
||
2 years ago
|
### vite
|
||
2 years ago
|
|
||
2 years ago
|
- Type: `import('vite').UserConfig`
|
||
2 years ago
|
|
||
2 years ago
|
Pass raw [Vite Config](https://vitejs.dev/config/) to internal Vite dev server / bundler.
|
||
2 years ago
|
|
||
2 years ago
|
```js
|
||
|
export default {
|
||
|
vite: {
|
||
|
// Vite config options
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
### vue
|
||
2 years ago
|
|
||
2 years ago
|
- Type: `import('@vitejs/plugin-vue').Options`
|
||
2 years ago
|
|
||
2 years ago
|
Pass raw [`@vitejs/plugin-vue` options](https://github.com/vitejs/vite-plugin-vue/tree/main/packages/plugin-vue#options) to the internal plugin instance.
|
||
2 years ago
|
|
||
2 years ago
|
```js
|
||
|
export default {
|
||
|
vue: {
|
||
|
// @vitejs/plugin-vue options
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
## Build Hooks
|
||
2 years ago
|
|
||
|
VitePress build hooks allow you to add new functionality and behaviors to your website:
|
||
|
|
||
|
- Sitemap
|
||
|
- Search Indexing
|
||
|
- PWA
|
||
2 years ago
|
- Teleports
|
||
|
|
||
2 years ago
|
### buildEnd
|
||
2 years ago
|
|
||
|
- 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
|
||
|
export default {
|
||
|
async buildEnd(siteConfig) {
|
||
|
// ...
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
### postRender
|
||
2 years ago
|
|
||
|
- Type: `(context: SSGContext) => Awaitable<SSGContext | void>`
|
||
|
|
||
|
`postRender` is a build hook, called when SSG rendering is done. It will allow you to handle the teleports content during SSG.
|
||
|
|
||
|
```ts
|
||
|
export default {
|
||
|
async postRender(context) {
|
||
|
// ...
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
```ts
|
||
|
interface SSGContext {
|
||
|
content: string
|
||
|
teleports?: Record<string, string>
|
||
|
[key: string]: any
|
||
|
}
|
||
|
```
|
||
2 years ago
|
|
||
2 years ago
|
### transformHead
|
||
2 years ago
|
|
||
2 years ago
|
- Type: `(context: TransformContext) => Awaitable<HeadConfig[]>`
|
||
2 years ago
|
|
||
2 years ago
|
`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.
|
||
2 years ago
|
|
||
|
::: warning
|
||
1 year ago
|
Don't mutate anything inside the `context`.
|
||
2 years ago
|
:::
|
||
|
|
||
|
```ts
|
||
2 years ago
|
export default {
|
||
2 years ago
|
async transformHead(context) {
|
||
2 years ago
|
// ...
|
||
2 years ago
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
```
|
||
|
|
||
|
```ts
|
||
|
interface TransformContext {
|
||
2 years ago
|
page: string // e.g. index.md (relative to srcDir)
|
||
|
assets: string[] // all non-js/css assets as fully resolved public URL
|
||
2 years ago
|
siteConfig: SiteConfig
|
||
|
siteData: SiteData
|
||
|
pageData: PageData
|
||
|
title: string
|
||
|
description: string
|
||
|
head: HeadConfig[]
|
||
|
content: string
|
||
|
}
|
||
|
```
|
||
|
|
||
1 year ago
|
Note that this hook is only called when generating the site statically. It is not called during dev. If you need to add dynamic head entries during dev, you can use the [`transformPageData`](#transformpagedata) hook instead:
|
||
|
|
||
|
```ts
|
||
|
export default {
|
||
|
transformPageData(pageData) {
|
||
|
pageData.frontmatter.head ??= []
|
||
|
pageData.frontmatter.head.push([
|
||
|
'meta',
|
||
|
{
|
||
|
name: 'og:title',
|
||
|
content:
|
||
|
pageData.frontmatter.layout === 'home'
|
||
|
? `VitePress`
|
||
|
: `${pageData.title} | VitePress`
|
||
|
}
|
||
|
])
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
10 months ago
|
#### Example: Adding a canonical URL `<link>`
|
||
10 months ago
|
|
||
|
```ts
|
||
|
export default {
|
||
10 months ago
|
transformPageData(pageData) {
|
||
|
const canonicalUrl = `https://example.com/${pageData.relativePath}`
|
||
|
.replace(/index\.md$/, '')
|
||
|
.replace(/\.md$/, '.html')
|
||
|
|
||
|
pageData.frontmatter.head ??= []
|
||
|
pageData.frontmatter.head.push([
|
||
|
'link',
|
||
|
{ rel: 'canonical', href: canonicalUrl }
|
||
|
])
|
||
10 months ago
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
### transformHtml
|
||
2 years ago
|
|
||
1 year ago
|
- Type: `(code: string, id: string, context: TransformContext) => Awaitable<string | void>`
|
||
2 years ago
|
|
||
|
`transformHtml` is a build hook to transform the content of each page before saving to disk.
|
||
|
|
||
|
::: warning
|
||
1 year ago
|
Don't mutate anything inside the `context`. Also, modifying the html content may cause hydration problems in runtime.
|
||
2 years ago
|
:::
|
||
|
|
||
|
```ts
|
||
|
export default {
|
||
|
async transformHtml(code, id, context) {
|
||
2 years ago
|
// ...
|
||
2 years ago
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
### transformPageData
|
||
2 years ago
|
|
||
1 year ago
|
- Type: `(pageData: PageData, context: TransformPageContext) => Awaitable<Partial<PageData> | { [key: string]: any } | void>`
|
||
2 years ago
|
|
||
1 year ago
|
`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 the page data.
|
||
2 years ago
|
|
||
2 years ago
|
::: warning
|
||
1 year ago
|
Don't mutate anything inside the `context` and be careful that this might impact the performance of dev server, especially if you have some network requests or heavy computations (like generating images) in the hook. You can check for `process.env.NODE_ENV === 'production'` for conditional logic.
|
||
2 years ago
|
:::
|
||
|
|
||
2 years ago
|
```ts
|
||
|
export default {
|
||
2 years ago
|
async transformPageData(pageData, { siteConfig }) {
|
||
2 years ago
|
pageData.contributors = await getPageContributors(pageData.relativePath)
|
||
|
}
|
||
|
|
||
|
// or return data to be merged
|
||
2 years ago
|
async transformPageData(pageData, { siteConfig }) {
|
||
2 years ago
|
return {
|
||
|
contributors: await getPageContributors(pageData.relativePath)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
```
|
||
2 years ago
|
|
||
|
```ts
|
||
|
interface TransformPageContext {
|
||
|
siteConfig: SiteConfig
|
||
|
}
|
||
|
```
|