-
Notifications
You must be signed in to change notification settings - Fork 9
/
parser.js
339 lines (279 loc) · 6.76 KB
/
parser.js
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
const extend = require('xtend')
const marked = require('marked')
const inline = marked.InlineLexer.rules
function groupTokens (tokens) {
// defensive copy as we're going to mutate the shit out of this
tokens = tokens.slice()
const children = []
const container = {
type: 'container',
children
}
while (tokens.length) {
let group = readNextGroup(tokens)
if (!group) break
children.push(group)
}
return container
}
function readNextGroup (tokens) {
const first = tokens.shift()
if (first.type.endsWith('_end')) return
if (!first.type.endsWith('_start')) {
return first
}
// slice off '_start' suffix
const type = first.type.slice(0, first.type.length - 6)
const children = []
const parent = extend(first, {
type,
children
})
while (true) {
let group = readNextGroup(tokens)
if (!group) break
group.parent = parent
children.push(group)
if (!tokens.length) break
}
return parent
}
function ensureParents (token) {
token.children.forEach(child => child.parent = token)
return token
}
function Parser (text, opts) {
this.text = text
this.options = opts
}
Parser.prototype.lex = function lex (src) {
var tokens = []
, link
, text
, href
, cap;
while (src) {
// escape
if (cap = inline.escape.exec(src)) {
src = src.substring(cap[0].length);
// tokens.push(this.renderer.text(cap[1]));
tokens.push({
type: 'text',
text: cap[1]
});
continue;
}
// autolink
if (cap = inline.autolink.exec(src)) {
src = src.substring(cap[0].length);
if (cap[2] === '@') {
text = cap[1].charAt(6) === ':'
? this.mangle(cap[1].substring(7))
: this.mangle(cap[1]);
href = this.mangle('mailto:') + text;
} else {
text = cap[1];
href = text;
}
tokens.push({
type: 'link',
href: href,
text: text
});
continue;
}
// url (gfm)
if (!this.inLink && (cap = inline.url.exec(src))) {
src = src.substring(cap[0].length);
text = cap[1];
href = text;
tokens.push({
type: 'link',
href: href,
text: text
});
continue;
}
// tag
// if (cap = inline.tag.exec(src)) {
// if (!this.inLink && /^<a /i.test(cap[0])) {
// this.inLink = true;
// } else if (this.inLink && /^<\/a>/i.test(cap[0])) {
// this.inLink = false;
// }
// src = src.substring(cap[0].length);
// out += this.options.sanitize
// ? this.options.sanitizer
// ? this.options.sanitizer(cap[0])
// : escape(cap[0])
// : cap[0]
// continue;
// }
// link
if (cap = inline.link.exec(src)) {
src = src.substring(cap[0].length);
var result = this.outputLink(cap, {
href: cap[2],
title: cap[3]
});
tokens.push(result)
continue;
}
// reflink, nolink
if ((cap = inline.reflink.exec(src))
|| (cap = inline.nolink.exec(src))) {
src = src.substring(cap[0].length);
link = (cap[2] || cap[1]).replace(/\s+/g, ' ');
link = this.links[link.toLowerCase()];
if (!link || !link.href) {
tokens.push({
type: 'text',
text: cap[0].charAt(0)
})
src = cap[0].substring(1) + src;
continue;
}
tokens.push(this.outputLink(cap, link));
continue;
}
// strong
if (cap = inline.strong.exec(src)) {
src = src.substring(cap[0].length);
tokens.push(ensureParents({
type: 'strong',
children: this.lex(cap[2] || cap[1])
}));
continue;
}
// em
if (cap = inline.em.exec(src)) {
src = src.substring(cap[0].length);
tokens.push(ensureParents({
type: 'em',
children: this.lex(cap[2] || cap[1])
}))
continue;
}
// code
if (cap = inline.code.exec(src)) {
src = src.substring(cap[0].length);
tokens.push({
type: 'code',
text: cap[2]
});
continue;
}
// br
if (cap = inline.br.exec(src)) {
src = src.substring(cap[0].length);
tokens.push({
type: 'br'
});
continue;
}
// del (gfm)
if (cap = inline.del.exec(src)) {
src = src.substring(cap[0].length);
tokens.push(ensureParents({
type: 'del',
children: this.lex(cap[1])
}));
continue;
}
// text
if (cap = inline.text.exec(src)) {
src = src.substring(cap[0].length);
tokens.push({
type: 'text',
text: cap[0]
});
continue;
}
if (src) {
throw new
Error('Infinite loop on byte: ' + src.charCodeAt(0));
}
}
return tokens
}
// originally InlineLexer.prototype.outputLink
Parser.prototype.outputLink = function (cap, link) {
var href = link.href
, title = link.title ? link.title : null;
if (cap[0].charAt(0) !== '!') {
this.inLink = true
return ensureParents({
type: 'link',
href: href,
title: title,
children: this.lex(cap[1])
})
}
return {
type: 'image',
href: href,
title: title,
text: cap[1]
};
}
Parser.prototype.mangle = function mangle (text) {
if (!this.options.mangle) return text
var out = ''
, l = text.length
, i = 0
, ch;
for (; i < l; i++) {
ch = text.charCodeAt(i);
if (Math.random() > 0.5) {
ch = 'x' + ch.toString(16);
}
out += '&#' + ch + ';';
}
return out;
}
const shallowFlatten = (arr, el) => arr.concat(el)
const splitParagraphs = tokens => tokens.map(token => {
if (token.type === 'paragraph') {
return token.text.split('\n').map(text => ({ type: text, text }))
}
return token
})
.reduce(shallowFlatten, [])
const normalizeLineBreaks = tokens => tokens.map((token, i) => {
if (token.type === 'space') {
return { type: 'br' }
}
if (token.type === 'text') {
const next = tokens[i + 1]
if (next && next.type === 'text') {
return [token, { type: 'br' }]
}
}
return token
})
.reduce(shallowFlatten, [])
Parser.prototype.parse = function parse () {
let tokens = marked.lexer(this.text, this.options)
let normalizedTokens = normalizeLineBreaks(splitParagraphs(tokens))
this.links = tokens.links
const expanded = normalizedTokens.map(token => {
if (token.text) {
const children = this.lex(token.text)
if (children.length === 1 && token.type === 'text') {
// same as input
} else {
delete token.text
token.children = children
ensureParents(token)
}
}
return token
})
return groupTokens(expanded)
}
Parser.parse = function (text, opts) {
return new Parser(text, opts).parse()
}
module.exports = {
parse: Parser.parse
}