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.
1194 lines
27 KiB
1194 lines
27 KiB
---
|
|
description: VitePress built-in Markdown extensions including custom containers, code blocks with syntax highlighting, line highlighting, code groups, and more.
|
|
outline: deep
|
|
---
|
|
|
|
# Markdown Extensions
|
|
|
|
VitePress comes with built in Markdown Extensions.
|
|
|
|
## Header Anchors
|
|
|
|
Headers automatically get anchor links applied. Rendering of anchors can be configured using the `markdown.anchor` option.
|
|
|
|
### Custom anchors
|
|
|
|
To specify a custom anchor tag for a heading instead of using the auto-generated one, add a suffix to the heading:
|
|
|
|
```
|
|
# Using custom anchors {#my-anchor}
|
|
```
|
|
|
|
This allows you to link to the heading as `#my-anchor` instead of the default `#using-custom-anchors`.
|
|
|
|
## Links
|
|
|
|
Both internal and external links get special treatment.
|
|
|
|
### Internal Links
|
|
|
|
Internal links are converted to router links for SPA navigation. Also, every `index.md` contained in each sub-directory will automatically be converted to `index.html`, with corresponding URL `/`.
|
|
|
|
For example, given the following directory structure:
|
|
|
|
```
|
|
.
|
|
├─ index.md
|
|
├─ foo
|
|
│ ├─ index.md
|
|
│ ├─ one.md
|
|
│ └─ two.md
|
|
└─ bar
|
|
├─ index.md
|
|
├─ three.md
|
|
└─ four.md
|
|
```
|
|
|
|
And providing you are in `foo/one.md`:
|
|
|
|
```md
|
|
[Home](/) <!-- sends the user to the root index.md -->
|
|
[foo](/foo/) <!-- sends the user to index.html of directory foo -->
|
|
[foo heading](./#heading) <!-- anchors user to a heading in the foo index file -->
|
|
[bar - three](../bar/three) <!-- you can omit extension -->
|
|
[bar - three](../bar/three.md) <!-- you can append .md -->
|
|
[bar - four](../bar/four.html) <!-- or you can append .html -->
|
|
```
|
|
|
|
### Page Suffix
|
|
|
|
Pages and internal links get generated with the `.html` suffix by default.
|
|
|
|
### External Links
|
|
|
|
Outbound links automatically get `target="_blank" rel="noreferrer"`:
|
|
|
|
- [vuejs.org](https://vuejs.org)
|
|
- [VitePress on GitHub](https://github.com/vuejs/vitepress)
|
|
|
|
## Frontmatter
|
|
|
|
[YAML frontmatter](https://jekyllrb.com/docs/front-matter/) is supported out of the box:
|
|
|
|
```yaml
|
|
---
|
|
title: Blogging Like a Hacker
|
|
lang: en-US
|
|
---
|
|
```
|
|
|
|
This data will be available to the rest of the page, along with all custom and theming components.
|
|
|
|
For more details, see [Frontmatter](../reference/frontmatter-config).
|
|
|
|
## GitHub-Style Tables
|
|
|
|
**Input**
|
|
|
|
```md
|
|
| Tables | Are | Cool |
|
|
| ------------- | :-----------: | ----: |
|
|
| col 3 is | right-aligned | $1600 |
|
|
| col 2 is | centered | $12 |
|
|
| zebra stripes | are neat | $1 |
|
|
```
|
|
|
|
**Output**
|
|
|
|
| Tables | Are | Cool |
|
|
| ------------- | :-----------: | -----: |
|
|
| col 3 is | right-aligned | \$1600 |
|
|
| col 2 is | centered | \$12 |
|
|
| zebra stripes | are neat | \$1 |
|
|
|
|
## Task Lists
|
|
|
|
**Input**
|
|
|
|
```md
|
|
- [ ] Write the press release
|
|
- [x] Update the website
|
|
```
|
|
|
|
**Output**
|
|
|
|
- [ ] Write the press release
|
|
- [x] Update the website
|
|
|
|
## Footnotes
|
|
|
|
**Input**
|
|
|
|
```md
|
|
Footnotes are supported[^1], including inline ones^[This is an inline footnote.].
|
|
|
|
[^1]: Definitions can contain **markdown** and are rendered at the end of the page.
|
|
```
|
|
|
|
**Output**
|
|
|
|
Footnotes are supported[^1], including inline ones^[This is an inline footnote.].
|
|
|
|
[^1]: Definitions can contain **markdown** and are rendered at the end of the page.
|
|
|
|
## Emoji :tada:
|
|
|
|
**Input**
|
|
|
|
```
|
|
:tada: :100:
|
|
```
|
|
|
|
**Output**
|
|
|
|
:tada: :100:
|
|
|
|
A [list of all emojis](https://github.com/mdit-plugins/mdit-plugins/blob/main/packages/plugin-emoji/src/data/full.ts) is available.
|
|
|
|
## Table of Contents
|
|
|
|
**Input**
|
|
|
|
```
|
|
[[toc]]
|
|
```
|
|
|
|
**Output**
|
|
|
|
[[toc]]
|
|
|
|
Rendering of the TOC can be configured using the `markdown.toc` option.
|
|
|
|
## Custom Containers
|
|
|
|
Custom containers can be defined by their types, titles, and contents.
|
|
|
|
### Default Title
|
|
|
|
**Input**
|
|
|
|
```md
|
|
::: info
|
|
This is an info box.
|
|
:::
|
|
|
|
::: tip
|
|
This is a tip.
|
|
:::
|
|
|
|
::: warning
|
|
This is a warning.
|
|
:::
|
|
|
|
::: danger
|
|
This is a dangerous warning.
|
|
:::
|
|
|
|
::: details
|
|
This is a details block.
|
|
:::
|
|
```
|
|
|
|
**Output**
|
|
|
|
::: info
|
|
This is an info box.
|
|
:::
|
|
|
|
::: tip
|
|
This is a tip.
|
|
:::
|
|
|
|
::: warning
|
|
This is a warning.
|
|
:::
|
|
|
|
::: danger
|
|
This is a dangerous warning.
|
|
:::
|
|
|
|
::: details
|
|
This is a details block.
|
|
:::
|
|
|
|
### Custom Title
|
|
|
|
You may set custom title by appending the text right after the "type" of the container.
|
|
|
|
**Input**
|
|
|
|
````md
|
|
::: danger STOP
|
|
Danger zone, do not proceed
|
|
:::
|
|
|
|
::: details Click me to toggle the code
|
|
```js
|
|
console.log('Hello, VitePress!')
|
|
```
|
|
:::
|
|
````
|
|
|
|
**Output**
|
|
|
|
::: danger STOP
|
|
Danger zone, do not proceed
|
|
:::
|
|
|
|
::: details Click me to toggle the code
|
|
```js
|
|
console.log('Hello, VitePress!')
|
|
```
|
|
:::
|
|
|
|
Also, you may set custom titles globally by adding the following content in site config, helpful if not writing in English:
|
|
|
|
```ts
|
|
// config.ts
|
|
export default defineConfig({
|
|
// ...
|
|
markdown: {
|
|
container: {
|
|
tipLabel: '提示',
|
|
warningLabel: '警告',
|
|
dangerLabel: '危险',
|
|
infoLabel: '信息',
|
|
detailsLabel: '详细信息'
|
|
}
|
|
}
|
|
// ...
|
|
})
|
|
```
|
|
|
|
On multilingual sites, these labels can also be overridden per locale - see [Per-locale Markdown Strings](./i18n#per-locale-markdown-strings).
|
|
|
|
### Registering New Containers
|
|
|
|
Beyond the built-in types, you can register additional containers by mapping their names to their default titles:
|
|
|
|
```ts
|
|
// config.ts
|
|
export default defineConfig({
|
|
// ...
|
|
markdown: {
|
|
container: {
|
|
customContainers: {
|
|
success: 'SUCCESS'
|
|
}
|
|
}
|
|
}
|
|
// ...
|
|
})
|
|
```
|
|
|
|
Registered names work like the built-in ones - including custom titles, attributes, and the [GitHub-style alert syntax](#github-flavored-alerts):
|
|
|
|
```md
|
|
::: success
|
|
You have completed the walkthrough!
|
|
:::
|
|
|
|
> [!SUCCESS] Custom title
|
|
> This renders the same way.
|
|
```
|
|
|
|
New containers ship without any styling, so add some in your theme using the container name as the class. For this example, the default theme's palette already provides fitting colors:
|
|
|
|
```css
|
|
/* .vitepress/theme/custom.css */
|
|
.custom-block.success {
|
|
border-color: transparent;
|
|
color: var(--vp-c-text-1);
|
|
background-color: var(--vp-c-success-soft);
|
|
}
|
|
```
|
|
|
|
### Nesting
|
|
|
|
The `:::` markers follow the same rules as fenced code blocks (` ``` `): a fence is only closed by a matching fence that is **at least as long** as the one that opened it. To nest containers (or to mix them with [code groups](#code-groups)) make the outer fence longer than the ones inside it.
|
|
|
|
**Input**
|
|
|
|
`````md
|
|
:::: info Outer container
|
|
This box contains another container.
|
|
|
|
::: details Inner container
|
|
```js
|
|
console.log('Hello, VitePress!')
|
|
```
|
|
:::
|
|
::::
|
|
`````
|
|
|
|
**Output**
|
|
|
|
:::: info Outer container
|
|
This box contains another container.
|
|
|
|
::: details Inner container
|
|
```js
|
|
console.log('Hello, VitePress!')
|
|
```
|
|
:::
|
|
::::
|
|
|
|
### Additional Attributes
|
|
|
|
You can add additional attributes to the custom containers. We use [@mdit/plugin-attrs](https://mdit-plugins.github.io/attrs.html) for this feature, and it is supported on almost all markdown elements. For example, you can set the `open` attribute to make the details block open by default:
|
|
|
|
**Input**
|
|
|
|
````md
|
|
::: details Click me to toggle the code {open}
|
|
```js
|
|
console.log('Hello, VitePress!')
|
|
```
|
|
:::
|
|
````
|
|
|
|
**Output**
|
|
|
|
::: details Click me to toggle the code {open}
|
|
```js
|
|
console.log('Hello, VitePress!')
|
|
```
|
|
:::
|
|
|
|
The special `no-title` attribute renders a container without a title element (it has no effect on `details`, which always needs its summary):
|
|
|
|
**Input**
|
|
|
|
```md
|
|
::: tip {no-title}
|
|
Just want to try it out? Skip to the [Quickstart](./getting-started).
|
|
:::
|
|
```
|
|
|
|
**Output**
|
|
|
|
::: tip {no-title}
|
|
Just want to try it out? Skip to the [Quickstart](./getting-started).
|
|
:::
|
|
|
|
### `raw`
|
|
|
|
This is a special container that can be used to prevent style and router conflicts with VitePress. This is especially useful when you're documenting component libraries.
|
|
|
|
**Syntax**
|
|
|
|
```md
|
|
::: raw
|
|
Wraps in a `<div class="vp-raw">`
|
|
:::
|
|
```
|
|
|
|
`vp-raw` class can be directly used on elements too. Style isolation is currently opt-in:
|
|
|
|
- Install `postcss` with your preferred package manager:
|
|
|
|
```sh
|
|
$ npm add -D postcss
|
|
```
|
|
|
|
- Create a file named `docs/postcss.config.mjs` and add this to it:
|
|
|
|
```js
|
|
import { postcssIsolateStyles } from 'vitepress'
|
|
|
|
export default {
|
|
plugins: [postcssIsolateStyles()]
|
|
}
|
|
```
|
|
|
|
You can pass its options like this:
|
|
|
|
```js
|
|
postcssIsolateStyles({
|
|
includeFiles: [/custom\.css/] // defaults to [/vp-doc\.css/, /base\.css/]
|
|
})
|
|
```
|
|
|
|
## GitHub-flavored Alerts
|
|
|
|
VitePress also supports [GitHub-flavored alerts](https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alerts) to render as callouts. They will be rendered the same as the [custom containers](#custom-containers). Unlike on GitHub, text placed right after the marker becomes the title of the alert (`> [!NOTE] Custom Title`), and [containers you registered yourself](#registering-new-containers) work here too.
|
|
|
|
```md
|
|
> [!NOTE]
|
|
> Highlights information that users should take into account, even when skimming.
|
|
|
|
> [!TIP]
|
|
> Optional information to help a user be more successful.
|
|
|
|
> [!IMPORTANT]
|
|
> Crucial information necessary for users to succeed.
|
|
|
|
> [!WARNING]
|
|
> Critical content demanding immediate user attention due to potential risks.
|
|
|
|
> [!CAUTION]
|
|
> Negative potential consequences of an action.
|
|
```
|
|
|
|
> [!NOTE]
|
|
> Highlights information that users should take into account, even when skimming.
|
|
|
|
> [!TIP]
|
|
> Optional information to help a user be more successful.
|
|
|
|
> [!IMPORTANT]
|
|
> Crucial information necessary for users to succeed.
|
|
|
|
> [!WARNING]
|
|
> Critical content demanding immediate user attention due to potential risks.
|
|
|
|
> [!CAUTION]
|
|
> Negative potential consequences of an action.
|
|
|
|
## Syntax Highlighting in Code Blocks
|
|
|
|
VitePress uses [Shiki](https://github.com/shikijs/shiki) to highlight language syntax in Markdown code blocks, using coloured text. Shiki supports a wide variety of programming languages. All you need to do is append a valid language alias to the beginning backticks for the code block:
|
|
|
|
**Input**
|
|
|
|
````
|
|
```js
|
|
export default {
|
|
name: 'MyComponent',
|
|
// ...
|
|
}
|
|
```
|
|
````
|
|
|
|
````
|
|
```html
|
|
<ul>
|
|
<li v-for="todo in todos" :key="todo.id">
|
|
{{ todo.text }}
|
|
</li>
|
|
</ul>
|
|
```
|
|
````
|
|
|
|
**Output**
|
|
|
|
```js
|
|
export default {
|
|
name: 'MyComponent'
|
|
// ...
|
|
}
|
|
```
|
|
|
|
```html
|
|
<ul>
|
|
<li v-for="todo in todos" :key="todo.id">
|
|
{{ todo.text }}
|
|
</li>
|
|
</ul>
|
|
```
|
|
|
|
A [list of valid languages](https://shiki.style/languages) is available on Shiki's repository.
|
|
|
|
You may also customize syntax highlight theme, configure language aliases, and set custom language labels in app config. Please see [`markdown` options](../reference/site-config#markdown) for more details.
|
|
|
|
## Line Highlighting in Code Blocks
|
|
|
|
**Input**
|
|
|
|
````
|
|
```js{4}
|
|
export default {
|
|
data () {
|
|
return {
|
|
msg: 'Highlighted!'
|
|
}
|
|
}
|
|
}
|
|
```
|
|
````
|
|
|
|
**Output**
|
|
|
|
```js{4}
|
|
export default {
|
|
data () {
|
|
return {
|
|
msg: 'Highlighted!'
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
In addition to a single line, you can also specify multiple single lines, ranges, or both:
|
|
|
|
- Line ranges: for example `{5-8}`, `{3-10}`, `{10-17}`
|
|
- Multiple single lines: for example `{4,7,9}`
|
|
- Line ranges and single lines: for example `{4,7-13,16,23-27,40}`
|
|
|
|
**Input**
|
|
|
|
````
|
|
```js{1,4,6-8}
|
|
export default { // Highlighted
|
|
data () {
|
|
return {
|
|
msg: `Highlighted!
|
|
This line isn't highlighted,
|
|
but this and the next 2 are.`,
|
|
motd: 'VitePress is awesome',
|
|
lorem: 'ipsum'
|
|
}
|
|
}
|
|
}
|
|
```
|
|
````
|
|
|
|
**Output**
|
|
|
|
```js{1,4,6-8}
|
|
export default { // Highlighted
|
|
data () {
|
|
return {
|
|
msg: `Highlighted!
|
|
This line isn't highlighted,
|
|
but this and the next 2 are.`,
|
|
motd: 'VitePress is awesome',
|
|
lorem: 'ipsum',
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
Alternatively, it's possible to highlight directly in the line by using the `// [!code highlight]` comment.
|
|
|
|
**Input**
|
|
|
|
````
|
|
```js
|
|
export default {
|
|
data () {
|
|
return {
|
|
msg: 'Highlighted!' // [!!code highlight]
|
|
}
|
|
}
|
|
}
|
|
```
|
|
````
|
|
|
|
**Output**
|
|
|
|
```js
|
|
export default {
|
|
data() {
|
|
return {
|
|
msg: 'Highlighted!' // [!code highlight]
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
## Focus in Code Blocks
|
|
|
|
Adding the `// [!code focus]` comment on a line will focus it and blur the other parts of the code.
|
|
|
|
Additionally, you can define a number of lines to focus using `// [!code focus:<lines>]`.
|
|
|
|
**Input**
|
|
|
|
````
|
|
```js
|
|
export default {
|
|
data () {
|
|
return {
|
|
msg: 'Focused!' // [!!code focus]
|
|
}
|
|
}
|
|
}
|
|
```
|
|
````
|
|
|
|
**Output**
|
|
|
|
```js
|
|
export default {
|
|
data() {
|
|
return {
|
|
msg: 'Focused!' // [!code focus]
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
## Colored Diffs in Code Blocks
|
|
|
|
Adding the `// [!code --]` or `// [!code ++]` comments on a line will create a diff of that line, while keeping the colors of the codeblock.
|
|
|
|
**Input**
|
|
|
|
````
|
|
```js
|
|
export default {
|
|
data () {
|
|
return {
|
|
msg: 'Removed' // [!!code --]
|
|
msg: 'Added' // [!!code ++]
|
|
}
|
|
}
|
|
}
|
|
```
|
|
````
|
|
|
|
**Output**
|
|
|
|
```js
|
|
export default {
|
|
data () {
|
|
return {
|
|
msg: 'Removed' // [!code --]
|
|
msg: 'Added' // [!code ++]
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
## Errors and Warnings in Code Blocks
|
|
|
|
Adding the `// [!code warning]` or `// [!code error]` comments on a line will color it accordingly.
|
|
|
|
**Input**
|
|
|
|
````
|
|
```js
|
|
export default {
|
|
data () {
|
|
return {
|
|
msg: 'Error', // [!!code error]
|
|
msg: 'Warning' // [!!code warning]
|
|
}
|
|
}
|
|
}
|
|
```
|
|
````
|
|
|
|
**Output**
|
|
|
|
```js
|
|
export default {
|
|
data() {
|
|
return {
|
|
msg: 'Error', // [!code error]
|
|
msg: 'Warning' // [!code warning]
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
## Line Numbers
|
|
|
|
You can enable line numbers for each code blocks via config:
|
|
|
|
```js
|
|
export default {
|
|
markdown: {
|
|
lineNumbers: true
|
|
}
|
|
}
|
|
```
|
|
|
|
Please see [`markdown` options](../reference/site-config#markdown) for more details.
|
|
|
|
You can add `:line-numbers` / `:no-line-numbers` mark in your fenced code blocks to override the value set in config.
|
|
|
|
You can also customize the starting line number by adding `=` after `:line-numbers`. For example, `:line-numbers=2` means the line numbers in code blocks will start from `2`.
|
|
|
|
**Input**
|
|
|
|
````md
|
|
```ts {1}
|
|
// line-numbers is disabled by default
|
|
const line2 = 'This is line 2'
|
|
const line3 = 'This is line 3'
|
|
```
|
|
|
|
```ts:line-numbers {1}
|
|
// line-numbers is enabled
|
|
const line2 = 'This is line 2'
|
|
const line3 = 'This is line 3'
|
|
```
|
|
|
|
```ts:line-numbers=2 {1}
|
|
// line-numbers is enabled and start from 2
|
|
const line3 = 'This is line 3'
|
|
const line4 = 'This is line 4'
|
|
```
|
|
````
|
|
|
|
**Output**
|
|
|
|
```ts {1}
|
|
// line-numbers is disabled by default
|
|
const line2 = 'This is line 2'
|
|
const line3 = 'This is line 3'
|
|
```
|
|
|
|
```ts:line-numbers {1}
|
|
// line-numbers is enabled
|
|
const line2 = 'This is line 2'
|
|
const line3 = 'This is line 3'
|
|
```
|
|
|
|
```ts:line-numbers=2 {1}
|
|
// line-numbers is enabled and start from 2
|
|
const line3 = 'This is line 3'
|
|
const line4 = 'This is line 4'
|
|
```
|
|
|
|
## Import Code Snippets
|
|
|
|
You can import code snippets from existing files via following syntax:
|
|
|
|
```md
|
|
<<< @/filepath
|
|
```
|
|
|
|
It also supports [line highlighting](#line-highlighting-in-code-blocks):
|
|
|
|
```md
|
|
<<< @/filepath{highlightLines}
|
|
```
|
|
|
|
**Input**
|
|
|
|
```md
|
|
<<< @/snippets/snippet.js{2}
|
|
```
|
|
|
|
**Code file**
|
|
|
|
<<< @/snippets/snippet.js
|
|
|
|
**Output**
|
|
|
|
<<< @/snippets/snippet.js{2}
|
|
|
|
::: tip
|
|
The value of `@` corresponds to the source root. By default it's the VitePress project root, unless `srcDir` is configured. Alternatively, you can also import from relative paths:
|
|
|
|
```md
|
|
<<< ../snippets/snippet.js
|
|
```
|
|
|
|
:::
|
|
|
|
You can also use a [VS Code region](https://code.visualstudio.com/docs/editor/codebasics#_folding) to only include the corresponding part of the code file. You can provide a custom region name after a `#` following the filepath:
|
|
|
|
**Input**
|
|
|
|
```md
|
|
<<< @/snippets/snippet-with-region.js#snippet{1}
|
|
```
|
|
|
|
**Code file**
|
|
|
|
<<< @/snippets/snippet-with-region.js
|
|
|
|
**Output**
|
|
|
|
<<< @/snippets/snippet-with-region.js#snippet{1}
|
|
|
|
If a file contains multiple regions with the same name, all of them are imported and concatenated — including regions written in different comment styles, such as a `<!-- #region -->` in the template and a `// #region` in the script of the same Vue SFC. The marker comments delimiting them are removed from the output; set `markdown.snippet.stripRegionMarkers` to `'all'` to also remove markers of other comment styles nested inside the region, or to `false` to keep all of them.
|
|
|
|
::: tip
|
|
Region names may contain letters, digits, `_`, `-` and `.`. Since the name is taken from the end of the path, a file whose name itself contains a `#` needs an explicit region — write `<<< ./my#file.js#region` rather than `<<< ./my#file.js`.
|
|
:::
|
|
|
|
::: warning
|
|
Importing a file or region that does not exist throws a build error. Set `markdown.snippet.silent: true` to log a warning and render nothing instead.
|
|
:::
|
|
|
|
You can also specify the language inside the braces (`{}`) like this:
|
|
|
|
```md
|
|
<<< @/snippets/snippet.cs{c#}
|
|
|
|
<!-- with line highlighting: -->
|
|
|
|
<<< @/snippets/snippet.cs{1,2,4-6 c#}
|
|
|
|
<!-- with line numbers: -->
|
|
|
|
<<< @/snippets/snippet.cs{1,2,4-6 c#:line-numbers}
|
|
```
|
|
|
|
This is helpful if source language cannot be inferred from your file extension. Only alphanumeric extensions are inferred, so files like `main.c++` or `scss.code-snippets` need the language spelled out this way.
|
|
|
|
Anything after the language inside the braces is passed along to the code block as extra attributes — for example, `<<< @/snippets/snippet.ts{ts twoslash}` enables twoslash processing when [`@shikijs/vitepress-twoslash`](https://shiki.style/packages/vitepress#twoslash) is configured. Note that attributes may not contain square brackets.
|
|
|
|
## Code Groups
|
|
|
|
You can group multiple code blocks like this:
|
|
|
|
**Input**
|
|
|
|
````md
|
|
::: code-group
|
|
|
|
```js [config.js]
|
|
/**
|
|
* @type {import('vitepress').UserConfig}
|
|
*/
|
|
const config = {
|
|
// ...
|
|
}
|
|
|
|
export default config
|
|
```
|
|
|
|
```ts [config.ts]
|
|
import type { UserConfig } from 'vitepress'
|
|
|
|
const config: UserConfig = {
|
|
// ...
|
|
}
|
|
|
|
export default config
|
|
```
|
|
|
|
:::
|
|
````
|
|
|
|
**Output**
|
|
|
|
::: code-group
|
|
|
|
```js [config.js]
|
|
/**
|
|
* @type {import('vitepress').UserConfig}
|
|
*/
|
|
const config = {
|
|
// ...
|
|
}
|
|
|
|
export default config
|
|
```
|
|
|
|
```ts [config.ts]
|
|
import type { UserConfig } from 'vitepress'
|
|
|
|
const config: UserConfig = {
|
|
// ...
|
|
}
|
|
|
|
export default config
|
|
```
|
|
|
|
:::
|
|
|
|
You can also [import snippets](#import-code-snippets) in code groups:
|
|
|
|
**Input**
|
|
|
|
```md
|
|
::: code-group
|
|
|
|
<!-- filename is used as title by default -->
|
|
|
|
<<< @/snippets/snippet.js
|
|
|
|
<!-- you can provide a custom one too -->
|
|
|
|
<<< @/snippets/snippet-with-region.js#snippet{1,2 ts:line-numbers} [snippet with region]
|
|
|
|
:::
|
|
```
|
|
|
|
**Output**
|
|
|
|
::: code-group
|
|
|
|
<<< @/snippets/snippet.js
|
|
|
|
<<< @/snippets/snippet-with-region.js#snippet{1,2 ts:line-numbers} [snippet with region]
|
|
|
|
:::
|
|
|
|
## Markdown File Inclusion
|
|
|
|
You can include a markdown file in another markdown file, even nested.
|
|
|
|
::: tip
|
|
You can also prefix the markdown path with `@`, and it will act as the source root. By default, the source root is the VitePress project root, unless `srcDir` is configured.
|
|
:::
|
|
|
|
For example, you can include a relative markdown file using this:
|
|
|
|
**Input**
|
|
|
|
```md
|
|
# Docs
|
|
|
|
## Basics
|
|
|
|
<!--@@include: ./parts/basics.md-->
|
|
```
|
|
|
|
**Part file** (`parts/basics.md`)
|
|
|
|
```md
|
|
Some getting started stuff.
|
|
|
|
### Configuration
|
|
|
|
Can be created using `.foorc.json`.
|
|
```
|
|
|
|
**Equivalent code**
|
|
|
|
```md
|
|
# Docs
|
|
|
|
## Basics
|
|
|
|
Some getting started stuff.
|
|
|
|
### Configuration
|
|
|
|
Can be created using `.foorc.json`.
|
|
```
|
|
|
|
It also supports selecting a line range:
|
|
|
|
**Input**
|
|
|
|
```md:line-numbers
|
|
# Docs
|
|
|
|
## Basics
|
|
|
|
<!--@@include: ./parts/basics.md{3,}-->
|
|
```
|
|
|
|
**Part file** (`parts/basics.md`)
|
|
|
|
```md:line-numbers
|
|
Some getting started stuff.
|
|
|
|
### Configuration
|
|
|
|
Can be created using `.foorc.json`.
|
|
```
|
|
|
|
**Equivalent code**
|
|
|
|
```md:line-numbers
|
|
# Docs
|
|
|
|
## Basics
|
|
|
|
### Configuration
|
|
|
|
Can be created using `.foorc.json`.
|
|
```
|
|
|
|
The format of the selected line range can be: `{3,}`, `{,10}`, `{1,10}`
|
|
|
|
You can also use a [VS Code region](https://code.visualstudio.com/docs/editor/codebasics#_folding) to only include the corresponding part of the code file. You can provide a custom region name after a `#` following the filepath:
|
|
|
|
**Input**
|
|
|
|
```md:line-numbers
|
|
# Docs
|
|
|
|
## Basics
|
|
|
|
<!--@@include: ./parts/basics.md#basic-usage{,2}-->
|
|
<!--@@include: ./parts/basics.md#basic-usage{5,}-->
|
|
```
|
|
|
|
**Part file** (`parts/basics.md`)
|
|
|
|
```md:line-numbers
|
|
<!-- #region basic-usage -->
|
|
## Usage Line 1
|
|
|
|
## Usage Line 2
|
|
|
|
## Usage Line 3
|
|
<!-- #endregion basic-usage -->
|
|
```
|
|
|
|
**Equivalent code**
|
|
|
|
```md:line-numbers
|
|
# Docs
|
|
|
|
## Basics
|
|
|
|
## Usage Line 1
|
|
|
|
## Usage Line 3
|
|
```
|
|
|
|
::: warning
|
|
Including a missing file, region, heading anchor, or an out-of-range line selection throws a build error. Set `markdown.include.silent: true` to log a warning and skip the inclusion instead.
|
|
:::
|
|
|
|
Instead of VS Code regions, you can also use header anchors to include a specific section of the file. For example, if you have a header in your markdown file like this:
|
|
|
|
```md
|
|
## My Base Section
|
|
|
|
Some content here.
|
|
|
|
### My Sub Section
|
|
|
|
Some more content here.
|
|
|
|
## Another Section
|
|
|
|
Content outside `My Base Section`.
|
|
```
|
|
|
|
You can include the `My Base Section` section like this:
|
|
|
|
```md
|
|
## My Extended Section
|
|
<!--@@include: ./parts/basics.md#my-base-section-->
|
|
```
|
|
|
|
**Equivalent code**
|
|
|
|
```md
|
|
## My Extended Section
|
|
|
|
Some content here.
|
|
|
|
### My Sub Section
|
|
|
|
Some more content here.
|
|
```
|
|
|
|
Here, `my-base-section` is the generated id of the heading element. In case it's not easily guessable, you can open the part file in your browser and click on the heading anchor (`#` symbol left to the heading when hovered) to see the id in the URL bar. Or use browser dev tools to inspect the element. Alternatively, you can also specify the id to the part file like this:
|
|
|
|
```md
|
|
## My Base Section {#custom-id}
|
|
```
|
|
|
|
and include it like this:
|
|
|
|
```md
|
|
<!--@@include: ./parts/basics.md#custom-id-->
|
|
```
|
|
|
|
Relative links and images inside included files resolve from the _included_ file's location, so a partial can link to its neighbors no matter which page includes it. Set `markdown.include.rebaseRelativeUrls: false` to leave them resolving relative to the including page instead.
|
|
|
|
### Including Code Files {#including-code-files}
|
|
|
|
Since inclusion happens before code blocks are parsed, the directive also works inside fences. Combined with a line range, this lets you show only part of a code file — an alternative to [importing snippets](#import-code-snippets) when regions are not an option:
|
|
|
|
**Input**
|
|
|
|
````md
|
|
```js
|
|
<!--@@include: @/snippets/snippet-with-region.js{2,4}-->
|
|
```
|
|
````
|
|
|
|
**Output**
|
|
|
|
```js
|
|
<!--@include: @/snippets/snippet-with-region.js{2,4}-->
|
|
```
|
|
|
|
Note that the included lines are inserted verbatim (indentation is preserved), and content containing backticks needs a longer outer fence.
|
|
|
|
## Math Equations
|
|
|
|
This is currently opt-in. To enable it, you need to install `markdown-it-mathjax3` and set `markdown.math` to `true` in your config file:
|
|
|
|
```sh
|
|
npm add -D markdown-it-mathjax3@^4
|
|
```
|
|
|
|
```ts [.vitepress/config.ts]
|
|
export default {
|
|
markdown: {
|
|
math: true
|
|
}
|
|
}
|
|
```
|
|
|
|
**Input**
|
|
|
|
```md
|
|
When $a \ne 0$, there are two solutions to $(ax^2 + bx + c = 0)$ and they are
|
|
$$ x = {-b \pm \sqrt{b^2-4ac} \over 2a} $$
|
|
|
|
**Maxwell's equations:**
|
|
|
|
| equation | description |
|
|
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- |
|
|
| $\nabla \cdot \vec{\mathbf{B}} = 0$ | divergence of $\vec{\mathbf{B}}$ is zero |
|
|
| $\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} = \vec{\mathbf{0}}$ | curl of $\vec{\mathbf{E}}$ is proportional to the rate of change of $\vec{\mathbf{B}}$ |
|
|
| $\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} = \frac{4\pi}{c}\vec{\mathbf{j}} \nabla \cdot \vec{\mathbf{E}} = 4 \pi \rho$ | _wha?_ |
|
|
```
|
|
|
|
**Output**
|
|
|
|
When $a \ne 0$, there are two solutions to $(ax^2 + bx + c = 0)$ and they are
|
|
$$ x = {-b \pm \sqrt{b^2-4ac} \over 2a} $$
|
|
|
|
**Maxwell's equations:**
|
|
|
|
| equation | description |
|
|
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- |
|
|
| $\nabla \cdot \vec{\mathbf{B}} = 0$ | divergence of $\vec{\mathbf{B}}$ is zero |
|
|
| $\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} = \vec{\mathbf{0}}$ | curl of $\vec{\mathbf{E}}$ is proportional to the rate of change of $\vec{\mathbf{B}}$ |
|
|
| $\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} = \frac{4\pi}{c}\vec{\mathbf{j}} \nabla \cdot \vec{\mathbf{E}} = 4 \pi \rho$ | _wha?_ |
|
|
|
|
## Image Lazy Loading
|
|
|
|
You can enable lazy loading for each image added via markdown by setting `lazyLoad` to `true` in your config file:
|
|
|
|
```js
|
|
export default {
|
|
markdown: {
|
|
image: {
|
|
// image lazy loading is disabled by default
|
|
lazyLoad: true
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
## Advanced Configuration
|
|
|
|
VitePress uses [markdown-it](https://github.com/markdown-it/markdown-it) as the Markdown renderer. A lot of the extensions above are implemented via custom plugins. You can further customize the `markdown-it` instance using the `markdown` option in `.vitepress/config.js`:
|
|
|
|
```js
|
|
import { defineConfig } from 'vitepress'
|
|
import { headerLink } from '@mdit/plugin-anchor'
|
|
import markdownItFoo from 'markdown-it-foo'
|
|
|
|
export default defineConfig({
|
|
markdown: {
|
|
// options for @mdit/plugin-anchor
|
|
// https://mdit-plugins.github.io/anchor.html
|
|
anchor: {
|
|
permalink: headerLink()
|
|
},
|
|
|
|
// options for @mdit-vue/plugin-toc
|
|
// https://github.com/mdit-vue/mdit-vue/tree/main/packages/plugin-toc#options
|
|
toc: { level: [1, 2] },
|
|
|
|
config: (md) => {
|
|
// use more markdown-it plugins!
|
|
md.use(markdownItFoo)
|
|
}
|
|
}
|
|
})
|
|
```
|
|
|
|
See full list of configurable properties in [Config Reference: App Config](../reference/site-config#markdown).
|