1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | 'use strict';
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | class ToMarkdownStringVisitor {
|
27 | |
28 |
|
29 |
|
30 |
|
31 |
|
32 | constructor(options) {
|
33 | this.options = options;
|
34 | }
|
35 | |
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | static visitChildren(visitor, thing, parameters, paramsFun) {
|
46 | const paramsFunActual = paramsFun ? paramsFun : ToMarkdownStringVisitor.mkParameters;
|
47 | const parametersIn = paramsFunActual(parameters);
|
48 |
|
49 | if (thing.nodes) {
|
50 | thing.nodes.forEach(node => {
|
51 | node.accept(visitor, parametersIn);
|
52 | });
|
53 | }
|
54 |
|
55 | return parametersIn.result;
|
56 | }
|
57 | |
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | static mkParameters(parametersOut) {
|
65 | let parameters = {};
|
66 | parameters.result = '';
|
67 | parameters.first = false;
|
68 | parameters.stack = parametersOut.stack.slice();
|
69 | return parameters;
|
70 | }
|
71 | |
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | static mkParametersInBlockQuote(parametersOut) {
|
79 | let parameters = {};
|
80 | parameters.result = '';
|
81 | parameters.first = false;
|
82 | parameters.stack = parametersOut.stack.slice();
|
83 | parameters.stack.push('block');
|
84 | return parameters;
|
85 | }
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | static mkParametersInList(parametersOut) {
|
94 | let parameters = {};
|
95 | parameters.result = '';
|
96 | parameters.first = true;
|
97 | parameters.stack = parametersOut.stack.slice();
|
98 | parameters.stack.push('list');
|
99 | return parameters;
|
100 | }
|
101 | |
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | static mkPrefix(parameters, newlines) {
|
110 | const stack = parameters.stack;
|
111 | const newlinesFix = parameters.first ? 0 : newlines;
|
112 | let prefix = '';
|
113 |
|
114 | for (let i = 0; i < stack.length; i++) {
|
115 | if (stack[i] === 'list') {
|
116 | prefix += ' ';
|
117 | } else if (stack[i] === 'block') {
|
118 | prefix += '> ';
|
119 | }
|
120 | }
|
121 |
|
122 | return ('\n' + prefix).repeat(newlinesFix);
|
123 | }
|
124 | |
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | static mkSetextHeading(level) {
|
132 | if (level === 1) {
|
133 | return '====';
|
134 | } else {
|
135 | return '----';
|
136 | }
|
137 | }
|
138 | |
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | static mkATXHeading(level) {
|
146 | return Array(level).fill('#').join('');
|
147 | }
|
148 | |
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | static escapeCodeBlock(input) {
|
156 | return input.replace(/`/g, '\\`');
|
157 | }
|
158 | |
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | static escapeText(input) {
|
166 | return input.replace(/[*`#&]/g, '\\$&')
|
167 | .replace(/^(\d+)\./g, '$1\\.')
|
168 | .replace(/^-/g, '\\-');
|
169 | }
|
170 | |
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | visit(thing, parameters) {
|
178 | const nodeText = thing.text ? thing.text : '';
|
179 |
|
180 | switch (thing.getType()) {
|
181 | case 'CodeBlock':
|
182 | parameters.result += ToMarkdownStringVisitor.mkPrefix(parameters, 2);
|
183 | parameters.result += "```".concat(thing.info ? ' ' + thing.info : '', "\n").concat(ToMarkdownStringVisitor.escapeCodeBlock(thing.text), "```");
|
184 | break;
|
185 |
|
186 | case 'Code':
|
187 | parameters.result += "`".concat(nodeText, "`");
|
188 | break;
|
189 |
|
190 | case 'HtmlInline':
|
191 | parameters.result += nodeText;
|
192 | break;
|
193 |
|
194 | case 'Emph':
|
195 | parameters.result += "*".concat(ToMarkdownStringVisitor.visitChildren(this, thing, parameters), "*");
|
196 | break;
|
197 |
|
198 | case 'Strong':
|
199 | parameters.result += "**".concat(ToMarkdownStringVisitor.visitChildren(this, thing, parameters), "**");
|
200 | break;
|
201 |
|
202 | case 'BlockQuote':
|
203 | parameters.result += ToMarkdownStringVisitor.visitChildren(this, thing, parameters, ToMarkdownStringVisitor.mkParametersInBlockQuote);
|
204 | break;
|
205 |
|
206 | case 'Heading':
|
207 | {
|
208 | const level = parseInt(thing.level);
|
209 |
|
210 | if (level < 3) {
|
211 | parameters.result += ToMarkdownStringVisitor.mkPrefix(parameters, 2);
|
212 | parameters.result += ToMarkdownStringVisitor.visitChildren(this, thing, parameters);
|
213 | parameters.result += ToMarkdownStringVisitor.mkPrefix(parameters, 1);
|
214 | parameters.result += ToMarkdownStringVisitor.mkSetextHeading(level);
|
215 | } else {
|
216 | parameters.result += ToMarkdownStringVisitor.mkPrefix(parameters, 2);
|
217 | parameters.result += ToMarkdownStringVisitor.mkATXHeading(level);
|
218 | parameters.result += ' ';
|
219 | parameters.result += ToMarkdownStringVisitor.visitChildren(this, thing, parameters);
|
220 | }
|
221 | }
|
222 | break;
|
223 |
|
224 | case 'ThematicBreak':
|
225 | parameters.result += ToMarkdownStringVisitor.mkPrefix(parameters, 2);
|
226 | parameters.result += '---';
|
227 | break;
|
228 |
|
229 | case 'Linebreak':
|
230 | parameters.result += '\\';
|
231 | parameters.result += ToMarkdownStringVisitor.mkPrefix(parameters, 1);
|
232 | break;
|
233 |
|
234 | case 'Softbreak':
|
235 | parameters.result += ToMarkdownStringVisitor.mkPrefix(parameters, 1);
|
236 | break;
|
237 |
|
238 | case 'Link':
|
239 | parameters.result += "[".concat(ToMarkdownStringVisitor.visitChildren(this, thing, parameters), "](").concat(thing.destination, " \"").concat(thing.title ? thing.title : '', "\")");
|
240 | break;
|
241 |
|
242 | case 'Image':
|
243 | parameters.result += "![".concat(ToMarkdownStringVisitor.visitChildren(this, thing, parameters), "](").concat(thing.destination, " \"").concat(thing.title ? thing.title : '', "\")");
|
244 | break;
|
245 |
|
246 | case 'Paragraph':
|
247 | parameters.result += ToMarkdownStringVisitor.mkPrefix(parameters, 2);
|
248 | parameters.result += "".concat(ToMarkdownStringVisitor.visitChildren(this, thing, parameters));
|
249 | break;
|
250 |
|
251 | case 'HtmlBlock':
|
252 | parameters.result += ToMarkdownStringVisitor.mkPrefix(parameters, 2);
|
253 | parameters.result += nodeText;
|
254 | break;
|
255 |
|
256 | case 'Text':
|
257 | parameters.result += ToMarkdownStringVisitor.escapeText(nodeText);
|
258 | break;
|
259 |
|
260 | case 'List':
|
261 | {
|
262 | const first = thing.start ? parseInt(thing.start) : 1;
|
263 | let index = first;
|
264 | thing.nodes.forEach(item => {
|
265 | if (thing.tight === 'false' && index !== first) {
|
266 | parameters.result += '\n';
|
267 | }
|
268 |
|
269 | if (thing.type === 'ordered') {
|
270 | parameters.result += "".concat(ToMarkdownStringVisitor.mkPrefix(parameters, 1)).concat(this.options.noIndex ? 1 : index, ". ").concat(ToMarkdownStringVisitor.visitChildren(this, item, parameters, ToMarkdownStringVisitor.mkParametersInList));
|
271 | } else {
|
272 | parameters.result += "".concat(ToMarkdownStringVisitor.mkPrefix(parameters, 1), "- ").concat(ToMarkdownStringVisitor.visitChildren(this, item, parameters, ToMarkdownStringVisitor.mkParametersInList));
|
273 | }
|
274 |
|
275 | index++;
|
276 | });
|
277 | }
|
278 | break;
|
279 |
|
280 | case 'Item':
|
281 | throw new Error('Item node should not occur outside of List nodes');
|
282 |
|
283 | case 'Document':
|
284 | parameters.result += ToMarkdownStringVisitor.visitChildren(this, thing, parameters);
|
285 | break;
|
286 |
|
287 | default:
|
288 | throw new Error("Unhandled type ".concat(thing.getType()));
|
289 | }
|
290 |
|
291 | parameters.first = false;
|
292 | }
|
293 |
|
294 | }
|
295 |
|
296 | module.exports = ToMarkdownStringVisitor; |
\ | No newline at end of file |