• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    vue3中展示markdown格式文章的三种形式

    发布者: 福建二哥 | 发布时间: 2025-6-16 07:43| 查看数: 169| 评论数: 0|帖子模式

    一、安装
    1. # 使用 npm
    2. npm i @kangc/v-md-editor -S

    3. # 使用yarn
    4. yarn add @kangc/v-md-editor
    复制代码
    二、三种实现形式


    1、编辑器的只读模式

    main.ts文件中配置:
    1. import VMdEditor from '@kangc/v-md-editor';
    2. import '@kangc/v-md-editor/lib/style/base-editor.css';

    3. const app = createApp(/*...*/);

    4. app.use(VMdEditor);
    复制代码
    使用的组件中:
    1. <template>
    2.   <v-md-editor v-model="text" mode="preview"></v-md-editor>
    3. </template>、
    4. //text为要传入的markdown格式的内容
    复制代码
    2、预览组件

    main.ts中配置:
    1. import VMdPreview from '@kangc/v-md-editor/lib/preview';
    2. import '@kangc/v-md-editor/lib/style/preview.css';

    3. const app = createApp(/*...*/);

    4. app.use(VMdPreview);
    复制代码
    使用的组件中:
    1. <template>
    2.   <v-md-preview :text="text"></v-md-preview>
    3. </template>
    4. //text为要传入的markdown格式的内容
    复制代码
    3、html预览组件

    main.ts中配置:
    1. import VMdPreviewHtml from '@kangc/v-md-editor/lib/preview-html';
    2. import '@kangc/v-md-editor/lib/style/preview-html.css';

    3. // 引入使用主题的样式
    4. import '@kangc/v-md-editor/lib/theme/style/vuepress';

    5. const app = createApp(/*...*/);

    6. app.use(VMdPreviewHtml);
    复制代码
    使用的组件中:
    1. <template>
    2.   <!-- preview-class 为主题的样式类名,例如vuepress就是vuepress-markdown-body -->
    3.   <v-md-preview-html :html="html" preview-class="vuepress-markdown-body"></v-md-preview-html>
    4. </template>
    复制代码
    三、实现其他功能


    1、设置外观
    1. import vuepressTheme from '@kangc/v-md-editor/lib/theme/vuepress.js';

    2. import '@kangc/v-md-editor/lib/theme/style/vuepress.css';   
    3. //这个css文件,它与 vuepressTheme 主题相关,定义了主题的颜色、字体、间距等样式。

    4. // 使用 vuepress 主题
    5. VueMarkdownEditor.use(vuepressTheme);
    复制代码
    2、对代码进行语法高亮并显示代码语言
    1. import Prism from 'prismjs';

    2. VueMarkdownEditor.use({
    3.   Prism,
    4. });
    复制代码
    3、代码块显示行号
    1. //main.ts中

    2. import VueMarkdownEditor from '@kangc/v-md-editor';
    3. import createLineNumbertPlugin from '@kangc/v-md-editor/lib/plugins/line-number/index';

    4. VueMarkdownEditor.use(createLineNumbertPlugin());
    复制代码
    4、高亮代码行
    1. import VueMarkdownEditor from '@kangc/v-md-editor';
    2. import createHighlightLinesPlugin from '@kangc/v-md-editor/lib/plugins/highlight-lines/index';
    3. import '@kangc/v-md-editor/lib/plugins/highlight-lines/highlight-lines.css';

    4. VueMarkdownEditor.use(createHighlightLinesPlugin());
    复制代码
    5、快捷复制代码

    main.ts中配置:
    1. import VueMarkdownEditor from '@kangc/v-md-editor';
    2. import createCopyCodePlugin from '@kangc/v-md-editor/lib/plugins/copy-code/index';
    3. import '@kangc/v-md-editor/lib/plugins/copy-code/copy-code.css';

    4. VueMarkdownEditor.use(createCopyCodePlugin());
    复制代码
    组件中使用:
    1. <template>
    2.   <v-md-editor v-model="text" height="500px" @copy-code-success="handleCopyCodeSuccess" />
    3. </template>

    4. //使用@copy-code-success
    5. <script>
    6. export default {
    7.   methods: {
    8.     handleCopyCodeSuccess(code) {
    9.       console.log(code);
    10.     },
    11.   },
    12. };
    13. </script>
    复制代码
    四、注意

    如果按正常流程配置后,内容出不来,可以选择自己新开一个项目再操作一遍,如果这个时候是正常的,那可能就是原来的项目配置的版本过低,可以选择更新一下项目中的各项配置

    五、方法补充

    Vue 3中如何实现Markdown展示
    使用Vue 3中的markdown-it或vue-markdown等库来实现类似React代码中的Markdown渲染功能。以下是一个完整的Vue 3实现方案:
    步骤一:安装必要的依赖
    1. npm install markdown-it vue-demi markdown-it-katex markdown-it-breaks markdown-it-mathjax3 markdown-it-highlightjs highlight.js
    复制代码
    步骤二:创建Markdown组件
    1. <template>
    2.   <div class="markdown-body" v-html="renderedContent"></div>
    3. </template>

    4. <script setup>
    5. import { ref, computed, onMounted } from 'vue'
    6. import MarkdownIt from 'markdown-it'
    7. import mk from 'markdown-it-katex'
    8. import breaks from 'markdown-it-breaks'
    9. import hljs from 'highlight.js'
    10. import 'highlight.js/styles/atom-one-light.css'
    11. import 'katex/dist/katex.min.css'

    12. const props = defineProps({
    13.   content: {
    14.     type: String,
    15.     required: true
    16.   }
    17. })

    18. // 创建markdown-it实例并配置插件
    19. const md = new MarkdownIt({
    20.   html: true,
    21.   linkify: true,
    22.   typographer: true,
    23.   breaks: true,
    24.   highlight: function (str, lang) {
    25.     if (lang && hljs.getLanguage(lang)) {
    26.       try {
    27.         return `<pre class="hljs"><code>${hljs.highlight(str, { language: lang, ignoreIllegals: true }).value}</code></pre>`;
    28.       } catch (__) {}
    29.     }
    30.     return `<pre class="hljs"><code>${md.utils.escapeHtml(str)}</code></pre>`;
    31.   }
    32. })
    33. .use(mk) // 启用KaTeX数学公式支持
    34. .use(breaks); // 启用换行支持

    35. // 配置链接在新窗口打开
    36. const defaultRender = md.renderer.rules.link_open || function(tokens, idx, options, env, self) {
    37.   return self.renderToken(tokens, idx, options);
    38. };
    39. md.renderer.rules.link_open = function (tokens, idx, options, env, self) {
    40.   tokens[idx].attrPush(['target', '_blank']);
    41.   return defaultRender(tokens, idx, options, env, self);
    42. };

    43. // 计算渲染后的HTML
    44. const renderedContent = computed(() => {
    45.   return md.render(props.content || '');
    46. });
    47. </script>

    48. <style>
    49. .markdown-body {
    50.   font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
    51.   line-height: 1.6;
    52.   padding: 20px;
    53.   color: #24292e;
    54. }

    55. .markdown-body code {
    56.   font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
    57.   background-color: rgba(27, 31, 35, 0.05);
    58.   border-radius: 3px;
    59.   padding: 0.2em 0.4em;
    60. }

    61. .markdown-body pre {
    62.   background-color: #f6f8fa;
    63.   border-radius: 3px;
    64.   padding: 16px;
    65.   overflow: auto;
    66. }

    67. .hljs {
    68.   padding: 0;
    69.   background: transparent;
    70. }
    71. </style>
    复制代码
    步骤三:使用组件
    1. <template>
    2.   <div>
    3.     <h1>Markdown 预览</h1>
    4.     <MarkdownView :content="markdownContent" />
    5.   </div>
    6. </template>

    7. <script setup>
    8. import MarkdownView from '@/components/MarkdownView.vue'
    9. import { ref } from 'vue'

    10. const markdownContent = ref(`
    11. # 标题

    12. 这是一段普通文本

    13. ## 代码示例
    14. \`\`\`javascript
    15. const hello = 'world';
    16. console.log(hello);
    17. \`\`\`

    18. ## 数学公式
    19. $E = mc^2$

    20. ## 表格
    21. | 姓名 | 年龄 |
    22. | ---- | ---- |
    23. | 张三 | 20   |
    24. | 李四 | 25   |
    25. `)
    26. </script>
    复制代码
    功能对照表
    以下是React示例和Vue实现的功能对照:
    React功能Vue实现方式ReactMarkdownmarkdown-itremark-mathmarkdown-it-katexremark-breaksmarkdown-it-breaksrehype-katexmarkdown-it-katex (内置支持)remark-gfmmarkdown-it (内置支持GFM)SyntaxHighlighterhighlight.js补充说明
    1.功能:

    • Markdown渲染
    • 数学公式支持
    • 代码高亮
    • 表格支持
    • 自动链接
    • 在新窗口打开链接
    2.如果需要更多功能,可以添加其他markdown-it插件,例如:
    1. npm install markdown-it-emoji markdown-it-sub markdown-it-sup markdown-it-footnote
    复制代码
    3.要实现与GitHub样式一致的显示效果,可以添加:
    1. npm install github-markdown-css
    复制代码
    4.然后在main.js中导入:
    1. import 'github-markdown-css/github-markdown.css'
    复制代码
    到此这篇关于vue3中展示markdown格式文章的三种形式的文章就介绍到这了,更多相关vue3展示markdown内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    来源:https://www.jb51.net/javascript/339451xm1.htm
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表