-
Notifications
You must be signed in to change notification settings - Fork 2
/
node.go
381 lines (342 loc) · 12.3 KB
/
node.go
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package main
import (
"encoding/json"
"github.com/usememos/gomark/ast"
)
type Node struct {
Type ast.NodeType `json:"type"`
Value BaseNode `json:"value"`
}
type BaseNode interface{}
type LineBreakNode struct{}
type ParagraphNode struct {
Children []*Node `json:"children"`
}
type CodeBlockNode struct {
Language string `json:"language"`
Content string `json:"content"`
}
type HeadingNode struct {
Level int `json:"level"`
Children []*Node `json:"children"`
}
type HorizontalRuleNode struct {
Symbol string `json:"symbol"`
}
type BlockquoteNode struct {
Children []*Node `json:"children"`
}
type OrderedListNode struct {
Number string `json:"number"`
Indent int `json:"indent"`
Children []*Node `json:"children"`
}
type UnorderedListNode struct {
Symbol string `json:"symbol"`
Indent int `json:"indent"`
Children []*Node `json:"children"`
}
type TaskListNode struct {
Symbol string `json:"symbol"`
Indent int `json:"indent"`
Complete bool `json:"complete"`
Children []*Node `json:"children"`
}
type MathBlockNode struct {
Content string `json:"content"`
}
type TableRowNode struct {
Cells []string `json:"cells"`
}
type TableNode struct {
Header []string `json:"header"`
Delimiter []string `json:"delimiter"`
Rows []TableRowNode `json:"rows"`
}
type EmbeddedContentNode struct {
ResourceName string `json:"resourceName"`
Params string `json:"params"`
}
type TextNode struct {
Content string `json:"content"`
}
type BoldNode struct {
Symbol string `json:"symbol"`
Children []*Node `json:"children"`
}
type ItalicNode struct {
Symbol string `json:"symbol"`
Content string `json:"content"`
}
type BoldItalicNode struct {
Symbol string `json:"symbol"`
Content string `json:"content"`
}
type CodeNode struct {
Content string `json:"content"`
}
type ImageNode struct {
AltText string `json:"altText"`
Url string `json:"url"`
}
type LinkNode struct {
Text string `json:"text"`
Url string `json:"url"`
}
type AutoLinkNode struct {
Url string `json:"url"`
IsRawText bool `json:"isRawText"`
}
type TagNode struct {
Content string `json:"content"`
}
type StrikethroughNode struct {
Content string `json:"content"`
}
type EscapingCharacterNode struct {
Symbol string `json:"symbol"`
}
type MathNode struct {
Content string `json:"content"`
}
type HighlightNode struct {
Content string `json:"content"`
}
type SubscriptNode struct {
Content string `json:"content"`
}
type SuperscriptNode struct {
Content string `json:"content"`
}
type ReferencedContentNode struct {
ResourceName string `json:"resourceName"`
Params string `json:"params"`
}
type SpoilerNode struct {
Content string `json:"content"`
}
func convertFromASTNode(node ast.Node) *Node {
n := &Node{
Type: node.Type(),
}
switch node.(type) {
case *ast.LineBreak:
n.Value = LineBreakNode{}
case *ast.Paragraph:
children := convertFromASTNodes(node.(*ast.Paragraph).Children)
n.Value = ParagraphNode{Children: children}
case *ast.CodeBlock:
n.Value = CodeBlockNode{Language: node.(*ast.CodeBlock).Language, Content: node.(*ast.CodeBlock).Content}
case *ast.Heading:
children := convertFromASTNodes(node.(*ast.Heading).Children)
n.Value = HeadingNode{Level: node.(*ast.Heading).Level, Children: children}
case *ast.HorizontalRule:
n.Value = HorizontalRuleNode{Symbol: node.(*ast.HorizontalRule).Symbol}
case *ast.Blockquote:
children := convertFromASTNodes(node.(*ast.Blockquote).Children)
n.Value = BlockquoteNode{Children: children}
case *ast.OrderedList:
children := convertFromASTNodes(node.(*ast.OrderedList).Children)
n.Value = OrderedListNode{Number: node.(*ast.OrderedList).Number, Indent: node.(*ast.OrderedList).Indent, Children: children}
case *ast.UnorderedList:
children := convertFromASTNodes(node.(*ast.UnorderedList).Children)
n.Value = UnorderedListNode{Symbol: node.(*ast.UnorderedList).Symbol, Indent: node.(*ast.UnorderedList).Indent, Children: children}
case *ast.TaskList:
children := convertFromASTNodes(node.(*ast.TaskList).Children)
n.Value = TaskListNode{Symbol: node.(*ast.TaskList).Symbol, Indent: node.(*ast.TaskList).Indent, Complete: node.(*ast.TaskList).Complete, Children: children}
case *ast.MathBlock:
n.Value = MathBlockNode{Content: node.(*ast.MathBlock).Content}
case *ast.Table:
rows := []TableRowNode{}
for _, row := range node.(*ast.Table).Rows {
rows = append(rows, TableRowNode{Cells: row})
}
n.Value = TableNode{Header: node.(*ast.Table).Header, Delimiter: node.(*ast.Table).Delimiter, Rows: rows}
case *ast.EmbeddedContent:
n.Value = EmbeddedContentNode{ResourceName: node.(*ast.EmbeddedContent).ResourceName, Params: node.(*ast.EmbeddedContent).Params}
case *ast.Text:
n.Value = TextNode{Content: node.(*ast.Text).Content}
case *ast.Bold:
children := convertFromASTNodes(node.(*ast.Bold).Children)
n.Value = BoldNode{Symbol: node.(*ast.Bold).Symbol, Children: children}
case *ast.Italic:
n.Value = ItalicNode{Symbol: node.(*ast.Italic).Symbol, Content: node.(*ast.Italic).Content}
case *ast.BoldItalic:
n.Value = BoldItalicNode{Symbol: node.(*ast.BoldItalic).Symbol, Content: node.(*ast.BoldItalic).Content}
case *ast.Code:
n.Value = CodeNode{Content: node.(*ast.Code).Content}
case *ast.Image:
n.Value = ImageNode{AltText: node.(*ast.Image).AltText, Url: node.(*ast.Image).URL}
case *ast.Link:
n.Value = LinkNode{Text: node.(*ast.Link).Text, Url: node.(*ast.Link).URL}
case *ast.AutoLink:
n.Value = AutoLinkNode{Url: node.(*ast.AutoLink).URL, IsRawText: node.(*ast.AutoLink).IsRawText}
case *ast.Tag:
n.Value = TagNode{Content: node.(*ast.Tag).Content}
case *ast.Strikethrough:
n.Value = StrikethroughNode{Content: node.(*ast.Strikethrough).Content}
case *ast.EscapingCharacter:
n.Value = EscapingCharacterNode{Symbol: node.(*ast.EscapingCharacter).Symbol}
case *ast.Math:
n.Value = MathNode{Content: node.(*ast.Math).Content}
case *ast.Highlight:
n.Value = HighlightNode{Content: node.(*ast.Highlight).Content}
case *ast.Subscript:
n.Value = SubscriptNode{Content: node.(*ast.Subscript).Content}
case *ast.Superscript:
n.Value = SuperscriptNode{Content: node.(*ast.Superscript).Content}
case *ast.ReferencedContent:
n.Value = ReferencedContentNode{ResourceName: node.(*ast.ReferencedContent).ResourceName, Params: node.(*ast.ReferencedContent).Params}
case *ast.Spoiler:
n.Value = SpoilerNode{Content: node.(*ast.Spoiler).Content}
default:
n.Value = TextNode{Content: ""}
}
return n
}
func convertFromASTNodes(rawNodes []ast.Node) []*Node {
nodes := []*Node{}
for _, node := range rawNodes {
nodes = append(nodes, convertFromASTNode(node))
}
return nodes
}
func convertToASTNode(node *Node) ast.Node {
valueBytes, _ := json.Marshal(node.Value)
switch node.Type {
case ast.LineBreakNode:
return &ast.LineBreak{}
case ast.ParagraphNode:
paragraphNode := ParagraphNode{}
json.Unmarshal(valueBytes, ¶graphNode)
children := convertToASTNodes(paragraphNode.Children)
return &ast.Paragraph{Children: children}
case ast.CodeBlockNode:
codeBlockNode := CodeBlockNode{}
json.Unmarshal(valueBytes, &codeBlockNode)
return &ast.CodeBlock{Language: codeBlockNode.Language, Content: codeBlockNode.Content}
case ast.HeadingNode:
headingNode := HeadingNode{}
json.Unmarshal(valueBytes, &headingNode)
children := convertToASTNodes(headingNode.Children)
return &ast.Heading{Level: headingNode.Level, Children: children}
case ast.HorizontalRuleNode:
horizontalRuleNode := HorizontalRuleNode{}
json.Unmarshal(valueBytes, &horizontalRuleNode)
return &ast.HorizontalRule{Symbol: horizontalRuleNode.Symbol}
case ast.BlockquoteNode:
blockquoteNode := BlockquoteNode{}
json.Unmarshal(valueBytes, &blockquoteNode)
children := convertToASTNodes(blockquoteNode.Children)
return &ast.Blockquote{Children: children}
case ast.OrderedListNode:
orderedListNode := OrderedListNode{}
json.Unmarshal(valueBytes, &orderedListNode)
children := convertToASTNodes(orderedListNode.Children)
return &ast.OrderedList{Number: orderedListNode.Number, Indent: orderedListNode.Indent, Children: children}
case ast.UnorderedListNode:
unorderedListNode := UnorderedListNode{}
json.Unmarshal(valueBytes, &unorderedListNode)
children := convertToASTNodes(unorderedListNode.Children)
return &ast.UnorderedList{Symbol: unorderedListNode.Symbol, Indent: unorderedListNode.Indent, Children: children}
case ast.TaskListNode:
taskListNode := TaskListNode{}
json.Unmarshal(valueBytes, &taskListNode)
children := convertToASTNodes(taskListNode.Children)
return &ast.TaskList{Symbol: taskListNode.Symbol, Indent: taskListNode.Indent, Complete: taskListNode.Complete, Children: children}
case ast.MathBlockNode:
mathBlockNode := MathBlockNode{}
json.Unmarshal(valueBytes, &mathBlockNode)
return &ast.MathBlock{Content: mathBlockNode.Content}
case ast.TableNode:
tableNode := TableNode{}
json.Unmarshal(valueBytes, &tableNode)
rows := [][]string{}
for _, row := range tableNode.Rows {
rows = append(rows, row.Cells)
}
return &ast.Table{Header: tableNode.Header, Delimiter: tableNode.Delimiter, Rows: rows}
case ast.EmbeddedContentNode:
embeddedContentNode := EmbeddedContentNode{}
json.Unmarshal(valueBytes, &embeddedContentNode)
return &ast.EmbeddedContent{ResourceName: embeddedContentNode.ResourceName, Params: embeddedContentNode.Params}
case ast.TextNode:
textNode := TextNode{}
json.Unmarshal(valueBytes, &textNode)
return &ast.Text{Content: textNode.Content}
case ast.BoldNode:
boldNode := BoldNode{}
json.Unmarshal(valueBytes, &boldNode)
children := convertToASTNodes(boldNode.Children)
return &ast.Bold{Symbol: boldNode.Symbol, Children: children}
case ast.ItalicNode:
italicNode := ItalicNode{}
json.Unmarshal(valueBytes, &italicNode)
return &ast.Italic{Symbol: italicNode.Symbol, Content: italicNode.Content}
case ast.BoldItalicNode:
boldItalicNode := BoldItalicNode{}
json.Unmarshal(valueBytes, &boldItalicNode)
return &ast.BoldItalic{Symbol: boldItalicNode.Symbol, Content: boldItalicNode.Content}
case ast.CodeNode:
codeNode := CodeNode{}
json.Unmarshal(valueBytes, &codeNode)
return &ast.Code{Content: codeNode.Content}
case ast.ImageNode:
imageNode := ImageNode{}
json.Unmarshal(valueBytes, &imageNode)
return &ast.Image{AltText: imageNode.AltText, URL: imageNode.Url}
case ast.LinkNode:
linkNode := LinkNode{}
json.Unmarshal(valueBytes, &linkNode)
return &ast.Link{Text: linkNode.Text, URL: linkNode.Url}
case ast.AutoLinkNode:
autoLinkNode := AutoLinkNode{}
json.Unmarshal(valueBytes, &autoLinkNode)
return &ast.AutoLink{URL: autoLinkNode.Url, IsRawText: autoLinkNode.IsRawText}
case ast.TagNode:
tagNode := TagNode{}
json.Unmarshal(valueBytes, &tagNode)
return &ast.Tag{Content: tagNode.Content}
case ast.StrikethroughNode:
strikethroughNode := StrikethroughNode{}
json.Unmarshal(valueBytes, &strikethroughNode)
return &ast.Strikethrough{Content: strikethroughNode.Content}
case ast.EscapingCharacterNode:
escapingCharacterNode := EscapingCharacterNode{}
json.Unmarshal(valueBytes, &escapingCharacterNode)
return &ast.EscapingCharacter{Symbol: escapingCharacterNode.Symbol}
case ast.MathNode:
mathNode := MathNode{}
json.Unmarshal(valueBytes, &mathNode)
return &ast.Math{Content: mathNode.Content}
case ast.HighlightNode:
highlightNode := HighlightNode{}
json.Unmarshal(valueBytes, &highlightNode)
return &ast.Highlight{Content: highlightNode.Content}
case ast.SubscriptNode:
subscriptNode := SubscriptNode{}
json.Unmarshal(valueBytes, &subscriptNode)
return &ast.Subscript{Content: subscriptNode.Content}
case ast.SuperscriptNode:
superscriptNode := SuperscriptNode{}
json.Unmarshal(valueBytes, &superscriptNode)
return &ast.Superscript{Content: superscriptNode.Content}
case ast.ReferencedContentNode:
referencedContentNode := ReferencedContentNode{}
json.Unmarshal(valueBytes, &referencedContentNode)
return &ast.ReferencedContent{ResourceName: referencedContentNode.ResourceName, Params: referencedContentNode.Params}
case ast.SpoilerNode:
spoilerNode := SpoilerNode{}
json.Unmarshal(valueBytes, &spoilerNode)
return &ast.Spoiler{Content: spoilerNode.Content}
default:
return &ast.Text{}
}
}
func convertToASTNodes(rawNodes []*Node) []ast.Node {
nodes := []ast.Node{}
for _, node := range rawNodes {
nodes = append(nodes, convertToASTNode(node))
}
return nodes
}