Vue in Markdown

Using Vue components, script setup, directives, and templating in markdown files

VitePress markdown files are compiled as Vue Single-File Components, enabling full Vue functionality.

Interpolation

Vue expressions work in markdown:

{{ 1 + 1 }}

{{ new Date().toLocaleDateString() }}

Directives

HTML with Vue directives:

<span v-for="i in 3">{{ i }}</span>

<div v-if="$frontmatter.showBanner">
  Banner content
</div>

Script and Style

Add <script setup> and <style> after frontmatter:

---
title: My Page
---

<script setup>
import { ref } from 'vue'
import MyComponent from './MyComponent.vue'

const count = ref(0)
</script>

# {{ $frontmatter.title }}

Count: {{ count }}

<button @click="count++">Increment</button>

<MyComponent />

<style module>
.button {
  color: red;
}
</style>

Note: Use <style module> instead of <style scoped> to avoid bloating page size.

Importing Components

Local import (code-split per page):

Global Components

Register in theme for use everywhere:

Then use in any markdown:

Important: Component names must contain a hyphen or be PascalCase to avoid being treated as inline HTML elements.

Runtime API

Access VitePress data:

Global Variables

Available without import:

Components in Headers

Escaping Vue Syntax

Prevent Vue interpolation:

Or use container:

Vue in Code Blocks

Enable Vue processing in fenced code with -vue suffix:

CSS Pre-processors

Supported out of the box (install the preprocessor):

Using Teleports

Teleport to body only with SSG:

VS Code IntelliSense

Enable Vue language features for .md files:

Key Points

  • Markdown files are Vue SFCs - use <script setup> and <style>

  • Access page data via useData() or $frontmatter global

  • Import components locally or register globally in theme

  • Use <style module> instead of <style scoped>

  • Wrap non-SSR components in <ClientOnly>

  • Component names must be PascalCase or contain hyphens

Last updated

Was this helpful?