Markdown

Make sure you have known Markdown well before reading this section. If not, please learn some Markdown tutorialsMarkdown - 图1open in new window first.

Syntax Extensions

The Markdown content in VuePress will be parsed by markdown-itMarkdown - 图2open in new window, which supports syntax extensionsMarkdown - 图3open in new window via markdown-it plugins.

This section will introduce built-in Markdown syntax extensions of VuePress.

You can also configure those built-in extensions, load more markdown-it plugins and implement your own extensions via markdown option and extendsMarkdown option.

Embedded

Embedded by markdown-it:

Header Anchors

You might have noticed that, a # anchor is displayed when you hover the mouse on the headers of each section. By clicking the # anchor, you can jump to the section directly.

TIP

This header anchors extension is supported by markdown-it-anchorMarkdown - 图6open in new window.

Config reference: markdown.anchor

Links

When using Markdown link syntaxMarkdown - 图7open in new window, VuePress will implement some conversions for you.

Take our documentation source files as an example:

  1. └─ docs
  2. ├─ guide
  3. ├─ getting-started.md
  4. ├─ markdown.md # <- Here we are
  5. └─ README.md
  6. ├─ reference
  7. └─ config.md
  8. └─ README.md

Raw Markdown

  1. <!-- relative path -->
  2. [Home](../README.md)
  3. [Config Reference](../reference/config.md)
  4. [Getting Started](./getting-started.md)
  5. <!-- absolute path -->
  6. [Guide](/guide/README.md)
  7. [Config Reference > markdown.links](/reference/config.md#links)
  8. <!-- URL -->
  9. [GitHub](https://github.com)

Converted to

  1. <template>
  2. <RouterLink to="/">Home</RouterLink>
  3. <RouterLink to="/reference/config.html">Config Reference</RouterLink>
  4. <RouterLink to="/guide/getting-started.html">Getting Started</RouterLink>
  5. <RouterLink to="/guide/">Guide</RouterLink>
  6. <RouterLink to="/reference/config.html#links">Config Reference &gt; markdown.links</RouterLink>
  7. <a href="https://github.com" target="_blank" rel="noopener noreferrer">GitHub<OutboundLink/></a>
  8. </template>

Rendered as

Home
Config Reference
Getting Started
Guide
Config Reference > markdown.links
GitHubMarkdown - 图8open in new window

Explanation

  • Internal links will be converted to <RouterLink> for SPA navigation.
  • Internal links to .md files will be converted to the page route path, and both absolute path and relative path are supported.
  • External links will get target="_blank" rel="noopener noreferrer" attrs and a Markdown - 图9open in new window indicator.

Suggestion

Try to use relative paths instead of absolute paths for internal links.

  • Relative paths are a valid links to the target files, and they can navigate correctly when browsing the source files in your editor or repository.
  • Relative paths are consistent in different locales, so you don’t need to change the locale path when translating your content.
  • When using absolute paths, if the base of your site is not "/", you will need to prepend the base manually or use base helper.

TIP

This links extension is supported by our built-in plugin.

Config reference: markdown.links

Also see: Built-in Components > OutboundLink

Emoji 🎉

You can add emoji to your Markdown content by typing :EMOJICODE:.

For a full list of available emoji and codes, check out emoji-cheat-sheet.comMarkdown - 图10open in new window.

Input

  1. VuePress 2 is out :tada: !

Output

VuePress 2 is out 🎉 !

TIP

This emoji extension is supported by markdown-it-emojiMarkdown - 图11open in new window.

Config reference: markdown.emoji

Table of Contents

If you want to put the table of contents (TOC) of your current page inside your Markdown content, you can use the [[toc]] syntax.

Input

  1. [[toc]]

Output

The headers in TOC will link to the corresponding header anchors, so TOC won’t work well if you disable header anchors.

TIP

This toc extension is supported by our built-in plugin, which is forked and modified from markdown-it-toc-done-rightMarkdown - 图12open in new window.

Config reference: markdown.toc

Code Blocks

Following code blocks extensions are implemented during markdown parsing in Node side. That means, the code blocks won’t be processed in client side.

Line Highlighting

You can highlight specified lines of your code blocks by adding line ranges mark in your fenced code blocks:

Input

  1. ```ts{1,6-8}
  2. import type { UserConfig } from '@vuepress/cli'
  3. export const config: UserConfig = {
  4. title: 'Hello, VuePress',
  5. themeConfig: {
  6. logo: 'https://vuejs.org/images/logo.png',
  7. },
  8. }
  9. ```

Output

  1. import type { UserConfig } from '@vuepress/cli'
  2. export const config: UserConfig = {
  3. title: 'Hello, VuePress',
  4. themeConfig: {
  5. logo: 'https://vuejs.org/images/logo.png',
  6. },
  7. }

Examples for line ranges mark:

  • Line ranges: {5-8}
  • Multiple single lines: {4,7,9}
  • Combined: {4,7-13,16,23-27,40}

TIP

This line highlighting extension is supported by our built-in plugin, which is forked and modified from markdown-it-highlight-linesMarkdown - 图13open in new window.

Config reference: markdown.code.highlightLines

Line Numbers

You must have noticed that the number of lines is displayed on the left side of code blocks. This is enabled by default and you can disable it in config.

You can add :line-numbers / :no-line-numbers mark in your fenced code blocks to override the value set in config.

Input

  1. ```ts
  2. // line-numbers is enabled by default
  3. const line2 = 'This is line 2'
  4. const line3 = 'This is line 3'
  5. ```
  6. ```ts:no-line-numbers
  7. // line-numbers is disabled
  8. const line2 = 'This is line 2'
  9. const line3 = 'This is line 3'
  10. ```

Output

  1. // line-numbers is enabled by default
  2. const line2 = 'This is line 2'
  3. const line3 = 'This is line 3'
  1. // line-numbers is disabled
  2. const line2 = 'This is line 2'
  3. const line3 = 'This is line 3'

TIP

This line numbers extension is supported by our built-in plugin.

Config reference: markdown.code.lineNumbers

Wrap with v-pre

As template syntax is allowed in Markdown, it would also work in code blocks, too.

To avoid your code blocks being compiled by Vue, VuePress will add v-preMarkdown - 图14open in new window directive to your code blocks by default, which can be disabled in config.

You can add :v-pre / :no-v-pre mark in your fenced code blocks to override the value set in config.

WARNING

The template syntax characters, for example, the “Mustache” syntax (double curly braces) might be parsed by the syntax highlighter. Thus, as the following example, :no-v-pre might not work well in some languages.

If you want to make Vue syntax work in those languages anyway, try to disable the default syntax highlighting and implement your own syntax highlighting in client side.

Input

  1. ```md
  2. <!-- This will be kept as is by default -->
  3. 1 + 2 + 3 = {{ 1 + 2 + 3 }}
  4. ```
  5. ```md:no-v-pre
  6. <!-- This will be compiled by Vue -->
  7. 1 + 2 + 3 = {{ 1 + 2 + 3 }}
  8. ```
  9. ```js:no-v-pre
  10. // This won't be compiled correctly because of js syntax highlighting
  11. const onePlusTwoPlusThree = {{ 1 + 2 + 3 }}
  12. ```

Output

  1. <!-- This will be kept as is -->
  2. 1 + 2 + 3 = {{ 1 + 2 + 3 }}
  1. <!-- This will be compiled by Vue -->
  2. 1 + 2 + 3 = 6
  1. // This won't be compiled correctly because of js syntax highlighting
  2. const onePlusTwoPlusThree = {{ 1 + 2 + 3 }}

TIP

This v-pre extension is supported by our built-in plugin.

Config reference: markdown.code.vPre

Using Vue in Markdown

This section will introduce some basic usage of Vue in Markdown.

Check out Cookbook > Markdown and Vue SFC for more details.

Template Syntax

As we know:

  • HTML is allowed in Markdown.
  • Vue template syntax is compatible with HTML.

That means, Vue template syntaxMarkdown - 图15open in new window is allowed in Markdown.

Input

  1. One plus one equals: {{ 1 + 1 }}
  2. <span v-for="i in 3"> span: {{ i }} </span>

Output

One plus one equals: 2

span: 1 span: 2 span: 3

Components

You can use Vue components directly in Markdown.

Input

  1. This is default theme built-in `<Badge />` component <Badge text="demo" />

Output

This is default theme built-in <Badge /> component demo

TIP

Check out the Built-in Components for a full list of built-in components.

Check out the Default Theme > Built-in Components for a full list of default theme built-in components.