@kangc/v-md-editor Documentation

repository·master·Indexed 23 days ago

https://github.com/code-farmer-i/vue-markdown-editor

A 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.

Tokens
13.8K
Snippets
47
Records
70
Agent score
79%

What's inside @kangc/v-md-editor

  1. Choose an editor implementation: Base Editor vs CodeMirror

    master

    The v-md-editor package provides two different ways to handle the editing area. Choose based on your project's resource constraints and user experience requirements:

    1. Base Editor: Uses a standard HTML textarea. It is lightweight but offers a basic editing experience.
    2. 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.
  2. Install the Copy Code plugin

    master

    To enable the ability to quickly copy code blocks using buttons, you must import and register the createCopyCodePlugin with the VueMarkdownEditor instance 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());
  3. Install and configure the Tip plugin

    master

    To use the Tip plugin in your Vue project, you must register it with the VueMarkdownEditor instance using createTipPlugin(). 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());
  4. Install and configure the Emoji plugin

    master

    To 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());
  5. Install the KaTeX plugin

    master

    After adding the CDN resources, import the createKatexPlugin from the @kangc/v-md-editor/lib/plugins/katex/cdn path and register it with your VueMarkdownEditor instance using .use().

    import VueMarkdownEditor from '@kangc/v-md-editor';
    import createKatexPlugin from '@kangc/v-md-editor/lib/plugins/katex/cdn';
    
    VueMarkdownEditor.use(createKatexPlugin());
  6. Use only the preview component for markdown rendering

    master

    If 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>
  7. Expand supported language highlighting in the VuePress theme

    master

    By 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);
  8. Install and setup the CodeMirror editor

    master

    To use the CodeMirror-based editor instead of the default editor, you must import the specific codemirror-editor module, its corresponding CSS, and a theme. You then need to register the theme with VMdEditor.use() and register the editor plugin with Vue using Vue.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);
  9. Enable and implement image uploading

    master

    To 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-menus prop to an empty array [] and ensuring image is included in your toolbar configuration (e.g., left-toolbar="... | image").

    Once enabled, the component emits an @upload-image event when a user selects files. You must handle this event to upload the files to your server and then use the provided insertImage callback 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>
  10. Configure the XSS extension whitelist

    master

    You 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: [],
      },
    });
  11. Customize rendering style with highlight.js

    master

    To use a custom rendering style without pre-defined themes (like github or vuepress) while using highlight.js for code highlighting, use createHljsTheme. You can extend the theme to register language packs or modify the markdown-it instance.

    // 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);