-
Notifications
You must be signed in to change notification settings - Fork 84
/
renderer.ts
186 lines (140 loc) · 4.02 KB
/
renderer.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import * as YAML from "yamljs";
export default class Renderer {
private lastLevel: number = 0;
private firstHeader: boolean = true;
private jsonObj: any = {};
private levels: Array<any> = [];
private curObj: any;
public code(code, lang, escaped) {
const ret = '`' + code.replace(/\n/g, " ") + '`"';
this.getObject().content.push(ret);
return ret;
}
public blockquote(quote) {
return quote;
}
public html(html) {
this.getObject().content.push(html);
return html;
}
public heading(text, level, raw) {
if (level < this.lastLevel) {
let lastRule: any = this.levels.pop();
while (lastRule != undefined && level < lastRule.level) {
lastRule = this.levels.pop();
}
if (lastRule != undefined && lastRule.level < level) {
this.levels.push(lastRule);
}
} else if (level == this.lastLevel) {
this.levels.pop();
}
const obj = this.getObject();
if (!obj.children) {
obj.children = {}
}
obj.children[text] = { level: level, content: [] };
this.levels.push({ name: text, level: level });
this.lastLevel = level;
}
public hr() {
this.getObject().content.push('---------------------------------------------------------');
return '---------------------------------------------------------';
}
public list(body, ordered) {
const listBody: Array<string> = JSON.parse('[' + body.slice(0, -1) + "]");
this.getObject().content = this.getObject().content.filter((s: string) => !listBody.includes(s));
this.getObject().content.push(listBody);
return '[' + body.slice(0, -1) + "]";
}
public listitem(text) {
return text.startsWith(',"') ? text.slice(1) + "," : '"' + text + '",';
}
public paragraph(text) {
this.getObject().content.push(text);
return text;
}
public table(header, body) {
const headerArray = JSON.parse(header.slice(0, -1));
const bodyArray = JSON.parse("[" + body.slice(0, -1) + "]");
const table = {};
for (let j = 0; j < headerArray.length; j++) {
table[headerArray[j]] = [];
for (let i = 0; i < bodyArray.length; i++) {
table[headerArray[j]].push(bodyArray[i][j]);
}
}
this.getObject().content.push({ "table": table });
return JSON.stringify(table);
}
public tablerow(content) {
return "[" + content.slice(0, -1) + "],";
}
public tablecell(content, flags) {
return '"' + content + '",';
}
// span level renderer
public strong(text) {
return '**' + text + '**';
}
public em(text) {
return '*' + text + '*';
}
public codespan(text) {
return '```' + text + '```';
}
public br() {
this.getObject().content.push("");
return '';
}
public del(text) {
return '~~' + text + '~~';
}
public link(href, title, text) {
return text + '(' + href + ')';
}
public image(href, title, text) {
return text + '(' + href + ')';
}
public text(text) {
return text.replace(/\n/g, " ");
}
private getObject(): any {
let obj: any = this.jsonObj;
for (let level of this.levels) {
obj = obj.children[level.name];
}
return obj;
}
private addChildrenToObject(parent: any, target: any) {
for (let childKey in parent.children) {
const child: any = parent.children[childKey]
target[childKey] = {};
if (child.content.length == 1) {
if (!child.hasOwnProperty("children")) {
target[childKey] = child.content[0];
} else {
target[childKey].content = child.content[0];
}
} else if (child.content.length > 0) {
target[childKey].content = child.content;
}
if (child.hasOwnProperty("children")) {
this.addChildrenToObject(child, target[childKey]);
}
}
}
public getOutput() {
const output: any = {};
this.addChildrenToObject(this.jsonObj, output);
return JSON.stringify(output, null, 4);
}
public getYamlOutput() {
const output: any = {};
this.addChildrenToObject(this.jsonObj, output);
return YAML.stringify(output, null, 4);
}
public getFullObject() {
return this.jsonObj;
}
}