-
Notifications
You must be signed in to change notification settings - Fork 51
/
markdown.ts
40 lines (35 loc) · 1.05 KB
/
markdown.ts
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
import MarkdownIt from 'markdown-it'
import {
MarkdownParser,
MarkdownSerializer,
MarkdownSerializerState,
MarkSerializerConfig,
TokenConfig,
} from 'prosemirror-markdown'
import { Node as ProsemirrorNode, Schema } from 'prosemirror-model'
import { ProsemirrorTransformer } from './types'
type NodeSerializer<S extends Schema = any> = (
state: MarkdownSerializerState<S>,
node: ProsemirrorNode<S>,
parent: ProsemirrorNode<S>,
index: number
) => void
export const createMarkdownTransformer = <S extends Schema>(
schema: S,
nodes: Record<string, NodeSerializer>,
marks: Record<string, MarkSerializerConfig>,
tokens: Record<string, TokenConfig>,
options: Record<string, unknown>
): ProsemirrorTransformer<string, S> => {
const tokenizer = new MarkdownIt()
const parser = new MarkdownParser(schema, tokenizer, tokens)
const serializer = new MarkdownSerializer<S>(nodes, marks)
return {
parse: (markdown) => {
return parser.parse(markdown)
},
serialize: (doc) => {
return serializer.serialize(doc, options)
},
}
}