import Prism from 'prismjs' import 'prismjs/components/prism-markdown' import React, { useCallback, useMemo } from 'react' import { Slate, Editable, withReact } from 'slate-react' import { Text, createEditor, Descendant } from 'slate' import { withHistory } from 'slate-history' import { css } from '@emotion/css' const MarkdownPreviewExample = () => { const renderLeaf = useCallback(props => , []) const editor = useMemo(() => withHistory(withReact(createEditor())), []) const decorate = useCallback(([node, path]) => { const ranges = [] if (!Text.isText(node)) { return ranges } const getLength = token => { if (typeof token === 'string') { return token.length } else if (typeof token.content === 'string') { return token.content.length } else { return token.content.reduce((l, t) => l + getLength(t), 0) } } const tokens = Prism.tokenize(node.text, Prism.languages.markdown) let start = 0 for (const token of tokens) { const length = getLength(token) const end = start + length if (typeof token !== 'string') { ranges.push({ [token.type]: true, anchor: { path, offset: start }, focus: { path, offset: end }, }) } start = end } return ranges }, []) return ( ) } const Leaf = ({ attributes, children, leaf }) => { return ( {children} ) } const initialValue: Descendant[] = [ { type: 'paragraph', children: [ { text: 'Slate is flexible enough to add **decorations** that can format text based on its content. For example, this editor has **Markdown** preview decorations on it, to make it _dead_ simple to make an editor with built-in Markdown previewing.', }, ], }, { type: 'paragraph', children: [{ text: '## Try it out!' }], }, { type: 'paragraph', children: [{ text: 'Try it out for yourself!' }], }, ] export default MarkdownPreviewExample