当前位置:首页 > VUE

vue实现markdown

2026-01-12 19:57:45VUE

Vue 实现 Markdown 编辑器

在 Vue 中实现 Markdown 编辑器可以通过集成现有的库或自行构建。以下是几种常见的方法:

使用 marked 和 highlight.js

安装依赖:

npm install marked highlight.js

在 Vue 组件中引入并使用:

<template>
  <div>
    <textarea v-model="markdown" @input="updatePreview"></textarea>
    <div v-html="compiledMarkdown"></div>
  </div>
</template>

<script>
import marked from 'marked';
import hljs from 'highlight.js';
import 'highlight.js/styles/github.css';

marked.setOptions({
  highlight: function(code, lang) {
    if (lang && hljs.getLanguage(lang)) {
      return hljs.highlight(lang, code).value;
    }
    return hljs.highlightAuto(code).value;
  }
});

export default {
  data() {
    return {
      markdown: '# Hello Markdown',
      compiledMarkdown: ''
    }
  },
  mounted() {
    this.updatePreview();
  },
  methods: {
    updatePreview() {
      this.compiledMarkdown = marked(this.markdown);
    }
  }
}
</script>

使用 vue-markdown 组件

安装依赖:

npm install vue-markdown

在组件中使用:

<template>
  <div>
    <textarea v-model="markdown"></textarea>
    <vue-markdown :source="markdown"></vue-markdown>
  </div>
</template>

<script>
import VueMarkdown from 'vue-markdown';

export default {
  components: {
    VueMarkdown
  },
  data() {
    return {
      markdown: '# Hello Vue Markdown'
    }
  }
}
</script>

使用 Vditor(功能丰富的 Markdown 编辑器)

安装依赖:

npm install vditor

在 Vue 中使用:

<template>
  <div id="vditor"></div>
</template>

<script>
import Vditor from 'vditor';
import 'vditor/dist/index.css';

export default {
  mounted() {
    this.vditor = new Vditor('vditor', {
      height: 500,
      after: () => {
        this.vditor.setValue('# Hello Vditor');
      }
    });
  },
  beforeDestroy() {
    if (this.vditor) {
      this.vditor.destroy();
    }
  }
}
</script>

自定义 Markdown 解析器

如果需要更轻量级的解决方案,可以自行实现基本 Markdown 解析:

<template>
  <div>
    <textarea v-model="markdown" @input="parseMarkdown"></textarea>
    <div v-html="htmlOutput"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      markdown: '# Hello',
      htmlOutput: ''
    }
  },
  methods: {
    parseMarkdown() {
      this.htmlOutput = this.markdown
        .replace(/^# (.*$)/gm, '<h1>$1</h1>')
        .replace(/^## (.*$)/gm, '<h2>$1</h2>')
        .replace(/^### (.*$)/gm, '<h3>$1</h3>')
        .replace(/\*\*(.*)\*\*/g, '<strong>$1</strong>')
        .replace(/\*(.*)\*/g, '<em>$1</em>')
        .replace(/\n/g, '<br>');
    }
  },
  mounted() {
    this.parseMarkdown();
  }
}
</script>

集成 Monaco Editor 实现高级编辑

对于需要类似 VSCode 体验的 Markdown 编辑器:

npm install monaco-editor

Vue 组件实现:

<template>
  <div id="editor-container" style="height: 500px"></div>
</template>

<script>
import * as monaco from 'monaco-editor';

export default {
  mounted() {
    this.editor = monaco.editor.create(document.getElementById('editor-container'), {
      value: '# Markdown Editor',
      language: 'markdown',
      theme: 'vs-dark',
      minimap: { enabled: true }
    });
  },
  beforeDestroy() {
    this.editor.dispose();
  }
}
</script>

功能比较

  • marked + highlight.js:轻量级,适合简单需求
  • vue-markdown:Vue 专用,中等功能
  • Vditor:功能全面,包含预览、工具栏等
  • Monaco Editor:专业级编辑体验,适合复杂场景
  • 自定义解析器:完全控制,但功能有限

根据项目需求选择合适的方案,大多数情况下使用现成库更高效可靠。

vue实现markdown

标签: vuemarkdown
分享给朋友:

相关文章

vue实现文章锚点定位

vue实现文章锚点定位

实现文章锚点定位的方法 在Vue中实现文章锚点定位可以通过多种方式完成,以下是几种常见的方法: 使用原生HTML锚点 通过HTML的id属性和<a>标签的href属性实现锚点跳转。在Vu…

实现vue模版

实现vue模版

Vue 模板的基本结构 Vue 模板基于 HTML 语法,通过 Vue 的指令和插值实现动态渲染。以下是一个基础模板示例: <template> <div id="app…

vue实现方法

vue实现方法

Vue 实现方法 Vue 是一种流行的前端框架,用于构建用户界面和单页应用。以下是几种常见的 Vue 实现方法: 创建 Vue 实例 通过 new Vue() 创建一个 Vue 实例,传入配置对象…

vue实现过程

vue实现过程

Vue 的实现过程 Vue 的实现过程可以分为多个核心模块,包括响应式系统、虚拟 DOM、模板编译等。以下是 Vue 的主要实现过程分析。 响应式系统 Vue 的响应式系统基于 Object.de…

vue实现RTMP

vue实现RTMP

Vue 中实现 RTMP 流播放 RTMP(Real-Time Messaging Protocol)是一种用于实时音视频流传输的协议。在 Vue 中实现 RTMP 播放通常需要借助第三方库或播放器。…

vue课程实现

vue课程实现

Vue 课程实现方案 课程内容设计 Vue课程应从基础到高级逐步展开,涵盖Vue核心概念、组件化开发、状态管理、路由、构建工具等。基础部分包括Vue实例、模板语法、计算属性和侦听器。中级部分深入组件通…