-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
markdown.js
506 lines (464 loc) · 14.4 KB
/
markdown.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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// @ts-check
/** @typedef {{
* type: string,
* text?: string,
* children?: MarkdownNode[],
* codeLang?: string,
* }} MarkdownBaseNode */
/** @typedef {MarkdownBaseNode & {
* type: 'text',
* text: string,
* }} MarkdownTextNode */
/** @typedef {MarkdownBaseNode & {
* type: 'h0' | 'h1' | 'h2' | 'h3' | 'h4',
* text: string,
* children: MarkdownNode[]
* }} MarkdownHeaderNode */
/** @typedef {MarkdownBaseNode & {
* type: 'li',
* text: string,
* liType: 'default' | 'bullet' | 'ordinal',
* children: MarkdownNode[]
* }} MarkdownLiNode */
/** @typedef {MarkdownBaseNode & {
* type: 'code',
* lines: string[],
* codeLang: string,
* title?: string,
* }} MarkdownCodeNode */
/** @typedef {MarkdownBaseNode & {
* type: 'note',
* noteType: string,
* }} MarkdownNoteNode */
/** @typedef {MarkdownBaseNode & {
* type: 'null',
* }} MarkdownNullNode */
/** @typedef {MarkdownBaseNode & {
* type: 'properties',
* lines: string[],
* }} MarkdownPropsNode */
/** @typedef {{
* maxColumns?: number,
* omitLastCR?: boolean,
* flattenText?: boolean,
* renderCodeBlockTitlesInHeader?: boolean,
* noteMode?: 'docusaurus' | 'compact',
* }} RenderOptions
*/
/** @typedef {MarkdownTextNode | MarkdownLiNode | MarkdownCodeNode | MarkdownNoteNode | MarkdownHeaderNode | MarkdownNullNode | MarkdownPropsNode } MarkdownNode */
function flattenWrappedLines(content) {
const inLines = content.replace(/\r\n/g, '\n').split('\n');
let inCodeBlock = false;
const outLines = [];
let outLineTokens = [];
for (const line of inLines) {
const trimmedLine = line.trim();
const singleLineExpression = line.startsWith('#');
const codeBlockBoundary = trimmedLine.startsWith('```') || trimmedLine.startsWith('---') || trimmedLine.startsWith(':::');
let flushLastParagraph = !trimmedLine
|| trimmedLine.startsWith('1.')
|| trimmedLine.startsWith('<')
|| trimmedLine.startsWith('>')
|| trimmedLine.startsWith('|')
|| trimmedLine.startsWith('-')
|| trimmedLine.startsWith('*')
|| line.match(/\[[^\]]+\]:.*/)
|| singleLineExpression;
if (codeBlockBoundary) {
inCodeBlock = !inCodeBlock;
flushLastParagraph = true;
}
if (flushLastParagraph && outLineTokens.length) {
outLines.push(outLineTokens.join('↵'));
outLineTokens = [];
}
if (inCodeBlock || singleLineExpression || codeBlockBoundary)
outLines.push(line);
else if (trimmedLine)
outLineTokens.push(outLineTokens.length ? line.trim() : line);
}
if (outLineTokens.length)
outLines.push(outLineTokens.join('↵'));
return outLines;
}
/**
* @param {string[]} lines
*/
function buildTree(lines) {
/** @type {MarkdownNode} */
const root = {
type: 'h0',
text: '<root>',
children: []
};
/** @type {MarkdownNode[]} */
const headerStack = [root];
/** @type {{ indent: string, node: MarkdownNode }[]} */
const sectionStack = [];
/**
* @param {string} indent
* @param {MarkdownNode} node
*/
const appendNode = (indent, node) => {
while (sectionStack.length && sectionStack[0].indent.length >= indent.length)
sectionStack.shift();
const parentNode = sectionStack.length ? sectionStack[0].node : headerStack[0];
if (!parentNode.children)
parentNode.children = [];
parentNode.children.push(node);
if (node.type === 'li')
sectionStack.unshift({ indent, node });
};
for (let i = 0; i < lines.length; ++i) {
let line = lines[i];
// Headers form hierarchy.
const header = line.match(/^(#+)/);
if (header) {
const h = header[1].length;
const node = /** @type {MarkdownNode} */({ type: 'h' + h, text: line.substring(h + 1), children: [] });
while (true) {
const lastH = +headerStack[0].type.substring(1);
if (h <= lastH)
headerStack.shift();
else
break;
}
/** @type {MarkdownNode[]}*/(headerStack[0].children).push(node);
headerStack.unshift(node);
continue;
}
// Remaining items respect indent-based nesting.
const [, indent, content] = /** @type {string[]} */ (line.match('^([ ]*)(.*)'));
if (content.startsWith('```')) {
const [codeLang, title] = parseCodeBlockMetadata(content);
/** @type {MarkdownNode} */
const node = {
type: 'code',
lines: [],
codeLang,
title,
};
line = lines[++i];
while (!line.trim().startsWith('```')) {
if (line && !line.startsWith(indent)) {
const from = Math.max(0, i - 5);
const to = Math.min(lines.length, from + 10);
const snippet = lines.slice(from, to);
throw new Error(`Bad code block: ${snippet.join('\n')}`);
}
if (line)
line = line.substring(indent.length);
node.lines.push(line);
line = lines[++i];
}
appendNode(indent, node);
continue;
}
if (content.startsWith(':::')) {
/** @type {MarkdownNode} */
const node = /** @type {MarkdownNoteNode} */ ({
type: 'note',
noteType: content.substring(3)
});
line = lines[++i];
const tokens = [];
while (!line.trim().startsWith(':::')) {
if (!line.startsWith(indent)) {
const from = Math.max(0, i - 5);
const to = Math.min(lines.length, from + 10);
const snippet = lines.slice(from, to);
throw new Error(`Bad comment block: ${snippet.join('\n')}`);
}
tokens.push(line.substring(indent.length));
line = lines[++i];
}
node.children = parse(tokens.join('\n'));
appendNode(indent, node);
continue;
}
if (content.startsWith('---')) {
/** @type {MarkdownNode} */
const node = {
type: 'properties',
lines: [],
};
line = lines[++i];
while (!line.trim().startsWith('---')) {
if (!line.startsWith(indent))
throw new Error('Bad header block ' + line);
node.lines.push(line.substring(indent.length));
line = lines[++i];
}
appendNode(indent, node);
continue;
}
const liType = content.match(/^(-|1.|\*) /);
const node = /** @type {MarkdownNode} */({ type: 'text', text: content });
if (liType) {
const liNode = /** @type {MarkdownLiNode} */(node);
liNode.type = 'li';
liNode.text = content.substring(liType[0].length);
if (content.startsWith('1.'))
liNode.liType = 'ordinal';
else if (content.startsWith('*'))
liNode.liType = 'bullet';
else
liNode.liType = 'default';
}
const match = node.text?.match(/\*\*langs: (.*)\*\*(.*)/);
if (match) {
node.codeLang = match[1];
node.text = match[2];
}
appendNode(indent, node);
}
return root.children;
}
/**
* @param {String} firstLine
* @returns {[string, string|undefined]}
*/
function parseCodeBlockMetadata(firstLine) {
const withoutBackticks = firstLine.substring(3);
const match = withoutBackticks.match(/ title="(.+)"$/);
if (match)
return [withoutBackticks.substring(0, match.index), match[1]];
return [withoutBackticks, undefined];
}
/**
* @param {string} content
*/
function parse(content) {
return buildTree(flattenWrappedLines(content));
}
/**
* @param {MarkdownNode[]} nodes
* @param {RenderOptions=} options
*/
function render(nodes, options) {
const result = [];
let lastNode;
for (const node of nodes) {
if (node.type === 'null')
continue;
innerRenderMdNode('', node, /** @type {MarkdownNode} */ (lastNode), result, options);
lastNode = node;
}
if (!options?.omitLastCR && result[result.length - 1] !== '')
result.push('');
return result.join('\n');
}
/**
* @param {string} indent
* @param {MarkdownNode} node
* @param {MarkdownNode} lastNode
* @param {RenderOptions=} options
* @param {string[]} result
*/
function innerRenderMdNode(indent, node, lastNode, result, options) {
const newLine = () => {
if (result.length && (result[result.length - 1] || '').trim() !== '')
result.push(indent);
};
if (node.type.startsWith('h')) {
const headerNode = /** @type {MarkdownHeaderNode} */ (node);
newLine();
const depth = +node.type.substring(1);
result.push(`${'#'.repeat(depth)} ${headerNode.text}`);
let lastNode = node;
for (const child of node.children || []) {
innerRenderMdNode('', child, lastNode, result, options);
lastNode = child;
}
}
if (node.type === 'text') {
const bothTables = node.text.startsWith('|') && lastNode && lastNode.type === 'text' && lastNode.text.startsWith('|');
const bothGen = node.text.startsWith('<!--') && lastNode && lastNode.type === 'text' && lastNode.text.startsWith('<!--');
const bothComments = node.text.startsWith('>') && lastNode && lastNode.type === 'text' && lastNode.text.startsWith('>');
const bothLinks = node.text.match(/\[[^\]]+\]:/) && lastNode && lastNode.type === 'text' && lastNode.text.match(/\[[^\]]+\]:/);
if (!bothTables && !bothGen && !bothComments && !bothLinks && lastNode && lastNode.text)
newLine();
result.push(wrapText(node.text, options, indent));
return;
}
if (node.type === 'code') {
newLine();
result.push(`${indent}\`\`\`${node.codeLang}${(options?.renderCodeBlockTitlesInHeader && node.title) ? ' title="' + node.title + '"' : ''}`);
if (!options?.renderCodeBlockTitlesInHeader && node.title)
result.push(`${indent}// ${node.title}`);
for (const line of node.lines)
result.push(indent + line);
result.push(`${indent}\`\`\``);
newLine();
return;
}
if (node.type === 'note') {
newLine();
if (options?.noteMode !== 'compact')
result.push(`${indent}:::${node.noteType}`);
const children = node.children ?? [];
if (options?.noteMode === 'compact') {
children[0] = {
type: 'text',
text: `**NOTE** ${children[0].text}`,
}
}
for (const child of children) {
innerRenderMdNode(indent, child, lastNode, result, options);
lastNode = child;
}
if (options?.noteMode !== 'compact')
result.push(`${indent}:::`);
newLine();
return;
}
if (node.type === 'properties') {
result.push(`${indent}---`);
for (const line of node.lines)
result.push(indent + line);
result.push(`${indent}---`);
newLine();
return;
}
if (node.type === 'li') {
let char;
switch (node.liType) {
case 'bullet': char = '*'; break;
case 'default': char = '-'; break;
case 'ordinal': char = '1.'; break;
}
result.push(wrapText(node.text, options, `${indent}${char} `));
const newIndent = indent + ' '.repeat(char.length + 1);
for (const child of node.children || []) {
innerRenderMdNode(newIndent, child, lastNode, result, options);
lastNode = child;
}
}
}
/**
* @param {string} text
*/
function tokenizeNoBreakLinks(text) {
const links = [];
// Don't wrap simple links with spaces.
text = text.replace(/\[[^\]]+\]/g, match => {
links.push(match);
return `[${links.length - 1}]`;
});
return text.split(' ').map(c => c.replace(/\[(\d+)\]/g, (_, p1) => links[+p1]));
}
/**
* @param {string} text
* @param {RenderOptions|undefined} options
* @param {string} prefix
* @returns {string}
*/
function wrapText(text, options, prefix) {
if (options?.flattenText)
text = text.replace(/↵/g, ' ');
const lines = text.split(/[\n↵]/);
const result = /** @type {string[]} */([]);
const indent = ' '.repeat(prefix.length);
for (const line of lines)
result.push(wrapLine(line, options?.maxColumns, result.length ? indent : prefix));
return result.join('\n');
}
/**
* @param {string} textLine
* @param {number|undefined} maxColumns
* @param {string} prefix
* @returns {string}
*/
function wrapLine(textLine, maxColumns, prefix) {
if (!maxColumns)
return prefix + textLine;
if (textLine.trim().startsWith('|'))
return prefix + textLine;
const indent = ' '.repeat(prefix.length);
const lines = [];
maxColumns -= indent.length;
const words = tokenizeNoBreakLinks(textLine);
let line = '';
for (const word of words) {
if (line.length && line.length + word.length < maxColumns) {
line += ' ' + word;
} else {
if (line)
lines.push(line);
line = (lines.length ? indent : prefix) + word;
}
}
if (line)
lines.push(line);
return lines.join('\n');
}
/**
* @param {MarkdownNode} node
*/
function clone(node) {
const copy = { ...node };
copy.children = copy.children ? copy.children.map(c => clone(c)) : undefined;
return copy;
}
/**
* @param {MarkdownNode[]} nodes
* @param {function(MarkdownNode, number): void} visitor
*/
function visitAll(nodes, visitor) {
for (const node of nodes)
visit(node, visitor);
}
/**
* @param {MarkdownNode} node
* @param {function(MarkdownNode, number): void} visitor
*/
function visit(node, visitor, depth = 0) {
visitor(node, depth);
for (const n of node.children || [])
visit(n, visitor, depth + 1);
}
/**
* @param {MarkdownNode[]} nodes
* @param {string} language
* @return {MarkdownNode[]}
*/
function filterNodesForLanguage(nodes, language) {
const result = nodes.filter(node => {
if (!node.children)
return true;
for (let i = 0; i < node.children.length; i++) {
const child = node.children[i];
if (child.type !== 'li' || child.liType !== 'bullet' || !child.text.startsWith('langs:'))
continue;
const onlyText = child.text.substring('langs:'.length);
if (!onlyText)
return true;
const only = onlyText.split(',').map(l => l.trim());
node.children.splice(i, 1);
return only.includes(language);
}
return true;
});
result.forEach(n => {
if (!n.children)
return;
n.children = filterNodesForLanguage(n.children, language);
});
return result;
}
module.exports = { parse, render, clone, visitAll, visit, filterNodesForLanguage, wrapText };