sidebarDepth: 3

Markdown 扩展 {#markdown-extensions}

标题锚点 {#header-anchors}

标题会自动应用锚点链接。可以使用 markdown.anchor 选项配置锚点的渲染。

链接 {#links}

内部链接 {#internal-links}

内部链路转换为 router link 以用于 SPA 导航。另外,每个子目录包含的 index.md 都会自动转换为 index.html,并有对应的 URL /

例如,给定以下目录结构:

  1. .
  2. ├─ index.md
  3. ├─ foo
  4. ├─ index.md
  5. ├─ one.md
  6. └─ two.md
  7. └─ bar
  8. ├─ index.md
  9. ├─ three.md
  10. └─ four.md

且假设你在 foo/one.md 中:

  1. [Home](/) <!-- sends the user to the root index.md -->
  2. [foo](/foo/) <!-- sends the user to index.html of directory foo -->
  3. [foo heading](./#heading) <!-- anchors user to a heading in the foo index file -->
  4. [bar - three](../bar/three) <!-- you can omit extention -->
  5. [bar - three](../bar/three.md) <!-- you can append .md -->
  6. [bar - four](../bar/four.html) <!-- or you can append .html -->

页面后缀 {#page-suffix}

默认生成的页面和内部链接后缀为 .html

外部链接 {#external-links}

外部链接自动配置 target="_blank" rel="noopener noferrer"

Frontmatter {#frontmatter}

YAML frontmatter 支持开箱即用:

  1. ---
  2. title: Blogging Like a Hacker
  3. lang: en-US
  4. ---

该数据对页面的其余部分以及所有自定义和主题化组件都可用。

查看 Frontmatter 获取更多细节。

GitHub 风格 表格 {#github-style-tables}

输入

  1. | Tables | Are | Cool |
  2. | ------------- |:-------------:| -----:|
  3. | col 3 is | right-aligned | $1600 |
  4. | col 2 is | centered | $12 |
  5. | zebra stripes | are neat | $1 |

输出

Tables Are Cool
col 3 is right-aligned \$1600
col 2 is centered \$12
zebra stripes are neat \$1

Emoji :tada:

输入

  1. :tada: :100:

输出

:tada: :100:

可用的表情符号列表

目录 {#table-of-contents}

输入

  1. [[toc]]

输出

[[toc]]

可以使用 markdown.toc 选项配置目录的渲染。

自定义容器 {#custom-containers}

自定义容器可以通过它们的类型,标题和内容来定义。

默认标题 {#default-title}

输入

  1. ::: tip
  2. This is a tip
  3. :::
  4. ::: info
  5. This is an info box
  6. :::
  7. ::: warning
  8. This is a warning
  9. :::
  10. ::: danger
  11. This is a dangerous warning
  12. :::

输出

::: tip This is a tip :::

::: info This is an info box :::

::: warning This is a warning :::

::: danger This is a dangerous warning :::

自定义标题 {#custom-title}

输入

  1. ::: danger STOP
  2. Danger zone, do not proceed
  3. :::

输出

::: danger STOP Danger zone, do not proceed :::

代码块中的语法高亮显示 {#syntax-highlighting-in-code-blocks}

VitePress 使用 Prism 突出显示 Markdown 代码块中的语言语法,使用彩色文本。Prism 支持多种编程语言。你所需要做的就是在代码块的开头的反勾号后面添加一个有效的语言别名:

输入

  1. ```js
  2. export default {
  3. name: 'MyComponent',
  4. // ...
  5. }
  6. ```

输出

  1. export default {
  2. name: 'MyComponent'
  3. // ...
  4. }

输入

  1. ```html
  2. <ul>
  3. <li v-for="todo in todos" :key="todo.id">
  4. {{ todo.text }}
  5. </li>
  6. </ul>
  7. ```

输出

  1. <ul>
  2. <li v-for="todo in todos" :key="todo.id">{{ todo.text }}</li>
  3. </ul>

Prism 的网站上提供了 有效语言列表

代码块中的行高亮显示 {#line-highlighting-in-code-blocks}

输入

  1. ```js{4}
  2. export default {
  3. data () {
  4. return {
  5. msg: 'Highlighted!'
  6. }
  7. }
  8. }
  9. ```

输出

  1. export default {
  2. data () {
  3. return {
  4. msg: 'Highlighted!'
  5. }
  6. }
  7. }

除单行外,你还可以指定多个单行,范围或同时指定这两种:

  • 行范围:例如 {5-8}, {3-10}, {10-17}
  • 多行:例如 {4,7,9}
  • 行范围和单行:例如 {4,7-13,16,23-27,40}

输入

  1. ```js{1,4,6-7}
  2. export default { // Highlighted
  3. data () {
  4. return {
  5. msg: `Highlighted!
  6. This line isn't highlighted,
  7. but this and the next 2 are.`,
  8. motd: 'VitePress is awesome',
  9. lorem: 'ipsum',
  10. }
  11. }
  12. }
  13. ```

输出

  1. export default { // Highlighted
  2. data () {
  3. return {
  4. msg: `Highlighted!
  5. This line isn't highlighted,
  6. but this and the next 2 are.`,
  7. motd: 'VitePress is awesome',
  8. lorem: 'ipsum',
  9. }
  10. }
  11. }

行号 {#line-numbers}

你可以通过配置启用每个代码块的行号:

  1. module.exports = {
  2. markdown: {
  3. lineNumbers: true
  4. }
  5. }
  • Demo:
Image Image

导入代码片段 {#import-code-snippets}

你可以通过以下语法从现有文件中导入代码片段:

  1. <<< @/filepath

它还支持 行高亮显示

  1. <<< @/filepath{highlightLines}

输入

  1. <<< @/snippets/snippet.js{2}

代码文件

<<< @/snippets/snippet.js

输出

<<< @/snippets/snippet.js{2}

::: tip @ 的值对应于 process.cwd()。默认情况下,它是 VitePress 项目的根目录,除非配置了 srcDir。 :::

你还可以使用 VS Code region 来仅包括代码文件的相应部分。你可以在文件路径后的 # 后面提供自定义的范围名称(默认为 snippet):

输入

  1. <<< @/snippets/snippet-with-region.js{1}

代码文件

<<< @/snippets/snippet-with-region.js

输出

<<< @/snippets/snippet-with-region.js#snippet{1}

进阶配置 {#advanced-configuration}

VitePress 使用 markdown-it 作为 Markdown 渲染器。上面的许多扩展都是通过自定义插件实现的。你可以通过 .vitepress/config.js 中的 markdown 选项进一步自定义 markdown-it 实例:

  1. const anchor = require('markdown-it-anchor')
  2. module.exports = {
  3. markdown: {
  4. // options for markdown-it-anchor
  5. // https://github.com/valeriangalliat/markdown-it-anchor#permalinks
  6. anchor: {
  7. permalink: anchor.permalink.headerLink()
  8. },
  9. // options for markdown-it-table-of-contents
  10. toc: { includeLevel: [1, 2] },
  11. config: (md) => {
  12. // use more markdown-it plugins!
  13. md.use(require('markdown-it-xxx'))
  14. }
  15. }
  16. }