marked 4.2.12 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/marked.cjs +388 -279
- package/lib/marked.esm.js +254 -158
- package/lib/marked.umd.js +388 -279
- package/marked.min.js +2 -2
- package/package.json +14 -14
- package/src/Hooks.js +26 -0
- package/src/Parser.js +1 -2
- package/src/Renderer.js +1 -1
- package/src/Tokenizer.js +2 -0
- package/src/defaults.js +1 -0
- package/src/helpers.js +34 -19
- package/src/marked.js +176 -126
- package/src/rules.js +19 -15
package/lib/marked.esm.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* marked
|
|
2
|
+
* marked v5.0.0 - a markdown parser
|
|
3
3
|
* Copyright (c) 2011-2023, Christopher Jeffrey. (MIT Licensed)
|
|
4
4
|
* https://github.com/markedjs/marked
|
|
5
5
|
*/
|
|
@@ -19,6 +19,7 @@ function getDefaults() {
|
|
|
19
19
|
headerIds: true,
|
|
20
20
|
headerPrefix: '',
|
|
21
21
|
highlight: null,
|
|
22
|
+
hooks: null,
|
|
22
23
|
langPrefix: 'language-',
|
|
23
24
|
mangle: true,
|
|
24
25
|
pedantic: false,
|
|
@@ -183,23 +184,6 @@ function resolveUrl(base, href) {
|
|
|
183
184
|
|
|
184
185
|
const noopTest = { exec: function noopTest() {} };
|
|
185
186
|
|
|
186
|
-
function merge(obj) {
|
|
187
|
-
let i = 1,
|
|
188
|
-
target,
|
|
189
|
-
key;
|
|
190
|
-
|
|
191
|
-
for (; i < arguments.length; i++) {
|
|
192
|
-
target = arguments[i];
|
|
193
|
-
for (key in target) {
|
|
194
|
-
if (Object.prototype.hasOwnProperty.call(target, key)) {
|
|
195
|
-
obj[key] = target[key];
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
return obj;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
187
|
function splitCells(tableRow, count) {
|
|
204
188
|
// ensure that every cell-delimiting pipe has a space
|
|
205
189
|
// before it to distinguish it from an escaped pipe
|
|
@@ -290,10 +274,42 @@ function findClosingBracket(str, b) {
|
|
|
290
274
|
return -1;
|
|
291
275
|
}
|
|
292
276
|
|
|
293
|
-
function
|
|
294
|
-
if (opt
|
|
277
|
+
function checkDeprecations(opt, callback) {
|
|
278
|
+
if (!opt || opt.silent) {
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (callback) {
|
|
283
|
+
console.warn('marked(): callback is deprecated since version 5.0.0, should not be used and will be removed in the future. Read more here: https://marked.js.org/using_pro#async');
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
if (opt.sanitize || opt.sanitizer) {
|
|
295
287
|
console.warn('marked(): sanitize and sanitizer parameters are deprecated since version 0.7.0, should not be used and will be removed in the future. Read more here: https://marked.js.org/#/USING_ADVANCED.md#options');
|
|
296
288
|
}
|
|
289
|
+
|
|
290
|
+
if (opt.highlight || opt.langPrefix) {
|
|
291
|
+
console.warn('marked(): highlight and langPrefix parameters are deprecated since version 5.0.0, should not be used and will be removed in the future. Instead use https://www.npmjs.com/package/marked-highlight.');
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if (opt.mangle) {
|
|
295
|
+
console.warn('marked(): mangle parameter is deprecated since version 5.0.0, should not be used and will be removed in the future. Instead use https://www.npmjs.com/package/marked-mangle.');
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (opt.baseUrl) {
|
|
299
|
+
console.warn('marked(): baseUrl parameter is deprecated since version 5.0.0, should not be used and will be removed in the future. Instead use https://www.npmjs.com/package/marked-base-url.');
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if (opt.smartypants) {
|
|
303
|
+
console.warn('marked(): smartypants parameter is deprecated since version 5.0.0, should not be used and will be removed in the future. Instead use https://www.npmjs.com/package/marked-smartypants.');
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (opt.xhtml) {
|
|
307
|
+
console.warn('marked(): xhtml parameter is deprecated since version 5.0.0, should not be used and will be removed in the future. Instead use https://www.npmjs.com/package/marked-xhtml.');
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if (opt.headerIds || opt.headerPrefix) {
|
|
311
|
+
console.warn('marked(): headerIds and headerPrefix parameters are deprecated since version 5.0.0, should not be used and will be removed in the future. Instead use https://www.npmjs.com/package/marked-gfm-heading-id.');
|
|
312
|
+
}
|
|
297
313
|
}
|
|
298
314
|
|
|
299
315
|
// copied from https://stackoverflow.com/a/5450113/806777
|
|
@@ -672,6 +688,7 @@ class Tokenizer {
|
|
|
672
688
|
if (cap) {
|
|
673
689
|
const token = {
|
|
674
690
|
type: 'html',
|
|
691
|
+
block: true,
|
|
675
692
|
raw: cap[0],
|
|
676
693
|
pre: !this.options.sanitizer
|
|
677
694
|
&& (cap[1] === 'pre' || cap[1] === 'script' || cap[1] === 'style'),
|
|
@@ -829,6 +846,7 @@ class Tokenizer {
|
|
|
829
846
|
raw: cap[0],
|
|
830
847
|
inLink: this.lexer.state.inLink,
|
|
831
848
|
inRawBlock: this.lexer.state.inRawBlock,
|
|
849
|
+
block: false,
|
|
832
850
|
text: this.options.sanitize
|
|
833
851
|
? (this.options.sanitizer
|
|
834
852
|
? this.options.sanitizer(cap[0])
|
|
@@ -1109,7 +1127,7 @@ class Tokenizer {
|
|
|
1109
1127
|
const block = {
|
|
1110
1128
|
newline: /^(?: *(?:\n|$))+/,
|
|
1111
1129
|
code: /^( {4}[^\n]+(?:\n(?: *(?:\n|$))*)?)+/,
|
|
1112
|
-
fences: /^ {0,3}(`{3,}(?=[^`\n]
|
|
1130
|
+
fences: /^ {0,3}(`{3,}(?=[^`\n]*(?:\n|$))|~{3,})([^\n]*)(?:\n|$)(?:|([\s\S]*?)(?:\n|$))(?: {0,3}\1[~`]* *(?=\n|$)|$)/,
|
|
1113
1131
|
hr: /^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/,
|
|
1114
1132
|
heading: /^ {0,3}(#{1,6})(?=\s|$)(.*)(?:\n+|$)/,
|
|
1115
1133
|
blockquote: /^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/,
|
|
@@ -1184,17 +1202,18 @@ block.blockquote = edit(block.blockquote)
|
|
|
1184
1202
|
* Normal Block Grammar
|
|
1185
1203
|
*/
|
|
1186
1204
|
|
|
1187
|
-
block.normal =
|
|
1205
|
+
block.normal = { ...block };
|
|
1188
1206
|
|
|
1189
1207
|
/**
|
|
1190
1208
|
* GFM Block Grammar
|
|
1191
1209
|
*/
|
|
1192
1210
|
|
|
1193
|
-
block.gfm =
|
|
1211
|
+
block.gfm = {
|
|
1212
|
+
...block.normal,
|
|
1194
1213
|
table: '^ *([^\\n ].*\\|.*)\\n' // Header
|
|
1195
1214
|
+ ' {0,3}(?:\\| *)?(:?-+:? *(?:\\| *:?-+:? *)*)(?:\\| *)?' // Align
|
|
1196
1215
|
+ '(?:\\n((?:(?! *\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)' // Cells
|
|
1197
|
-
}
|
|
1216
|
+
};
|
|
1198
1217
|
|
|
1199
1218
|
block.gfm.table = edit(block.gfm.table)
|
|
1200
1219
|
.replace('hr', block.hr)
|
|
@@ -1222,7 +1241,8 @@ block.gfm.paragraph = edit(block._paragraph)
|
|
|
1222
1241
|
* Pedantic grammar (original John Gruber's loose markdown specification)
|
|
1223
1242
|
*/
|
|
1224
1243
|
|
|
1225
|
-
block.pedantic =
|
|
1244
|
+
block.pedantic = {
|
|
1245
|
+
...block.normal,
|
|
1226
1246
|
html: edit(
|
|
1227
1247
|
'^ *(?:comment *(?:\\n|\\s*$)'
|
|
1228
1248
|
+ '|<(tag)[\\s\\S]+?</\\1> *(?:\\n{2,}|\\s*$)' // closed tag
|
|
@@ -1246,7 +1266,7 @@ block.pedantic = merge({}, block.normal, {
|
|
|
1246
1266
|
.replace('|list', '')
|
|
1247
1267
|
.replace('|html', '')
|
|
1248
1268
|
.getRegex()
|
|
1249
|
-
}
|
|
1269
|
+
};
|
|
1250
1270
|
|
|
1251
1271
|
/**
|
|
1252
1272
|
* Inline-Level Grammar
|
|
@@ -1348,13 +1368,14 @@ inline.reflinkSearch = edit(inline.reflinkSearch, 'g')
|
|
|
1348
1368
|
* Normal Inline Grammar
|
|
1349
1369
|
*/
|
|
1350
1370
|
|
|
1351
|
-
inline.normal =
|
|
1371
|
+
inline.normal = { ...inline };
|
|
1352
1372
|
|
|
1353
1373
|
/**
|
|
1354
1374
|
* Pedantic Inline Grammar
|
|
1355
1375
|
*/
|
|
1356
1376
|
|
|
1357
|
-
inline.pedantic =
|
|
1377
|
+
inline.pedantic = {
|
|
1378
|
+
...inline.normal,
|
|
1358
1379
|
strong: {
|
|
1359
1380
|
start: /^__|\*\*/,
|
|
1360
1381
|
middle: /^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,
|
|
@@ -1373,20 +1394,21 @@ inline.pedantic = merge({}, inline.normal, {
|
|
|
1373
1394
|
reflink: edit(/^!?\[(label)\]\s*\[([^\]]*)\]/)
|
|
1374
1395
|
.replace('label', inline._label)
|
|
1375
1396
|
.getRegex()
|
|
1376
|
-
}
|
|
1397
|
+
};
|
|
1377
1398
|
|
|
1378
1399
|
/**
|
|
1379
1400
|
* GFM Inline Grammar
|
|
1380
1401
|
*/
|
|
1381
1402
|
|
|
1382
|
-
inline.gfm =
|
|
1403
|
+
inline.gfm = {
|
|
1404
|
+
...inline.normal,
|
|
1383
1405
|
escape: edit(inline.escape).replace('])', '~|])').getRegex(),
|
|
1384
1406
|
_extended_email: /[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/,
|
|
1385
1407
|
url: /^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/,
|
|
1386
1408
|
_backpedal: /(?:[^?!.,:;*_'"~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_'"~)]+(?!$))+/,
|
|
1387
1409
|
del: /^(~~?)(?=[^\s~])([\s\S]*?[^\s~])\1(?=[^~]|$)/,
|
|
1388
1410
|
text: /^([`~]+|[^`~])(?:(?= {2,}\n)|(?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)|[\s\S]*?(?:(?=[\\<!\[`*~_]|\b_|https?:\/\/|ftp:\/\/|www\.|$)|[^ ](?= {2,}\n)|[^a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-](?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)))/
|
|
1389
|
-
}
|
|
1411
|
+
};
|
|
1390
1412
|
|
|
1391
1413
|
inline.gfm.url = edit(inline.gfm.url, 'i')
|
|
1392
1414
|
.replace('email', inline.gfm._extended_email)
|
|
@@ -1395,13 +1417,14 @@ inline.gfm.url = edit(inline.gfm.url, 'i')
|
|
|
1395
1417
|
* GFM + Line Breaks Inline Grammar
|
|
1396
1418
|
*/
|
|
1397
1419
|
|
|
1398
|
-
inline.breaks =
|
|
1420
|
+
inline.breaks = {
|
|
1421
|
+
...inline.gfm,
|
|
1399
1422
|
br: edit(inline.br).replace('{2,}', '*').getRegex(),
|
|
1400
1423
|
text: edit(inline.gfm.text)
|
|
1401
1424
|
.replace('\\b_', '\\b_| {2,}\\n')
|
|
1402
1425
|
.replace(/\{2,\}/g, '*')
|
|
1403
1426
|
.getRegex()
|
|
1404
|
-
}
|
|
1427
|
+
};
|
|
1405
1428
|
|
|
1406
1429
|
/**
|
|
1407
1430
|
* smartypants text replacement
|
|
@@ -1943,7 +1966,7 @@ class Renderer {
|
|
|
1943
1966
|
return `<blockquote>\n${quote}</blockquote>\n`;
|
|
1944
1967
|
}
|
|
1945
1968
|
|
|
1946
|
-
html(html) {
|
|
1969
|
+
html(html, block) {
|
|
1947
1970
|
return html;
|
|
1948
1971
|
}
|
|
1949
1972
|
|
|
@@ -2366,8 +2389,7 @@ class Parser {
|
|
|
2366
2389
|
continue;
|
|
2367
2390
|
}
|
|
2368
2391
|
case 'html': {
|
|
2369
|
-
|
|
2370
|
-
out += this.renderer.html(token.text);
|
|
2392
|
+
out += this.renderer.html(token.text, token.block);
|
|
2371
2393
|
continue;
|
|
2372
2394
|
}
|
|
2373
2395
|
case 'paragraph': {
|
|
@@ -2478,122 +2500,194 @@ class Parser {
|
|
|
2478
2500
|
}
|
|
2479
2501
|
}
|
|
2480
2502
|
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
function marked(src, opt, callback) {
|
|
2485
|
-
// throw error in case of non string input
|
|
2486
|
-
if (typeof src === 'undefined' || src === null) {
|
|
2487
|
-
throw new Error('marked(): input parameter is undefined or null');
|
|
2503
|
+
class Hooks {
|
|
2504
|
+
constructor(options) {
|
|
2505
|
+
this.options = options || defaults;
|
|
2488
2506
|
}
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2507
|
+
|
|
2508
|
+
static passThroughHooks = new Set([
|
|
2509
|
+
'preprocess',
|
|
2510
|
+
'postprocess'
|
|
2511
|
+
]);
|
|
2512
|
+
|
|
2513
|
+
/**
|
|
2514
|
+
* Process markdown before marked
|
|
2515
|
+
*/
|
|
2516
|
+
preprocess(markdown) {
|
|
2517
|
+
return markdown;
|
|
2492
2518
|
}
|
|
2493
2519
|
|
|
2494
|
-
|
|
2495
|
-
|
|
2496
|
-
|
|
2520
|
+
/**
|
|
2521
|
+
* Process HTML after marked is finished
|
|
2522
|
+
*/
|
|
2523
|
+
postprocess(html) {
|
|
2524
|
+
return html;
|
|
2497
2525
|
}
|
|
2526
|
+
}
|
|
2498
2527
|
|
|
2499
|
-
|
|
2500
|
-
|
|
2528
|
+
function onError(silent, async, callback) {
|
|
2529
|
+
return (e) => {
|
|
2530
|
+
e.message += '\nPlease report this to https://github.com/markedjs/marked.';
|
|
2501
2531
|
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2532
|
+
if (silent) {
|
|
2533
|
+
const msg = '<p>An error occurred:</p><pre>'
|
|
2534
|
+
+ escape(e.message + '', true)
|
|
2535
|
+
+ '</pre>';
|
|
2536
|
+
if (async) {
|
|
2537
|
+
return Promise.resolve(msg);
|
|
2538
|
+
}
|
|
2539
|
+
if (callback) {
|
|
2540
|
+
callback(null, msg);
|
|
2541
|
+
return;
|
|
2542
|
+
}
|
|
2543
|
+
return msg;
|
|
2544
|
+
}
|
|
2505
2545
|
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
} catch (e) {
|
|
2509
|
-
return callback(e);
|
|
2546
|
+
if (async) {
|
|
2547
|
+
return Promise.reject(e);
|
|
2510
2548
|
}
|
|
2549
|
+
if (callback) {
|
|
2550
|
+
callback(e);
|
|
2551
|
+
return;
|
|
2552
|
+
}
|
|
2553
|
+
throw e;
|
|
2554
|
+
};
|
|
2555
|
+
}
|
|
2511
2556
|
|
|
2512
|
-
|
|
2513
|
-
|
|
2557
|
+
function parseMarkdown(lexer, parser) {
|
|
2558
|
+
return (src, opt, callback) => {
|
|
2559
|
+
if (typeof opt === 'function') {
|
|
2560
|
+
callback = opt;
|
|
2561
|
+
opt = null;
|
|
2562
|
+
}
|
|
2514
2563
|
|
|
2515
|
-
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
marked.walkTokens(tokens, opt.walkTokens);
|
|
2519
|
-
}
|
|
2520
|
-
out = Parser.parse(tokens, opt);
|
|
2521
|
-
} catch (e) {
|
|
2522
|
-
err = e;
|
|
2523
|
-
}
|
|
2524
|
-
}
|
|
2564
|
+
const origOpt = { ...opt };
|
|
2565
|
+
opt = { ...marked.defaults, ...origOpt };
|
|
2566
|
+
const throwError = onError(opt.silent, opt.async, callback);
|
|
2525
2567
|
|
|
2526
|
-
|
|
2568
|
+
// throw error in case of non string input
|
|
2569
|
+
if (typeof src === 'undefined' || src === null) {
|
|
2570
|
+
return throwError(new Error('marked(): input parameter is undefined or null'));
|
|
2571
|
+
}
|
|
2572
|
+
if (typeof src !== 'string') {
|
|
2573
|
+
return throwError(new Error('marked(): input parameter is of type '
|
|
2574
|
+
+ Object.prototype.toString.call(src) + ', string expected'));
|
|
2575
|
+
}
|
|
2527
2576
|
|
|
2528
|
-
|
|
2529
|
-
? callback(err)
|
|
2530
|
-
: callback(null, out);
|
|
2531
|
-
};
|
|
2577
|
+
checkDeprecations(opt, callback);
|
|
2532
2578
|
|
|
2533
|
-
if (
|
|
2534
|
-
|
|
2579
|
+
if (opt.hooks) {
|
|
2580
|
+
opt.hooks.options = opt;
|
|
2535
2581
|
}
|
|
2536
2582
|
|
|
2537
|
-
|
|
2583
|
+
if (callback) {
|
|
2584
|
+
const highlight = opt.highlight;
|
|
2585
|
+
let tokens;
|
|
2586
|
+
|
|
2587
|
+
try {
|
|
2588
|
+
if (opt.hooks) {
|
|
2589
|
+
src = opt.hooks.preprocess(src);
|
|
2590
|
+
}
|
|
2591
|
+
tokens = lexer(src, opt);
|
|
2592
|
+
} catch (e) {
|
|
2593
|
+
return throwError(e);
|
|
2594
|
+
}
|
|
2538
2595
|
|
|
2539
|
-
|
|
2596
|
+
const done = function(err) {
|
|
2597
|
+
let out;
|
|
2540
2598
|
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
2544
|
-
|
|
2545
|
-
setTimeout(() => {
|
|
2546
|
-
highlight(token.text, token.lang, function(err, code) {
|
|
2547
|
-
if (err) {
|
|
2548
|
-
return done(err);
|
|
2599
|
+
if (!err) {
|
|
2600
|
+
try {
|
|
2601
|
+
if (opt.walkTokens) {
|
|
2602
|
+
marked.walkTokens(tokens, opt.walkTokens);
|
|
2549
2603
|
}
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2604
|
+
out = parser(tokens, opt);
|
|
2605
|
+
if (opt.hooks) {
|
|
2606
|
+
out = opt.hooks.postprocess(out);
|
|
2553
2607
|
}
|
|
2608
|
+
} catch (e) {
|
|
2609
|
+
err = e;
|
|
2610
|
+
}
|
|
2611
|
+
}
|
|
2554
2612
|
|
|
2555
|
-
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
|
|
2560
|
-
|
|
2613
|
+
opt.highlight = highlight;
|
|
2614
|
+
|
|
2615
|
+
return err
|
|
2616
|
+
? throwError(err)
|
|
2617
|
+
: callback(null, out);
|
|
2618
|
+
};
|
|
2619
|
+
|
|
2620
|
+
if (!highlight || highlight.length < 3) {
|
|
2621
|
+
return done();
|
|
2561
2622
|
}
|
|
2562
|
-
});
|
|
2563
2623
|
|
|
2564
|
-
|
|
2565
|
-
|
|
2624
|
+
delete opt.highlight;
|
|
2625
|
+
|
|
2626
|
+
if (!tokens.length) return done();
|
|
2627
|
+
|
|
2628
|
+
let pending = 0;
|
|
2629
|
+
marked.walkTokens(tokens, function(token) {
|
|
2630
|
+
if (token.type === 'code') {
|
|
2631
|
+
pending++;
|
|
2632
|
+
setTimeout(() => {
|
|
2633
|
+
highlight(token.text, token.lang, function(err, code) {
|
|
2634
|
+
if (err) {
|
|
2635
|
+
return done(err);
|
|
2636
|
+
}
|
|
2637
|
+
if (code != null && code !== token.text) {
|
|
2638
|
+
token.text = code;
|
|
2639
|
+
token.escaped = true;
|
|
2640
|
+
}
|
|
2641
|
+
|
|
2642
|
+
pending--;
|
|
2643
|
+
if (pending === 0) {
|
|
2644
|
+
done();
|
|
2645
|
+
}
|
|
2646
|
+
});
|
|
2647
|
+
}, 0);
|
|
2648
|
+
}
|
|
2649
|
+
});
|
|
2650
|
+
|
|
2651
|
+
if (pending === 0) {
|
|
2652
|
+
done();
|
|
2653
|
+
}
|
|
2654
|
+
|
|
2655
|
+
return;
|
|
2566
2656
|
}
|
|
2567
2657
|
|
|
2568
|
-
|
|
2569
|
-
|
|
2658
|
+
if (opt.async) {
|
|
2659
|
+
return Promise.resolve(opt.hooks ? opt.hooks.preprocess(src) : src)
|
|
2660
|
+
.then(src => lexer(src, opt))
|
|
2661
|
+
.then(tokens => opt.walkTokens ? Promise.all(marked.walkTokens(tokens, opt.walkTokens)).then(() => tokens) : tokens)
|
|
2662
|
+
.then(tokens => parser(tokens, opt))
|
|
2663
|
+
.then(html => opt.hooks ? opt.hooks.postprocess(html) : html)
|
|
2664
|
+
.catch(throwError);
|
|
2665
|
+
}
|
|
2570
2666
|
|
|
2571
|
-
|
|
2572
|
-
|
|
2573
|
-
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2667
|
+
try {
|
|
2668
|
+
if (opt.hooks) {
|
|
2669
|
+
src = opt.hooks.preprocess(src);
|
|
2670
|
+
}
|
|
2671
|
+
const tokens = lexer(src, opt);
|
|
2672
|
+
if (opt.walkTokens) {
|
|
2673
|
+
marked.walkTokens(tokens, opt.walkTokens);
|
|
2674
|
+
}
|
|
2675
|
+
let html = parser(tokens, opt);
|
|
2676
|
+
if (opt.hooks) {
|
|
2677
|
+
html = opt.hooks.postprocess(html);
|
|
2678
|
+
}
|
|
2679
|
+
return html;
|
|
2680
|
+
} catch (e) {
|
|
2681
|
+
return throwError(e);
|
|
2577
2682
|
}
|
|
2578
|
-
|
|
2579
|
-
|
|
2683
|
+
};
|
|
2684
|
+
}
|
|
2580
2685
|
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
.then(() => {
|
|
2587
|
-
return Parser.parse(tokens, opt);
|
|
2588
|
-
})
|
|
2589
|
-
.catch(onError);
|
|
2590
|
-
}
|
|
2591
|
-
marked.walkTokens(tokens, opt.walkTokens);
|
|
2592
|
-
}
|
|
2593
|
-
return Parser.parse(tokens, opt);
|
|
2594
|
-
} catch (e) {
|
|
2595
|
-
onError(e);
|
|
2596
|
-
}
|
|
2686
|
+
/**
|
|
2687
|
+
* Marked
|
|
2688
|
+
*/
|
|
2689
|
+
function marked(src, opt, callback) {
|
|
2690
|
+
return parseMarkdown(Lexer.lex, Parser.parse)(src, opt, callback);
|
|
2597
2691
|
}
|
|
2598
2692
|
|
|
2599
2693
|
/**
|
|
@@ -2602,7 +2696,7 @@ function marked(src, opt, callback) {
|
|
|
2602
2696
|
|
|
2603
2697
|
marked.options =
|
|
2604
2698
|
marked.setOptions = function(opt) {
|
|
2605
|
-
|
|
2699
|
+
marked.defaults = { ...marked.defaults, ...opt };
|
|
2606
2700
|
changeDefaults(marked.defaults);
|
|
2607
2701
|
return marked;
|
|
2608
2702
|
};
|
|
@@ -2620,10 +2714,10 @@ marked.use = function(...args) {
|
|
|
2620
2714
|
|
|
2621
2715
|
args.forEach((pack) => {
|
|
2622
2716
|
// copy options to new object
|
|
2623
|
-
const opts =
|
|
2717
|
+
const opts = { ...pack };
|
|
2624
2718
|
|
|
2625
2719
|
// set async to true if it was set to true before
|
|
2626
|
-
opts.async = marked.defaults.async || opts.async;
|
|
2720
|
+
opts.async = marked.defaults.async || opts.async || false;
|
|
2627
2721
|
|
|
2628
2722
|
// ==-- Parse "addon" extensions --== //
|
|
2629
2723
|
if (pack.extensions) {
|
|
@@ -2710,6 +2804,35 @@ marked.use = function(...args) {
|
|
|
2710
2804
|
opts.tokenizer = tokenizer;
|
|
2711
2805
|
}
|
|
2712
2806
|
|
|
2807
|
+
// ==-- Parse Hooks extensions --== //
|
|
2808
|
+
if (pack.hooks) {
|
|
2809
|
+
const hooks = marked.defaults.hooks || new Hooks();
|
|
2810
|
+
for (const prop in pack.hooks) {
|
|
2811
|
+
const prevHook = hooks[prop];
|
|
2812
|
+
if (Hooks.passThroughHooks.has(prop)) {
|
|
2813
|
+
hooks[prop] = (arg) => {
|
|
2814
|
+
if (marked.defaults.async) {
|
|
2815
|
+
return Promise.resolve(pack.hooks[prop].call(hooks, arg)).then(ret => {
|
|
2816
|
+
return prevHook.call(hooks, ret);
|
|
2817
|
+
});
|
|
2818
|
+
}
|
|
2819
|
+
|
|
2820
|
+
const ret = pack.hooks[prop].call(hooks, arg);
|
|
2821
|
+
return prevHook.call(hooks, ret);
|
|
2822
|
+
};
|
|
2823
|
+
} else {
|
|
2824
|
+
hooks[prop] = (...args) => {
|
|
2825
|
+
let ret = pack.hooks[prop].apply(hooks, args);
|
|
2826
|
+
if (ret === false) {
|
|
2827
|
+
ret = prevHook.apply(hooks, args);
|
|
2828
|
+
}
|
|
2829
|
+
return ret;
|
|
2830
|
+
};
|
|
2831
|
+
}
|
|
2832
|
+
}
|
|
2833
|
+
opts.hooks = hooks;
|
|
2834
|
+
}
|
|
2835
|
+
|
|
2713
2836
|
// ==-- Parse WalkTokens extensions --== //
|
|
2714
2837
|
if (pack.walkTokens) {
|
|
2715
2838
|
const walkTokens = marked.defaults.walkTokens;
|
|
@@ -2769,35 +2892,7 @@ marked.walkTokens = function(tokens, callback) {
|
|
|
2769
2892
|
* Parse Inline
|
|
2770
2893
|
* @param {string} src
|
|
2771
2894
|
*/
|
|
2772
|
-
marked.parseInline =
|
|
2773
|
-
// throw error in case of non string input
|
|
2774
|
-
if (typeof src === 'undefined' || src === null) {
|
|
2775
|
-
throw new Error('marked.parseInline(): input parameter is undefined or null');
|
|
2776
|
-
}
|
|
2777
|
-
if (typeof src !== 'string') {
|
|
2778
|
-
throw new Error('marked.parseInline(): input parameter is of type '
|
|
2779
|
-
+ Object.prototype.toString.call(src) + ', string expected');
|
|
2780
|
-
}
|
|
2781
|
-
|
|
2782
|
-
opt = merge({}, marked.defaults, opt || {});
|
|
2783
|
-
checkSanitizeDeprecation(opt);
|
|
2784
|
-
|
|
2785
|
-
try {
|
|
2786
|
-
const tokens = Lexer.lexInline(src, opt);
|
|
2787
|
-
if (opt.walkTokens) {
|
|
2788
|
-
marked.walkTokens(tokens, opt.walkTokens);
|
|
2789
|
-
}
|
|
2790
|
-
return Parser.parseInline(tokens, opt);
|
|
2791
|
-
} catch (e) {
|
|
2792
|
-
e.message += '\nPlease report this to https://github.com/markedjs/marked.';
|
|
2793
|
-
if (opt.silent) {
|
|
2794
|
-
return '<p>An error occurred:</p><pre>'
|
|
2795
|
-
+ escape(e.message + '', true)
|
|
2796
|
-
+ '</pre>';
|
|
2797
|
-
}
|
|
2798
|
-
throw e;
|
|
2799
|
-
}
|
|
2800
|
-
};
|
|
2895
|
+
marked.parseInline = parseMarkdown(Lexer.lexInline, Parser.parseInline);
|
|
2801
2896
|
|
|
2802
2897
|
/**
|
|
2803
2898
|
* Expose
|
|
@@ -2810,6 +2905,7 @@ marked.Lexer = Lexer;
|
|
|
2810
2905
|
marked.lexer = Lexer.lex;
|
|
2811
2906
|
marked.Tokenizer = Tokenizer;
|
|
2812
2907
|
marked.Slugger = Slugger;
|
|
2908
|
+
marked.Hooks = Hooks;
|
|
2813
2909
|
marked.parse = marked;
|
|
2814
2910
|
|
|
2815
2911
|
const options = marked.options;
|
|
@@ -2821,4 +2917,4 @@ const parse = marked;
|
|
|
2821
2917
|
const parser = Parser.parse;
|
|
2822
2918
|
const lexer = Lexer.lex;
|
|
2823
2919
|
|
|
2824
|
-
export { Lexer, Parser, Renderer, Slugger, TextRenderer, Tokenizer, defaults, getDefaults, lexer, marked, options, parse, parseInline, parser, setOptions, use, walkTokens };
|
|
2920
|
+
export { Hooks, Lexer, Parser, Renderer, Slugger, TextRenderer, Tokenizer, defaults, getDefaults, lexer, marked, options, parse, parseInline, parser, setOptions, use, walkTokens };
|