vue3中使用markdown编辑和展示

安装支持vue3版本的v-md-editor插件

# 使用 npm
npm i @kangc/v-md-editor@next -S

# 使用yarn
yarn add @kangc/v-md-editor@next

在vue3项目中引入

import { creatApp } from 'vue';
import VueMarkdownEditor from '@kangc/v-md-editor';
import '@kangc/v-md-editor/lib/style/base-editor.css';
import vuepressTheme from '@kangc/v-md-editor/lib/theme/vuepress.js';
import '@kangc/v-md-editor/lib/theme/style/vuepress.css';

VueMarkdownEditor.use(vuepressTheme);

const app = creatApp(/*...*/);

app.use(VueMarkdownEditor);

在组件中使用

<template>
  <v-md-editor v-model="text" height="400px"></v-md-editor>
</template>

<script>
export default {
  data() {
    return {
      text: '',
    };
  },
};
</script>

使用导航

onMounted(()=>{
            axios.all([requestArticleDetail(),requestComment()])
            .then(axios.spread((articleRes,commentListRes)=>{
                data.value=articleRes.data.res[0];
                content.value=articleRes.data.res[0].articleContent;
                commentList.value=commentListRes.data.res;
            }))
            .then(()=>{
                //注意要在请求完成之后才能去获取dom节点,因为dom节点的生成依赖于请求的数据
               const anchors = document.querySelectorAll('.v-md-editor-preview h1,h2,h3,h4,h5,h6');
            //    anchors.forEach(item=>{
            //        console.log(item.offsetTop);
            //    })
                titles.value = Array.from(anchors).filter((title) => !!title.innerText.trim());
                const hTags = Array.from(new Set(titles.value.map((title) => title.tagName))).sort();
                titles.value = titles.value.map((el) => ({
                    title: el.innerText,
                    lineIndex: el.getAttribute('data-v-md-line'),
                    indent: hTags.indexOf(el.tagName),
                }));
            })
            
        })

导航点击事件

const handleAnchorClick=(anchor)=>{
            const navItem=document.querySelector('.v-md-editor-preview');
            const { lineIndex } = anchor;
            const heading = navItem.querySelector(
            `.v-md-editor-preview [data-v-md-line="${lineIndex}"]`);
            const top=heading.offsetTop
            let timer=setInterval(()=>{
                if(document.documentElement.scrollTop<top)
                {
                    document.documentElement.scrollTop += 60
                    if(document.documentElement.scrollTop>=top)
                        clearInterval(timer)
                }
                else
                {
                    document.documentElement.scrollTop -= 60
                    if(document.documentElement.scrollTop<=top)
                        clearInterval(timer)
                }
            },20)
        }
