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

好用的 Markdown 编辑器组件

ByteMD

bytedance/bytemd: ByteMD v1 repository (github.com)

这里由于我的项目是 Next,所以安装 @bytemd/react, 阅读官方文档,执行命令来安装编辑器主体、以及 gfm(表格支持)插件、highlight 代码高亮插件:

npm i @bytemd/react
npm i @bytemd/plugin-highlight @bytemd/plugin-gfm

但是浏览器的样式不好看,我们可以引入第三方主题:
github-markdown-css

npm install github-markdown-css
import 'github-markdown-css/github-markdown-light.css';

然后使用组件:

src/components/MdEditor/index.tsx

import { Editor } from "@bytemd/react";
import gfm from "@bytemd/plugin-gfm";
import highlight from "@bytemd/plugin-highlight";
import 'github-markdown-css/github-markdown-light.css';
import "bytemd/dist/index.css";
import "highlight.js/styles/vs.css";
import "./index.css";interface Props {value?: string;onChange?: (v: string) => void;placeholder?: string;
}const plugins = [gfm(), highlight()];/*** Markdown 编辑器* @param props* @constructor*/
const MdEditor = (props: Props) => {const { value = "", onChange, placeholder } = props;return (<div className="md-editor"><Editorvalue={value || ""}placeholder={placeholder}mode="split"plugins={plugins}onChange={onChange}/></div>);
};export default MdEditor;

把 MdEditor 当前输入的值暴露给父组件,便于父组件去使用,同时也是提高组件的通用性,所以定义了属性和属性类型,把 value 和 onChange 事件交给父组件去管理。

src/components/MdEditor/index.css

.md-editor {.bytemd-toolbar-icon.bytemd-tippy.bytemd-tippy-right:last-child {display: none;}
}

隐藏编辑器中不需要的操作图标(像 GitHub 图标)

编辑好文本,自然有浏览文本的地方,所以浏览器:

src/components/MdViewer/index.tsx

import { Viewer } from "@bytemd/react";
import gfm from "@bytemd/plugin-gfm";
import highlight from "@bytemd/plugin-highlight";
import 'github-markdown-css/github-markdown-light.css';
import "bytemd/dist/index.css";
import "highlight.js/styles/vs.css";
import "./index.css";interface Props {value?: string;
}const plugins = [gfm(), highlight()];/*** Markdown 浏览器* @param props* @constructor*/
const MdViewer = (props: Props) => {const { value = "" } = props;return (<div className="md-viewer"><Viewer value={value} plugins={plugins} /></div>);
};export default MdViewer;

src/components/MdViewer/index.css

.md-viewer {.bytemd-toolbar-icon.bytemd-tippy.bytemd-tippy-right:last-child {display: none;}
}

可以在任意客户端渲染页面(或组件)引入组件进行测试,这是因为该组件用到了 useRef 之类的仅客户端才支持的函数。

const [text, setText] = useState<string>('');<MdEditor value={text} onChange={setText} />
<MdViewer value={text} />

md-editor-v3

文本编辑器/md-editor-v3 (gitee.com)

这个是之前写 Vue3 用过的一个编辑器,也很不错,用法简单,同样支持 Vue、React 等。

安装

yarn add md-editor-v3

更多使用及贡献方式参考:md-editor-extension

编辑器模式

<template><MdEditor v-model="text" />
</template><script setup>
import { ref } from 'vue';
import { MdEditor } from 'md-editor-v3';
import 'md-editor-v3/lib/style.css';const text = ref('# Hello Editor');
</script>

仅预览模式

<template><MdPreview :editorId="id" :modelValue="text" /><MdCatalog :editorId="id" :scrollElement="scrollElement" />
</template><script setup>
import { ref } from 'vue';
import { MdPreview, MdCatalog } from 'md-editor-v3';
import 'md-editor-v3/lib/preview.css';const id = 'preview-only';
const text = ref('# Hello Editor');
const scrollElement = document.documentElement;
</script>

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Python和MATLAB(Java)及Arduino和Raspberry Pi(树莓派)点扩展函数导图
  • Laravel安全应用模块示例教程
  • 【视频讲解】Python贝叶斯卷积神经网络分类胸部X光图像数据集实例
  • 仿华为车机UI--图标从Workspace拖动到Hotseat同时保留图标在原来位置
  • 从监控到智能:EasyCVR视频汇聚平台助力加油站安全监管升级转型
  • 黑神话:游戏的诞生
  • 桥接模式bridge
  • leetcode :746使用最小花费爬楼梯
  • 微软云技术深度解析与实战案例
  • 算法打卡——田忌赛马问题
  • (pycharm)安装python库函数Matplotlib步骤
  • 微服务架构设计模式简要介绍
  • 经验笔记:Spring Boot项目结构
  • Nacos注册中心与OpenFeign远程调用
  • PHP轻量级高性能HTTP服务框架 - webman
  • Bootstrap JS插件Alert源码分析
  • create-react-app做的留言板
  • CSS中外联样式表代表的含义
  • es6(二):字符串的扩展
  • Java深入 - 深入理解Java集合
  • Java-详解HashMap
  • leetcode388. Longest Absolute File Path
  • npx命令介绍
  • Python打包系统简单入门
  • Python连接Oracle
  • rabbitmq延迟消息示例
  • session共享问题解决方案
  • Swoft 源码剖析 - 代码自动更新机制
  • 翻译 | 老司机带你秒懂内存管理 - 第一部(共三部)
  • 精益 React 学习指南 (Lean React)- 1.5 React 与 DOM
  • 聊一聊前端的监控
  • 腾讯优测优分享 | Android碎片化问题小结——关于闪光灯的那些事儿
  • 吴恩达Deep Learning课程练习题参考答案——R语言版
  • 《天龙八部3D》Unity技术方案揭秘
  • ionic入门之数据绑定显示-1
  • 好程序员大数据教程Hadoop全分布安装(非HA)
  • 支付宝花15年解决的这个问题,顶得上做出十个支付宝 ...
  • ​Distil-Whisper:比Whisper快6倍,体积小50%的语音识别模型
  • ​LeetCode解法汇总518. 零钱兑换 II
  • ​低代码平台的核心价值与优势
  • ‌移动管家手机智能控制汽车系统
  • #数据结构 笔记一
  • (13)DroneCAN 适配器节点(一)
  • (4)事件处理——(6)给.ready()回调函数传递一个参数(Passing an argument to the .ready() callback)...
  • (Java数据结构)ArrayList
  • (安卓)跳转应用市场APP详情页的方式
  • (博弈 sg入门)kiki's game -- hdu -- 2147
  • (二) 初入MySQL 【数据库管理】
  • (二十五)admin-boot项目之集成消息队列Rabbitmq
  • (深入.Net平台的软件系统分层开发).第一章.上机练习.20170424
  • (十六)Flask之蓝图
  • (转载)虚幻引擎3--【UnrealScript教程】章节一:20.location和rotation
  • (轉貼) 資訊相關科系畢業的學生,未來會是什麼樣子?(Misc)
  • ****** 二 ******、软设笔记【数据结构】-KMP算法、树、二叉树
  • .MSSQLSERVER 导入导出 命令集--堪称经典,值得借鉴!