Frontmatter and Data Binding in Vue Markdown
Interpolate frontmatter and reactive data into Markdown with the Comark binding plugin in Vue and Vite.
App.vue
<script setup lang="ts">
import { reactive } from 'vue'
import { Comark } from '@comark/vue'
import binding, { Binding } from '@comark/vue/plugins/binding'
// Runtime data exposed to bindings via the `data.` namespace.
const data = reactive({
user: {
name: 'Ada',
role: 'admin',
},
stats: {
users: 1200,
uptime: '99.9%',
},
})
const markdown = `---
release:
version: 2.5.1
codename: Firefly
---
# {{ frontmatter.release.codename || Unnamed }} — v{{ frontmatter.release.version }}
Hello **{{ data.user.name || friend }}** (role: {{ data.user.role }}), welcome back!
## Platform stats
| Metric | Value |
| ------ | -------------------------------- |
| Users | {{ data.stats.users }} |
| Uptime | {{ data.stats.uptime }} |
| Plan | {{ data.plan \\|\\| community }} |
## Components see their own props
::card{title="Component props"}
Inside the card the binding below pulls the card's title via \`props\`:
{{ props.title }}
::
`
</script>
<template>
<Suspense>
<Comark
:markdown="markdown"
:plugins="[binding()]"
:components="{ Binding }"
:data="data"
/>
</Suspense>
</template>
This example interpolates YAML frontmatter, reactive application state, and component props into Markdown in a Vue and Vite app with @comark/vue/plugins/binding.
Features
{{ path }}interpolation: reads values from frontmatter, the renderer'sdataprop, or an enclosing component'sprops.|| defaultfallback: renders an inline fallback when the dot path is undefined.- Conditional blocks: selects branches with
::ifand#elseusing truthiness or comparison operators. - Typed prop bindings: passes strings, numbers, booleans, and objects without stringifying them first.
Usage
Import the plugin and its Vue components:
import binding, { Binding, If } from '@comark/vue/plugins/binding'Pass them to
<Markdown>along with reactivedata:<Markdown :value="markdown" :plugins="[binding()]" :components="{ Binding, If }" :data="data" />Reference values with
{{ path || default }}in Markdown:--- release: version: 2.5.1 --- Hello, {{ data.user.name || friend }}. You are on v{{ frontmatter.release.version }}.
Namespaces
Bindings resolve against four namespaces:
| Namespace | Source |
|---|---|
frontmatter | YAML frontmatter on the parsed document |
meta | Plugin metadata attached to document.meta |
data | The data prop passed to <Markdown> |
props | Resolved props of the enclosing component |