import React from 'react'
import MarkdownIt from 'markdown-it'
import hljs from 'highlight.js'
import 'highlight.js/styles/xcode.css'
import MdEditor from 'react-markdown-editor-lite'
import 'react-markdown-editor-lite/lib/index.css'
// Initialize Markdown parser
const mdParser = new MarkdownIt()
// Checkbox plugin
mdParser.use(require('markdown-it-checkbox'), {
divWrap: true,
divClass: 'cb',
idPrefix: 'cbx_'
})
// link attributes
mdParser.use(require('markdown-it-link-attributes'), {
attrs: {
target: '_blank',
rel: 'noopener'
}
})
// Syntax highlighting
mdParser.options.highlight = (str, lang) => {
if (lang && hljs.getLanguage(lang)) {
try {
return '<pre class="hljs"><code>' +
hljs.highlight(lang, str, true).value +
'</code></pre>'
} catch (__) { }
}
return '<pre class="hljs"><code>' + mdParser.utils.escapeHtml(str) + '</code></pre>';
}
// Markdown UI
export default function MarkdownEditor ({ text, onTextChange }) {
function handleEditorChange({ html, text }) {
onTextChange(text)
}
return (
<MdEditor
value={text}
style={{ height: "500px", marginTop: 8, marginBottom: 4 }}
renderHTML={(text) => mdParser.render(text)}
onChange={handleEditorChange}
/>
)
}