/
Serialize.ts
169 lines (151 loc) · 3.63 KB
/
Serialize.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
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
import type { AdvAst } from '@advjs/types'
import type * as Mdast from 'mdast'
import { parseText } from './syntax'
import { advNodeMap, parseAdvCode, scriptSuffix } from './syntax/code'
/**
* 序列化类
*/
export class Serialize {
/**
* 解析
*/
parse(child: Mdast.Content): AdvAst.Item | undefined {
let node: AdvAst.Item | undefined
switch (child.type) {
case 'blockquote':
node = this.blockquote(child)
break
case 'paragraph':
node = this.paragraph(child)
break
case 'text':
node = this.text(child)
break
case 'code':
node = this.code(child)
break
case 'list':
node = this.list(child)
break
default:
break
}
if (node)
return node
}
/**
* 处理标题
*/
heading(node: Mdast.Heading): AdvAst.Heading {
const info: AdvAst.Heading = {
...node,
}
return info
}
/**
* 处理引用块
*/
blockquote(node: Mdast.Blockquote): AdvAst.Narration {
const info: AdvAst.Narration = {
type: 'narration',
children: node.children.map((item) => {
return ((item as Mdast.Paragraph).children[0] as Mdast.Text).value
}),
}
return info
}
/**
* 处理代码块
* @param node
*/
code(node: Mdast.Code) {
const info: AdvAst.Code = {
type: 'code',
lang: node.lang || '',
meta: node.meta || '',
value: null,
}
if (!node.lang)
return
const lang = node.lang.toLowerCase()
// advnode suffix
for (const item of advNodeMap) {
if (scriptSuffix.includes(lang)) {
info.value = node.value
return info
}
if (item.suffix.includes(lang)) {
info.lang = 'advnode'
info.value = item.parse(node.value)
return info
}
}
const advSuffix = ['adv', 'advscript']
if (advSuffix.includes(lang)) {
// handle script
parseAdvCode()
}
return info
}
/**
* 处理段落
*/
paragraph(node: Mdast.Paragraph) {
if (node.children.length === 1 && node.children[0].type === 'text')
return this.text(node.children[0])
// parse 'character: some words'
let astNode: AdvAst.Child = { type: 'unknown' }
if (node.children[0].type === 'text')
astNode = this.text(node.children[0])
if (astNode.type === 'dialog') {
const dialogChildren = astNode.children
if (node.children.length > 1) {
node.children.slice(1).forEach((child) => {
dialogChildren.push(this.parse(child) as AdvAst.PhrasingContent)
})
}
}
return astNode
}
/**
* 处理文本
* 更多的扩展语法在此实现(从普通的段落文本中进行解析)
*/
text(node: Mdast.Text) {
return parseText(node)
}
/**
* handle list for choice
* - [x] xxx
*/
list(node: Mdast.List): AdvAst.Choices | undefined {
// todo
const advNode: AdvAst.Choices = {
type: 'choices',
choices: [],
}
for (const item of node.children) {
if (item.type !== 'listItem')
return
if (item.children.length) {
const choice: AdvAst.Choice = {
type: 'choice',
text: '',
do: {
type: 'code',
value: '',
},
}
if (item.children[0].type === 'paragraph') {
const text = (item.children[0].children[0] as Mdast.Text).value
// const do = 'advFunc'
choice.text = text
}
if (item.children[1]?.type === 'code')
choice.do = item.children[1]
advNode.choices.push(choice)
}
}
return advNode
}
}