@kangc/v-md-editor Documentation
repository·master·Indexed 23 days ago
https://github.com/code-farmer-i/vue-markdown-editorA Markdown editor built for Vue 3 (version 2.3.18) featuring a rich editing experience with support for themes like VuePress. It offers two implementation options: a lightweight Base Editor using a standard textarea and a feature-rich CodeMirror editor. The library includes a dedicated Preview Component for read-only rendering, internationalization support for multiple languages, and a plugin system for features such as emoji support, code block copying, and line highlighting.
What's inside @kangc/v-md-editor
- If your application does not require an interactive editing interface and you only need to render existing markdown content into HTML, use the Preview Component instead of the full editor.
Choose an editor implementation: Base Editor vs CodeMirror
masterThe
v-md-editorpackage provides two different ways to handle the editing area. Choose based on your project's resource constraints and user experience requirements:- Base Editor: Uses a standard HTML
textarea. It is lightweight but offers a basic editing experience. - CodeMirror: Uses the CodeMirror library for the editing area. It provides a superior, feature-rich editing experience but increases the bundle size and resource usage.
- Base Editor: Uses a standard HTML
Install the Copy Code plugin
masterTo enable the ability to quickly copy code blocks using buttons, you must import and register the
createCopyCodePluginwith theVueMarkdownEditorinstance using the.use()method.import VueMarkdownEditor from '@kangc/v-md-editor'; import createCopyCodePlugin from '@kangc/v-md-editor/lib/plugins/copy-code/index'; VueMarkdownEditor.use(createCopyCodePlugin());Install and configure the Tip plugin
masterTo use the Tip plugin in your Vue project, you must register it with the
VueMarkdownEditorinstance usingcreateTipPlugin(). This plugin adds support for tip-style components within the editor.import VueMarkdownEditor from '@kangc/v-md-editor'; import createTipPlugin from '@kangc/v-md-editor/lib/plugins/tip/index'; VueMarkdownEditor.use(createTipPlugin());Install and configure the Emoji plugin
masterTo add emoji support to the editor, you must register the emoji plugin using
VueMarkdownEditor.use(). You need to import the main editor package and the specific emoji plugin factory function from@kangc/v-md-editor/lib/plugins/emoji/index.import VueMarkdownEditor from '@kangc/v-md-editor'; import createEmojiPlugin from '@kangc/v-md-editor/lib/plugins/emoji/index'; VueMarkdownEditor.use(createEmojiPlugin());Install the KaTeX plugin
masterAfter adding the CDN resources, import the
createKatexPluginfrom the@kangc/v-md-editor/lib/plugins/katex/cdnpath and register it with yourVueMarkdownEditorinstance using.use().import VueMarkdownEditor from '@kangc/v-md-editor'; import createKatexPlugin from '@kangc/v-md-editor/lib/plugins/katex/cdn'; VueMarkdownEditor.use(createKatexPlugin());Use only the preview component for markdown rendering
masterIf your project only needs to display markdown without editing capabilities, you can import only the preview component and its associated styles to reduce bundle size. You must also provide a theme (e.g.,
github).// main.js import VMdPreview from '@kangc/v-md-editor/lib/preview'; import '@kangc/v-md-editor/lib/style/preview.css'; // Introduce the theme you use. Take the github theme as an example here import githubTheme from '@kangc/v-md-editor/lib/theme/github'; VMdPreview.use(githubTheme); Vue.use(VMdPreview);<template> <v-md-preview :text="markdown"></v-md-preview> </template> <script> export default { data() { return { markdown: '### title', }; }, }; </script>Expand supported language highlighting in the VuePress theme
masterBy default, the theme package only supports a limited set of languages (markup, html, xml, svg, mathml, css, clike, javascript) to keep the package size small. To support additional languages, you must import the corresponding PrismJS language packs after calling
VueMarkdownEditor.use(vuepressTheme). If you import them before, the highlighting will not take effect.import VueMarkdownEditor from '@kangc/v-md-editor'; import vuepressTheme from '@kangc/v-md-editor/lib/theme/vuepress.js'; // Introduce prism language packs as needed, here is json as an example import 'prismjs/components/prism-json'; VueMarkdownEditor.use(vuepressTheme);Install and setup the CodeMirror editor
masterTo use the CodeMirror-based editor instead of the default editor, you must import the specific
codemirror-editormodule, its corresponding CSS, and a theme. You then need to register the theme withVMdEditor.use()and register the editor plugin with Vue usingVue.use().import Vue from 'vue'; import VMdEditor from '@kangc/v-md-editor/lib/codemirror-editor'; import '@kangc/v-md-editor/lib/style/codemirror-editor.css'; import githubTheme from '@kangc/v-md-editor/lib/theme/github.js'; VMdEditor.use(githubTheme); Vue.use(VMdEditor);Enable and implement image uploading
masterTo allow users to upload images, you must first enable the image menu in the toolbar. By default, the upload image menu is disabled. You can enable it by setting the
:disabled-menusprop to an empty array[]and ensuringimageis included in your toolbar configuration (e.g.,left-toolbar="... | image").Once enabled, the component emits an
@upload-imageevent when a user selects files. You must handle this event to upload the files to your server and then use the providedinsertImagecallback to place the resulting image URL into the editor.<template> <v-md-editor v-model="text" left-toolbar="undo redo | image" :disabled-menus="[]" @upload-image="handleUploadImage" height="500px" /> </template> <script> export default { data() { return { text: '', }; }, methods: { handleUploadImage(event, insertImage, files) { // 1. Upload the files to your server // 2. Use insertImage to add the result to the editor insertImage({ url: 'https://example.com/path/to/image.jpg', desc: 'image description', }); }, }, }; </script>Configure the XSS extension whitelist
masterYou can customize the XSS (Cross-Site Scripting) protection by extending the default whitelist using
VMdEditor.xss.extend. This allows you to permit specific HTML tags or attributes that are otherwise stripped by the default security filter. The configuration object follows the schema used by the xss library.import VMdEditor from '@kangc/v-md-editor'; VMdEditor.xss.extend({ // extend white list whiteList: { source: [], }, });Customize rendering style with highlight.js
masterTo use a custom rendering style without pre-defined themes (like github or vuepress) while using
highlight.jsfor code highlighting, usecreateHljsTheme. You can extend the theme to register language packs or modify themarkdown-itinstance.// main.js import Vue from 'vue'; import VueMarkdownEditor from '@kangc/v-md-editor'; import '@kangc/v-md-editor/lib/style/base-editor.css'; import createHljsTheme from '@kangc/v-md-editor/lib/theme/hljs'; // Introduce highlightjs language packs as needed, here is json as an example import json from 'highlight.js/lib/languages/json'; const hljsTheme = createHljsTheme(); hljsTheme.extend((md, hljs) => { // md is a markdown-it instance, you can modify the configuration here, and use plugin for syntax expansion // md.set(option).use(plugin); // Register Language Pack hljs.registerLanguage('json', json); }); VueMarkdownEditor.theme(hljsTheme); Vue.use(VueMarkdownEditor);