一、安装
# 使用 npm npm i @kangc/v-md-editor -S # 使用yarn yarn add @kangc/v-md-editor
二、三种实现形式
1、编辑器的只读模式
main.ts文件中配置:
import VMdEditor from '@kangc/v-md-editor'; import '@kangc/v-md-editor/lib/style/base-editor.css'; const app = createApp(/*...*/); app.use(VMdEditor);
使用的组件中:
<template> <v-md-editor v-model="text" mode="preview"></v-md-editor> </template>、 //text为要传入的markdown格式的内容
2、预览组件
main.ts中配置:
import VMdPreview from '@kangc/v-md-editor/lib/preview'; import '@kangc/v-md-editor/lib/style/preview.css'; const app = createApp(/*...*/); app.use(VMdPreview);
使用的组件中:
<template> <v-md-preview :text="text"></v-md-preview> </template> //text为要传入的markdown格式的内容
3、html预览组件
main.ts中配置:
import VMdPreviewHtml from '@kangc/v-md-editor/lib/preview-html'; import '@kangc/v-md-editor/lib/style/preview-html.css'; // 引入使用主题的样式 import '@kangc/v-md-editor/lib/theme/style/vuepress'; const app = createApp(/*...*/); app.use(VMdPreviewHtml);
使用的组件中:
<template> <!-- preview-class 为主题的样式类名,例如vuepress就是vuepress-markdown-body --> <v-md-preview-html :html="html" preview-class="vuepress-markdown-body"></v-md-preview-html> </template>
三、实现其他功能
1、设置外观
import vuepressTheme from '@kangc/v-md-editor/lib/theme/vuepress.js'; import '@kangc/v-md-editor/lib/theme/style/vuepress.css'; //这个css文件,它与 vuepressTheme 主题相关,定义了主题的颜色、字体、间距等样式。 // 使用 vuepress 主题 VueMarkdownEditor.use(vuepressTheme);
2、对代码进行语法高亮并显示代码语言
import Prism from 'prismjs'; VueMarkdownEditor.use({ Prism, });
3、代码块显示行号
//main.ts中 import VueMarkdownEditor from '@kangc/v-md-editor'; import createLineNumbertPlugin from '@kangc/v-md-editor/lib/plugins/line-number/index'; VueMarkdownEditor.use(createLineNumbertPlugin());
4、高亮代码行
import VueMarkdownEditor from '@kangc/v-md-editor'; import createHighlightLinesPlugin from '@kangc/v-md-editor/lib/plugins/highlight-lines/index'; import '@kangc/v-md-editor/lib/plugins/highlight-lines/highlight-lines.css'; VueMarkdownEditor.use(createHighlightLinesPlugin());
5、快捷复制代码
main.ts中配置:
import VueMarkdownEditor from '@kangc/v-md-editor'; import createCopyCodePlugin from '@kangc/v-md-editor/lib/plugins/copy-code/index'; import '@kangc/v-md-editor/lib/plugins/copy-code/copy-code.css'; VueMarkdownEditor.use(createCopyCodePlugin());
组件中使用:
<template> <v-md-editor v-model="text" height="500px" @copy-code-success="handleCopyCodeSuccess" /> </template> //使用@copy-code-success <script> export default { methods: { handleCopyCodeSuccess(code) { console.log(code); }, }, }; </script>
四、注意
如果按正常流程配置后,内容出不来,可以选择自己新开一个项目再操作一遍,如果这个时候是正常的,那可能就是原来的项目配置的版本过低,可以选择更新一下项目中的各项配置
五、方法补充
Vue 3中如何实现Markdown展示
使用Vue 3中的markdown-it或vue-markdown等库来实现类似React代码中的Markdown渲染功能。以下是一个完整的Vue 3实现方案:
步骤一:安装必要的依赖
npm install markdown-it vue-demi markdown-it-katex markdown-it-breaks markdown-it-mathjax3 markdown-it-highlightjs highlight.js
步骤二:创建Markdown组件
<template> <div class="markdown-body" v-html="renderedContent"></div> </template> <script setup> import { ref, computed, onMounted } from 'vue' import MarkdownIt from 'markdown-it' import mk from 'markdown-it-katex' import breaks from 'markdown-it-breaks' import hljs from 'highlight.js' import 'highlight.js/styles/atom-one-light.css' import 'katex/dist/katex.min.css' const props = defineProps({ content: { type: String, required: true } }) // 创建markdown-it实例并配置插件 const md = new MarkdownIt({ html: true, linkify: true, typographer: true, breaks: true, highlight: function (str, lang) { if (lang && hljs.getLanguage(lang)) { try { return `<pre class="hljs"><code>${hljs.highlight(str, { language: lang, ignoreIllegals: true }).value}</code></pre>`; } catch (__) {} } return `<pre class="hljs"><code>${md.utils.escapeHtml(str)}</code></pre>`; } }) .use(mk) // 启用KaTeX数学公式支持 .use(breaks); // 启用换行支持 // 配置链接在新窗口打开 const defaultRender = md.renderer.rules.link_open || function(tokens, idx, options, env, self) { return self.renderToken(tokens, idx, options); }; md.renderer.rules.link_open = function (tokens, idx, options, env, self) { tokens[idx].attrPush(['target', '_blank']); return defaultRender(tokens, idx, options, env, self); }; // 计算渲染后的HTML const renderedContent = computed(() => { return md.render(props.content || ''); }); </script> <style> .markdown-body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif; line-height: 1.6; padding: 20px; color: #24292e; } .markdown-body code { font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; padding: 0.2em 0.4em; } .markdown-body pre { background-color: #f6f8fa; border-radius: 3px; padding: 16px; overflow: auto; } .hljs { padding: 0; background: transparent; } </style>
步骤三:使用组件
<template> <div> <h1>Markdown 预览</h1> <MarkdownView :content="markdownContent" /> </div> </template> <script setup> import MarkdownView from '@/components/MarkdownView.vue' import { ref } from 'vue' const markdownContent = ref(` # 标题 这是一段普通文本 ## 代码示例 \`\`\`javascript const hello = 'world'; console.log(hello); \`\`\` ## 数学公式 $E = mc^2$ ## 表格 | 姓名 | 年龄 | | ---- | ---- | | 张三 | 20 | | 李四 | 25 | `) </script>
功能对照表
以下是React示例和Vue实现的功能对照:
React功能 | Vue实现方式 |
---|---|
ReactMarkdown | markdown-it |
remark-math | markdown-it-katex |
remark-breaks | markdown-it-breaks |
rehype-katex | markdown-it-katex (内置支持) |
remark-gfm | markdown-it (内置支持GFM) |
SyntaxHighlighter | highlight.js |
补充说明
1.功能:
- Markdown渲染
- 数学公式支持
- 代码高亮
- 表格支持
- 自动链接
- 在新窗口打开链接
2.如果需要更多功能,可以添加其他markdown-it插件,例如:
npm install markdown-it-emoji markdown-it-sub markdown-it-sup markdown-it-footnote
3.要实现与GitHub样式一致的显示效果,可以添加:
npm install github-markdown-css
4.然后在main.js中导入:
import 'github-markdown-css/github-markdown.css'
到此这篇关于vue3中展示markdown格式文章的三种形式的文章就介绍到这了,更多相关vue3展示markdown内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
来源链接:https://www.jb51.net/javascript/339451xm1.htm
© 版权声明
本站所有资源来自于网络,仅供学习与参考,请勿用于商业用途,否则产生的一切后果将由您(转载者)自己承担!
如有侵犯您的版权,请及时联系3500663466#qq.com(#换@),我们将第一时间删除本站数据。
如有侵犯您的版权,请及时联系3500663466#qq.com(#换@),我们将第一时间删除本站数据。
THE END
暂无评论内容