Md编辑器整合-react

推荐md编辑器 - ByteMD

在线体验:ByteMD Playground

git 链接:GitHub - pd4d10/bytemd: ByteMD v1 repository

安装

编辑器本体

复制代码
pnpm install @bytemd/react

官方插件 - 代码高亮 & 支持 GFM 功能(自动链接文字、删除线、表格、任务列表等)

复制代码
pnpm install @bytemd/plugin-highlight @bytemd/plugin-gfm

MdEditor 组件

复制代码
import gfm from '@bytemd/plugin-gfm'
import { Editor } from '@bytemd/react'
import highlight from '@bytemd/plugin-highlight'
import 'bytemd/dist/index.css'
import 'highlight.js/styles/vs.css'
import './github-markdown-light.css'
import './index.css'

interface Props {
    value?: string
    onChange?: (value: string) => void
    placeholder?: string
}

const plugins = [gfm(), highlight()]

/**
 * 编辑器
 * @param props
 * @constructor
 */
const MdEditor = (props: Props) => {
    const { value = '', onChange, placeholder } = props

    return (
        <Editor
            className="md-editor"
            value={value}
            placeholder={placeholder}
            mode="split"
            plugins={plugins}
            onChange={onChange}
        />
    )
}

export default MdEditor

MdViewer 组件

复制代码
import gfm from '@bytemd/plugin-gfm'
import { Viewer } from '@bytemd/react'
import highlight from '@bytemd/plugin-highlight'
import 'bytemd/dist/index.css'
import 'highlight.js/styles/vs.css'
import '../MdEditor/github-markdown-light.css'
import './index.css'

interface Props {
    value?: string
}

const plugins = [gfm(), highlight()]

/**
 * Md 浏览器
 * @param props
 * @constructor
 */
const MdViewer = (props: Props) => {
    const { value = '' } = props

    return (
        <div className="md-viewer">
            <Viewer value={value} plugins={plugins} />
        </div>
    )
}

export default MdViewer

引入 github markdown 样式

github 地址:GitHub - sindresorhus/github-markdown-css: The minimal amount of CSS to replicate the GitHub Markdown style · GitHub

测试

在全局基础布局组件引入编辑器组件进行效果测试

复制代码
'use client'
import { ProLayout } from '@ant-design/pro-components'
import { Dropdown, Input } from 'antd'
import React, { useEffect, useState } from 'react'
import MdEditor from '@/components/MdEditor'
import MdViewer from '@/components/MdViewer'

interface props {
    children: React.ReactNode
}


export default function BasicLayout({ children }: props) {

    const [text, setText] = useState<string>('')
    ...

    return (
        <div
            id="basic-layout"
            style={{
                height: '100vh',
            }}
        >
            <ProLayout
                ...
            >
                <MdEditor value={text} onChange={setText} />
                <MdViewer value={text} />
                {children}
            </ProLayout>
        </div>
    )
}

效果图

相关推荐
前端snow3 小时前
ai agent --- postgreSQL 关系型数据库
前端
星辰徐哥4 小时前
鸿蒙平台 KDevelop 集成开发环境适配实战:基于 Electron 壳方案的跨平台多语言代码编辑器开发
electron·编辑器·harmonyos
卷无止境4 小时前
ECharts:把数据变成故事的可视化利器
前端
Eric_见嘉4 小时前
在职前端 Skill 和 MCP 分享
前端·后端·agent
liangshanbo12155 小时前
面试题:如何优化 Webpack 的打包速度?
前端·webpack·node.js
wengad5 小时前
从 wangEditor 迁到 wangEditor-next
前端·javascript·vue.js
行者-全栈开发6 小时前
【前端/JS框架】CVE-2026-3854:GitHub Enterprise Server X-Stat注入XSS漏洞深度剖析与紧急修复指南
前端·javascript·github
计算机魔术师7 小时前
AMD砸82亿买下李飞飞,这次不卖芯片卖什么?
前端
hzxpaipai7 小时前
杭州企业官网怎么建设?从策划到上线的5步建站方法
运维·服务器·前端·网络
怕浪猫8 小时前
2026年金九银十,我面了22个前端
前端·javascript·面试