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

效果图

相关推荐
颜进强2 小时前
14 - OpenSpec 老页面改造骨架:定位 + 增量 + 回归三件套
前端·后端·ai编程
做前端的娜娜子2 小时前
async/await 错误处理:try...catch vs .catch() 完全指南
前端·面试·掘金·金石计划
半个落月2 小时前
React 性能优化入门:用 memo 避免无关的子组件重复渲染
前端·react.js
北城笑笑2 小时前
Server 18 ,Nginx + Vite 前端部署排错实战:从 `/api/gpt/chat` 404 到 9012 后端服务的完整定位过程
linux·运维·前端·nginx·ubuntu·vue
江华森2 小时前
从抓包到嗅探:用 C 语言把《计算机网络》第 9-13 章全部跑一遍
前端
IT_陈寒3 小时前
Vite打包给我挖的这个坑,差点搞崩我的项目
前端·人工智能·后端
恋猫de小郭3 小时前
Flutter A2UI 的正确用法,怎么把 AI 和动态 UI 结合有效生产
android·前端·flutter
程序员-珍3 小时前
安卓开发:同名 styles.xml 在不同分支/文件夹的合并规则
android·xml·前端
Ashley的成长之路3 小时前
前端性能优化实战手册·第5篇(最终篇):性能监控与 CI/CD 集成
前端·ci/cd·性能优化
chemddd3 小时前
第四节课:前端
前端