Vue Markdown Renderer
vue-markdown-render is a lightweight and simple wrapper for the popular markdown-it parsing library, specifically designed for Vue 3 applications. It provides a `VueMarkdown` component that allows rendering Markdown source into HTML directly within Vue templates. The package is written in pure TypeScript, offering full type support. The current stable version is 2.3.0, released in late 2023, indicating an active maintenance and development cadence with regular updates for dependencies like markdown-it. Key differentiators include its minimalistic API, full TypeScript integration, and direct support for passing markdown-it options and plugins, making it flexible for various rendering needs without significant overhead. It requires Vue 3.x as a peer dependency.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'setup') or similar errors related to Vue component initialization.
cause Attempting to use `vue-markdown-render` v2.x in a Vue 2 project, where the component API is incompatible.fixEnsure your project is using Vue 3.x. If you must use Vue 2, downgrade `vue-markdown-render` to a 1.x version. -
Property 'VueMarkdown' does not exist on type '...' or similar TypeScript errors during compilation.
cause Missing or incorrect type definitions for `vue-markdown-render` or its dependencies (specifically `markdown-it`).fixInstall `@types/markdown-it` as a dev dependency: `npm install @types/markdown-it --save-dev`. -
Unknown custom element: <vue-markdown> - did you register the component correctly? For recursive components, make sure to provide the 'name' option.
cause The `VueMarkdown` component was not properly registered in the parent component's `components` option.fixAdd `VueMarkdown` to the `components` object within your Vue component definition, e.g., `components: { VueMarkdown }`.
Warnings
- breaking Version 2.0.0 introduced a breaking change by switching entirely to Vue 3. Applications built with Vue 2 will not be compatible with `vue-markdown-render@2.x`.
- gotcha When using TypeScript, the `@types/markdown-it` package must be installed as a development dependency (`npm install @types/markdown-it --save-dev`) to ensure proper type inference for markdown-it options and plugins.
- gotcha As of v2.3.0, the package upgraded its internal `markdown-it` dependency to v14. While generally backward compatible, review markdown-it's own changelog for any subtle breaking changes or deprecated features if you rely on advanced or custom markdown-it configurations.
- deprecated Prior to v2.1.1, the component might have been used with the Options API exclusively. While still supported, `v2.1.1` introduced a refactor to use Composition API internally, and the provided quickstart examples now predominantly use Composition API with `<script setup>` for better integration with modern Vue 3 practices.
Install
-
npm install vue-markdown-render -
yarn add vue-markdown-render -
pnpm add vue-markdown-render
Imports
- VueMarkdown
const VueMarkdown = require('vue-markdown-render')import VueMarkdown from 'vue-markdown-render'
- defineComponent
import Vue from 'vue'; Vue.defineComponent(...)
import { defineComponent, ref } from 'vue' - MarkdownItAnchor (example plugin)
import { MarkdownItAnchor } from 'markdown-it-anchor'import MarkdownItAnchor from 'markdown-it-anchor'
Quickstart
import { defineComponent, ref } from 'vue';
import VueMarkdown from 'vue-markdown-render';
import MarkdownItAnchor from 'markdown-it-anchor';
export default defineComponent({
name: 'MarkdownViewer',
components: {
VueMarkdown
},
setup() {
const src = ref('# Hello, VueMarkdown\n\nThis is some **dynamic** content rendered from Markdown.\n\n## Features\n\n- Lightweight
- TypeScript support
- Plugin ready (e.g., markdown-it-anchor)
[Link to GitHub](https://github.com/cloudacy/vue-markdown-render)');
const options = {
html: true,
linkify: true,
typographer: true
};
const plugins = [MarkdownItAnchor];
return {
src,
options,
plugins
};
},
template: `
<div>
<VueMarkdown :source="src" :options="options" :plugins="plugins" />
</div>
`
});