marked 12.0.2 → 13.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 +438 -162
- package/lib/marked.cjs.map +1 -1
- package/lib/marked.d.cts +50 -37
- package/lib/marked.d.ts +50 -37
- package/lib/marked.esm.js +438 -162
- package/lib/marked.esm.js.map +1 -1
- package/lib/marked.umd.js +438 -162
- package/lib/marked.umd.js.map +1 -1
- package/man/marked.1 +1 -1
- package/marked.min.js +2 -2
- package/package.json +15 -12
package/lib/marked.umd.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* marked
|
|
2
|
+
* marked v13.0.0 - a markdown parser
|
|
3
3
|
* Copyright (c) 2011-2024, Christopher Jeffrey. (MIT Licensed)
|
|
4
4
|
* https://github.com/markedjs/marked
|
|
5
5
|
*/
|
|
@@ -321,23 +321,82 @@
|
|
|
321
321
|
if (cap) {
|
|
322
322
|
return {
|
|
323
323
|
type: 'hr',
|
|
324
|
-
raw: cap[0]
|
|
324
|
+
raw: rtrim(cap[0], '\n')
|
|
325
325
|
};
|
|
326
326
|
}
|
|
327
327
|
}
|
|
328
328
|
blockquote(src) {
|
|
329
329
|
const cap = this.rules.block.blockquote.exec(src);
|
|
330
330
|
if (cap) {
|
|
331
|
-
|
|
332
|
-
let
|
|
333
|
-
text =
|
|
334
|
-
const
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
331
|
+
let lines = rtrim(cap[0], '\n').split('\n');
|
|
332
|
+
let raw = '';
|
|
333
|
+
let text = '';
|
|
334
|
+
const tokens = [];
|
|
335
|
+
while (lines.length > 0) {
|
|
336
|
+
let inBlockquote = false;
|
|
337
|
+
const currentLines = [];
|
|
338
|
+
let i;
|
|
339
|
+
for (i = 0; i < lines.length; i++) {
|
|
340
|
+
// get lines up to a continuation
|
|
341
|
+
if (/^ {0,3}>/.test(lines[i])) {
|
|
342
|
+
currentLines.push(lines[i]);
|
|
343
|
+
inBlockquote = true;
|
|
344
|
+
}
|
|
345
|
+
else if (!inBlockquote) {
|
|
346
|
+
currentLines.push(lines[i]);
|
|
347
|
+
}
|
|
348
|
+
else {
|
|
349
|
+
break;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
lines = lines.slice(i);
|
|
353
|
+
const currentRaw = currentLines.join('\n');
|
|
354
|
+
const currentText = currentRaw
|
|
355
|
+
// precede setext continuation with 4 spaces so it isn't a setext
|
|
356
|
+
.replace(/\n {0,3}((?:=+|-+) *)(?=\n|$)/g, '\n $1')
|
|
357
|
+
.replace(/^ {0,3}>[ \t]?/gm, '');
|
|
358
|
+
raw = raw ? `${raw}\n${currentRaw}` : currentRaw;
|
|
359
|
+
text = text ? `${text}\n${currentText}` : currentText;
|
|
360
|
+
// parse blockquote lines as top level tokens
|
|
361
|
+
// merge paragraphs if this is a continuation
|
|
362
|
+
const top = this.lexer.state.top;
|
|
363
|
+
this.lexer.state.top = true;
|
|
364
|
+
this.lexer.blockTokens(currentText, tokens, true);
|
|
365
|
+
this.lexer.state.top = top;
|
|
366
|
+
// if there is no continuation then we are done
|
|
367
|
+
if (lines.length === 0) {
|
|
368
|
+
break;
|
|
369
|
+
}
|
|
370
|
+
const lastToken = tokens[tokens.length - 1];
|
|
371
|
+
if (lastToken?.type === 'code') {
|
|
372
|
+
// blockquote continuation cannot be preceded by a code block
|
|
373
|
+
break;
|
|
374
|
+
}
|
|
375
|
+
else if (lastToken?.type === 'blockquote') {
|
|
376
|
+
// include continuation in nested blockquote
|
|
377
|
+
const oldToken = lastToken;
|
|
378
|
+
const newText = oldToken.raw + '\n' + lines.join('\n');
|
|
379
|
+
const newToken = this.blockquote(newText);
|
|
380
|
+
tokens[tokens.length - 1] = newToken;
|
|
381
|
+
raw = raw.substring(0, raw.length - oldToken.raw.length) + newToken.raw;
|
|
382
|
+
text = text.substring(0, text.length - oldToken.text.length) + newToken.text;
|
|
383
|
+
break;
|
|
384
|
+
}
|
|
385
|
+
else if (lastToken?.type === 'list') {
|
|
386
|
+
// include continuation in nested list
|
|
387
|
+
const oldToken = lastToken;
|
|
388
|
+
const newText = oldToken.raw + '\n' + lines.join('\n');
|
|
389
|
+
const newToken = this.list(newText);
|
|
390
|
+
tokens[tokens.length - 1] = newToken;
|
|
391
|
+
raw = raw.substring(0, raw.length - lastToken.raw.length) + newToken.raw;
|
|
392
|
+
text = text.substring(0, text.length - oldToken.raw.length) + newToken.raw;
|
|
393
|
+
lines = newText.substring(tokens[tokens.length - 1].raw.length).split('\n');
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
338
397
|
return {
|
|
339
398
|
type: 'blockquote',
|
|
340
|
-
raw
|
|
399
|
+
raw,
|
|
341
400
|
tokens,
|
|
342
401
|
text
|
|
343
402
|
};
|
|
@@ -574,17 +633,21 @@
|
|
|
574
633
|
item.align.push(null);
|
|
575
634
|
}
|
|
576
635
|
}
|
|
577
|
-
for (
|
|
636
|
+
for (let i = 0; i < headers.length; i++) {
|
|
578
637
|
item.header.push({
|
|
579
|
-
text:
|
|
580
|
-
tokens: this.lexer.inline(
|
|
638
|
+
text: headers[i],
|
|
639
|
+
tokens: this.lexer.inline(headers[i]),
|
|
640
|
+
header: true,
|
|
641
|
+
align: item.align[i]
|
|
581
642
|
});
|
|
582
643
|
}
|
|
583
644
|
for (const row of rows) {
|
|
584
|
-
item.rows.push(splitCells(row, item.header.length).map(cell => {
|
|
645
|
+
item.rows.push(splitCells(row, item.header.length).map((cell, i) => {
|
|
585
646
|
return {
|
|
586
647
|
text: cell,
|
|
587
|
-
tokens: this.lexer.inline(cell)
|
|
648
|
+
tokens: this.lexer.inline(cell),
|
|
649
|
+
header: false,
|
|
650
|
+
align: item.align[i]
|
|
588
651
|
};
|
|
589
652
|
}));
|
|
590
653
|
}
|
|
@@ -1280,7 +1343,7 @@
|
|
|
1280
1343
|
this.inlineQueue = [];
|
|
1281
1344
|
return this.tokens;
|
|
1282
1345
|
}
|
|
1283
|
-
blockTokens(src, tokens = []) {
|
|
1346
|
+
blockTokens(src, tokens = [], lastParagraphClipped = false) {
|
|
1284
1347
|
if (this.options.pedantic) {
|
|
1285
1348
|
src = src.replace(/\t/g, ' ').replace(/^ +$/gm, '');
|
|
1286
1349
|
}
|
|
@@ -1292,7 +1355,6 @@
|
|
|
1292
1355
|
let token;
|
|
1293
1356
|
let lastToken;
|
|
1294
1357
|
let cutSrc;
|
|
1295
|
-
let lastParagraphClipped;
|
|
1296
1358
|
while (src) {
|
|
1297
1359
|
if (this.options.extensions
|
|
1298
1360
|
&& this.options.extensions.block
|
|
@@ -1418,7 +1480,7 @@
|
|
|
1418
1480
|
}
|
|
1419
1481
|
if (this.state.top && (token = this.tokenizer.paragraph(cutSrc))) {
|
|
1420
1482
|
lastToken = tokens[tokens.length - 1];
|
|
1421
|
-
if (lastParagraphClipped && lastToken
|
|
1483
|
+
if (lastParagraphClipped && lastToken?.type === 'paragraph') {
|
|
1422
1484
|
lastToken.raw += '\n' + token.raw;
|
|
1423
1485
|
lastToken.text += '\n' + token.text;
|
|
1424
1486
|
this.inlineQueue.pop();
|
|
@@ -1637,53 +1699,103 @@
|
|
|
1637
1699
|
*/
|
|
1638
1700
|
class _Renderer {
|
|
1639
1701
|
options;
|
|
1702
|
+
parser; // set by the parser
|
|
1640
1703
|
constructor(options) {
|
|
1641
1704
|
this.options = options || exports.defaults;
|
|
1642
1705
|
}
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1706
|
+
space(token) {
|
|
1707
|
+
return '';
|
|
1708
|
+
}
|
|
1709
|
+
code({ text, lang, escaped }) {
|
|
1710
|
+
const langString = (lang || '').match(/^\S*/)?.[0];
|
|
1711
|
+
const code = text.replace(/\n$/, '') + '\n';
|
|
1712
|
+
if (!langString) {
|
|
1647
1713
|
return '<pre><code>'
|
|
1648
1714
|
+ (escaped ? code : escape$1(code, true))
|
|
1649
1715
|
+ '</code></pre>\n';
|
|
1650
1716
|
}
|
|
1651
1717
|
return '<pre><code class="language-'
|
|
1652
|
-
+ escape$1(
|
|
1718
|
+
+ escape$1(langString)
|
|
1653
1719
|
+ '">'
|
|
1654
1720
|
+ (escaped ? code : escape$1(code, true))
|
|
1655
1721
|
+ '</code></pre>\n';
|
|
1656
1722
|
}
|
|
1657
|
-
blockquote(
|
|
1658
|
-
|
|
1723
|
+
blockquote({ tokens }) {
|
|
1724
|
+
const body = this.parser.parse(tokens);
|
|
1725
|
+
return `<blockquote>\n${body}</blockquote>\n`;
|
|
1659
1726
|
}
|
|
1660
|
-
html(
|
|
1661
|
-
return
|
|
1727
|
+
html({ text }) {
|
|
1728
|
+
return text;
|
|
1662
1729
|
}
|
|
1663
|
-
heading(
|
|
1664
|
-
|
|
1665
|
-
return `<h${level}>${text}</h${level}>\n`;
|
|
1730
|
+
heading({ tokens, depth }) {
|
|
1731
|
+
return `<h${depth}>${this.parser.parseInline(tokens)}</h${depth}>\n`;
|
|
1666
1732
|
}
|
|
1667
|
-
hr() {
|
|
1733
|
+
hr(token) {
|
|
1668
1734
|
return '<hr>\n';
|
|
1669
1735
|
}
|
|
1670
|
-
list(
|
|
1736
|
+
list(token) {
|
|
1737
|
+
const ordered = token.ordered;
|
|
1738
|
+
const start = token.start;
|
|
1739
|
+
let body = '';
|
|
1740
|
+
for (let j = 0; j < token.items.length; j++) {
|
|
1741
|
+
const item = token.items[j];
|
|
1742
|
+
body += this.listitem(item);
|
|
1743
|
+
}
|
|
1671
1744
|
const type = ordered ? 'ol' : 'ul';
|
|
1672
|
-
const
|
|
1673
|
-
return '<' + type +
|
|
1674
|
-
}
|
|
1675
|
-
listitem(
|
|
1676
|
-
|
|
1745
|
+
const startAttr = (ordered && start !== 1) ? (' start="' + start + '"') : '';
|
|
1746
|
+
return '<' + type + startAttr + '>\n' + body + '</' + type + '>\n';
|
|
1747
|
+
}
|
|
1748
|
+
listitem(item) {
|
|
1749
|
+
let itemBody = '';
|
|
1750
|
+
if (item.task) {
|
|
1751
|
+
const checkbox = this.checkbox({ checked: !!item.checked });
|
|
1752
|
+
if (item.loose) {
|
|
1753
|
+
if (item.tokens.length > 0 && item.tokens[0].type === 'paragraph') {
|
|
1754
|
+
item.tokens[0].text = checkbox + ' ' + item.tokens[0].text;
|
|
1755
|
+
if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') {
|
|
1756
|
+
item.tokens[0].tokens[0].text = checkbox + ' ' + item.tokens[0].tokens[0].text;
|
|
1757
|
+
}
|
|
1758
|
+
}
|
|
1759
|
+
else {
|
|
1760
|
+
item.tokens.unshift({
|
|
1761
|
+
type: 'text',
|
|
1762
|
+
raw: checkbox + ' ',
|
|
1763
|
+
text: checkbox + ' '
|
|
1764
|
+
});
|
|
1765
|
+
}
|
|
1766
|
+
}
|
|
1767
|
+
else {
|
|
1768
|
+
itemBody += checkbox + ' ';
|
|
1769
|
+
}
|
|
1770
|
+
}
|
|
1771
|
+
itemBody += this.parser.parse(item.tokens, !!item.loose);
|
|
1772
|
+
return `<li>${itemBody}</li>\n`;
|
|
1677
1773
|
}
|
|
1678
|
-
checkbox(checked) {
|
|
1774
|
+
checkbox({ checked }) {
|
|
1679
1775
|
return '<input '
|
|
1680
1776
|
+ (checked ? 'checked="" ' : '')
|
|
1681
1777
|
+ 'disabled="" type="checkbox">';
|
|
1682
1778
|
}
|
|
1683
|
-
paragraph(
|
|
1684
|
-
return `<p>${
|
|
1779
|
+
paragraph({ tokens }) {
|
|
1780
|
+
return `<p>${this.parser.parseInline(tokens)}</p>\n`;
|
|
1685
1781
|
}
|
|
1686
|
-
table(
|
|
1782
|
+
table(token) {
|
|
1783
|
+
let header = '';
|
|
1784
|
+
// header
|
|
1785
|
+
let cell = '';
|
|
1786
|
+
for (let j = 0; j < token.header.length; j++) {
|
|
1787
|
+
cell += this.tablecell(token.header[j]);
|
|
1788
|
+
}
|
|
1789
|
+
header += this.tablerow({ text: cell });
|
|
1790
|
+
let body = '';
|
|
1791
|
+
for (let j = 0; j < token.rows.length; j++) {
|
|
1792
|
+
const row = token.rows[j];
|
|
1793
|
+
cell = '';
|
|
1794
|
+
for (let k = 0; k < row.length; k++) {
|
|
1795
|
+
cell += this.tablecell(row[k]);
|
|
1796
|
+
}
|
|
1797
|
+
body += this.tablerow({ text: cell });
|
|
1798
|
+
}
|
|
1687
1799
|
if (body)
|
|
1688
1800
|
body = `<tbody>${body}</tbody>`;
|
|
1689
1801
|
return '<table>\n'
|
|
@@ -1693,35 +1805,37 @@
|
|
|
1693
1805
|
+ body
|
|
1694
1806
|
+ '</table>\n';
|
|
1695
1807
|
}
|
|
1696
|
-
tablerow(
|
|
1697
|
-
return `<tr>\n${
|
|
1808
|
+
tablerow({ text }) {
|
|
1809
|
+
return `<tr>\n${text}</tr>\n`;
|
|
1698
1810
|
}
|
|
1699
|
-
tablecell(
|
|
1700
|
-
const
|
|
1701
|
-
const
|
|
1702
|
-
|
|
1811
|
+
tablecell(token) {
|
|
1812
|
+
const content = this.parser.parseInline(token.tokens);
|
|
1813
|
+
const type = token.header ? 'th' : 'td';
|
|
1814
|
+
const tag = token.align
|
|
1815
|
+
? `<${type} align="${token.align}">`
|
|
1703
1816
|
: `<${type}>`;
|
|
1704
1817
|
return tag + content + `</${type}>\n`;
|
|
1705
1818
|
}
|
|
1706
1819
|
/**
|
|
1707
1820
|
* span level renderer
|
|
1708
1821
|
*/
|
|
1709
|
-
strong(
|
|
1710
|
-
return `<strong>${
|
|
1822
|
+
strong({ tokens }) {
|
|
1823
|
+
return `<strong>${this.parser.parseInline(tokens)}</strong>`;
|
|
1711
1824
|
}
|
|
1712
|
-
em(
|
|
1713
|
-
return `<em>${
|
|
1825
|
+
em({ tokens }) {
|
|
1826
|
+
return `<em>${this.parser.parseInline(tokens)}</em>`;
|
|
1714
1827
|
}
|
|
1715
|
-
codespan(text) {
|
|
1828
|
+
codespan({ text }) {
|
|
1716
1829
|
return `<code>${text}</code>`;
|
|
1717
1830
|
}
|
|
1718
|
-
br() {
|
|
1831
|
+
br(token) {
|
|
1719
1832
|
return '<br>';
|
|
1720
1833
|
}
|
|
1721
|
-
del(
|
|
1722
|
-
return `<del>${
|
|
1834
|
+
del({ tokens }) {
|
|
1835
|
+
return `<del>${this.parser.parseInline(tokens)}</del>`;
|
|
1723
1836
|
}
|
|
1724
|
-
link(href, title,
|
|
1837
|
+
link({ href, title, tokens }) {
|
|
1838
|
+
const text = this.parser.parseInline(tokens);
|
|
1725
1839
|
const cleanHref = cleanUrl(href);
|
|
1726
1840
|
if (cleanHref === null) {
|
|
1727
1841
|
return text;
|
|
@@ -1734,7 +1848,7 @@
|
|
|
1734
1848
|
out += '>' + text + '</a>';
|
|
1735
1849
|
return out;
|
|
1736
1850
|
}
|
|
1737
|
-
image(href, title, text) {
|
|
1851
|
+
image({ href, title, text }) {
|
|
1738
1852
|
const cleanHref = cleanUrl(href);
|
|
1739
1853
|
if (cleanHref === null) {
|
|
1740
1854
|
return text;
|
|
@@ -1747,8 +1861,8 @@
|
|
|
1747
1861
|
out += '>';
|
|
1748
1862
|
return out;
|
|
1749
1863
|
}
|
|
1750
|
-
text(
|
|
1751
|
-
return text;
|
|
1864
|
+
text(token) {
|
|
1865
|
+
return 'tokens' in token && token.tokens ? this.parser.parseInline(token.tokens) : token.text;
|
|
1752
1866
|
}
|
|
1753
1867
|
}
|
|
1754
1868
|
|
|
@@ -1758,28 +1872,28 @@
|
|
|
1758
1872
|
*/
|
|
1759
1873
|
class _TextRenderer {
|
|
1760
1874
|
// no need for block level renderers
|
|
1761
|
-
strong(text) {
|
|
1875
|
+
strong({ text }) {
|
|
1762
1876
|
return text;
|
|
1763
1877
|
}
|
|
1764
|
-
em(text) {
|
|
1878
|
+
em({ text }) {
|
|
1765
1879
|
return text;
|
|
1766
1880
|
}
|
|
1767
|
-
codespan(text) {
|
|
1881
|
+
codespan({ text }) {
|
|
1768
1882
|
return text;
|
|
1769
1883
|
}
|
|
1770
|
-
del(text) {
|
|
1884
|
+
del({ text }) {
|
|
1771
1885
|
return text;
|
|
1772
1886
|
}
|
|
1773
|
-
html(text) {
|
|
1887
|
+
html({ text }) {
|
|
1774
1888
|
return text;
|
|
1775
1889
|
}
|
|
1776
|
-
text(text) {
|
|
1890
|
+
text({ text }) {
|
|
1777
1891
|
return text;
|
|
1778
1892
|
}
|
|
1779
|
-
link(
|
|
1893
|
+
link({ text }) {
|
|
1780
1894
|
return '' + text;
|
|
1781
1895
|
}
|
|
1782
|
-
image(
|
|
1896
|
+
image({ text }) {
|
|
1783
1897
|
return '' + text;
|
|
1784
1898
|
}
|
|
1785
1899
|
br() {
|
|
@@ -1799,6 +1913,7 @@
|
|
|
1799
1913
|
this.options.renderer = this.options.renderer || new _Renderer();
|
|
1800
1914
|
this.renderer = this.options.renderer;
|
|
1801
1915
|
this.renderer.options = this.options;
|
|
1916
|
+
this.renderer.parser = this;
|
|
1802
1917
|
this.textRenderer = new _TextRenderer();
|
|
1803
1918
|
}
|
|
1804
1919
|
/**
|
|
@@ -1821,116 +1936,72 @@
|
|
|
1821
1936
|
parse(tokens, top = true) {
|
|
1822
1937
|
let out = '';
|
|
1823
1938
|
for (let i = 0; i < tokens.length; i++) {
|
|
1824
|
-
const
|
|
1939
|
+
const anyToken = tokens[i];
|
|
1825
1940
|
// Run any renderer extensions
|
|
1826
|
-
if (this.options.extensions && this.options.extensions.renderers && this.options.extensions.renderers[
|
|
1827
|
-
const genericToken =
|
|
1941
|
+
if (this.options.extensions && this.options.extensions.renderers && this.options.extensions.renderers[anyToken.type]) {
|
|
1942
|
+
const genericToken = anyToken;
|
|
1828
1943
|
const ret = this.options.extensions.renderers[genericToken.type].call({ parser: this }, genericToken);
|
|
1829
1944
|
if (ret !== false || !['space', 'hr', 'heading', 'code', 'table', 'blockquote', 'list', 'html', 'paragraph', 'text'].includes(genericToken.type)) {
|
|
1830
1945
|
out += ret || '';
|
|
1831
1946
|
continue;
|
|
1832
1947
|
}
|
|
1833
1948
|
}
|
|
1949
|
+
const token = anyToken;
|
|
1834
1950
|
switch (token.type) {
|
|
1835
1951
|
case 'space': {
|
|
1952
|
+
out += this.renderer.space(token);
|
|
1836
1953
|
continue;
|
|
1837
1954
|
}
|
|
1838
1955
|
case 'hr': {
|
|
1839
|
-
out += this.renderer.hr();
|
|
1956
|
+
out += this.renderer.hr(token);
|
|
1840
1957
|
continue;
|
|
1841
1958
|
}
|
|
1842
1959
|
case 'heading': {
|
|
1843
|
-
|
|
1844
|
-
out += this.renderer.heading(this.parseInline(headingToken.tokens), headingToken.depth, unescape(this.parseInline(headingToken.tokens, this.textRenderer)));
|
|
1960
|
+
out += this.renderer.heading(token);
|
|
1845
1961
|
continue;
|
|
1846
1962
|
}
|
|
1847
1963
|
case 'code': {
|
|
1848
|
-
|
|
1849
|
-
out += this.renderer.code(codeToken.text, codeToken.lang, !!codeToken.escaped);
|
|
1964
|
+
out += this.renderer.code(token);
|
|
1850
1965
|
continue;
|
|
1851
1966
|
}
|
|
1852
1967
|
case 'table': {
|
|
1853
|
-
|
|
1854
|
-
let header = '';
|
|
1855
|
-
// header
|
|
1856
|
-
let cell = '';
|
|
1857
|
-
for (let j = 0; j < tableToken.header.length; j++) {
|
|
1858
|
-
cell += this.renderer.tablecell(this.parseInline(tableToken.header[j].tokens), { header: true, align: tableToken.align[j] });
|
|
1859
|
-
}
|
|
1860
|
-
header += this.renderer.tablerow(cell);
|
|
1861
|
-
let body = '';
|
|
1862
|
-
for (let j = 0; j < tableToken.rows.length; j++) {
|
|
1863
|
-
const row = tableToken.rows[j];
|
|
1864
|
-
cell = '';
|
|
1865
|
-
for (let k = 0; k < row.length; k++) {
|
|
1866
|
-
cell += this.renderer.tablecell(this.parseInline(row[k].tokens), { header: false, align: tableToken.align[k] });
|
|
1867
|
-
}
|
|
1868
|
-
body += this.renderer.tablerow(cell);
|
|
1869
|
-
}
|
|
1870
|
-
out += this.renderer.table(header, body);
|
|
1968
|
+
out += this.renderer.table(token);
|
|
1871
1969
|
continue;
|
|
1872
1970
|
}
|
|
1873
1971
|
case 'blockquote': {
|
|
1874
|
-
|
|
1875
|
-
const body = this.parse(blockquoteToken.tokens);
|
|
1876
|
-
out += this.renderer.blockquote(body);
|
|
1972
|
+
out += this.renderer.blockquote(token);
|
|
1877
1973
|
continue;
|
|
1878
1974
|
}
|
|
1879
1975
|
case 'list': {
|
|
1880
|
-
|
|
1881
|
-
const ordered = listToken.ordered;
|
|
1882
|
-
const start = listToken.start;
|
|
1883
|
-
const loose = listToken.loose;
|
|
1884
|
-
let body = '';
|
|
1885
|
-
for (let j = 0; j < listToken.items.length; j++) {
|
|
1886
|
-
const item = listToken.items[j];
|
|
1887
|
-
const checked = item.checked;
|
|
1888
|
-
const task = item.task;
|
|
1889
|
-
let itemBody = '';
|
|
1890
|
-
if (item.task) {
|
|
1891
|
-
const checkbox = this.renderer.checkbox(!!checked);
|
|
1892
|
-
if (loose) {
|
|
1893
|
-
if (item.tokens.length > 0 && item.tokens[0].type === 'paragraph') {
|
|
1894
|
-
item.tokens[0].text = checkbox + ' ' + item.tokens[0].text;
|
|
1895
|
-
if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') {
|
|
1896
|
-
item.tokens[0].tokens[0].text = checkbox + ' ' + item.tokens[0].tokens[0].text;
|
|
1897
|
-
}
|
|
1898
|
-
}
|
|
1899
|
-
else {
|
|
1900
|
-
item.tokens.unshift({
|
|
1901
|
-
type: 'text',
|
|
1902
|
-
text: checkbox + ' '
|
|
1903
|
-
});
|
|
1904
|
-
}
|
|
1905
|
-
}
|
|
1906
|
-
else {
|
|
1907
|
-
itemBody += checkbox + ' ';
|
|
1908
|
-
}
|
|
1909
|
-
}
|
|
1910
|
-
itemBody += this.parse(item.tokens, loose);
|
|
1911
|
-
body += this.renderer.listitem(itemBody, task, !!checked);
|
|
1912
|
-
}
|
|
1913
|
-
out += this.renderer.list(body, ordered, start);
|
|
1976
|
+
out += this.renderer.list(token);
|
|
1914
1977
|
continue;
|
|
1915
1978
|
}
|
|
1916
1979
|
case 'html': {
|
|
1917
|
-
|
|
1918
|
-
out += this.renderer.html(htmlToken.text, htmlToken.block);
|
|
1980
|
+
out += this.renderer.html(token);
|
|
1919
1981
|
continue;
|
|
1920
1982
|
}
|
|
1921
1983
|
case 'paragraph': {
|
|
1922
|
-
|
|
1923
|
-
out += this.renderer.paragraph(this.parseInline(paragraphToken.tokens));
|
|
1984
|
+
out += this.renderer.paragraph(token);
|
|
1924
1985
|
continue;
|
|
1925
1986
|
}
|
|
1926
1987
|
case 'text': {
|
|
1927
1988
|
let textToken = token;
|
|
1928
|
-
let body =
|
|
1989
|
+
let body = this.renderer.text(textToken);
|
|
1929
1990
|
while (i + 1 < tokens.length && tokens[i + 1].type === 'text') {
|
|
1930
1991
|
textToken = tokens[++i];
|
|
1931
|
-
body += '\n' +
|
|
1992
|
+
body += '\n' + this.renderer.text(textToken);
|
|
1993
|
+
}
|
|
1994
|
+
if (top) {
|
|
1995
|
+
out += this.renderer.paragraph({
|
|
1996
|
+
type: 'paragraph',
|
|
1997
|
+
raw: body,
|
|
1998
|
+
text: body,
|
|
1999
|
+
tokens: [{ type: 'text', raw: body, text: body }]
|
|
2000
|
+
});
|
|
2001
|
+
}
|
|
2002
|
+
else {
|
|
2003
|
+
out += body;
|
|
1932
2004
|
}
|
|
1933
|
-
out += top ? this.renderer.paragraph(body) : body;
|
|
1934
2005
|
continue;
|
|
1935
2006
|
}
|
|
1936
2007
|
default: {
|
|
@@ -1954,63 +2025,55 @@
|
|
|
1954
2025
|
renderer = renderer || this.renderer;
|
|
1955
2026
|
let out = '';
|
|
1956
2027
|
for (let i = 0; i < tokens.length; i++) {
|
|
1957
|
-
const
|
|
2028
|
+
const anyToken = tokens[i];
|
|
1958
2029
|
// Run any renderer extensions
|
|
1959
|
-
if (this.options.extensions && this.options.extensions.renderers && this.options.extensions.renderers[
|
|
1960
|
-
const ret = this.options.extensions.renderers[
|
|
1961
|
-
if (ret !== false || !['escape', 'html', 'link', 'image', 'strong', 'em', 'codespan', 'br', 'del', 'text'].includes(
|
|
2030
|
+
if (this.options.extensions && this.options.extensions.renderers && this.options.extensions.renderers[anyToken.type]) {
|
|
2031
|
+
const ret = this.options.extensions.renderers[anyToken.type].call({ parser: this }, anyToken);
|
|
2032
|
+
if (ret !== false || !['escape', 'html', 'link', 'image', 'strong', 'em', 'codespan', 'br', 'del', 'text'].includes(anyToken.type)) {
|
|
1962
2033
|
out += ret || '';
|
|
1963
2034
|
continue;
|
|
1964
2035
|
}
|
|
1965
2036
|
}
|
|
2037
|
+
const token = anyToken;
|
|
1966
2038
|
switch (token.type) {
|
|
1967
2039
|
case 'escape': {
|
|
1968
|
-
|
|
1969
|
-
out += renderer.text(escapeToken.text);
|
|
2040
|
+
out += renderer.text(token);
|
|
1970
2041
|
break;
|
|
1971
2042
|
}
|
|
1972
2043
|
case 'html': {
|
|
1973
|
-
|
|
1974
|
-
out += renderer.html(tagToken.text);
|
|
2044
|
+
out += renderer.html(token);
|
|
1975
2045
|
break;
|
|
1976
2046
|
}
|
|
1977
2047
|
case 'link': {
|
|
1978
|
-
|
|
1979
|
-
out += renderer.link(linkToken.href, linkToken.title, this.parseInline(linkToken.tokens, renderer));
|
|
2048
|
+
out += renderer.link(token);
|
|
1980
2049
|
break;
|
|
1981
2050
|
}
|
|
1982
2051
|
case 'image': {
|
|
1983
|
-
|
|
1984
|
-
out += renderer.image(imageToken.href, imageToken.title, imageToken.text);
|
|
2052
|
+
out += renderer.image(token);
|
|
1985
2053
|
break;
|
|
1986
2054
|
}
|
|
1987
2055
|
case 'strong': {
|
|
1988
|
-
|
|
1989
|
-
out += renderer.strong(this.parseInline(strongToken.tokens, renderer));
|
|
2056
|
+
out += renderer.strong(token);
|
|
1990
2057
|
break;
|
|
1991
2058
|
}
|
|
1992
2059
|
case 'em': {
|
|
1993
|
-
|
|
1994
|
-
out += renderer.em(this.parseInline(emToken.tokens, renderer));
|
|
2060
|
+
out += renderer.em(token);
|
|
1995
2061
|
break;
|
|
1996
2062
|
}
|
|
1997
2063
|
case 'codespan': {
|
|
1998
|
-
|
|
1999
|
-
out += renderer.codespan(codespanToken.text);
|
|
2064
|
+
out += renderer.codespan(token);
|
|
2000
2065
|
break;
|
|
2001
2066
|
}
|
|
2002
2067
|
case 'br': {
|
|
2003
|
-
out += renderer.br();
|
|
2068
|
+
out += renderer.br(token);
|
|
2004
2069
|
break;
|
|
2005
2070
|
}
|
|
2006
2071
|
case 'del': {
|
|
2007
|
-
|
|
2008
|
-
out += renderer.del(this.parseInline(delToken.tokens, renderer));
|
|
2072
|
+
out += renderer.del(token);
|
|
2009
2073
|
break;
|
|
2010
2074
|
}
|
|
2011
2075
|
case 'text': {
|
|
2012
|
-
|
|
2013
|
-
out += renderer.text(textToken.text);
|
|
2076
|
+
out += renderer.text(token);
|
|
2014
2077
|
break;
|
|
2015
2078
|
}
|
|
2016
2079
|
default: {
|
|
@@ -2186,15 +2249,19 @@
|
|
|
2186
2249
|
if (!(prop in renderer)) {
|
|
2187
2250
|
throw new Error(`renderer '${prop}' does not exist`);
|
|
2188
2251
|
}
|
|
2189
|
-
if (
|
|
2252
|
+
if (['options', 'parser'].includes(prop)) {
|
|
2190
2253
|
// ignore options property
|
|
2191
2254
|
continue;
|
|
2192
2255
|
}
|
|
2193
2256
|
const rendererProp = prop;
|
|
2194
|
-
|
|
2257
|
+
let rendererFunc = pack.renderer[rendererProp];
|
|
2195
2258
|
const prevRenderer = renderer[rendererProp];
|
|
2196
2259
|
// Replace renderer with func to run extension, but fall back if false
|
|
2197
2260
|
renderer[rendererProp] = (...args) => {
|
|
2261
|
+
if (!pack.useNewRenderer) {
|
|
2262
|
+
// TODO: Remove this in next major version
|
|
2263
|
+
rendererFunc = this.#convertRendererFunction(rendererFunc, rendererProp, renderer);
|
|
2264
|
+
}
|
|
2198
2265
|
let ret = rendererFunc.apply(renderer, args);
|
|
2199
2266
|
if (ret === false) {
|
|
2200
2267
|
ret = prevRenderer.apply(renderer, args);
|
|
@@ -2285,6 +2352,215 @@
|
|
|
2285
2352
|
});
|
|
2286
2353
|
return this;
|
|
2287
2354
|
}
|
|
2355
|
+
// TODO: Remove this in next major release
|
|
2356
|
+
#convertRendererFunction(func, prop, renderer) {
|
|
2357
|
+
switch (prop) {
|
|
2358
|
+
case 'heading':
|
|
2359
|
+
return function (token) {
|
|
2360
|
+
if (!token.type || token.type !== prop) {
|
|
2361
|
+
// @ts-ignore
|
|
2362
|
+
// eslint-disable-next-line prefer-rest-params
|
|
2363
|
+
return func.apply(this, arguments);
|
|
2364
|
+
}
|
|
2365
|
+
return func(renderer.parser.parseInline(token.tokens), token.depth, unescape(renderer.parser.parseInline(token.tokens, renderer.parser.textRenderer)));
|
|
2366
|
+
};
|
|
2367
|
+
case 'code':
|
|
2368
|
+
return function (token) {
|
|
2369
|
+
if (!token.type || token.type !== prop) {
|
|
2370
|
+
// @ts-ignore
|
|
2371
|
+
// eslint-disable-next-line prefer-rest-params
|
|
2372
|
+
return func.apply(this, arguments);
|
|
2373
|
+
}
|
|
2374
|
+
return func(token.text, token.lang, !!token.escaped);
|
|
2375
|
+
};
|
|
2376
|
+
case 'table':
|
|
2377
|
+
return function (token) {
|
|
2378
|
+
if (!token.type || token.type !== prop) {
|
|
2379
|
+
// @ts-ignore
|
|
2380
|
+
// eslint-disable-next-line prefer-rest-params
|
|
2381
|
+
return func.apply(this, arguments);
|
|
2382
|
+
}
|
|
2383
|
+
let header = '';
|
|
2384
|
+
// header
|
|
2385
|
+
let cell = '';
|
|
2386
|
+
for (let j = 0; j < token.header.length; j++) {
|
|
2387
|
+
cell += this.tablecell({
|
|
2388
|
+
text: token.header[j].text,
|
|
2389
|
+
tokens: token.header[j].tokens,
|
|
2390
|
+
header: true,
|
|
2391
|
+
align: token.align[j]
|
|
2392
|
+
});
|
|
2393
|
+
}
|
|
2394
|
+
header += this.tablerow({ text: cell });
|
|
2395
|
+
let body = '';
|
|
2396
|
+
for (let j = 0; j < token.rows.length; j++) {
|
|
2397
|
+
const row = token.rows[j];
|
|
2398
|
+
cell = '';
|
|
2399
|
+
for (let k = 0; k < row.length; k++) {
|
|
2400
|
+
cell += this.tablecell({
|
|
2401
|
+
text: row[k].text,
|
|
2402
|
+
tokens: row[k].tokens,
|
|
2403
|
+
header: false,
|
|
2404
|
+
align: token.align[k]
|
|
2405
|
+
});
|
|
2406
|
+
}
|
|
2407
|
+
body += this.tablerow({ text: cell });
|
|
2408
|
+
}
|
|
2409
|
+
return func(header, body);
|
|
2410
|
+
};
|
|
2411
|
+
case 'blockquote':
|
|
2412
|
+
return function (token) {
|
|
2413
|
+
if (!token.type || token.type !== prop) {
|
|
2414
|
+
// @ts-ignore
|
|
2415
|
+
// eslint-disable-next-line prefer-rest-params
|
|
2416
|
+
return func.apply(this, arguments);
|
|
2417
|
+
}
|
|
2418
|
+
const body = this.parser.parse(token.tokens);
|
|
2419
|
+
return func(body);
|
|
2420
|
+
};
|
|
2421
|
+
case 'list':
|
|
2422
|
+
return function (token) {
|
|
2423
|
+
if (!token.type || token.type !== prop) {
|
|
2424
|
+
// @ts-ignore
|
|
2425
|
+
// eslint-disable-next-line prefer-rest-params
|
|
2426
|
+
return func.apply(this, arguments);
|
|
2427
|
+
}
|
|
2428
|
+
const ordered = token.ordered;
|
|
2429
|
+
const start = token.start;
|
|
2430
|
+
const loose = token.loose;
|
|
2431
|
+
let body = '';
|
|
2432
|
+
for (let j = 0; j < token.items.length; j++) {
|
|
2433
|
+
const item = token.items[j];
|
|
2434
|
+
const checked = item.checked;
|
|
2435
|
+
const task = item.task;
|
|
2436
|
+
let itemBody = '';
|
|
2437
|
+
if (item.task) {
|
|
2438
|
+
const checkbox = this.checkbox({ checked: !!checked });
|
|
2439
|
+
if (loose) {
|
|
2440
|
+
if (item.tokens.length > 0 && item.tokens[0].type === 'paragraph') {
|
|
2441
|
+
item.tokens[0].text = checkbox + ' ' + item.tokens[0].text;
|
|
2442
|
+
if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') {
|
|
2443
|
+
item.tokens[0].tokens[0].text = checkbox + ' ' + item.tokens[0].tokens[0].text;
|
|
2444
|
+
}
|
|
2445
|
+
}
|
|
2446
|
+
else {
|
|
2447
|
+
item.tokens.unshift({
|
|
2448
|
+
type: 'text',
|
|
2449
|
+
text: checkbox + ' '
|
|
2450
|
+
});
|
|
2451
|
+
}
|
|
2452
|
+
}
|
|
2453
|
+
else {
|
|
2454
|
+
itemBody += checkbox + ' ';
|
|
2455
|
+
}
|
|
2456
|
+
}
|
|
2457
|
+
itemBody += this.parser.parse(item.tokens, loose);
|
|
2458
|
+
body += this.listitem({
|
|
2459
|
+
type: 'list_item',
|
|
2460
|
+
raw: itemBody,
|
|
2461
|
+
text: itemBody,
|
|
2462
|
+
task,
|
|
2463
|
+
checked: !!checked,
|
|
2464
|
+
loose,
|
|
2465
|
+
tokens: item.tokens
|
|
2466
|
+
});
|
|
2467
|
+
}
|
|
2468
|
+
return func(body, ordered, start);
|
|
2469
|
+
};
|
|
2470
|
+
case 'html':
|
|
2471
|
+
return function (token) {
|
|
2472
|
+
if (!token.type || token.type !== prop) {
|
|
2473
|
+
// @ts-ignore
|
|
2474
|
+
// eslint-disable-next-line prefer-rest-params
|
|
2475
|
+
return func.apply(this, arguments);
|
|
2476
|
+
}
|
|
2477
|
+
return func(token.text, token.block);
|
|
2478
|
+
};
|
|
2479
|
+
case 'paragraph':
|
|
2480
|
+
return function (token) {
|
|
2481
|
+
if (!token.type || token.type !== prop) {
|
|
2482
|
+
// @ts-ignore
|
|
2483
|
+
// eslint-disable-next-line prefer-rest-params
|
|
2484
|
+
return func.apply(this, arguments);
|
|
2485
|
+
}
|
|
2486
|
+
return func(this.parser.parseInline(token.tokens));
|
|
2487
|
+
};
|
|
2488
|
+
case 'escape':
|
|
2489
|
+
return function (token) {
|
|
2490
|
+
if (!token.type || token.type !== prop) {
|
|
2491
|
+
// @ts-ignore
|
|
2492
|
+
// eslint-disable-next-line prefer-rest-params
|
|
2493
|
+
return func.apply(this, arguments);
|
|
2494
|
+
}
|
|
2495
|
+
return func(token.text);
|
|
2496
|
+
};
|
|
2497
|
+
case 'link':
|
|
2498
|
+
return function (token) {
|
|
2499
|
+
if (!token.type || token.type !== prop) {
|
|
2500
|
+
// @ts-ignore
|
|
2501
|
+
// eslint-disable-next-line prefer-rest-params
|
|
2502
|
+
return func.apply(this, arguments);
|
|
2503
|
+
}
|
|
2504
|
+
return func(token.href, token.title, this.parser.parseInline(token.tokens));
|
|
2505
|
+
};
|
|
2506
|
+
case 'image':
|
|
2507
|
+
return function (token) {
|
|
2508
|
+
if (!token.type || token.type !== prop) {
|
|
2509
|
+
// @ts-ignore
|
|
2510
|
+
// eslint-disable-next-line prefer-rest-params
|
|
2511
|
+
return func.apply(this, arguments);
|
|
2512
|
+
}
|
|
2513
|
+
return func(token.href, token.title, token.text);
|
|
2514
|
+
};
|
|
2515
|
+
case 'strong':
|
|
2516
|
+
return function (token) {
|
|
2517
|
+
if (!token.type || token.type !== prop) {
|
|
2518
|
+
// @ts-ignore
|
|
2519
|
+
// eslint-disable-next-line prefer-rest-params
|
|
2520
|
+
return func.apply(this, arguments);
|
|
2521
|
+
}
|
|
2522
|
+
return func(this.parser.parseInline(token.tokens));
|
|
2523
|
+
};
|
|
2524
|
+
case 'em':
|
|
2525
|
+
return function (token) {
|
|
2526
|
+
if (!token.type || token.type !== prop) {
|
|
2527
|
+
// @ts-ignore
|
|
2528
|
+
// eslint-disable-next-line prefer-rest-params
|
|
2529
|
+
return func.apply(this, arguments);
|
|
2530
|
+
}
|
|
2531
|
+
return func(this.parser.parseInline(token.tokens));
|
|
2532
|
+
};
|
|
2533
|
+
case 'codespan':
|
|
2534
|
+
return function (token) {
|
|
2535
|
+
if (!token.type || token.type !== prop) {
|
|
2536
|
+
// @ts-ignore
|
|
2537
|
+
// eslint-disable-next-line prefer-rest-params
|
|
2538
|
+
return func.apply(this, arguments);
|
|
2539
|
+
}
|
|
2540
|
+
return func(token.text);
|
|
2541
|
+
};
|
|
2542
|
+
case 'del':
|
|
2543
|
+
return function (token) {
|
|
2544
|
+
if (!token.type || token.type !== prop) {
|
|
2545
|
+
// @ts-ignore
|
|
2546
|
+
// eslint-disable-next-line prefer-rest-params
|
|
2547
|
+
return func.apply(this, arguments);
|
|
2548
|
+
}
|
|
2549
|
+
return func(this.parser.parseInline(token.tokens));
|
|
2550
|
+
};
|
|
2551
|
+
case 'text':
|
|
2552
|
+
return function (token) {
|
|
2553
|
+
if (!token.type || token.type !== prop) {
|
|
2554
|
+
// @ts-ignore
|
|
2555
|
+
// eslint-disable-next-line prefer-rest-params
|
|
2556
|
+
return func.apply(this, arguments);
|
|
2557
|
+
}
|
|
2558
|
+
return func(token.text);
|
|
2559
|
+
};
|
|
2560
|
+
// do nothing
|
|
2561
|
+
}
|
|
2562
|
+
return func;
|
|
2563
|
+
}
|
|
2288
2564
|
setOptions(opt) {
|
|
2289
2565
|
this.defaults = { ...this.defaults, ...opt };
|
|
2290
2566
|
return this;
|