md-editor 带有预览功能的简单 Markdown 编辑器,使用 React.js 和 TypeScript 实现。 提供具有语法突出显示,自动按需懒加载 prismjs 的 187 种语言高亮的简单 Markdown 编辑器。 这基于 textarea 封装,因此它不依赖于任何现代代码编辑器,如 Acs,CodeMirror,Monaco 等,同时支持工具栏高度定制。
textarea
uiw
npm i @uiw/react-md-editor
import React from "react"; import ReactDOM from "react-dom"; import MDEditor from '@uiw/react-md-editor'; export default function App() { const [value, setValue] = React.useState("**Hello world!!!**"); return ( <div className="container"> <MDEditor value={value} onChange={setValue} /> <MDEditor.Markdown source={value} /> </div> ); }
import React from "react"; import ReactDOM from "react-dom"; import MDEditor, { commands } from '@uiw/react-md-editor'; const title3: commands.ICommand = { name: 'title3', keyCommand: 'title3', buttonProps: { 'aria-label': 'Insert title3' }, icon: ( <svg width="12" height="12" viewBox="0 0 520 520"> <path fill="currentColor" d="M15.7083333,468 C7.03242448,468 0,462.030833 0,454.666667 L0,421.333333 C0,413.969167 7.03242448,408 15.7083333,408 L361.291667,408 C369.967576,408 377,413.969167 377,421.333333 L377,454.666667 C377,462.030833 369.967576,468 361.291667,468 L15.7083333,468 Z M21.6666667,366 C9.69989583,366 0,359.831861 0,352.222222 L0,317.777778 C0,310.168139 9.69989583,304 21.6666667,304 L498.333333,304 C510.300104,304 520,310.168139 520,317.777778 L520,352.222222 C520,359.831861 510.300104,366 498.333333,366 L21.6666667,366 Z M136.835938,64 L136.835937,126 L107.25,126 L107.25,251 L40.75,251 L40.75,126 L-5.68434189e-14,126 L-5.68434189e-14,64 L136.835938,64 Z M212,64 L212,251 L161.648438,251 L161.648438,64 L212,64 Z M378,64 L378,126 L343.25,126 L343.25,251 L281.75,251 L281.75,126 L238,126 L238,64 L378,64 Z M449.047619,189.550781 L520,189.550781 L520,251 L405,251 L405,64 L449.047619,64 L449.047619,189.550781 Z" /> </svg> ), execute: (state: commands.TextState, api: commands.TextApi) => { let modifyText = `### ${state.selectedText}\n`; if (!state.selectedText) { modifyText = `### `; } api.replaceSelection(modifyText); }, }; export default function App() { const [value, setValue] = React.useState("**Hello world!!!**"); return ( <div className="container"> <MDEditor value="Hello Markdown!" commands={[ commands.bold, commands.hr, commands.italic, commands.divider, commands.codeEdit, commands.codeLive, commands.codePreview, commands.divider, commands.fullscreen, // Custom Toolbars title3, ]} /> </div> ); }
import React from "react"; import ReactDOM from "react-dom"; import MDEditor from '@uiw/react-md-editor'; export default function App() { return ( <div className="container"> <MDEditor.Markdown source="Hello Markdown!" /> </div> ); }
KaTeX 是一个快速,易于使用的 JavaScript 库,用于在 Web 上进行 TeX 数学渲染,我们通过 KaTeX执行数学渲染。
KaTeX
以下实例是在 CodeSandbox 中的预览效果
npm install katex import React from "react"; import ReactDOM from "react-dom"; import MDEditor from '@uiw/react-md-editor'; import katex from 'katex'; import 'katex/dist/katex.css'; const mdKaTeX = `This is to display the \`\$\$\c = \\pm\\sqrt{a^2 + b^2}\$\$\` in one line \`\`\`KaTeX c = \\pm\\sqrt{a^2 + b^2} \`\`\` `; const renderers = { inlineCode: ({ children }) => { if (/^\$\$(.*)\$\$/.test(children)) { const html = katex.renderToString(children.replace(/^\$\$(.*)\$\$/, '$1'), { throwOnError: false, }); return <code dangerouslySetInnerHTML={{ __html: html }} /> } return children; }, code: ({ children, language, value }) => { if (language.toLocaleLowerCase() === 'katex') { const html = katex.renderToString(value, { throwOnError: false }); return ( <pre> <code dangerouslySetInnerHTML={{ __html: html }} /> </pre> ); } return children; } } export default function App() { return ( <MDEditor value={mdKaTeX} previewOptions={{ renderers: renderers }} /> ); }