-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverters.ts
128 lines (111 loc) · 3 KB
/
converters.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
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
import Node from '@markdoc/markdoc/src/ast/node';
import * as fromAst from './converter/fromAst';
import * as toAst from './converter/toAst';
import type { Converters } from "./types";
export default {
fromAst: {
block: {
document(node, config) {
return {
type: 'doc',
content: fromAst.convertChildren(node, config)
};
},
heading(node, config) {
return {
type: 'heading',
attrs: { level: node.attributes.level },
content: fromAst.convertChildren(node, config)
};
},
paragraph(node, config) {
return {
type: 'paragraph',
content: fromAst.convertChildren(node, config)
};
},
list(node, config) {
return {
type: node.attributes.ordered ? 'orderedList' : 'bulletList',
content: fromAst.convertChildren(node, config)
};
},
// TODO: handle loose/tight list distinction
item(node, config) {
return {
type: 'listItem',
content: [
{
type: 'paragraph',
content: fromAst.convertChildren(node, config)
}
]
};
},
tag(node, config) {
return {
type: 'markdocTag',
content: fromAst.convertChildren(node, config),
attrs: {
attributes: node.attributes,
tag: node.tag,
}
}
}
},
inline: {
strong(node, config) {
return { type: 'bold' };
},
em(node, config) {
return { type: 'italic' };
},
link(node, config) {
return {
type: 'link',
attrs: { href: node.attributes.href }
};
}
}
},
toAst: {
nodes: {
doc(node, config) {
return new Node('document', {}, toAst.convertChildren(node, config));
},
heading(node, config) {
return new Node('heading', { level: node.attrs.level }, [toAst.convertInline(node, config)]);
},
paragraph(node, config) {
return new Node('paragraph', {}, [toAst.convertInline(node, config)]);
},
bulletList(node, config) {
return new Node('list', { ordered: false }, toAst.convertChildren(node, config));
},
orderedList(node, config) {
return new Node('list', { ordered: true }, toAst.convertChildren(node, config));
},
listItem(node, config) {
return new Node('item', {}, toAst.convertChildren(node, config));
},
markdocTag(node, config) {
return new Node('tag', node.attrs.attributes,
toAst.convertChildren(node, config), node.attrs.tag);
}
},
marks: {
bold(mark, config) {
return new Node('strong');
},
italic(mark, config) {
return new Node('em');
},
link(mark, config) {
return new Node('link', { href: mark.attrs.href });
},
comment(mark, config) {
return [];
}
}
}
} as Converters;