Repository navigation
Expand file tree
/
Copy pathmarkdown-preview.jsx
More file actions
141 lines (140 loc) · 3.71 KB
/
Copy pathmarkdown-preview.jsx
File metadata and controls
141 lines (140 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { css } from '@emotion/css'
import Prism from 'prismjs'
import 'prismjs/components/prism-markdown'
import React, { useCallback, useMemo } from 'react'
import { Node, createEditor } from 'slate'
import { withHistory } from 'slate-history'
import { Editable, Slate, withReact } from 'slate-react'
const markdownGrammar = Prism.languages.extend('markdown', {})
const titles = markdownGrammar.title
if (Array.isArray(titles)) {
for (const title of titles) {
if (!(title instanceof RegExp)) {
title.inside = {
...title.inside,
bold: markdownGrammar.bold,
italic: markdownGrammar.italic,
'code-snippet': markdownGrammar['code-snippet'],
}
}
}
}
const decoratedTokenTypes = new Set([
'bold',
'italic',
'underlined',
'title',
'list',
'hr',
'blockquote',
'code',
])
const MarkdownPreviewExample = () => {
const renderLeaf = useCallback(props => <Leaf {...props} />, [])
const editor = useMemo(() => withHistory(withReact(createEditor())), [])
const decorate = useCallback(([node, path]) => {
const ranges = []
if (!Node.isText(node)) {
return ranges
}
const decorateTokens = (tokens, offset) => {
for (const token of tokens) {
const start = offset
if (typeof token === 'string') {
offset += token.length
} else {
const content = Array.isArray(token.content)
? token.content
: [token.content]
offset = decorateTokens(content, offset)
if (decoratedTokenTypes.has(token.type)) {
ranges.push({
[token.type]: true,
anchor: { path, offset: start },
focus: { path, offset },
})
}
}
}
return offset
}
decorateTokens(Prism.tokenize(node.text, markdownGrammar), 0)
return ranges
}, [])
return (
<Slate editor={editor} initialValue={initialValue}>
<Editable
aria-label="Markdown editor"
decorate={decorate}
renderLeaf={renderLeaf}
placeholder="Write some markdown..."
/>
</Slate>
)
}
const Leaf = ({ attributes, children, leaf }) => {
return (
<span
{...attributes}
className={css`
font-weight: ${leaf.bold && 'bold'};
font-style: ${leaf.italic && 'italic'};
text-decoration: ${leaf.underlined && 'underline'};
${leaf.title &&
css`
display: inline-block;
font-weight: bold;
font-size: 20px;
margin: 20px 0 10px 0;
`}
${leaf.list &&
css`
padding-left: 10px;
font-size: 20px;
line-height: 10px;
`}
${leaf.hr &&
css`
display: block;
text-align: center;
border-bottom: 2px solid #ddd;
`}
${leaf.blockquote &&
css`
display: inline-block;
border-left: 2px solid #ddd;
padding-left: 10px;
color: #aaa;
font-style: italic;
`}
${leaf.code &&
css`
font-family: monospace;
background-color: #eee;
padding: 3px;
`}
`}
>
{children}
</span>
)
}
const initialValue = [
{
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