<think> 我们已经在之前的回答中介绍了如何在Vue3中将Markdown语法渲染为HTML页面。现在,用户询问的是如何在Vue3使用Markdown编辑器,即实现一个可以编辑Markdown并实时预览的组件。 根据引用[2][3][4][5],我们可以知道在Vue3中有多种Markdown编辑器可供选择。下面我将介绍两种在Vue3中常用的Markdown编辑使用方法: ### 1. 使用 `@kangc/v-md-editor` (推荐) 引用[3]提到了`v-md-editor`,这是一个支持Vue3Markdown编辑器,具有丰富的功能(如主题切换、代码高亮、图片上传等)。 #### 安装与使用 ```bash # 安装 npm install @kangc/v-md-editor@next -S ``` #### 全局注册 ```typescript // main.ts import { createApp } from &#39;vue&#39;; import VMdEditor from &#39;@kangc/v-md-editor&#39;; import &#39;@kangc/v-md-editor/lib/style/base-editor.css&#39;; import vuepressTheme from &#39;@kangc/v-md-editor/lib/theme/vuepress.js&#39;; import &#39;@kangc/v-md-editor/lib/theme/style/vuepress.css&#39;; // 引入prismjs import Prism from &#39;prismjs&#39;; VMdEditor.use(vuepressTheme, { Prism, }); const app = createApp(App); app.use(VMdEditor); app.mount(&#39;#app&#39;); ``` #### 在组件中使用 ```vue <template> <v-md-editor v-model="text" height="500px"></v-md-editor> </template> <script setup lang="ts"> import { ref } from &#39;vue&#39;; const text = ref(&#39;&#39;); </script> ``` ### 2. 使用 `mavon-editor` 引用[4]提到`mavon-editor`同时支持Vue2Vue3,它也是一个功能丰富的Markdown编辑器。 #### 安装 ```bash npm install mavon-editor --save ``` #### 全局注册 ```typescript // main.ts import { createApp } from &#39;vue&#39;; import mavonEditor from &#39;mavon-editor&#39;; import &#39;mavon-editor/dist/css/index.css&#39;; const app = createApp(App); app.use(mavonEditor); app.mount(&#39;#app&#39;); ``` #### 在组件中使用 ```vue <template> <mavon-editor v-model="value"/> </template> <script setup lang="ts"> import { ref } from &#39;vue&#39;; const value = ref(&#39;&#39;); </script> ``` ### 3. 使用 `vditor` (纯JS,不依赖Vue) 引用[5]提到了`vditor`,它是一个现代的Markdown编辑器,可以独立于框架使用,也可以集成到Vue中。 #### 安装 ```bash npm install vditor --save ``` #### 在Vue3组件中使用 ```vue <template> <div id="vditor"></div> </template> <script setup lang="ts"> import { onMounted, ref } from &#39;vue&#39;; import Vditor from &#39;vditor&#39;; import &#39;vditor/dist/index.css&#39;; const contentEditor = ref<Vditor | null>(null); onMounted(() => { contentEditor.value = new Vditor(&#39;vditor&#39;, { height: 500, toolbarConfig: { pin: true, }, cache: { enable: false, }, after: () => { // 编辑器初始化完成后,可以设置初始内容 contentEditor.value?.setValue(&#39;# Welcome to Vditor!&#39;); }, }); }); </script> ``` ### 对比推荐 - **`@kangc/v-md-editor`**:专为Vue3设计,API友好,样式美观,功能齐全,推荐使用- **`mavon-editor`**:成熟稳定,同时支持Vue2Vue3,但界面相对传统。 - **`vditor`**:不依赖框架,功能强大,但需要手动封装成Vue组件。 ### 完整示例:使用 `@kangc/v-md-editor` 实现双向编辑预览 ```vue <template> <div class="markdown-editor"> <v-md-editor v-model="text" :mode="mode" height="600px" @change="handleChange" @save="handleSave" > <template #left-toolbar> <button @click="mode = &#39;edit&#39;">编辑</button> <button @click="mode = &#39;preview&#39;">预览</button> </template> </v-md-editor> </div> </template> <script setup lang="ts"> import { ref } from &#39;vue&#39;; const text = ref(&#39;# 标题\n编辑内容...&#39;); const mode = ref<&#39;edit&#39; | &#39;preview&#39;>(&#39;edit&#39;); const handleChange = (newText: string) => { console.log(&#39;内容变更:&#39;, newText); }; const handleSave = () => { console.log(&#39;保存内容:&#39;, text.value); }; </script> <style scoped> .markdown-editor { margin: 20px; } </style> ``` ### 注意事项 1. **图片上传**:编辑器通常需要配置图片上传功能,可参考各编辑器的文档实现。 2. **主题定制**:可以通过CSS覆盖默认样式进行定制。 3. **性能优化**:对于超大文档,考虑使用防抖或分块渲染。 §§相关问题§§ 1. 如何在Markdown编辑器中实现图片上传功能? 2. Vue3中如何自定义Markdown编辑器的工具栏? 3. 如何将Markdown编辑器与后端API集成实现自动保存? </think> ### Vue3使用 Markdown 编辑器的方法详解 在 Vue3使用 Markdown 编辑器有多种方案,以下是四种主流实现方式(含完整代码): --- ### 方案一:使用 `@kangc/v-md-editor`(推荐) **特点**:专为 Vue3 设计、支持主题切换、代码高亮、图片上传 ```bash # 安装 npm install @kangc/v-md-editor@next -S ``` ```vue <template> <v-md-editor v-model="content" height="500px" :disabled-menus="[]" @upload-image="handleImageUpload" /> </template> <script setup> import { ref } from &#39;vue&#39;; import VMdEditor from &#39;@kangc/v-md-editor&#39;; import &#39;@kangc/v-md-editor/lib/style/base-editor.css&#39;; import githubTheme from &#39;@kangc/v-md-editor/lib/theme/github.js&#39;; import &#39;@kangc/v-md-editor/lib/theme/style/github.css&#39;; // 启用主题 VMdEditor.use(githubTheme); const content = ref(&#39;# 标题\n> 编辑内容...&#39;); // 图片上传处理 const handleImageUpload = (file) => { return new Promise((resolve) => { const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = () => resolve(reader.result); }); }; </script> ``` --- ### 方案二:使用 `mavon-editor`(兼容 Vue2/Vue3) **特点**:双屏编辑预览、公式支持、流程图 ```bash # 安装 npm install mavon-editor ``` ```vue <template> <mavon-editor v-model="content" :toolbars="toolbarConfig" @change="handleChange" /> </template> <script setup> import { ref } from &#39;vue&#39;; import mavonEditor from &#39;mavon-editor&#39;; import &#39;mavon-editor/dist/css/index.css&#39;; const content = ref(&#39;&#39;); const toolbarConfig = { bold: true, // 加粗 italic: true, // 斜体 header: true // 标题 }; const handleChange = (value) => { console.log(&#39;内容变更:&#39;, value); }; </script> ``` --- ### 方案三:使用 `vditor`(框架无关) **特点**:高性能、支持脑图、数学公式 ```bash # 安装 npm install vditor ``` ```vue <template> <div id="vditor-container"></div> </template> <script setup> import { onMounted, ref } from &#39;vue&#39;; import Vditor from &#39;vditor&#39;; import &#39;vditor/dist/index.css&#39;; const editor = ref(null); onMounted(() => { editor.value = new Vditor(&#39;vditor-container&#39;, { height: 500, toolbarConfig: { pin: true }, cache: { enable: false }, after: () => { editor.value.setValue(&#39;## Vditor 示例\n$E=mc^2$&#39;); } }); }); </script> ``` --- ### 方案四:使用 `element-plus` 集成(轻量级) ```vue <template> <el-input v-model="content" type="textarea" rows="10" placeholder="输入Markdown..." /> <div v-html="compiledMarkdown" class="preview"/> </template> <script setup> import { ref, computed } from &#39;vue&#39;; import { marked } from &#39;marked&#39;; import DOMPurify from &#39;dompurify&#39;; const content = ref(&#39;&#39;); const compiledMarkdown = computed(() => { return DOMPurify.sanitize(marked.parse(content.value)); }); </script> <style scoped> .preview { border: 1px solid #ddd; padding: 15px; margin-top: 20px; min-height: 200px; } </style> ``` --- ### 功能对比表 | 编辑器 | Vue3支持 | 实时预览 | 图片上传 | 数学公式 | 主题切换 | |----------------|----------|----------|----------|----------|----------| | v-md-editor | ✅ | ✅ | ✅ | ✅ | ✅ | | mavon-editor | ✅ | ✅ | ✅ | ✅ | ❌ | | vditor | ✅ | ✅ | ✅ | ✅ | ✅ | | element-plus | ✅ | 需自实现 | ❌ | ❌ | ❌ | > 推荐选择 `@kangc/v-md-editor`,API 设计最符合 Vue3 习惯,且维护活跃[^3] --- ### 实时双向同步示例(编辑器+预览) ```vue <template> <div class="editor-container"> <v-md-editor v-model="content" height="400px"/> <div class="preview"> <v-md-preview :text="content" /> </div> </div> </template> <script setup> import { ref } from &#39;vue&#39;; import VMdEditor from &#39;@kangc/v-md-editor&#39;; import VMdPreview from &#39;@kangc/v-md-editor/lib/preview&#39;; import &#39;@kangc/v-md-editor/lib/style/preview.css&#39;; const content = ref(&#39;&#39;); </script> <style scoped> .editor-container { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; } .preview { border-left: 1px solid #eee; padding-left: 20px; } </style> ``` --- ### 常见问题解决 1. **图片上传失败** 实现 `@upload-image` 事件,需返回 `Promise<image_url>` ```typescript const handleImageUpload = async (file) => { const formData = new FormData(); formData.append(&#39;file&#39;, file); const { url } = await axios.post(&#39;/api/upload&#39;, formData); return url; // 返回图片URL }; ``` 2. **数学公式不显示** 在 `v-md-editor` 中额外配置: ```typescript import createKatexPlugin from &#39;@kangc/v-md-editor/lib/plugins/katex/cdn&#39;; VMdEditor.use(createKatexPlugin()); ``` 3. **暗黑模式切换** ```typescript // 切换主题 import darkTheme from &#39;@kangc/v-md-editor/lib/theme/dark&#39;; VMdEditor.use(darkTheme); ``` ---
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值