当前位置:首页 > 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 组件实现:

vue实现markdown

<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:专业级编辑体验,适合复杂场景
  • 自定义解析器:完全控制,但功能有限

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

标签: vuemarkdown
分享给朋友:

相关文章

vue实现倒计时抢券

vue实现倒计时抢券

Vue 实现倒计时抢券功能 核心逻辑 倒计时抢券功能需要结合时间计算、状态管理和界面交互。通过 Vue 的响应式特性和生命周期钩子,可以高效实现这一需求。 实现步骤 1. 数据准备 在 Vue 组件…

vue实现app

vue实现app

Vue 实现 App 的常见方法 使用 Vue 开发移动端 App 主要有两种主流方案:混合开发(Hybrid)和原生渲染。以下是具体实现方式和工具链: 混合开发方案(WebView 嵌入) 通过…

vue搜索实现

vue搜索实现

Vue 搜索功能实现方法 基于计算属性的搜索 在 Vue 中可以通过计算属性实现简单的搜索功能。这种方式适合小型数据集,利用 JavaScript 的 filter 方法进行筛选。 data() {…

vue 实现平面

vue 实现平面

Vue 实现平面的方法 在 Vue 中实现平面效果通常涉及 CSS 样式、UI 框架或自定义组件的使用。以下是几种常见方法: 使用 CSS 样式 通过 Vue 的样式绑定或 scoped CSS 为…

vue实现 弹窗

vue实现 弹窗

Vue 实现弹窗的基本方法 使用 Vue 实现弹窗可以通过多种方式,包括组件化、指令、插件等。以下是几种常见的实现方法。 组件化实现弹窗 创建一个独立的弹窗组件,通过 props 控制显示与隐藏。…

vue实现投票

vue实现投票

Vue实现投票功能 数据准备 在Vue组件的data中定义投票相关数据,包括选项列表和当前选中状态: data() { return { options: [ { id: 1…