当前位置: 首页 > article >正文

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

一、安装

# 使用 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>

http://www.kler.cn/a/566923.html

相关文章:

  • 阿里云oss文件上传springboot若依java
  • 25新闻研究生复试面试问题汇总 新闻专业知识问题很全! 新闻复试全流程攻略 新闻考研复试调剂真题总结
  • 深度解读 AMS1117:从电气参数到应用电路的全面剖析
  • day02_Java基础
  • 网络安全技术与应用
  • C++题解(31) 2025顺德一中少科院信息学创新班(四期)考核:U537296 青蛙的距离 题解
  • Tomcat的server.xml配置详解
  • Tomcat10下载安装教程
  • ssh配置 远程控制 远程协作 github本地配置
  • 量子计算 + 药物开发:打开分子模拟的新纪元
  • java面试笔记(二)
  • 版图自动化连接算法开发 00002 ------ 添加一个中间点实现 Manhattan 方式连接两个给定的坐标点
  • Brave 132 编译指南 Android 篇 - 编译准备:系统要求与推荐工具 (三)
  • 苹果iPhone 17 Pro系列将配备12GB内存,AI功能成升级关键
  • 512台无人机!GCBF+如何实现大规模多智能体动态避障的?
  • 单链表删除算法(p=L; j=0;与p=p->next;j=1的辨析)
  • HBuilder X中,uni-app、js的延时操作及定时器
  • 【网络】网络层IP协议
  • Ubuntu22.04安装docker教程
  • 大白话解释负载均衡Nginx是什么 有什么用 怎么用#