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

效果图

相关推荐
JNX_SEMI1 小时前
ATR2652 GNSS双频LNA:0.75dB噪声系数与22dB高增益设计
前端·单片机·嵌入式硬件·物联网·硬件工程
拾年2751 小时前
Node.js 异步进化论:从 path 路径拼接到 fs 文件读取,我终于理解了回调地狱的救赎之路
前端·node.js
花颜yyds2 小时前
React Konva 开发基础速查手册
前端
小粉粉hhh2 小时前
Node.js(五)——编写接口
前端·javascript·node.js
MegatronKing2 小时前
AI时代我们要如何玩抓包测试
前端·后端·测试
uhakadotcom2 小时前
Scrapy-Redis 里面提供哪些即开即用的功能能力模块
前端·面试·github
进击切图仔2 小时前
SAM3 微调标注流水线和 Label Studio
java·服务器·前端
自然 醒3 小时前
前端如何实现在线预览office常见文件功能?
前端·vue.js
肉肉不吃 肉3 小时前
前端调试跨域如何解决
前端·vue.js
Liora_Yvonne3 小时前
为什么你写了3年前端还是搭不好一个项目
前端·架构