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>
    )
}

效果图

相关推荐
东方小月7 小时前
从零开发一个 Coding Agent(四):使用状态机校验大模型事件流
前端·人工智能·后端
Csvn7 小时前
🧩 ESM vs CJS 混用的 7 个「天坑」——从 TypeScript 编译到 Node 与浏览器
前端
Csvn7 小时前
🎯 Web 性能 API 集合:Performance Observer 的 5 个冷门妙用
前端
Csvn8 小时前
深入 React 闭包陷阱:从根源上理解并根治 stale closure
前端
whyfail8 小时前
前端学 Spring Boot(8):接口为什么越用越慢?
前端·spring boot·后端
用户059540174468 小时前
LangChain 记忆测试踩坑实录:这两个坑让我排查了 4 小时
前端·css
程序员黑豆9 小时前
鸿蒙应用开发:@Monitor 装饰器使用教程
前端·harmonyos
SamChan909 小时前
在Web应用中集成PDF多语言翻译功能:PDFTranslator API实战指南
前端·python·ai·pdf·yapi·机器翻译
kyriewen9 小时前
AI Agent 9秒删光了生产数据库——我给自己的项目做了5个紧急检查
前端·ai编程·claude
IT_陈寒10 小时前
JavaScript的this又双叒叕让我怀疑人生了
前端·人工智能·后端