marked 7.0.0 → 7.0.2
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 +2541 -2180
- package/lib/marked.cjs.map +1 -1
- package/lib/marked.d.ts +692 -591
- package/lib/marked.esm.js +2522 -2136
- package/lib/marked.esm.js.map +1 -1
- package/lib/marked.umd.js +2566 -2179
- package/lib/marked.umd.js.map +1 -1
- package/marked.min.js +3 -3
- package/package.json +14 -13
- package/lib/marked.d.cts +0 -624
- package/src/Hooks.ts +0 -29
- package/src/Instance.ts +0 -381
- package/src/Lexer.ts +0 -517
- package/src/MarkedOptions.ts +0 -212
- package/src/Parser.ts +0 -291
- package/src/Renderer.ts +0 -167
- package/src/Slugger.ts +0 -51
- package/src/TextRenderer.ts +0 -42
- package/src/Tokenizer.ts +0 -810
- package/src/Tokens.ts +0 -199
- package/src/defaults.ts +0 -35
- package/src/helpers.ts +0 -264
- package/src/marked.ts +0 -144
- package/src/rules.ts +0 -380
package/lib/marked.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* marked v7.0.
|
|
2
|
+
* marked v7.0.2 - a markdown parser
|
|
3
3
|
* Copyright (c) 2011-2023, Christopher Jeffrey. (MIT Licensed)
|
|
4
4
|
* https://github.com/markedjs/marked
|
|
5
5
|
*/
|
|
@@ -9,2281 +9,2642 @@
|
|
|
9
9
|
* The code in this file is generated from files in ./src/
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
var __defProp = Object.defineProperty;
|
|
14
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
15
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
16
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
17
|
-
var __export = (target, all) => {
|
|
18
|
-
for (var name in all)
|
|
19
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
20
|
-
};
|
|
21
|
-
var __copyProps = (to, from, except, desc) => {
|
|
22
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
23
|
-
for (let key of __getOwnPropNames(from))
|
|
24
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
25
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
26
|
-
}
|
|
27
|
-
return to;
|
|
28
|
-
};
|
|
29
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
-
var __accessCheck = (obj, member, msg) => {
|
|
31
|
-
if (!member.has(obj))
|
|
32
|
-
throw TypeError("Cannot " + msg);
|
|
33
|
-
};
|
|
34
|
-
var __privateAdd = (obj, member, value) => {
|
|
35
|
-
if (member.has(obj))
|
|
36
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
37
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
38
|
-
};
|
|
39
|
-
var __privateMethod = (obj, member, method) => {
|
|
40
|
-
__accessCheck(obj, member, "access private method");
|
|
41
|
-
return method;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
// src/marked.ts
|
|
45
|
-
var marked_exports = {};
|
|
46
|
-
__export(marked_exports, {
|
|
47
|
-
Hooks: () => _Hooks,
|
|
48
|
-
Lexer: () => _Lexer2,
|
|
49
|
-
Marked: () => Marked,
|
|
50
|
-
Parser: () => _Parser,
|
|
51
|
-
Renderer: () => _Renderer,
|
|
52
|
-
Slugger: () => _Slugger,
|
|
53
|
-
TextRenderer: () => _TextRenderer,
|
|
54
|
-
Tokenizer: () => _Tokenizer,
|
|
55
|
-
defaults: () => _defaults,
|
|
56
|
-
getDefaults: () => _getDefaults,
|
|
57
|
-
lexer: () => lexer,
|
|
58
|
-
marked: () => marked,
|
|
59
|
-
options: () => options,
|
|
60
|
-
parse: () => parse,
|
|
61
|
-
parseInline: () => parseInline,
|
|
62
|
-
parser: () => parser,
|
|
63
|
-
setOptions: () => setOptions,
|
|
64
|
-
use: () => use,
|
|
65
|
-
walkTokens: () => walkTokens
|
|
66
|
-
});
|
|
67
|
-
module.exports = __toCommonJS(marked_exports);
|
|
12
|
+
'use strict';
|
|
68
13
|
|
|
69
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Gets the original marked default options.
|
|
16
|
+
*/
|
|
70
17
|
function _getDefaults() {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
18
|
+
return {
|
|
19
|
+
async: false,
|
|
20
|
+
baseUrl: null,
|
|
21
|
+
breaks: false,
|
|
22
|
+
extensions: null,
|
|
23
|
+
gfm: true,
|
|
24
|
+
headerIds: false,
|
|
25
|
+
headerPrefix: '',
|
|
26
|
+
highlight: null,
|
|
27
|
+
hooks: null,
|
|
28
|
+
langPrefix: 'language-',
|
|
29
|
+
mangle: false,
|
|
30
|
+
pedantic: false,
|
|
31
|
+
renderer: null,
|
|
32
|
+
sanitize: false,
|
|
33
|
+
sanitizer: null,
|
|
34
|
+
silent: false,
|
|
35
|
+
smartypants: false,
|
|
36
|
+
tokenizer: null,
|
|
37
|
+
walkTokens: null,
|
|
38
|
+
xhtml: false
|
|
39
|
+
};
|
|
93
40
|
}
|
|
94
|
-
|
|
41
|
+
exports.defaults = _getDefaults();
|
|
95
42
|
function changeDefaults(newDefaults) {
|
|
96
|
-
|
|
43
|
+
exports.defaults = newDefaults;
|
|
97
44
|
}
|
|
98
45
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
46
|
+
/**
|
|
47
|
+
* Helpers
|
|
48
|
+
*/
|
|
49
|
+
const escapeTest = /[&<>"']/;
|
|
50
|
+
const escapeReplace = new RegExp(escapeTest.source, 'g');
|
|
51
|
+
const escapeTestNoEncode = /[<>"']|&(?!(#\d{1,7}|#[Xx][a-fA-F0-9]{1,6}|\w+);)/;
|
|
52
|
+
const escapeReplaceNoEncode = new RegExp(escapeTestNoEncode.source, 'g');
|
|
53
|
+
const escapeReplacements = {
|
|
54
|
+
'&': '&',
|
|
55
|
+
'<': '<',
|
|
56
|
+
'>': '>',
|
|
57
|
+
'"': '"',
|
|
58
|
+
"'": '''
|
|
110
59
|
};
|
|
111
|
-
|
|
60
|
+
const getEscapeReplacement = (ch) => escapeReplacements[ch];
|
|
112
61
|
function escape(html, encode) {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
62
|
+
if (encode) {
|
|
63
|
+
if (escapeTest.test(html)) {
|
|
64
|
+
return html.replace(escapeReplace, getEscapeReplacement);
|
|
65
|
+
}
|
|
116
66
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
67
|
+
else {
|
|
68
|
+
if (escapeTestNoEncode.test(html)) {
|
|
69
|
+
return html.replace(escapeReplaceNoEncode, getEscapeReplacement);
|
|
70
|
+
}
|
|
120
71
|
}
|
|
121
|
-
|
|
122
|
-
return html;
|
|
72
|
+
return html;
|
|
123
73
|
}
|
|
124
|
-
|
|
74
|
+
const unescapeTest = /&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/ig;
|
|
125
75
|
function unescape(html) {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
76
|
+
// explicitly match decimal, hex, and named HTML entities
|
|
77
|
+
return html.replace(unescapeTest, (_, n) => {
|
|
78
|
+
n = n.toLowerCase();
|
|
79
|
+
if (n === 'colon')
|
|
80
|
+
return ':';
|
|
81
|
+
if (n.charAt(0) === '#') {
|
|
82
|
+
return n.charAt(1) === 'x'
|
|
83
|
+
? String.fromCharCode(parseInt(n.substring(2), 16))
|
|
84
|
+
: String.fromCharCode(+n.substring(1));
|
|
85
|
+
}
|
|
86
|
+
return '';
|
|
87
|
+
});
|
|
135
88
|
}
|
|
136
|
-
|
|
89
|
+
const caret = /(^|[^\[])\^/g;
|
|
137
90
|
function edit(regex, opt) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
91
|
+
regex = typeof regex === 'string' ? regex : regex.source;
|
|
92
|
+
opt = opt || '';
|
|
93
|
+
const obj = {
|
|
94
|
+
replace: (name, val) => {
|
|
95
|
+
val = typeof val === 'object' && 'source' in val ? val.source : val;
|
|
96
|
+
val = val.replace(caret, '$1');
|
|
97
|
+
regex = regex.replace(name, val);
|
|
98
|
+
return obj;
|
|
99
|
+
},
|
|
100
|
+
getRegex: () => {
|
|
101
|
+
return new RegExp(regex, opt);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
return obj;
|
|
152
105
|
}
|
|
153
|
-
|
|
154
|
-
|
|
106
|
+
const nonWordAndColonTest = /[^\w:]/g;
|
|
107
|
+
const originIndependentUrl = /^$|^[a-z][a-z0-9+.-]*:|^[?#]/i;
|
|
155
108
|
function cleanUrl(sanitize, base, href) {
|
|
156
|
-
|
|
157
|
-
|
|
109
|
+
if (sanitize) {
|
|
110
|
+
let prot;
|
|
111
|
+
try {
|
|
112
|
+
prot = decodeURIComponent(unescape(href))
|
|
113
|
+
.replace(nonWordAndColonTest, '')
|
|
114
|
+
.toLowerCase();
|
|
115
|
+
}
|
|
116
|
+
catch (e) {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
if (prot.indexOf('javascript:') === 0 || prot.indexOf('vbscript:') === 0 || prot.indexOf('data:') === 0) {
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (base && !originIndependentUrl.test(href)) {
|
|
124
|
+
href = resolveUrl(base, href);
|
|
125
|
+
}
|
|
158
126
|
try {
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
if (base && !originIndependentUrl.test(href)) {
|
|
168
|
-
href = resolveUrl(base, href);
|
|
169
|
-
}
|
|
170
|
-
try {
|
|
171
|
-
href = encodeURI(href).replace(/%25/g, "%");
|
|
172
|
-
} catch (e) {
|
|
173
|
-
return null;
|
|
174
|
-
}
|
|
175
|
-
return href;
|
|
127
|
+
href = encodeURI(href).replace(/%25/g, '%');
|
|
128
|
+
}
|
|
129
|
+
catch (e) {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
return href;
|
|
176
133
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
134
|
+
const baseUrls = {};
|
|
135
|
+
const justDomain = /^[^:]+:\/*[^/]*$/;
|
|
136
|
+
const protocol = /^([^:]+:)[\s\S]*$/;
|
|
137
|
+
const domain = /^([^:]+:\/*[^/]*)[\s\S]*$/;
|
|
181
138
|
function resolveUrl(base, href) {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
139
|
+
if (!baseUrls[' ' + base]) {
|
|
140
|
+
// we can ignore everything in base after the last slash of its path component,
|
|
141
|
+
// but we might need to add _that_
|
|
142
|
+
// https://tools.ietf.org/html/rfc3986#section-3
|
|
143
|
+
if (justDomain.test(base)) {
|
|
144
|
+
baseUrls[' ' + base] = base + '/';
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
baseUrls[' ' + base] = rtrim(base, '/', true);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
base = baseUrls[' ' + base];
|
|
151
|
+
const relativeBase = base.indexOf(':') === -1;
|
|
152
|
+
if (href.substring(0, 2) === '//') {
|
|
153
|
+
if (relativeBase) {
|
|
154
|
+
return href;
|
|
155
|
+
}
|
|
156
|
+
return base.replace(protocol, '$1') + href;
|
|
157
|
+
}
|
|
158
|
+
else if (href.charAt(0) === '/') {
|
|
159
|
+
if (relativeBase) {
|
|
160
|
+
return href;
|
|
161
|
+
}
|
|
162
|
+
return base.replace(domain, '$1') + href;
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
return base + href;
|
|
166
|
+
}
|
|
204
167
|
}
|
|
205
|
-
|
|
168
|
+
const noopTest = { exec: () => null };
|
|
206
169
|
function splitCells(tableRow, count) {
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
cells.
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
cells.
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
170
|
+
// ensure that every cell-delimiting pipe has a space
|
|
171
|
+
// before it to distinguish it from an escaped pipe
|
|
172
|
+
const row = tableRow.replace(/\|/g, (match, offset, str) => {
|
|
173
|
+
let escaped = false, curr = offset;
|
|
174
|
+
while (--curr >= 0 && str[curr] === '\\')
|
|
175
|
+
escaped = !escaped;
|
|
176
|
+
if (escaped) {
|
|
177
|
+
// odd number of slashes means | is escaped
|
|
178
|
+
// so we leave it alone
|
|
179
|
+
return '|';
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
// add space before unescaped |
|
|
183
|
+
return ' |';
|
|
184
|
+
}
|
|
185
|
+
}), cells = row.split(/ \|/);
|
|
186
|
+
let i = 0;
|
|
187
|
+
// First/last cell in a row cannot be empty if it has no leading/trailing pipe
|
|
188
|
+
if (!cells[0].trim()) {
|
|
189
|
+
cells.shift();
|
|
190
|
+
}
|
|
191
|
+
if (cells.length > 0 && !cells[cells.length - 1].trim()) {
|
|
192
|
+
cells.pop();
|
|
193
|
+
}
|
|
194
|
+
if (count) {
|
|
195
|
+
if (cells.length > count) {
|
|
196
|
+
cells.splice(count);
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
while (cells.length < count)
|
|
200
|
+
cells.push('');
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
for (; i < cells.length; i++) {
|
|
204
|
+
// leading or trailing whitespace is ignored per the gfm spec
|
|
205
|
+
cells[i] = cells[i].trim().replace(/\\\|/g, '|');
|
|
206
|
+
}
|
|
207
|
+
return cells;
|
|
234
208
|
}
|
|
209
|
+
/**
|
|
210
|
+
* Remove trailing 'c's. Equivalent to str.replace(/c*$/, '').
|
|
211
|
+
* /c*$/ is vulnerable to REDOS.
|
|
212
|
+
*
|
|
213
|
+
* @param str
|
|
214
|
+
* @param c
|
|
215
|
+
* @param invert Remove suffix of non-c chars instead. Default falsey.
|
|
216
|
+
*/
|
|
235
217
|
function rtrim(str, c, invert) {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
218
|
+
const l = str.length;
|
|
219
|
+
if (l === 0) {
|
|
220
|
+
return '';
|
|
221
|
+
}
|
|
222
|
+
// Length of suffix matching the invert condition.
|
|
223
|
+
let suffLen = 0;
|
|
224
|
+
// Step left until we fail to match the invert condition.
|
|
225
|
+
while (suffLen < l) {
|
|
226
|
+
const currChar = str.charAt(l - suffLen - 1);
|
|
227
|
+
if (currChar === c && !invert) {
|
|
228
|
+
suffLen++;
|
|
229
|
+
}
|
|
230
|
+
else if (currChar !== c && invert) {
|
|
231
|
+
suffLen++;
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return str.slice(0, l - suffLen);
|
|
252
238
|
}
|
|
253
239
|
function findClosingBracket(str, b) {
|
|
254
|
-
|
|
240
|
+
if (str.indexOf(b[1]) === -1) {
|
|
241
|
+
return -1;
|
|
242
|
+
}
|
|
243
|
+
const l = str.length;
|
|
244
|
+
let level = 0, i = 0;
|
|
245
|
+
for (; i < l; i++) {
|
|
246
|
+
if (str[i] === '\\') {
|
|
247
|
+
i++;
|
|
248
|
+
}
|
|
249
|
+
else if (str[i] === b[0]) {
|
|
250
|
+
level++;
|
|
251
|
+
}
|
|
252
|
+
else if (str[i] === b[1]) {
|
|
253
|
+
level--;
|
|
254
|
+
if (level < 0) {
|
|
255
|
+
return i;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
255
259
|
return -1;
|
|
256
|
-
}
|
|
257
|
-
const l = str.length;
|
|
258
|
-
let level = 0, i = 0;
|
|
259
|
-
for (; i < l; i++) {
|
|
260
|
-
if (str[i] === "\\") {
|
|
261
|
-
i++;
|
|
262
|
-
} else if (str[i] === b[0]) {
|
|
263
|
-
level++;
|
|
264
|
-
} else if (str[i] === b[1]) {
|
|
265
|
-
level--;
|
|
266
|
-
if (level < 0) {
|
|
267
|
-
return i;
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
return -1;
|
|
272
260
|
}
|
|
273
261
|
function checkDeprecations(opt, callback) {
|
|
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
|
-
|
|
262
|
+
if (!opt || opt.silent) {
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
if (callback) {
|
|
266
|
+
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');
|
|
267
|
+
}
|
|
268
|
+
if (opt.sanitize || opt.sanitizer) {
|
|
269
|
+
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');
|
|
270
|
+
}
|
|
271
|
+
if (opt.highlight || opt.langPrefix !== 'language-') {
|
|
272
|
+
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.');
|
|
273
|
+
}
|
|
274
|
+
if (opt.mangle) {
|
|
275
|
+
console.warn('marked(): mangle parameter is enabled by default, but is deprecated since version 5.0.0, and will be removed in the future. To clear this warning, install https://www.npmjs.com/package/marked-mangle, or disable by setting `{mangle: false}`.');
|
|
276
|
+
}
|
|
277
|
+
if (opt.baseUrl) {
|
|
278
|
+
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.');
|
|
279
|
+
}
|
|
280
|
+
if (opt.smartypants) {
|
|
281
|
+
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.');
|
|
282
|
+
}
|
|
283
|
+
if (opt.xhtml) {
|
|
284
|
+
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.');
|
|
285
|
+
}
|
|
286
|
+
if (opt.headerIds || opt.headerPrefix) {
|
|
287
|
+
console.warn('marked(): headerIds and headerPrefix parameters enabled by default, but are deprecated since version 5.0.0, and will be removed in the future. To clear this warning, install https://www.npmjs.com/package/marked-gfm-heading-id, or disable by setting `{headerIds: false}`.');
|
|
288
|
+
}
|
|
301
289
|
}
|
|
302
290
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
291
|
+
function outputLink(cap, link, raw, lexer) {
|
|
292
|
+
const href = link.href;
|
|
293
|
+
const title = link.title ? escape(link.title) : null;
|
|
294
|
+
const text = cap[1].replace(/\\([\[\]])/g, '$1');
|
|
295
|
+
if (cap[0].charAt(0) !== '!') {
|
|
296
|
+
lexer.state.inLink = true;
|
|
297
|
+
const token = {
|
|
298
|
+
type: 'link',
|
|
299
|
+
raw,
|
|
300
|
+
href,
|
|
301
|
+
title,
|
|
302
|
+
text,
|
|
303
|
+
tokens: lexer.inlineTokens(text)
|
|
304
|
+
};
|
|
305
|
+
lexer.state.inLink = false;
|
|
306
|
+
return token;
|
|
307
|
+
}
|
|
308
|
+
return {
|
|
309
|
+
type: 'image',
|
|
310
|
+
raw,
|
|
311
|
+
href,
|
|
312
|
+
title,
|
|
313
|
+
text: escape(text)
|
|
317
314
|
};
|
|
318
|
-
lexer2.state.inLink = false;
|
|
319
|
-
return token;
|
|
320
|
-
}
|
|
321
|
-
return {
|
|
322
|
-
type: "image",
|
|
323
|
-
raw,
|
|
324
|
-
href,
|
|
325
|
-
title,
|
|
326
|
-
text: escape(text)
|
|
327
|
-
};
|
|
328
315
|
}
|
|
329
316
|
function indentCodeCompensation(raw, text) {
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
317
|
+
const matchIndentToCode = raw.match(/^(\s+)(?:```)/);
|
|
318
|
+
if (matchIndentToCode === null) {
|
|
319
|
+
return text;
|
|
320
|
+
}
|
|
321
|
+
const indentToCode = matchIndentToCode[1];
|
|
322
|
+
return text
|
|
323
|
+
.split('\n')
|
|
324
|
+
.map(node => {
|
|
325
|
+
const matchIndentInNode = node.match(/^\s+/);
|
|
326
|
+
if (matchIndentInNode === null) {
|
|
327
|
+
return node;
|
|
328
|
+
}
|
|
329
|
+
const [indentInNode] = matchIndentInNode;
|
|
330
|
+
if (indentInNode.length >= indentToCode.length) {
|
|
331
|
+
return node.slice(indentToCode.length);
|
|
332
|
+
}
|
|
333
|
+
return node;
|
|
334
|
+
})
|
|
335
|
+
.join('\n');
|
|
346
336
|
}
|
|
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
|
-
raw = cap[0];
|
|
459
|
-
src = src.substring(raw.length);
|
|
460
|
-
line = cap[2].split("\n", 1)[0].replace(/^\t+/, (t) => " ".repeat(3 * t.length));
|
|
461
|
-
nextLine = src.split("\n", 1)[0];
|
|
462
|
-
if (this.options.pedantic) {
|
|
463
|
-
indent = 2;
|
|
464
|
-
itemContents = line.trimLeft();
|
|
465
|
-
} else {
|
|
466
|
-
indent = cap[2].search(/[^ ]/);
|
|
467
|
-
indent = indent > 4 ? 1 : indent;
|
|
468
|
-
itemContents = line.slice(indent);
|
|
469
|
-
indent += cap[1].length;
|
|
470
|
-
}
|
|
471
|
-
blankLine = false;
|
|
472
|
-
if (!line && /^ *$/.test(nextLine)) {
|
|
473
|
-
raw += nextLine + "\n";
|
|
474
|
-
src = src.substring(nextLine.length + 1);
|
|
475
|
-
endEarly = true;
|
|
476
|
-
}
|
|
477
|
-
if (!endEarly) {
|
|
478
|
-
const nextBulletRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:[*+-]|\\d{1,9}[.)])((?:[ ][^\\n]*)?(?:\\n|$))`);
|
|
479
|
-
const hrRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}((?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$)`);
|
|
480
|
-
const fencesBeginRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:\`\`\`|~~~)`);
|
|
481
|
-
const headingBeginRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}#`);
|
|
482
|
-
while (src) {
|
|
483
|
-
rawLine = src.split("\n", 1)[0];
|
|
484
|
-
nextLine = rawLine;
|
|
337
|
+
/**
|
|
338
|
+
* Tokenizer
|
|
339
|
+
*/
|
|
340
|
+
class _Tokenizer {
|
|
341
|
+
options;
|
|
342
|
+
rules;
|
|
343
|
+
lexer;
|
|
344
|
+
constructor(options) {
|
|
345
|
+
this.options = options || exports.defaults;
|
|
346
|
+
}
|
|
347
|
+
space(src) {
|
|
348
|
+
const cap = this.rules.block.newline.exec(src);
|
|
349
|
+
if (cap && cap[0].length > 0) {
|
|
350
|
+
return {
|
|
351
|
+
type: 'space',
|
|
352
|
+
raw: cap[0]
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
code(src) {
|
|
357
|
+
const cap = this.rules.block.code.exec(src);
|
|
358
|
+
if (cap) {
|
|
359
|
+
const text = cap[0].replace(/^ {1,4}/gm, '');
|
|
360
|
+
return {
|
|
361
|
+
type: 'code',
|
|
362
|
+
raw: cap[0],
|
|
363
|
+
codeBlockStyle: 'indented',
|
|
364
|
+
text: !this.options.pedantic
|
|
365
|
+
? rtrim(text, '\n')
|
|
366
|
+
: text
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
fences(src) {
|
|
371
|
+
const cap = this.rules.block.fences.exec(src);
|
|
372
|
+
if (cap) {
|
|
373
|
+
const raw = cap[0];
|
|
374
|
+
const text = indentCodeCompensation(raw, cap[3] || '');
|
|
375
|
+
return {
|
|
376
|
+
type: 'code',
|
|
377
|
+
raw,
|
|
378
|
+
lang: cap[2] ? cap[2].trim().replace(this.rules.inline._escapes, '$1') : cap[2],
|
|
379
|
+
text
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
heading(src) {
|
|
384
|
+
const cap = this.rules.block.heading.exec(src);
|
|
385
|
+
if (cap) {
|
|
386
|
+
let text = cap[2].trim();
|
|
387
|
+
// remove trailing #s
|
|
388
|
+
if (/#$/.test(text)) {
|
|
389
|
+
const trimmed = rtrim(text, '#');
|
|
390
|
+
if (this.options.pedantic) {
|
|
391
|
+
text = trimmed.trim();
|
|
392
|
+
}
|
|
393
|
+
else if (!trimmed || / $/.test(trimmed)) {
|
|
394
|
+
// CommonMark requires space before trailing #s
|
|
395
|
+
text = trimmed.trim();
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
return {
|
|
399
|
+
type: 'heading',
|
|
400
|
+
raw: cap[0],
|
|
401
|
+
depth: cap[1].length,
|
|
402
|
+
text,
|
|
403
|
+
tokens: this.lexer.inline(text)
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
hr(src) {
|
|
408
|
+
const cap = this.rules.block.hr.exec(src);
|
|
409
|
+
if (cap) {
|
|
410
|
+
return {
|
|
411
|
+
type: 'hr',
|
|
412
|
+
raw: cap[0]
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
blockquote(src) {
|
|
417
|
+
const cap = this.rules.block.blockquote.exec(src);
|
|
418
|
+
if (cap) {
|
|
419
|
+
const text = cap[0].replace(/^ *>[ \t]?/gm, '');
|
|
420
|
+
const top = this.lexer.state.top;
|
|
421
|
+
this.lexer.state.top = true;
|
|
422
|
+
const tokens = this.lexer.blockTokens(text);
|
|
423
|
+
this.lexer.state.top = top;
|
|
424
|
+
return {
|
|
425
|
+
type: 'blockquote',
|
|
426
|
+
raw: cap[0],
|
|
427
|
+
tokens,
|
|
428
|
+
text
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
list(src) {
|
|
433
|
+
let cap = this.rules.block.list.exec(src);
|
|
434
|
+
if (cap) {
|
|
435
|
+
let raw, istask, ischecked, indent, i, blankLine, endsWithBlankLine, line, nextLine, rawLine, itemContents, endEarly;
|
|
436
|
+
let bull = cap[1].trim();
|
|
437
|
+
const isordered = bull.length > 1;
|
|
438
|
+
const list = {
|
|
439
|
+
type: 'list',
|
|
440
|
+
raw: '',
|
|
441
|
+
ordered: isordered,
|
|
442
|
+
start: isordered ? +bull.slice(0, -1) : '',
|
|
443
|
+
loose: false,
|
|
444
|
+
items: []
|
|
445
|
+
};
|
|
446
|
+
bull = isordered ? `\\d{1,9}\\${bull.slice(-1)}` : `\\${bull}`;
|
|
485
447
|
if (this.options.pedantic) {
|
|
486
|
-
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
tokens: this.lexer.inline(cap[1])
|
|
666
|
-
};
|
|
667
|
-
}
|
|
668
|
-
}
|
|
669
|
-
paragraph(src) {
|
|
670
|
-
const cap = this.rules.block.paragraph.exec(src);
|
|
671
|
-
if (cap) {
|
|
672
|
-
const text = cap[1].charAt(cap[1].length - 1) === "\n" ? cap[1].slice(0, -1) : cap[1];
|
|
673
|
-
return {
|
|
674
|
-
type: "paragraph",
|
|
675
|
-
raw: cap[0],
|
|
676
|
-
text,
|
|
677
|
-
tokens: this.lexer.inline(text)
|
|
678
|
-
};
|
|
679
|
-
}
|
|
680
|
-
}
|
|
681
|
-
text(src) {
|
|
682
|
-
const cap = this.rules.block.text.exec(src);
|
|
683
|
-
if (cap) {
|
|
684
|
-
return {
|
|
685
|
-
type: "text",
|
|
686
|
-
raw: cap[0],
|
|
687
|
-
text: cap[0],
|
|
688
|
-
tokens: this.lexer.inline(cap[0])
|
|
689
|
-
};
|
|
690
|
-
}
|
|
691
|
-
}
|
|
692
|
-
escape(src) {
|
|
693
|
-
const cap = this.rules.inline.escape.exec(src);
|
|
694
|
-
if (cap) {
|
|
695
|
-
return {
|
|
696
|
-
type: "escape",
|
|
697
|
-
raw: cap[0],
|
|
698
|
-
text: escape(cap[1])
|
|
699
|
-
};
|
|
700
|
-
}
|
|
701
|
-
}
|
|
702
|
-
tag(src) {
|
|
703
|
-
const cap = this.rules.inline.tag.exec(src);
|
|
704
|
-
if (cap) {
|
|
705
|
-
if (!this.lexer.state.inLink && /^<a /i.test(cap[0])) {
|
|
706
|
-
this.lexer.state.inLink = true;
|
|
707
|
-
} else if (this.lexer.state.inLink && /^<\/a>/i.test(cap[0])) {
|
|
708
|
-
this.lexer.state.inLink = false;
|
|
709
|
-
}
|
|
710
|
-
if (!this.lexer.state.inRawBlock && /^<(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
|
|
711
|
-
this.lexer.state.inRawBlock = true;
|
|
712
|
-
} else if (this.lexer.state.inRawBlock && /^<\/(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
|
|
713
|
-
this.lexer.state.inRawBlock = false;
|
|
714
|
-
}
|
|
715
|
-
return {
|
|
716
|
-
type: this.options.sanitize ? "text" : "html",
|
|
717
|
-
raw: cap[0],
|
|
718
|
-
inLink: this.lexer.state.inLink,
|
|
719
|
-
inRawBlock: this.lexer.state.inRawBlock,
|
|
720
|
-
block: false,
|
|
721
|
-
text: this.options.sanitize ? this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0]) : cap[0]
|
|
722
|
-
};
|
|
723
|
-
}
|
|
724
|
-
}
|
|
725
|
-
link(src) {
|
|
726
|
-
const cap = this.rules.inline.link.exec(src);
|
|
727
|
-
if (cap) {
|
|
728
|
-
const trimmedUrl = cap[2].trim();
|
|
729
|
-
if (!this.options.pedantic && /^</.test(trimmedUrl)) {
|
|
730
|
-
if (!/>$/.test(trimmedUrl)) {
|
|
731
|
-
return;
|
|
732
|
-
}
|
|
733
|
-
const rtrimSlash = rtrim(trimmedUrl.slice(0, -1), "\\");
|
|
734
|
-
if ((trimmedUrl.length - rtrimSlash.length) % 2 === 0) {
|
|
735
|
-
return;
|
|
736
|
-
}
|
|
737
|
-
} else {
|
|
738
|
-
const lastParenIndex = findClosingBracket(cap[2], "()");
|
|
739
|
-
if (lastParenIndex > -1) {
|
|
740
|
-
const start = cap[0].indexOf("!") === 0 ? 5 : 4;
|
|
741
|
-
const linkLen = start + cap[1].length + lastParenIndex;
|
|
742
|
-
cap[2] = cap[2].substring(0, lastParenIndex);
|
|
743
|
-
cap[0] = cap[0].substring(0, linkLen).trim();
|
|
744
|
-
cap[3] = "";
|
|
745
|
-
}
|
|
746
|
-
}
|
|
747
|
-
let href = cap[2];
|
|
748
|
-
let title = "";
|
|
749
|
-
if (this.options.pedantic) {
|
|
750
|
-
const link = /^([^'"]*[^\s])\s+(['"])(.*)\2/.exec(href);
|
|
751
|
-
if (link) {
|
|
752
|
-
href = link[1];
|
|
753
|
-
title = link[3];
|
|
754
|
-
}
|
|
755
|
-
} else {
|
|
756
|
-
title = cap[3] ? cap[3].slice(1, -1) : "";
|
|
757
|
-
}
|
|
758
|
-
href = href.trim();
|
|
759
|
-
if (/^</.test(href)) {
|
|
760
|
-
if (this.options.pedantic && !/>$/.test(trimmedUrl)) {
|
|
761
|
-
href = href.slice(1);
|
|
762
|
-
} else {
|
|
763
|
-
href = href.slice(1, -1);
|
|
764
|
-
}
|
|
765
|
-
}
|
|
766
|
-
return outputLink(cap, {
|
|
767
|
-
href: href ? href.replace(this.rules.inline._escapes, "$1") : href,
|
|
768
|
-
title: title ? title.replace(this.rules.inline._escapes, "$1") : title
|
|
769
|
-
}, cap[0], this.lexer);
|
|
770
|
-
}
|
|
771
|
-
}
|
|
772
|
-
reflink(src, links) {
|
|
773
|
-
let cap;
|
|
774
|
-
if ((cap = this.rules.inline.reflink.exec(src)) || (cap = this.rules.inline.nolink.exec(src))) {
|
|
775
|
-
let link = (cap[2] || cap[1]).replace(/\s+/g, " ");
|
|
776
|
-
link = links[link.toLowerCase()];
|
|
777
|
-
if (!link) {
|
|
778
|
-
const text = cap[0].charAt(0);
|
|
779
|
-
return {
|
|
780
|
-
type: "text",
|
|
781
|
-
raw: text,
|
|
782
|
-
text
|
|
783
|
-
};
|
|
784
|
-
}
|
|
785
|
-
return outputLink(cap, link, cap[0], this.lexer);
|
|
786
|
-
}
|
|
787
|
-
}
|
|
788
|
-
emStrong(src, maskedSrc, prevChar = "") {
|
|
789
|
-
let match = this.rules.inline.emStrong.lDelim.exec(src);
|
|
790
|
-
if (!match)
|
|
791
|
-
return;
|
|
792
|
-
if (match[3] && prevChar.match(/[\p{L}\p{N}]/u))
|
|
793
|
-
return;
|
|
794
|
-
const nextChar = match[1] || match[2] || "";
|
|
795
|
-
if (!nextChar || !prevChar || this.rules.inline.punctuation.exec(prevChar)) {
|
|
796
|
-
const lLength = match[0].length - 1;
|
|
797
|
-
let rDelim, rLength, delimTotal = lLength, midDelimTotal = 0;
|
|
798
|
-
const endReg = match[0][0] === "*" ? this.rules.inline.emStrong.rDelimAst : this.rules.inline.emStrong.rDelimUnd;
|
|
799
|
-
endReg.lastIndex = 0;
|
|
800
|
-
maskedSrc = maskedSrc.slice(-1 * src.length + lLength);
|
|
801
|
-
while ((match = endReg.exec(maskedSrc)) != null) {
|
|
802
|
-
rDelim = match[1] || match[2] || match[3] || match[4] || match[5] || match[6];
|
|
803
|
-
if (!rDelim)
|
|
804
|
-
continue;
|
|
805
|
-
rLength = rDelim.length;
|
|
806
|
-
if (match[3] || match[4]) {
|
|
807
|
-
delimTotal += rLength;
|
|
808
|
-
continue;
|
|
809
|
-
} else if (match[5] || match[6]) {
|
|
810
|
-
if (lLength % 3 && !((lLength + rLength) % 3)) {
|
|
811
|
-
midDelimTotal += rLength;
|
|
812
|
-
continue;
|
|
813
|
-
}
|
|
814
|
-
}
|
|
815
|
-
delimTotal -= rLength;
|
|
816
|
-
if (delimTotal > 0)
|
|
817
|
-
continue;
|
|
818
|
-
rLength = Math.min(rLength, rLength + delimTotal + midDelimTotal);
|
|
819
|
-
const raw = src.slice(0, lLength + match.index + rLength + 1);
|
|
820
|
-
if (Math.min(lLength, rLength) % 2) {
|
|
821
|
-
const text2 = raw.slice(1, -1);
|
|
822
|
-
return {
|
|
823
|
-
type: "em",
|
|
824
|
-
raw,
|
|
825
|
-
text: text2,
|
|
826
|
-
tokens: this.lexer.inlineTokens(text2)
|
|
827
|
-
};
|
|
448
|
+
bull = isordered ? bull : '[*+-]';
|
|
449
|
+
}
|
|
450
|
+
// Get next list item
|
|
451
|
+
const itemRegex = new RegExp(`^( {0,3}${bull})((?:[\t ][^\\n]*)?(?:\\n|$))`);
|
|
452
|
+
// Check if current bullet point can start a new List Item
|
|
453
|
+
while (src) {
|
|
454
|
+
endEarly = false;
|
|
455
|
+
if (!(cap = itemRegex.exec(src))) {
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
458
|
+
if (this.rules.block.hr.test(src)) { // End list if bullet was actually HR (possibly move into itemRegex?)
|
|
459
|
+
break;
|
|
460
|
+
}
|
|
461
|
+
raw = cap[0];
|
|
462
|
+
src = src.substring(raw.length);
|
|
463
|
+
line = cap[2].split('\n', 1)[0].replace(/^\t+/, (t) => ' '.repeat(3 * t.length));
|
|
464
|
+
nextLine = src.split('\n', 1)[0];
|
|
465
|
+
if (this.options.pedantic) {
|
|
466
|
+
indent = 2;
|
|
467
|
+
itemContents = line.trimLeft();
|
|
468
|
+
}
|
|
469
|
+
else {
|
|
470
|
+
indent = cap[2].search(/[^ ]/); // Find first non-space char
|
|
471
|
+
indent = indent > 4 ? 1 : indent; // Treat indented code blocks (> 4 spaces) as having only 1 indent
|
|
472
|
+
itemContents = line.slice(indent);
|
|
473
|
+
indent += cap[1].length;
|
|
474
|
+
}
|
|
475
|
+
blankLine = false;
|
|
476
|
+
if (!line && /^ *$/.test(nextLine)) { // Items begin with at most one blank line
|
|
477
|
+
raw += nextLine + '\n';
|
|
478
|
+
src = src.substring(nextLine.length + 1);
|
|
479
|
+
endEarly = true;
|
|
480
|
+
}
|
|
481
|
+
if (!endEarly) {
|
|
482
|
+
const nextBulletRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:[*+-]|\\d{1,9}[.)])((?:[ \t][^\\n]*)?(?:\\n|$))`);
|
|
483
|
+
const hrRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}((?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$)`);
|
|
484
|
+
const fencesBeginRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:\`\`\`|~~~)`);
|
|
485
|
+
const headingBeginRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}#`);
|
|
486
|
+
// Check if following lines should be included in List Item
|
|
487
|
+
while (src) {
|
|
488
|
+
rawLine = src.split('\n', 1)[0];
|
|
489
|
+
nextLine = rawLine;
|
|
490
|
+
// Re-align to follow commonmark nesting rules
|
|
491
|
+
if (this.options.pedantic) {
|
|
492
|
+
nextLine = nextLine.replace(/^ {1,4}(?=( {4})*[^ ])/g, ' ');
|
|
493
|
+
}
|
|
494
|
+
// End list item if found code fences
|
|
495
|
+
if (fencesBeginRegex.test(nextLine)) {
|
|
496
|
+
break;
|
|
497
|
+
}
|
|
498
|
+
// End list item if found start of new heading
|
|
499
|
+
if (headingBeginRegex.test(nextLine)) {
|
|
500
|
+
break;
|
|
501
|
+
}
|
|
502
|
+
// End list item if found start of new bullet
|
|
503
|
+
if (nextBulletRegex.test(nextLine)) {
|
|
504
|
+
break;
|
|
505
|
+
}
|
|
506
|
+
// Horizontal rule found
|
|
507
|
+
if (hrRegex.test(src)) {
|
|
508
|
+
break;
|
|
509
|
+
}
|
|
510
|
+
if (nextLine.search(/[^ ]/) >= indent || !nextLine.trim()) { // Dedent if possible
|
|
511
|
+
itemContents += '\n' + nextLine.slice(indent);
|
|
512
|
+
}
|
|
513
|
+
else {
|
|
514
|
+
// not enough indentation
|
|
515
|
+
if (blankLine) {
|
|
516
|
+
break;
|
|
517
|
+
}
|
|
518
|
+
// paragraph continuation unless last line was a different block level element
|
|
519
|
+
if (line.search(/[^ ]/) >= 4) { // indented code block
|
|
520
|
+
break;
|
|
521
|
+
}
|
|
522
|
+
if (fencesBeginRegex.test(line)) {
|
|
523
|
+
break;
|
|
524
|
+
}
|
|
525
|
+
if (headingBeginRegex.test(line)) {
|
|
526
|
+
break;
|
|
527
|
+
}
|
|
528
|
+
if (hrRegex.test(line)) {
|
|
529
|
+
break;
|
|
530
|
+
}
|
|
531
|
+
itemContents += '\n' + nextLine;
|
|
532
|
+
}
|
|
533
|
+
if (!blankLine && !nextLine.trim()) { // Check if current line is blank
|
|
534
|
+
blankLine = true;
|
|
535
|
+
}
|
|
536
|
+
raw += rawLine + '\n';
|
|
537
|
+
src = src.substring(rawLine.length + 1);
|
|
538
|
+
line = nextLine.slice(indent);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
if (!list.loose) {
|
|
542
|
+
// If the previous item ended with a blank line, the list is loose
|
|
543
|
+
if (endsWithBlankLine) {
|
|
544
|
+
list.loose = true;
|
|
545
|
+
}
|
|
546
|
+
else if (/\n *\n *$/.test(raw)) {
|
|
547
|
+
endsWithBlankLine = true;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
// Check for task list items
|
|
551
|
+
if (this.options.gfm) {
|
|
552
|
+
istask = /^\[[ xX]\] /.exec(itemContents);
|
|
553
|
+
if (istask) {
|
|
554
|
+
ischecked = istask[0] !== '[ ] ';
|
|
555
|
+
itemContents = itemContents.replace(/^\[[ xX]\] +/, '');
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
list.items.push({
|
|
559
|
+
type: 'list_item',
|
|
560
|
+
raw,
|
|
561
|
+
task: !!istask,
|
|
562
|
+
checked: ischecked,
|
|
563
|
+
loose: false,
|
|
564
|
+
text: itemContents
|
|
565
|
+
});
|
|
566
|
+
list.raw += raw;
|
|
567
|
+
}
|
|
568
|
+
// Do not consume newlines at end of final item. Alternatively, make itemRegex *start* with any newlines to simplify/speed up endsWithBlankLine logic
|
|
569
|
+
list.items[list.items.length - 1].raw = raw.trimRight();
|
|
570
|
+
list.items[list.items.length - 1].text = itemContents.trimRight();
|
|
571
|
+
list.raw = list.raw.trimRight();
|
|
572
|
+
const l = list.items.length;
|
|
573
|
+
// Item child tokens handled here at end because we needed to have the final item to trim it first
|
|
574
|
+
for (i = 0; i < l; i++) {
|
|
575
|
+
this.lexer.state.top = false;
|
|
576
|
+
list.items[i].tokens = this.lexer.blockTokens(list.items[i].text, []);
|
|
577
|
+
if (!list.loose) {
|
|
578
|
+
// Check if list should be loose
|
|
579
|
+
const spacers = list.items[i].tokens.filter(t => t.type === 'space');
|
|
580
|
+
const hasMultipleLineBreaks = spacers.length > 0 && spacers.some(t => /\n.*\n/.test(t.raw));
|
|
581
|
+
list.loose = hasMultipleLineBreaks;
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
// Set all items to loose if list is loose
|
|
585
|
+
if (list.loose) {
|
|
586
|
+
for (i = 0; i < l; i++) {
|
|
587
|
+
list.items[i].loose = true;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
return list;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
html(src) {
|
|
594
|
+
const cap = this.rules.block.html.exec(src);
|
|
595
|
+
if (cap) {
|
|
596
|
+
const token = {
|
|
597
|
+
type: 'html',
|
|
598
|
+
block: true,
|
|
599
|
+
raw: cap[0],
|
|
600
|
+
pre: !this.options.sanitizer
|
|
601
|
+
&& (cap[1] === 'pre' || cap[1] === 'script' || cap[1] === 'style'),
|
|
602
|
+
text: cap[0]
|
|
603
|
+
};
|
|
604
|
+
if (this.options.sanitize) {
|
|
605
|
+
const text = this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0]);
|
|
606
|
+
const paragraph = token;
|
|
607
|
+
paragraph.type = 'paragraph';
|
|
608
|
+
paragraph.text = text;
|
|
609
|
+
paragraph.tokens = this.lexer.inline(text);
|
|
610
|
+
}
|
|
611
|
+
return token;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
def(src) {
|
|
615
|
+
const cap = this.rules.block.def.exec(src);
|
|
616
|
+
if (cap) {
|
|
617
|
+
const tag = cap[1].toLowerCase().replace(/\s+/g, ' ');
|
|
618
|
+
const href = cap[2] ? cap[2].replace(/^<(.*)>$/, '$1').replace(this.rules.inline._escapes, '$1') : '';
|
|
619
|
+
const title = cap[3] ? cap[3].substring(1, cap[3].length - 1).replace(this.rules.inline._escapes, '$1') : cap[3];
|
|
620
|
+
return {
|
|
621
|
+
type: 'def',
|
|
622
|
+
tag,
|
|
623
|
+
raw: cap[0],
|
|
624
|
+
href,
|
|
625
|
+
title
|
|
626
|
+
};
|
|
828
627
|
}
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
}
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
628
|
+
}
|
|
629
|
+
table(src) {
|
|
630
|
+
const cap = this.rules.block.table.exec(src);
|
|
631
|
+
if (cap) {
|
|
632
|
+
const item = {
|
|
633
|
+
type: 'table',
|
|
634
|
+
raw: cap[0],
|
|
635
|
+
header: splitCells(cap[1]).map(c => {
|
|
636
|
+
return { text: c };
|
|
637
|
+
}),
|
|
638
|
+
align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
|
|
639
|
+
rows: cap[3] && cap[3].trim() ? cap[3].replace(/\n[ \t]*$/, '').split('\n') : []
|
|
640
|
+
};
|
|
641
|
+
if (item.header.length === item.align.length) {
|
|
642
|
+
let l = item.align.length;
|
|
643
|
+
let i, j, k, row;
|
|
644
|
+
for (i = 0; i < l; i++) {
|
|
645
|
+
if (/^ *-+: *$/.test(item.align[i])) {
|
|
646
|
+
item.align[i] = 'right';
|
|
647
|
+
}
|
|
648
|
+
else if (/^ *:-+: *$/.test(item.align[i])) {
|
|
649
|
+
item.align[i] = 'center';
|
|
650
|
+
}
|
|
651
|
+
else if (/^ *:-+ *$/.test(item.align[i])) {
|
|
652
|
+
item.align[i] = 'left';
|
|
653
|
+
}
|
|
654
|
+
else {
|
|
655
|
+
item.align[i] = null;
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
l = item.rows.length;
|
|
659
|
+
for (i = 0; i < l; i++) {
|
|
660
|
+
item.rows[i] = splitCells(item.rows[i], item.header.length).map(c => {
|
|
661
|
+
return { text: c };
|
|
662
|
+
});
|
|
663
|
+
}
|
|
664
|
+
// parse child tokens inside headers and cells
|
|
665
|
+
// header child tokens
|
|
666
|
+
l = item.header.length;
|
|
667
|
+
for (j = 0; j < l; j++) {
|
|
668
|
+
item.header[j].tokens = this.lexer.inline(item.header[j].text);
|
|
669
|
+
}
|
|
670
|
+
// cell child tokens
|
|
671
|
+
l = item.rows.length;
|
|
672
|
+
for (j = 0; j < l; j++) {
|
|
673
|
+
row = item.rows[j];
|
|
674
|
+
for (k = 0; k < row.length; k++) {
|
|
675
|
+
row[k].tokens = this.lexer.inline(row[k].text);
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
return item;
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
lheading(src) {
|
|
683
|
+
const cap = this.rules.block.lheading.exec(src);
|
|
684
|
+
if (cap) {
|
|
685
|
+
return {
|
|
686
|
+
type: 'heading',
|
|
687
|
+
raw: cap[0],
|
|
688
|
+
depth: cap[2].charAt(0) === '=' ? 1 : 2,
|
|
689
|
+
text: cap[1],
|
|
690
|
+
tokens: this.lexer.inline(cap[1])
|
|
691
|
+
};
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
paragraph(src) {
|
|
695
|
+
const cap = this.rules.block.paragraph.exec(src);
|
|
696
|
+
if (cap) {
|
|
697
|
+
const text = cap[1].charAt(cap[1].length - 1) === '\n'
|
|
698
|
+
? cap[1].slice(0, -1)
|
|
699
|
+
: cap[1];
|
|
700
|
+
return {
|
|
701
|
+
type: 'paragraph',
|
|
702
|
+
raw: cap[0],
|
|
703
|
+
text,
|
|
704
|
+
tokens: this.lexer.inline(text)
|
|
705
|
+
};
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
text(src) {
|
|
709
|
+
const cap = this.rules.block.text.exec(src);
|
|
710
|
+
if (cap) {
|
|
711
|
+
return {
|
|
712
|
+
type: 'text',
|
|
713
|
+
raw: cap[0],
|
|
714
|
+
text: cap[0],
|
|
715
|
+
tokens: this.lexer.inline(cap[0])
|
|
716
|
+
};
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
escape(src) {
|
|
720
|
+
const cap = this.rules.inline.escape.exec(src);
|
|
721
|
+
if (cap) {
|
|
722
|
+
return {
|
|
723
|
+
type: 'escape',
|
|
724
|
+
raw: cap[0],
|
|
725
|
+
text: escape(cap[1])
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
tag(src) {
|
|
730
|
+
const cap = this.rules.inline.tag.exec(src);
|
|
731
|
+
if (cap) {
|
|
732
|
+
if (!this.lexer.state.inLink && /^<a /i.test(cap[0])) {
|
|
733
|
+
this.lexer.state.inLink = true;
|
|
734
|
+
}
|
|
735
|
+
else if (this.lexer.state.inLink && /^<\/a>/i.test(cap[0])) {
|
|
736
|
+
this.lexer.state.inLink = false;
|
|
737
|
+
}
|
|
738
|
+
if (!this.lexer.state.inRawBlock && /^<(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
|
|
739
|
+
this.lexer.state.inRawBlock = true;
|
|
740
|
+
}
|
|
741
|
+
else if (this.lexer.state.inRawBlock && /^<\/(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
|
|
742
|
+
this.lexer.state.inRawBlock = false;
|
|
743
|
+
}
|
|
744
|
+
return {
|
|
745
|
+
type: this.options.sanitize
|
|
746
|
+
? 'text'
|
|
747
|
+
: 'html',
|
|
748
|
+
raw: cap[0],
|
|
749
|
+
inLink: this.lexer.state.inLink,
|
|
750
|
+
inRawBlock: this.lexer.state.inRawBlock,
|
|
751
|
+
block: false,
|
|
752
|
+
text: this.options.sanitize
|
|
753
|
+
? (this.options.sanitizer
|
|
754
|
+
? this.options.sanitizer(cap[0])
|
|
755
|
+
: escape(cap[0]))
|
|
756
|
+
: cap[0]
|
|
757
|
+
};
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
link(src) {
|
|
761
|
+
const cap = this.rules.inline.link.exec(src);
|
|
762
|
+
if (cap) {
|
|
763
|
+
const trimmedUrl = cap[2].trim();
|
|
764
|
+
if (!this.options.pedantic && /^</.test(trimmedUrl)) {
|
|
765
|
+
// commonmark requires matching angle brackets
|
|
766
|
+
if (!(/>$/.test(trimmedUrl))) {
|
|
767
|
+
return;
|
|
768
|
+
}
|
|
769
|
+
// ending angle bracket cannot be escaped
|
|
770
|
+
const rtrimSlash = rtrim(trimmedUrl.slice(0, -1), '\\');
|
|
771
|
+
if ((trimmedUrl.length - rtrimSlash.length) % 2 === 0) {
|
|
772
|
+
return;
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
else {
|
|
776
|
+
// find closing parenthesis
|
|
777
|
+
const lastParenIndex = findClosingBracket(cap[2], '()');
|
|
778
|
+
if (lastParenIndex > -1) {
|
|
779
|
+
const start = cap[0].indexOf('!') === 0 ? 5 : 4;
|
|
780
|
+
const linkLen = start + cap[1].length + lastParenIndex;
|
|
781
|
+
cap[2] = cap[2].substring(0, lastParenIndex);
|
|
782
|
+
cap[0] = cap[0].substring(0, linkLen).trim();
|
|
783
|
+
cap[3] = '';
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
let href = cap[2];
|
|
787
|
+
let title = '';
|
|
788
|
+
if (this.options.pedantic) {
|
|
789
|
+
// split pedantic href and title
|
|
790
|
+
const link = /^([^'"]*[^\s])\s+(['"])(.*)\2/.exec(href);
|
|
791
|
+
if (link) {
|
|
792
|
+
href = link[1];
|
|
793
|
+
title = link[3];
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
else {
|
|
797
|
+
title = cap[3] ? cap[3].slice(1, -1) : '';
|
|
798
|
+
}
|
|
799
|
+
href = href.trim();
|
|
800
|
+
if (/^</.test(href)) {
|
|
801
|
+
if (this.options.pedantic && !(/>$/.test(trimmedUrl))) {
|
|
802
|
+
// pedantic allows starting angle bracket without ending angle bracket
|
|
803
|
+
href = href.slice(1);
|
|
804
|
+
}
|
|
805
|
+
else {
|
|
806
|
+
href = href.slice(1, -1);
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
return outputLink(cap, {
|
|
810
|
+
href: href ? href.replace(this.rules.inline._escapes, '$1') : href,
|
|
811
|
+
title: title ? title.replace(this.rules.inline._escapes, '$1') : title
|
|
812
|
+
}, cap[0], this.lexer);
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
reflink(src, links) {
|
|
816
|
+
let cap;
|
|
817
|
+
if ((cap = this.rules.inline.reflink.exec(src))
|
|
818
|
+
|| (cap = this.rules.inline.nolink.exec(src))) {
|
|
819
|
+
let link = (cap[2] || cap[1]).replace(/\s+/g, ' ');
|
|
820
|
+
link = links[link.toLowerCase()];
|
|
821
|
+
if (!link) {
|
|
822
|
+
const text = cap[0].charAt(0);
|
|
823
|
+
return {
|
|
824
|
+
type: 'text',
|
|
825
|
+
raw: text,
|
|
826
|
+
text
|
|
827
|
+
};
|
|
828
|
+
}
|
|
829
|
+
return outputLink(cap, link, cap[0], this.lexer);
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
emStrong(src, maskedSrc, prevChar = '') {
|
|
833
|
+
let match = this.rules.inline.emStrong.lDelim.exec(src);
|
|
834
|
+
if (!match)
|
|
835
|
+
return;
|
|
836
|
+
// _ can't be between two alphanumerics. \p{L}\p{N} includes non-english alphabet/numbers as well
|
|
837
|
+
if (match[3] && prevChar.match(/[\p{L}\p{N}]/u))
|
|
838
|
+
return;
|
|
839
|
+
const nextChar = match[1] || match[2] || '';
|
|
840
|
+
if (!nextChar || !prevChar || this.rules.inline.punctuation.exec(prevChar)) {
|
|
841
|
+
const lLength = match[0].length - 1;
|
|
842
|
+
let rDelim, rLength, delimTotal = lLength, midDelimTotal = 0;
|
|
843
|
+
const endReg = match[0][0] === '*' ? this.rules.inline.emStrong.rDelimAst : this.rules.inline.emStrong.rDelimUnd;
|
|
844
|
+
endReg.lastIndex = 0;
|
|
845
|
+
// Clip maskedSrc to same section of string as src (move to lexer?)
|
|
846
|
+
maskedSrc = maskedSrc.slice(-1 * src.length + lLength);
|
|
847
|
+
while ((match = endReg.exec(maskedSrc)) != null) {
|
|
848
|
+
rDelim = match[1] || match[2] || match[3] || match[4] || match[5] || match[6];
|
|
849
|
+
if (!rDelim)
|
|
850
|
+
continue; // skip single * in __abc*abc__
|
|
851
|
+
rLength = rDelim.length;
|
|
852
|
+
if (match[3] || match[4]) { // found another Left Delim
|
|
853
|
+
delimTotal += rLength;
|
|
854
|
+
continue;
|
|
855
|
+
}
|
|
856
|
+
else if (match[5] || match[6]) { // either Left or Right Delim
|
|
857
|
+
if (lLength % 3 && !((lLength + rLength) % 3)) {
|
|
858
|
+
midDelimTotal += rLength;
|
|
859
|
+
continue; // CommonMark Emphasis Rules 9-10
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
delimTotal -= rLength;
|
|
863
|
+
if (delimTotal > 0)
|
|
864
|
+
continue; // Haven't found enough closing delimiters
|
|
865
|
+
// Remove extra characters. *a*** -> *a*
|
|
866
|
+
rLength = Math.min(rLength, rLength + delimTotal + midDelimTotal);
|
|
867
|
+
const raw = src.slice(0, lLength + match.index + rLength + 1);
|
|
868
|
+
// Create `em` if smallest delimiter has odd char count. *a***
|
|
869
|
+
if (Math.min(lLength, rLength) % 2) {
|
|
870
|
+
const text = raw.slice(1, -1);
|
|
871
|
+
return {
|
|
872
|
+
type: 'em',
|
|
873
|
+
raw,
|
|
874
|
+
text,
|
|
875
|
+
tokens: this.lexer.inlineTokens(text)
|
|
876
|
+
};
|
|
877
|
+
}
|
|
878
|
+
// Create 'strong' if smallest delimiter has even char count. **a***
|
|
879
|
+
const text = raw.slice(2, -2);
|
|
880
|
+
return {
|
|
881
|
+
type: 'strong',
|
|
882
|
+
raw,
|
|
883
|
+
text,
|
|
884
|
+
tokens: this.lexer.inlineTokens(text)
|
|
885
|
+
};
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
codespan(src) {
|
|
890
|
+
const cap = this.rules.inline.code.exec(src);
|
|
891
|
+
if (cap) {
|
|
892
|
+
let text = cap[2].replace(/\n/g, ' ');
|
|
893
|
+
const hasNonSpaceChars = /[^ ]/.test(text);
|
|
894
|
+
const hasSpaceCharsOnBothEnds = /^ /.test(text) && / $/.test(text);
|
|
895
|
+
if (hasNonSpaceChars && hasSpaceCharsOnBothEnds) {
|
|
896
|
+
text = text.substring(1, text.length - 1);
|
|
897
|
+
}
|
|
898
|
+
text = escape(text, true);
|
|
899
|
+
return {
|
|
900
|
+
type: 'codespan',
|
|
901
|
+
raw: cap[0],
|
|
902
|
+
text
|
|
903
|
+
};
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
br(src) {
|
|
907
|
+
const cap = this.rules.inline.br.exec(src);
|
|
908
|
+
if (cap) {
|
|
909
|
+
return {
|
|
910
|
+
type: 'br',
|
|
911
|
+
raw: cap[0]
|
|
912
|
+
};
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
del(src) {
|
|
916
|
+
const cap = this.rules.inline.del.exec(src);
|
|
917
|
+
if (cap) {
|
|
918
|
+
return {
|
|
919
|
+
type: 'del',
|
|
920
|
+
raw: cap[0],
|
|
921
|
+
text: cap[2],
|
|
922
|
+
tokens: this.lexer.inlineTokens(cap[2])
|
|
923
|
+
};
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
autolink(src, mangle) {
|
|
927
|
+
const cap = this.rules.inline.autolink.exec(src);
|
|
928
|
+
if (cap) {
|
|
929
|
+
let text, href;
|
|
930
|
+
if (cap[2] === '@') {
|
|
931
|
+
text = escape(this.options.mangle ? mangle(cap[1]) : cap[1]);
|
|
932
|
+
href = 'mailto:' + text;
|
|
933
|
+
}
|
|
934
|
+
else {
|
|
935
|
+
text = escape(cap[1]);
|
|
936
|
+
href = text;
|
|
937
|
+
}
|
|
938
|
+
return {
|
|
939
|
+
type: 'link',
|
|
940
|
+
raw: cap[0],
|
|
941
|
+
text,
|
|
942
|
+
href,
|
|
943
|
+
tokens: [
|
|
944
|
+
{
|
|
945
|
+
type: 'text',
|
|
946
|
+
raw: text,
|
|
947
|
+
text
|
|
948
|
+
}
|
|
949
|
+
]
|
|
950
|
+
};
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
url(src, mangle) {
|
|
954
|
+
let cap;
|
|
955
|
+
if (cap = this.rules.inline.url.exec(src)) {
|
|
956
|
+
let text, href;
|
|
957
|
+
if (cap[2] === '@') {
|
|
958
|
+
text = escape(this.options.mangle ? mangle(cap[0]) : cap[0]);
|
|
959
|
+
href = 'mailto:' + text;
|
|
960
|
+
}
|
|
961
|
+
else {
|
|
962
|
+
// do extended autolink path validation
|
|
963
|
+
let prevCapZero;
|
|
964
|
+
do {
|
|
965
|
+
prevCapZero = cap[0];
|
|
966
|
+
cap[0] = this.rules.inline._backpedal.exec(cap[0])[0];
|
|
967
|
+
} while (prevCapZero !== cap[0]);
|
|
968
|
+
text = escape(cap[0]);
|
|
969
|
+
if (cap[1] === 'www.') {
|
|
970
|
+
href = 'http://' + cap[0];
|
|
971
|
+
}
|
|
972
|
+
else {
|
|
973
|
+
href = cap[0];
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
return {
|
|
977
|
+
type: 'link',
|
|
978
|
+
raw: cap[0],
|
|
979
|
+
text,
|
|
980
|
+
href,
|
|
981
|
+
tokens: [
|
|
982
|
+
{
|
|
983
|
+
type: 'text',
|
|
984
|
+
raw: text,
|
|
985
|
+
text
|
|
986
|
+
}
|
|
987
|
+
]
|
|
988
|
+
};
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
inlineText(src, smartypants) {
|
|
992
|
+
const cap = this.rules.inline.text.exec(src);
|
|
993
|
+
if (cap) {
|
|
994
|
+
let text;
|
|
995
|
+
if (this.lexer.state.inRawBlock) {
|
|
996
|
+
text = this.options.sanitize ? (this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0])) : cap[0];
|
|
997
|
+
}
|
|
998
|
+
else {
|
|
999
|
+
text = escape(this.options.smartypants ? smartypants(cap[0]) : cap[0]);
|
|
1000
|
+
}
|
|
1001
|
+
return {
|
|
1002
|
+
type: 'text',
|
|
1003
|
+
raw: cap[0],
|
|
1004
|
+
text
|
|
1005
|
+
};
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
954
1009
|
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
1010
|
+
/**
|
|
1011
|
+
* Block-Level Grammar
|
|
1012
|
+
*/
|
|
1013
|
+
// Not all rules are defined in the object literal
|
|
1014
|
+
// @ts-expect-error
|
|
1015
|
+
const block = {
|
|
1016
|
+
newline: /^(?: *(?:\n|$))+/,
|
|
1017
|
+
code: /^( {4}[^\n]+(?:\n(?: *(?:\n|$))*)?)+/,
|
|
1018
|
+
fences: /^ {0,3}(`{3,}(?=[^`\n]*(?:\n|$))|~{3,})([^\n]*)(?:\n|$)(?:|([\s\S]*?)(?:\n|$))(?: {0,3}\1[~`]* *(?=\n|$)|$)/,
|
|
1019
|
+
hr: /^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/,
|
|
1020
|
+
heading: /^ {0,3}(#{1,6})(?=\s|$)(.*)(?:\n+|$)/,
|
|
1021
|
+
blockquote: /^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/,
|
|
1022
|
+
list: /^( {0,3}bull)([ \t][^\n]+?)?(?:\n|$)/,
|
|
1023
|
+
html: '^ {0,3}(?:' // optional indentation
|
|
1024
|
+
+ '<(script|pre|style|textarea)[\\s>][\\s\\S]*?(?:</\\1>[^\\n]*\\n+|$)' // (1)
|
|
1025
|
+
+ '|comment[^\\n]*(\\n+|$)' // (2)
|
|
1026
|
+
+ '|<\\?[\\s\\S]*?(?:\\?>\\n*|$)' // (3)
|
|
1027
|
+
+ '|<![A-Z][\\s\\S]*?(?:>\\n*|$)' // (4)
|
|
1028
|
+
+ '|<!\\[CDATA\\[[\\s\\S]*?(?:\\]\\]>\\n*|$)' // (5)
|
|
1029
|
+
+ '|</?(tag)(?: +|\\n|/?>)[\\s\\S]*?(?:(?:\\n *)+\\n|$)' // (6)
|
|
1030
|
+
+ '|<(?!script|pre|style|textarea)([a-z][\\w-]*)(?:attribute)*? */?>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n *)+\\n|$)' // (7) open tag
|
|
1031
|
+
+ '|</(?!script|pre|style|textarea)[a-z][\\w-]*\\s*>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n *)+\\n|$)' // (7) closing tag
|
|
1032
|
+
+ ')',
|
|
1033
|
+
def: /^ {0,3}\[(label)\]: *(?:\n *)?([^<\s][^\s]*|<.*?>)(?:(?: +(?:\n *)?| *\n *)(title))? *(?:\n+|$)/,
|
|
1034
|
+
table: noopTest,
|
|
1035
|
+
lheading: /^((?:(?!^bull ).|\n(?!\n|bull ))+?)\n {0,3}(=+|-+) *(?:\n+|$)/,
|
|
1036
|
+
// regex template, placeholders will be replaced according to different paragraph
|
|
1037
|
+
// interruption rules of commonmark and the original markdown spec:
|
|
1038
|
+
_paragraph: /^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html|table| +\n)[^\n]+)*)/,
|
|
1039
|
+
text: /^[^\n]+/
|
|
972
1040
|
};
|
|
973
1041
|
block._label = /(?!\s*\])(?:\\.|[^\[\]\\])+/;
|
|
974
1042
|
block._title = /(?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))/;
|
|
975
|
-
block.def = edit(block.def)
|
|
1043
|
+
block.def = edit(block.def)
|
|
1044
|
+
.replace('label', block._label)
|
|
1045
|
+
.replace('title', block._title)
|
|
1046
|
+
.getRegex();
|
|
976
1047
|
block.bullet = /(?:[*+-]|\d{1,9}[.)])/;
|
|
977
|
-
block.listItemStart = edit(/^( *)(bull) */)
|
|
978
|
-
|
|
979
|
-
|
|
1048
|
+
block.listItemStart = edit(/^( *)(bull) */)
|
|
1049
|
+
.replace('bull', block.bullet)
|
|
1050
|
+
.getRegex();
|
|
1051
|
+
block.list = edit(block.list)
|
|
1052
|
+
.replace(/bull/g, block.bullet)
|
|
1053
|
+
.replace('hr', '\\n+(?=\\1?(?:(?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$))')
|
|
1054
|
+
.replace('def', '\\n+(?=' + block.def.source + ')')
|
|
1055
|
+
.getRegex();
|
|
1056
|
+
block._tag = 'address|article|aside|base|basefont|blockquote|body|caption'
|
|
1057
|
+
+ '|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption'
|
|
1058
|
+
+ '|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe'
|
|
1059
|
+
+ '|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option'
|
|
1060
|
+
+ '|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr'
|
|
1061
|
+
+ '|track|ul';
|
|
980
1062
|
block._comment = /<!--(?!-?>)[\s\S]*?(?:-->|$)/;
|
|
981
|
-
block.html = edit(block.html,
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
1063
|
+
block.html = edit(block.html, 'i')
|
|
1064
|
+
.replace('comment', block._comment)
|
|
1065
|
+
.replace('tag', block._tag)
|
|
1066
|
+
.replace('attribute', / +[a-zA-Z:_][\w.:-]*(?: *= *"[^"\n]*"| *= *'[^'\n]*'| *= *[^\s"'=<>`]+)?/)
|
|
1067
|
+
.getRegex();
|
|
1068
|
+
block.lheading = edit(block.lheading)
|
|
1069
|
+
.replace(/bull/g, block.bullet) // lists can interrupt
|
|
1070
|
+
.getRegex();
|
|
1071
|
+
block.paragraph = edit(block._paragraph)
|
|
1072
|
+
.replace('hr', block.hr)
|
|
1073
|
+
.replace('heading', ' {0,3}#{1,6} ')
|
|
1074
|
+
.replace('|lheading', '') // setex headings don't interrupt commonmark paragraphs
|
|
1075
|
+
.replace('|table', '')
|
|
1076
|
+
.replace('blockquote', ' {0,3}>')
|
|
1077
|
+
.replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
|
|
1078
|
+
.replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
|
|
1079
|
+
.replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)')
|
|
1080
|
+
.replace('tag', block._tag) // pars can be interrupted by type (6) html blocks
|
|
1081
|
+
.getRegex();
|
|
1082
|
+
block.blockquote = edit(block.blockquote)
|
|
1083
|
+
.replace('paragraph', block.paragraph)
|
|
1084
|
+
.getRegex();
|
|
1085
|
+
/**
|
|
1086
|
+
* Normal Block Grammar
|
|
1087
|
+
*/
|
|
985
1088
|
block.normal = { ...block };
|
|
1089
|
+
/**
|
|
1090
|
+
* GFM Block Grammar
|
|
1091
|
+
*/
|
|
986
1092
|
block.gfm = {
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
1093
|
+
...block.normal,
|
|
1094
|
+
table: '^ *([^\\n ].*\\|.*)\\n' // Header
|
|
1095
|
+
+ ' {0,3}(?:\\| *)?(:?-+:? *(?:\\| *:?-+:? *)*)(?:\\| *)?' // Align
|
|
1096
|
+
+ '(?:\\n((?:(?! *\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)' // Cells
|
|
990
1097
|
};
|
|
991
|
-
block.gfm.table = edit(block.gfm.table)
|
|
992
|
-
|
|
1098
|
+
block.gfm.table = edit(block.gfm.table)
|
|
1099
|
+
.replace('hr', block.hr)
|
|
1100
|
+
.replace('heading', ' {0,3}#{1,6} ')
|
|
1101
|
+
.replace('blockquote', ' {0,3}>')
|
|
1102
|
+
.replace('code', ' {4}[^\\n]')
|
|
1103
|
+
.replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
|
|
1104
|
+
.replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
|
|
1105
|
+
.replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)')
|
|
1106
|
+
.replace('tag', block._tag) // tables can be interrupted by type (6) html blocks
|
|
1107
|
+
.getRegex();
|
|
1108
|
+
block.gfm.paragraph = edit(block._paragraph)
|
|
1109
|
+
.replace('hr', block.hr)
|
|
1110
|
+
.replace('heading', ' {0,3}#{1,6} ')
|
|
1111
|
+
.replace('|lheading', '') // setex headings don't interrupt commonmark paragraphs
|
|
1112
|
+
.replace('table', block.gfm.table) // interrupt paragraphs with table
|
|
1113
|
+
.replace('blockquote', ' {0,3}>')
|
|
1114
|
+
.replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
|
|
1115
|
+
.replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
|
|
1116
|
+
.replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)')
|
|
1117
|
+
.replace('tag', block._tag) // pars can be interrupted by type (6) html blocks
|
|
1118
|
+
.getRegex();
|
|
1119
|
+
/**
|
|
1120
|
+
* Pedantic grammar (original John Gruber's loose markdown specification)
|
|
1121
|
+
*/
|
|
993
1122
|
block.pedantic = {
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1123
|
+
...block.normal,
|
|
1124
|
+
html: edit('^ *(?:comment *(?:\\n|\\s*$)'
|
|
1125
|
+
+ '|<(tag)[\\s\\S]+?</\\1> *(?:\\n{2,}|\\s*$)' // closed tag
|
|
1126
|
+
+ '|<tag(?:"[^"]*"|\'[^\']*\'|\\s[^\'"/>\\s]*)*?/?> *(?:\\n{2,}|\\s*$))')
|
|
1127
|
+
.replace('comment', block._comment)
|
|
1128
|
+
.replace(/tag/g, '(?!(?:'
|
|
1129
|
+
+ 'a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub'
|
|
1130
|
+
+ '|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)'
|
|
1131
|
+
+ '\\b)\\w+(?!:|[^\\w\\s@]*@)\\b')
|
|
1132
|
+
.getRegex(),
|
|
1133
|
+
def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +(["(][^\n]+[")]))? *(?:\n+|$)/,
|
|
1134
|
+
heading: /^(#{1,6})(.*)(?:\n+|$)/,
|
|
1135
|
+
fences: noopTest,
|
|
1136
|
+
lheading: /^(.+?)\n {0,3}(=+|-+) *(?:\n+|$)/,
|
|
1137
|
+
paragraph: edit(block.normal._paragraph)
|
|
1138
|
+
.replace('hr', block.hr)
|
|
1139
|
+
.replace('heading', ' *#{1,6} *[^\n]')
|
|
1140
|
+
.replace('lheading', block.lheading)
|
|
1141
|
+
.replace('blockquote', ' {0,3}>')
|
|
1142
|
+
.replace('|fences', '')
|
|
1143
|
+
.replace('|list', '')
|
|
1144
|
+
.replace('|html', '')
|
|
1145
|
+
.getRegex()
|
|
1004
1146
|
};
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1147
|
+
/**
|
|
1148
|
+
* Inline-Level Grammar
|
|
1149
|
+
*/
|
|
1150
|
+
// Not all rules are defined in the object literal
|
|
1151
|
+
// @ts-expect-error
|
|
1152
|
+
const inline = {
|
|
1153
|
+
escape: /^\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/,
|
|
1154
|
+
autolink: /^<(scheme:[^\s\x00-\x1f<>]*|email)>/,
|
|
1155
|
+
url: noopTest,
|
|
1156
|
+
tag: '^comment'
|
|
1157
|
+
+ '|^</[a-zA-Z][\\w:-]*\\s*>' // self-closing tag
|
|
1158
|
+
+ '|^<[a-zA-Z][\\w-]*(?:attribute)*?\\s*/?>' // open tag
|
|
1159
|
+
+ '|^<\\?[\\s\\S]*?\\?>' // processing instruction, e.g. <?php ?>
|
|
1160
|
+
+ '|^<![a-zA-Z]+\\s[\\s\\S]*?>' // declaration, e.g. <!DOCTYPE html>
|
|
1161
|
+
+ '|^<!\\[CDATA\\[[\\s\\S]*?\\]\\]>',
|
|
1162
|
+
link: /^!?\[(label)\]\(\s*(href)(?:\s+(title))?\s*\)/,
|
|
1163
|
+
reflink: /^!?\[(label)\]\[(ref)\]/,
|
|
1164
|
+
nolink: /^!?\[(ref)\](?:\[\])?/,
|
|
1165
|
+
reflinkSearch: 'reflink|nolink(?!\\()',
|
|
1166
|
+
emStrong: {
|
|
1167
|
+
lDelim: /^(?:\*+(?:((?!\*)[punct])|[^\s*]))|^_+(?:((?!_)[punct])|([^\s_]))/,
|
|
1168
|
+
// (1) and (2) can only be a Right Delimiter. (3) and (4) can only be Left. (5) and (6) can be either Left or Right.
|
|
1169
|
+
// | Skip orphan inside strong | Consume to delim | (1) #*** | (2) a***#, a*** | (3) #***a, ***a | (4) ***# | (5) #***# | (6) a***a
|
|
1170
|
+
rDelimAst: /^[^_*]*?__[^_*]*?\*[^_*]*?(?=__)|[^*]+(?=[^*])|(?!\*)[punct](\*+)(?=[\s]|$)|[^punct\s](\*+)(?!\*)(?=[punct\s]|$)|(?!\*)[punct\s](\*+)(?=[^punct\s])|[\s](\*+)(?!\*)(?=[punct])|(?!\*)[punct](\*+)(?!\*)(?=[punct])|[^punct\s](\*+)(?=[^punct\s])/,
|
|
1171
|
+
rDelimUnd: /^[^_*]*?\*\*[^_*]*?_[^_*]*?(?=\*\*)|[^_]+(?=[^_])|(?!_)[punct](_+)(?=[\s]|$)|[^punct\s](_+)(?!_)(?=[punct\s]|$)|(?!_)[punct\s](_+)(?=[^punct\s])|[\s](_+)(?!_)(?=[punct])|(?!_)[punct](_+)(?!_)(?=[punct])/ // ^- Not allowed for _
|
|
1172
|
+
},
|
|
1173
|
+
code: /^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/,
|
|
1174
|
+
br: /^( {2,}|\\)\n(?!\s*$)/,
|
|
1175
|
+
del: noopTest,
|
|
1176
|
+
text: /^(`+|[^`])(?:(?= {2,}\n)|[\s\S]*?(?:(?=[\\<!\[`*_]|\b_|$)|[^ ](?= {2,}\n)))/,
|
|
1177
|
+
punctuation: /^((?![*_])[\spunctuation])/
|
|
1028
1178
|
};
|
|
1029
|
-
|
|
1030
|
-
inline.
|
|
1179
|
+
// list of unicode punctuation marks, plus any missing characters from CommonMark spec
|
|
1180
|
+
inline._punctuation = '\\p{P}$+<=>`^|~';
|
|
1181
|
+
inline.punctuation = edit(inline.punctuation, 'u').replace(/punctuation/g, inline._punctuation).getRegex();
|
|
1182
|
+
// sequences em should skip over [title](link), `code`, <html>
|
|
1031
1183
|
inline.blockSkip = /\[[^[\]]*?\]\([^\(\)]*?\)|`[^`]*?`|<[^<>]*?>/g;
|
|
1032
1184
|
inline.anyPunctuation = /\\[punct]/g;
|
|
1033
1185
|
inline._escapes = /\\([punct])/g;
|
|
1034
|
-
inline._comment = edit(block._comment).replace(
|
|
1035
|
-
inline.emStrong.lDelim = edit(inline.emStrong.lDelim,
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
inline.
|
|
1039
|
-
|
|
1186
|
+
inline._comment = edit(block._comment).replace('(?:-->|$)', '-->').getRegex();
|
|
1187
|
+
inline.emStrong.lDelim = edit(inline.emStrong.lDelim, 'u')
|
|
1188
|
+
.replace(/punct/g, inline._punctuation)
|
|
1189
|
+
.getRegex();
|
|
1190
|
+
inline.emStrong.rDelimAst = edit(inline.emStrong.rDelimAst, 'gu')
|
|
1191
|
+
.replace(/punct/g, inline._punctuation)
|
|
1192
|
+
.getRegex();
|
|
1193
|
+
inline.emStrong.rDelimUnd = edit(inline.emStrong.rDelimUnd, 'gu')
|
|
1194
|
+
.replace(/punct/g, inline._punctuation)
|
|
1195
|
+
.getRegex();
|
|
1196
|
+
inline.anyPunctuation = edit(inline.anyPunctuation, 'gu')
|
|
1197
|
+
.replace(/punct/g, inline._punctuation)
|
|
1198
|
+
.getRegex();
|
|
1199
|
+
inline._escapes = edit(inline._escapes, 'gu')
|
|
1200
|
+
.replace(/punct/g, inline._punctuation)
|
|
1201
|
+
.getRegex();
|
|
1040
1202
|
inline._scheme = /[a-zA-Z][a-zA-Z0-9+.-]{1,31}/;
|
|
1041
1203
|
inline._email = /[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+(@)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(?![-_])/;
|
|
1042
|
-
inline.autolink = edit(inline.autolink)
|
|
1204
|
+
inline.autolink = edit(inline.autolink)
|
|
1205
|
+
.replace('scheme', inline._scheme)
|
|
1206
|
+
.replace('email', inline._email)
|
|
1207
|
+
.getRegex();
|
|
1043
1208
|
inline._attribute = /\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?/;
|
|
1044
|
-
inline.tag = edit(inline.tag)
|
|
1209
|
+
inline.tag = edit(inline.tag)
|
|
1210
|
+
.replace('comment', inline._comment)
|
|
1211
|
+
.replace('attribute', inline._attribute)
|
|
1212
|
+
.getRegex();
|
|
1045
1213
|
inline._label = /(?:\[(?:\\.|[^\[\]\\])*\]|\\.|`[^`]*`|[^\[\]\\`])*?/;
|
|
1046
1214
|
inline._href = /<(?:\\.|[^\n<>\\])+>|[^\s\x00-\x1f]*/;
|
|
1047
1215
|
inline._title = /"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/;
|
|
1048
|
-
inline.link = edit(inline.link)
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1216
|
+
inline.link = edit(inline.link)
|
|
1217
|
+
.replace('label', inline._label)
|
|
1218
|
+
.replace('href', inline._href)
|
|
1219
|
+
.replace('title', inline._title)
|
|
1220
|
+
.getRegex();
|
|
1221
|
+
inline.reflink = edit(inline.reflink)
|
|
1222
|
+
.replace('label', inline._label)
|
|
1223
|
+
.replace('ref', block._label)
|
|
1224
|
+
.getRegex();
|
|
1225
|
+
inline.nolink = edit(inline.nolink)
|
|
1226
|
+
.replace('ref', block._label)
|
|
1227
|
+
.getRegex();
|
|
1228
|
+
inline.reflinkSearch = edit(inline.reflinkSearch, 'g')
|
|
1229
|
+
.replace('reflink', inline.reflink)
|
|
1230
|
+
.replace('nolink', inline.nolink)
|
|
1231
|
+
.getRegex();
|
|
1232
|
+
/**
|
|
1233
|
+
* Normal Inline Grammar
|
|
1234
|
+
*/
|
|
1052
1235
|
inline.normal = { ...inline };
|
|
1236
|
+
/**
|
|
1237
|
+
* Pedantic Inline Grammar
|
|
1238
|
+
*/
|
|
1053
1239
|
inline.pedantic = {
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1240
|
+
...inline.normal,
|
|
1241
|
+
strong: {
|
|
1242
|
+
start: /^__|\*\*/,
|
|
1243
|
+
middle: /^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,
|
|
1244
|
+
endAst: /\*\*(?!\*)/g,
|
|
1245
|
+
endUnd: /__(?!_)/g
|
|
1246
|
+
},
|
|
1247
|
+
em: {
|
|
1248
|
+
start: /^_|\*/,
|
|
1249
|
+
middle: /^()\*(?=\S)([\s\S]*?\S)\*(?!\*)|^_(?=\S)([\s\S]*?\S)_(?!_)/,
|
|
1250
|
+
endAst: /\*(?!\*)/g,
|
|
1251
|
+
endUnd: /_(?!_)/g
|
|
1252
|
+
},
|
|
1253
|
+
link: edit(/^!?\[(label)\]\((.*?)\)/)
|
|
1254
|
+
.replace('label', inline._label)
|
|
1255
|
+
.getRegex(),
|
|
1256
|
+
reflink: edit(/^!?\[(label)\]\s*\[([^\]]*)\]/)
|
|
1257
|
+
.replace('label', inline._label)
|
|
1258
|
+
.getRegex()
|
|
1069
1259
|
};
|
|
1260
|
+
/**
|
|
1261
|
+
* GFM Inline Grammar
|
|
1262
|
+
*/
|
|
1070
1263
|
inline.gfm = {
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1264
|
+
...inline.normal,
|
|
1265
|
+
escape: edit(inline.escape).replace('])', '~|])').getRegex(),
|
|
1266
|
+
_extended_email: /[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/,
|
|
1267
|
+
url: /^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/,
|
|
1268
|
+
_backpedal: /(?:[^?!.,:;*_'"~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_'"~)]+(?!$))+/,
|
|
1269
|
+
del: /^(~~?)(?=[^\s~])([\s\S]*?[^\s~])\1(?=[^~]|$)/,
|
|
1270
|
+
text: /^([`~]+|[^`~])(?:(?= {2,}\n)|(?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)|[\s\S]*?(?:(?=[\\<!\[`*~_]|\b_|https?:\/\/|ftp:\/\/|www\.|$)|[^ ](?= {2,}\n)|[^a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-](?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)))/
|
|
1078
1271
|
};
|
|
1079
|
-
inline.gfm.url = edit(inline.gfm.url,
|
|
1272
|
+
inline.gfm.url = edit(inline.gfm.url, 'i')
|
|
1273
|
+
.replace('email', inline.gfm._extended_email)
|
|
1274
|
+
.getRegex();
|
|
1275
|
+
/**
|
|
1276
|
+
* GFM + Line Breaks Inline Grammar
|
|
1277
|
+
*/
|
|
1080
1278
|
inline.breaks = {
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1279
|
+
...inline.gfm,
|
|
1280
|
+
br: edit(inline.br).replace('{2,}', '*').getRegex(),
|
|
1281
|
+
text: edit(inline.gfm.text)
|
|
1282
|
+
.replace('\\b_', '\\b_| {2,}\\n')
|
|
1283
|
+
.replace(/\{2,\}/g, '*')
|
|
1284
|
+
.getRegex()
|
|
1084
1285
|
};
|
|
1085
1286
|
|
|
1086
|
-
|
|
1287
|
+
/**
|
|
1288
|
+
* smartypants text replacement
|
|
1289
|
+
*/
|
|
1087
1290
|
function smartypants(text) {
|
|
1088
|
-
|
|
1291
|
+
return text
|
|
1292
|
+
// em-dashes
|
|
1293
|
+
.replace(/---/g, '\u2014')
|
|
1294
|
+
// en-dashes
|
|
1295
|
+
.replace(/--/g, '\u2013')
|
|
1296
|
+
// opening singles
|
|
1297
|
+
.replace(/(^|[-\u2014/(\[{"\s])'/g, '$1\u2018')
|
|
1298
|
+
// closing singles & apostrophes
|
|
1299
|
+
.replace(/'/g, '\u2019')
|
|
1300
|
+
// opening doubles
|
|
1301
|
+
.replace(/(^|[-\u2014/(\[{\u2018\s])"/g, '$1\u201c')
|
|
1302
|
+
// closing doubles
|
|
1303
|
+
.replace(/"/g, '\u201d')
|
|
1304
|
+
// ellipses
|
|
1305
|
+
.replace(/\.{3}/g, '\u2026');
|
|
1089
1306
|
}
|
|
1307
|
+
/**
|
|
1308
|
+
* mangle email addresses
|
|
1309
|
+
*/
|
|
1090
1310
|
function mangle(text) {
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
return out;
|
|
1101
|
-
}
|
|
1102
|
-
var _Lexer2 = class __Lexer {
|
|
1103
|
-
constructor(options2) {
|
|
1104
|
-
this.tokens = [];
|
|
1105
|
-
this.tokens.links = /* @__PURE__ */ Object.create(null);
|
|
1106
|
-
this.options = options2 || _defaults;
|
|
1107
|
-
this.options.tokenizer = this.options.tokenizer || new _Tokenizer();
|
|
1108
|
-
this.tokenizer = this.options.tokenizer;
|
|
1109
|
-
this.tokenizer.options = this.options;
|
|
1110
|
-
this.tokenizer.lexer = this;
|
|
1111
|
-
this.inlineQueue = [];
|
|
1112
|
-
this.state = {
|
|
1113
|
-
inLink: false,
|
|
1114
|
-
inRawBlock: false,
|
|
1115
|
-
top: true
|
|
1116
|
-
};
|
|
1117
|
-
const rules = {
|
|
1118
|
-
block: block.normal,
|
|
1119
|
-
inline: inline.normal
|
|
1120
|
-
};
|
|
1121
|
-
if (this.options.pedantic) {
|
|
1122
|
-
rules.block = block.pedantic;
|
|
1123
|
-
rules.inline = inline.pedantic;
|
|
1124
|
-
} else if (this.options.gfm) {
|
|
1125
|
-
rules.block = block.gfm;
|
|
1126
|
-
if (this.options.breaks) {
|
|
1127
|
-
rules.inline = inline.breaks;
|
|
1128
|
-
} else {
|
|
1129
|
-
rules.inline = inline.gfm;
|
|
1130
|
-
}
|
|
1131
|
-
}
|
|
1132
|
-
this.tokenizer.rules = rules;
|
|
1133
|
-
}
|
|
1134
|
-
/**
|
|
1135
|
-
* Expose Rules
|
|
1136
|
-
*/
|
|
1137
|
-
static get rules() {
|
|
1138
|
-
return {
|
|
1139
|
-
block,
|
|
1140
|
-
inline
|
|
1141
|
-
};
|
|
1142
|
-
}
|
|
1143
|
-
/**
|
|
1144
|
-
* Static Lex Method
|
|
1145
|
-
*/
|
|
1146
|
-
static lex(src, options2) {
|
|
1147
|
-
const lexer2 = new __Lexer(options2);
|
|
1148
|
-
return lexer2.lex(src);
|
|
1149
|
-
}
|
|
1150
|
-
/**
|
|
1151
|
-
* Static Lex Inline Method
|
|
1152
|
-
*/
|
|
1153
|
-
static lexInline(src, options2) {
|
|
1154
|
-
const lexer2 = new __Lexer(options2);
|
|
1155
|
-
return lexer2.inlineTokens(src);
|
|
1156
|
-
}
|
|
1157
|
-
/**
|
|
1158
|
-
* Preprocessing
|
|
1159
|
-
*/
|
|
1160
|
-
lex(src) {
|
|
1161
|
-
src = src.replace(/\r\n|\r/g, "\n");
|
|
1162
|
-
this.blockTokens(src, this.tokens);
|
|
1163
|
-
let next;
|
|
1164
|
-
while (next = this.inlineQueue.shift()) {
|
|
1165
|
-
this.inlineTokens(next.src, next.tokens);
|
|
1166
|
-
}
|
|
1167
|
-
return this.tokens;
|
|
1168
|
-
}
|
|
1169
|
-
blockTokens(src, tokens = []) {
|
|
1170
|
-
if (this.options.pedantic) {
|
|
1171
|
-
src = src.replace(/\t/g, " ").replace(/^ +$/gm, "");
|
|
1172
|
-
} else {
|
|
1173
|
-
src = src.replace(/^( *)(\t+)/gm, (_, leading, tabs) => {
|
|
1174
|
-
return leading + " ".repeat(tabs.length);
|
|
1175
|
-
});
|
|
1176
|
-
}
|
|
1177
|
-
let token, lastToken, cutSrc, lastParagraphClipped;
|
|
1178
|
-
while (src) {
|
|
1179
|
-
if (this.options.extensions && this.options.extensions.block && this.options.extensions.block.some((extTokenizer) => {
|
|
1180
|
-
if (token = extTokenizer.call({ lexer: this }, src, tokens)) {
|
|
1181
|
-
src = src.substring(token.raw.length);
|
|
1182
|
-
tokens.push(token);
|
|
1183
|
-
return true;
|
|
1184
|
-
}
|
|
1185
|
-
return false;
|
|
1186
|
-
})) {
|
|
1187
|
-
continue;
|
|
1188
|
-
}
|
|
1189
|
-
if (token = this.tokenizer.space(src)) {
|
|
1190
|
-
src = src.substring(token.raw.length);
|
|
1191
|
-
if (token.raw.length === 1 && tokens.length > 0) {
|
|
1192
|
-
tokens[tokens.length - 1].raw += "\n";
|
|
1193
|
-
} else {
|
|
1194
|
-
tokens.push(token);
|
|
1195
|
-
}
|
|
1196
|
-
continue;
|
|
1197
|
-
}
|
|
1198
|
-
if (token = this.tokenizer.code(src)) {
|
|
1199
|
-
src = src.substring(token.raw.length);
|
|
1200
|
-
lastToken = tokens[tokens.length - 1];
|
|
1201
|
-
if (lastToken && (lastToken.type === "paragraph" || lastToken.type === "text")) {
|
|
1202
|
-
lastToken.raw += "\n" + token.raw;
|
|
1203
|
-
lastToken.text += "\n" + token.text;
|
|
1204
|
-
this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
|
|
1205
|
-
} else {
|
|
1206
|
-
tokens.push(token);
|
|
1207
|
-
}
|
|
1208
|
-
continue;
|
|
1209
|
-
}
|
|
1210
|
-
if (token = this.tokenizer.fences(src)) {
|
|
1211
|
-
src = src.substring(token.raw.length);
|
|
1212
|
-
tokens.push(token);
|
|
1213
|
-
continue;
|
|
1214
|
-
}
|
|
1215
|
-
if (token = this.tokenizer.heading(src)) {
|
|
1216
|
-
src = src.substring(token.raw.length);
|
|
1217
|
-
tokens.push(token);
|
|
1218
|
-
continue;
|
|
1219
|
-
}
|
|
1220
|
-
if (token = this.tokenizer.hr(src)) {
|
|
1221
|
-
src = src.substring(token.raw.length);
|
|
1222
|
-
tokens.push(token);
|
|
1223
|
-
continue;
|
|
1224
|
-
}
|
|
1225
|
-
if (token = this.tokenizer.blockquote(src)) {
|
|
1226
|
-
src = src.substring(token.raw.length);
|
|
1227
|
-
tokens.push(token);
|
|
1228
|
-
continue;
|
|
1229
|
-
}
|
|
1230
|
-
if (token = this.tokenizer.list(src)) {
|
|
1231
|
-
src = src.substring(token.raw.length);
|
|
1232
|
-
tokens.push(token);
|
|
1233
|
-
continue;
|
|
1234
|
-
}
|
|
1235
|
-
if (token = this.tokenizer.html(src)) {
|
|
1236
|
-
src = src.substring(token.raw.length);
|
|
1237
|
-
tokens.push(token);
|
|
1238
|
-
continue;
|
|
1239
|
-
}
|
|
1240
|
-
if (token = this.tokenizer.def(src)) {
|
|
1241
|
-
src = src.substring(token.raw.length);
|
|
1242
|
-
lastToken = tokens[tokens.length - 1];
|
|
1243
|
-
if (lastToken && (lastToken.type === "paragraph" || lastToken.type === "text")) {
|
|
1244
|
-
lastToken.raw += "\n" + token.raw;
|
|
1245
|
-
lastToken.text += "\n" + token.raw;
|
|
1246
|
-
this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
|
|
1247
|
-
} else if (!this.tokens.links[token.tag]) {
|
|
1248
|
-
this.tokens.links[token.tag] = {
|
|
1249
|
-
href: token.href,
|
|
1250
|
-
title: token.title
|
|
1251
|
-
};
|
|
1252
|
-
}
|
|
1253
|
-
continue;
|
|
1254
|
-
}
|
|
1255
|
-
if (token = this.tokenizer.table(src)) {
|
|
1256
|
-
src = src.substring(token.raw.length);
|
|
1257
|
-
tokens.push(token);
|
|
1258
|
-
continue;
|
|
1259
|
-
}
|
|
1260
|
-
if (token = this.tokenizer.lheading(src)) {
|
|
1261
|
-
src = src.substring(token.raw.length);
|
|
1262
|
-
tokens.push(token);
|
|
1263
|
-
continue;
|
|
1264
|
-
}
|
|
1265
|
-
cutSrc = src;
|
|
1266
|
-
if (this.options.extensions && this.options.extensions.startBlock) {
|
|
1267
|
-
let startIndex = Infinity;
|
|
1268
|
-
const tempSrc = src.slice(1);
|
|
1269
|
-
let tempStart;
|
|
1270
|
-
this.options.extensions.startBlock.forEach((getStartIndex) => {
|
|
1271
|
-
tempStart = getStartIndex.call({ lexer: this }, tempSrc);
|
|
1272
|
-
if (typeof tempStart === "number" && tempStart >= 0) {
|
|
1273
|
-
startIndex = Math.min(startIndex, tempStart);
|
|
1274
|
-
}
|
|
1275
|
-
});
|
|
1276
|
-
if (startIndex < Infinity && startIndex >= 0) {
|
|
1277
|
-
cutSrc = src.substring(0, startIndex + 1);
|
|
1278
|
-
}
|
|
1279
|
-
}
|
|
1280
|
-
if (this.state.top && (token = this.tokenizer.paragraph(cutSrc))) {
|
|
1281
|
-
lastToken = tokens[tokens.length - 1];
|
|
1282
|
-
if (lastParagraphClipped && lastToken.type === "paragraph") {
|
|
1283
|
-
lastToken.raw += "\n" + token.raw;
|
|
1284
|
-
lastToken.text += "\n" + token.text;
|
|
1285
|
-
this.inlineQueue.pop();
|
|
1286
|
-
this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
|
|
1287
|
-
} else {
|
|
1288
|
-
tokens.push(token);
|
|
1289
|
-
}
|
|
1290
|
-
lastParagraphClipped = cutSrc.length !== src.length;
|
|
1291
|
-
src = src.substring(token.raw.length);
|
|
1292
|
-
continue;
|
|
1293
|
-
}
|
|
1294
|
-
if (token = this.tokenizer.text(src)) {
|
|
1295
|
-
src = src.substring(token.raw.length);
|
|
1296
|
-
lastToken = tokens[tokens.length - 1];
|
|
1297
|
-
if (lastToken && lastToken.type === "text") {
|
|
1298
|
-
lastToken.raw += "\n" + token.raw;
|
|
1299
|
-
lastToken.text += "\n" + token.text;
|
|
1300
|
-
this.inlineQueue.pop();
|
|
1301
|
-
this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
|
|
1302
|
-
} else {
|
|
1303
|
-
tokens.push(token);
|
|
1304
|
-
}
|
|
1305
|
-
continue;
|
|
1306
|
-
}
|
|
1307
|
-
if (src) {
|
|
1308
|
-
const errMsg = "Infinite loop on byte: " + src.charCodeAt(0);
|
|
1309
|
-
if (this.options.silent) {
|
|
1310
|
-
console.error(errMsg);
|
|
1311
|
-
break;
|
|
1312
|
-
} else {
|
|
1313
|
-
throw new Error(errMsg);
|
|
1314
|
-
}
|
|
1315
|
-
}
|
|
1316
|
-
}
|
|
1317
|
-
this.state.top = true;
|
|
1318
|
-
return tokens;
|
|
1319
|
-
}
|
|
1320
|
-
inline(src, tokens = []) {
|
|
1321
|
-
this.inlineQueue.push({ src, tokens });
|
|
1322
|
-
return tokens;
|
|
1323
|
-
}
|
|
1324
|
-
/**
|
|
1325
|
-
* Lexing/Compiling
|
|
1326
|
-
*/
|
|
1327
|
-
inlineTokens(src, tokens = []) {
|
|
1328
|
-
let token, lastToken, cutSrc;
|
|
1329
|
-
let maskedSrc = src;
|
|
1330
|
-
let match;
|
|
1331
|
-
let keepPrevChar, prevChar;
|
|
1332
|
-
if (this.tokens.links) {
|
|
1333
|
-
const links = Object.keys(this.tokens.links);
|
|
1334
|
-
if (links.length > 0) {
|
|
1335
|
-
while ((match = this.tokenizer.rules.inline.reflinkSearch.exec(maskedSrc)) != null) {
|
|
1336
|
-
if (links.includes(match[0].slice(match[0].lastIndexOf("[") + 1, -1))) {
|
|
1337
|
-
maskedSrc = maskedSrc.slice(0, match.index) + "[" + "a".repeat(match[0].length - 2) + "]" + maskedSrc.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex);
|
|
1338
|
-
}
|
|
1339
|
-
}
|
|
1340
|
-
}
|
|
1341
|
-
}
|
|
1342
|
-
while ((match = this.tokenizer.rules.inline.blockSkip.exec(maskedSrc)) != null) {
|
|
1343
|
-
maskedSrc = maskedSrc.slice(0, match.index) + "[" + "a".repeat(match[0].length - 2) + "]" + maskedSrc.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);
|
|
1344
|
-
}
|
|
1345
|
-
while ((match = this.tokenizer.rules.inline.anyPunctuation.exec(maskedSrc)) != null) {
|
|
1346
|
-
maskedSrc = maskedSrc.slice(0, match.index) + "++" + maskedSrc.slice(this.tokenizer.rules.inline.anyPunctuation.lastIndex);
|
|
1347
|
-
}
|
|
1348
|
-
while (src) {
|
|
1349
|
-
if (!keepPrevChar) {
|
|
1350
|
-
prevChar = "";
|
|
1351
|
-
}
|
|
1352
|
-
keepPrevChar = false;
|
|
1353
|
-
if (this.options.extensions && this.options.extensions.inline && this.options.extensions.inline.some((extTokenizer) => {
|
|
1354
|
-
if (token = extTokenizer.call({ lexer: this }, src, tokens)) {
|
|
1355
|
-
src = src.substring(token.raw.length);
|
|
1356
|
-
tokens.push(token);
|
|
1357
|
-
return true;
|
|
1358
|
-
}
|
|
1359
|
-
return false;
|
|
1360
|
-
})) {
|
|
1361
|
-
continue;
|
|
1362
|
-
}
|
|
1363
|
-
if (token = this.tokenizer.escape(src)) {
|
|
1364
|
-
src = src.substring(token.raw.length);
|
|
1365
|
-
tokens.push(token);
|
|
1366
|
-
continue;
|
|
1367
|
-
}
|
|
1368
|
-
if (token = this.tokenizer.tag(src)) {
|
|
1369
|
-
src = src.substring(token.raw.length);
|
|
1370
|
-
lastToken = tokens[tokens.length - 1];
|
|
1371
|
-
if (lastToken && token.type === "text" && lastToken.type === "text") {
|
|
1372
|
-
lastToken.raw += token.raw;
|
|
1373
|
-
lastToken.text += token.text;
|
|
1374
|
-
} else {
|
|
1375
|
-
tokens.push(token);
|
|
1376
|
-
}
|
|
1377
|
-
continue;
|
|
1378
|
-
}
|
|
1379
|
-
if (token = this.tokenizer.link(src)) {
|
|
1380
|
-
src = src.substring(token.raw.length);
|
|
1381
|
-
tokens.push(token);
|
|
1382
|
-
continue;
|
|
1383
|
-
}
|
|
1384
|
-
if (token = this.tokenizer.reflink(src, this.tokens.links)) {
|
|
1385
|
-
src = src.substring(token.raw.length);
|
|
1386
|
-
lastToken = tokens[tokens.length - 1];
|
|
1387
|
-
if (lastToken && token.type === "text" && lastToken.type === "text") {
|
|
1388
|
-
lastToken.raw += token.raw;
|
|
1389
|
-
lastToken.text += token.text;
|
|
1390
|
-
} else {
|
|
1391
|
-
tokens.push(token);
|
|
1392
|
-
}
|
|
1393
|
-
continue;
|
|
1394
|
-
}
|
|
1395
|
-
if (token = this.tokenizer.emStrong(src, maskedSrc, prevChar)) {
|
|
1396
|
-
src = src.substring(token.raw.length);
|
|
1397
|
-
tokens.push(token);
|
|
1398
|
-
continue;
|
|
1399
|
-
}
|
|
1400
|
-
if (token = this.tokenizer.codespan(src)) {
|
|
1401
|
-
src = src.substring(token.raw.length);
|
|
1402
|
-
tokens.push(token);
|
|
1403
|
-
continue;
|
|
1404
|
-
}
|
|
1405
|
-
if (token = this.tokenizer.br(src)) {
|
|
1406
|
-
src = src.substring(token.raw.length);
|
|
1407
|
-
tokens.push(token);
|
|
1408
|
-
continue;
|
|
1409
|
-
}
|
|
1410
|
-
if (token = this.tokenizer.del(src)) {
|
|
1411
|
-
src = src.substring(token.raw.length);
|
|
1412
|
-
tokens.push(token);
|
|
1413
|
-
continue;
|
|
1414
|
-
}
|
|
1415
|
-
if (token = this.tokenizer.autolink(src, mangle)) {
|
|
1416
|
-
src = src.substring(token.raw.length);
|
|
1417
|
-
tokens.push(token);
|
|
1418
|
-
continue;
|
|
1419
|
-
}
|
|
1420
|
-
if (!this.state.inLink && (token = this.tokenizer.url(src, mangle))) {
|
|
1421
|
-
src = src.substring(token.raw.length);
|
|
1422
|
-
tokens.push(token);
|
|
1423
|
-
continue;
|
|
1424
|
-
}
|
|
1425
|
-
cutSrc = src;
|
|
1426
|
-
if (this.options.extensions && this.options.extensions.startInline) {
|
|
1427
|
-
let startIndex = Infinity;
|
|
1428
|
-
const tempSrc = src.slice(1);
|
|
1429
|
-
let tempStart;
|
|
1430
|
-
this.options.extensions.startInline.forEach((getStartIndex) => {
|
|
1431
|
-
tempStart = getStartIndex.call({ lexer: this }, tempSrc);
|
|
1432
|
-
if (typeof tempStart === "number" && tempStart >= 0) {
|
|
1433
|
-
startIndex = Math.min(startIndex, tempStart);
|
|
1434
|
-
}
|
|
1435
|
-
});
|
|
1436
|
-
if (startIndex < Infinity && startIndex >= 0) {
|
|
1437
|
-
cutSrc = src.substring(0, startIndex + 1);
|
|
1438
|
-
}
|
|
1439
|
-
}
|
|
1440
|
-
if (token = this.tokenizer.inlineText(cutSrc, smartypants)) {
|
|
1441
|
-
src = src.substring(token.raw.length);
|
|
1442
|
-
if (token.raw.slice(-1) !== "_") {
|
|
1443
|
-
prevChar = token.raw.slice(-1);
|
|
1444
|
-
}
|
|
1445
|
-
keepPrevChar = true;
|
|
1446
|
-
lastToken = tokens[tokens.length - 1];
|
|
1447
|
-
if (lastToken && lastToken.type === "text") {
|
|
1448
|
-
lastToken.raw += token.raw;
|
|
1449
|
-
lastToken.text += token.text;
|
|
1450
|
-
} else {
|
|
1451
|
-
tokens.push(token);
|
|
1452
|
-
}
|
|
1453
|
-
continue;
|
|
1454
|
-
}
|
|
1455
|
-
if (src) {
|
|
1456
|
-
const errMsg = "Infinite loop on byte: " + src.charCodeAt(0);
|
|
1457
|
-
if (this.options.silent) {
|
|
1458
|
-
console.error(errMsg);
|
|
1459
|
-
break;
|
|
1460
|
-
} else {
|
|
1461
|
-
throw new Error(errMsg);
|
|
1462
|
-
}
|
|
1463
|
-
}
|
|
1464
|
-
}
|
|
1465
|
-
return tokens;
|
|
1466
|
-
}
|
|
1467
|
-
};
|
|
1468
|
-
|
|
1469
|
-
// src/Renderer.ts
|
|
1470
|
-
var _Renderer = class {
|
|
1471
|
-
constructor(options2) {
|
|
1472
|
-
this.options = options2 || _defaults;
|
|
1473
|
-
}
|
|
1474
|
-
code(code, infostring, escaped) {
|
|
1475
|
-
const lang = (infostring || "").match(/\S*/)[0];
|
|
1476
|
-
if (this.options.highlight) {
|
|
1477
|
-
const out = this.options.highlight(code, lang);
|
|
1478
|
-
if (out != null && out !== code) {
|
|
1479
|
-
escaped = true;
|
|
1480
|
-
code = out;
|
|
1481
|
-
}
|
|
1482
|
-
}
|
|
1483
|
-
code = code.replace(/\n$/, "") + "\n";
|
|
1484
|
-
if (!lang) {
|
|
1485
|
-
return "<pre><code>" + (escaped ? code : escape(code, true)) + "</code></pre>\n";
|
|
1486
|
-
}
|
|
1487
|
-
return '<pre><code class="' + this.options.langPrefix + escape(lang) + '">' + (escaped ? code : escape(code, true)) + "</code></pre>\n";
|
|
1488
|
-
}
|
|
1489
|
-
blockquote(quote) {
|
|
1490
|
-
return `<blockquote>
|
|
1491
|
-
${quote}</blockquote>
|
|
1492
|
-
`;
|
|
1493
|
-
}
|
|
1494
|
-
html(html, block2) {
|
|
1495
|
-
return html;
|
|
1496
|
-
}
|
|
1497
|
-
heading(text, level, raw, slugger) {
|
|
1498
|
-
if (this.options.headerIds) {
|
|
1499
|
-
const id = this.options.headerPrefix + slugger.slug(raw);
|
|
1500
|
-
return `<h${level} id="${id}">${text}</h${level}>
|
|
1501
|
-
`;
|
|
1502
|
-
}
|
|
1503
|
-
return `<h${level}>${text}</h${level}>
|
|
1504
|
-
`;
|
|
1505
|
-
}
|
|
1506
|
-
hr() {
|
|
1507
|
-
return this.options.xhtml ? "<hr/>\n" : "<hr>\n";
|
|
1508
|
-
}
|
|
1509
|
-
list(body, ordered, start) {
|
|
1510
|
-
const type = ordered ? "ol" : "ul", startatt = ordered && start !== 1 ? ' start="' + start + '"' : "";
|
|
1511
|
-
return "<" + type + startatt + ">\n" + body + "</" + type + ">\n";
|
|
1512
|
-
}
|
|
1513
|
-
listitem(text, task, checked) {
|
|
1514
|
-
return `<li>${text}</li>
|
|
1515
|
-
`;
|
|
1516
|
-
}
|
|
1517
|
-
checkbox(checked) {
|
|
1518
|
-
return "<input " + (checked ? 'checked="" ' : "") + 'disabled="" type="checkbox"' + (this.options.xhtml ? " /" : "") + "> ";
|
|
1519
|
-
}
|
|
1520
|
-
paragraph(text) {
|
|
1521
|
-
return `<p>${text}</p>
|
|
1522
|
-
`;
|
|
1523
|
-
}
|
|
1524
|
-
table(header, body) {
|
|
1525
|
-
if (body)
|
|
1526
|
-
body = `<tbody>${body}</tbody>`;
|
|
1527
|
-
return "<table>\n<thead>\n" + header + "</thead>\n" + body + "</table>\n";
|
|
1528
|
-
}
|
|
1529
|
-
tablerow(content) {
|
|
1530
|
-
return `<tr>
|
|
1531
|
-
${content}</tr>
|
|
1532
|
-
`;
|
|
1533
|
-
}
|
|
1534
|
-
tablecell(content, flags) {
|
|
1535
|
-
const type = flags.header ? "th" : "td";
|
|
1536
|
-
const tag = flags.align ? `<${type} align="${flags.align}">` : `<${type}>`;
|
|
1537
|
-
return tag + content + `</${type}>
|
|
1538
|
-
`;
|
|
1539
|
-
}
|
|
1540
|
-
/**
|
|
1541
|
-
* span level renderer
|
|
1542
|
-
*/
|
|
1543
|
-
strong(text) {
|
|
1544
|
-
return `<strong>${text}</strong>`;
|
|
1545
|
-
}
|
|
1546
|
-
em(text) {
|
|
1547
|
-
return `<em>${text}</em>`;
|
|
1548
|
-
}
|
|
1549
|
-
codespan(text) {
|
|
1550
|
-
return `<code>${text}</code>`;
|
|
1551
|
-
}
|
|
1552
|
-
br() {
|
|
1553
|
-
return this.options.xhtml ? "<br/>" : "<br>";
|
|
1554
|
-
}
|
|
1555
|
-
del(text) {
|
|
1556
|
-
return `<del>${text}</del>`;
|
|
1557
|
-
}
|
|
1558
|
-
link(href, title, text) {
|
|
1559
|
-
href = cleanUrl(this.options.sanitize, this.options.baseUrl, href);
|
|
1560
|
-
if (href === null) {
|
|
1561
|
-
return text;
|
|
1562
|
-
}
|
|
1563
|
-
let out = '<a href="' + href + '"';
|
|
1564
|
-
if (title) {
|
|
1565
|
-
out += ' title="' + title + '"';
|
|
1566
|
-
}
|
|
1567
|
-
out += ">" + text + "</a>";
|
|
1311
|
+
let out = '', i, ch;
|
|
1312
|
+
const l = text.length;
|
|
1313
|
+
for (i = 0; i < l; i++) {
|
|
1314
|
+
ch = text.charCodeAt(i);
|
|
1315
|
+
if (Math.random() > 0.5) {
|
|
1316
|
+
ch = 'x' + ch.toString(16);
|
|
1317
|
+
}
|
|
1318
|
+
out += '&#' + ch + ';';
|
|
1319
|
+
}
|
|
1568
1320
|
return out;
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1321
|
+
}
|
|
1322
|
+
/**
|
|
1323
|
+
* Block Lexer
|
|
1324
|
+
*/
|
|
1325
|
+
class _Lexer {
|
|
1326
|
+
tokens;
|
|
1327
|
+
options;
|
|
1328
|
+
state;
|
|
1329
|
+
tokenizer;
|
|
1330
|
+
inlineQueue;
|
|
1331
|
+
constructor(options) {
|
|
1332
|
+
// TokenList cannot be created in one go
|
|
1333
|
+
// @ts-expect-error
|
|
1334
|
+
this.tokens = [];
|
|
1335
|
+
this.tokens.links = Object.create(null);
|
|
1336
|
+
this.options = options || exports.defaults;
|
|
1337
|
+
this.options.tokenizer = this.options.tokenizer || new _Tokenizer();
|
|
1338
|
+
this.tokenizer = this.options.tokenizer;
|
|
1339
|
+
this.tokenizer.options = this.options;
|
|
1340
|
+
this.tokenizer.lexer = this;
|
|
1341
|
+
this.inlineQueue = [];
|
|
1342
|
+
this.state = {
|
|
1343
|
+
inLink: false,
|
|
1344
|
+
inRawBlock: false,
|
|
1345
|
+
top: true
|
|
1346
|
+
};
|
|
1347
|
+
const rules = {
|
|
1348
|
+
block: block.normal,
|
|
1349
|
+
inline: inline.normal
|
|
1350
|
+
};
|
|
1351
|
+
if (this.options.pedantic) {
|
|
1352
|
+
rules.block = block.pedantic;
|
|
1353
|
+
rules.inline = inline.pedantic;
|
|
1354
|
+
}
|
|
1355
|
+
else if (this.options.gfm) {
|
|
1356
|
+
rules.block = block.gfm;
|
|
1357
|
+
if (this.options.breaks) {
|
|
1358
|
+
rules.inline = inline.breaks;
|
|
1359
|
+
}
|
|
1360
|
+
else {
|
|
1361
|
+
rules.inline = inline.gfm;
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1364
|
+
this.tokenizer.rules = rules;
|
|
1574
1365
|
}
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1366
|
+
/**
|
|
1367
|
+
* Expose Rules
|
|
1368
|
+
*/
|
|
1369
|
+
static get rules() {
|
|
1370
|
+
return {
|
|
1371
|
+
block,
|
|
1372
|
+
inline
|
|
1373
|
+
};
|
|
1578
1374
|
}
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
}
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1375
|
+
/**
|
|
1376
|
+
* Static Lex Method
|
|
1377
|
+
*/
|
|
1378
|
+
static lex(src, options) {
|
|
1379
|
+
const lexer = new _Lexer(options);
|
|
1380
|
+
return lexer.lex(src);
|
|
1381
|
+
}
|
|
1382
|
+
/**
|
|
1383
|
+
* Static Lex Inline Method
|
|
1384
|
+
*/
|
|
1385
|
+
static lexInline(src, options) {
|
|
1386
|
+
const lexer = new _Lexer(options);
|
|
1387
|
+
return lexer.inlineTokens(src);
|
|
1388
|
+
}
|
|
1389
|
+
/**
|
|
1390
|
+
* Preprocessing
|
|
1391
|
+
*/
|
|
1392
|
+
lex(src) {
|
|
1393
|
+
src = src
|
|
1394
|
+
.replace(/\r\n|\r/g, '\n');
|
|
1395
|
+
this.blockTokens(src, this.tokens);
|
|
1396
|
+
let next;
|
|
1397
|
+
while (next = this.inlineQueue.shift()) {
|
|
1398
|
+
this.inlineTokens(next.src, next.tokens);
|
|
1399
|
+
}
|
|
1400
|
+
return this.tokens;
|
|
1401
|
+
}
|
|
1402
|
+
blockTokens(src, tokens = []) {
|
|
1403
|
+
if (this.options.pedantic) {
|
|
1404
|
+
src = src.replace(/\t/g, ' ').replace(/^ +$/gm, '');
|
|
1405
|
+
}
|
|
1406
|
+
else {
|
|
1407
|
+
src = src.replace(/^( *)(\t+)/gm, (_, leading, tabs) => {
|
|
1408
|
+
return leading + ' '.repeat(tabs.length);
|
|
1409
|
+
});
|
|
1410
|
+
}
|
|
1411
|
+
let token;
|
|
1412
|
+
let lastToken;
|
|
1413
|
+
let cutSrc;
|
|
1414
|
+
let lastParagraphClipped;
|
|
1415
|
+
while (src) {
|
|
1416
|
+
if (this.options.extensions
|
|
1417
|
+
&& this.options.extensions.block
|
|
1418
|
+
&& this.options.extensions.block.some((extTokenizer) => {
|
|
1419
|
+
if (token = extTokenizer.call({ lexer: this }, src, tokens)) {
|
|
1420
|
+
src = src.substring(token.raw.length);
|
|
1421
|
+
tokens.push(token);
|
|
1422
|
+
return true;
|
|
1423
|
+
}
|
|
1424
|
+
return false;
|
|
1425
|
+
})) {
|
|
1426
|
+
continue;
|
|
1427
|
+
}
|
|
1428
|
+
// newline
|
|
1429
|
+
if (token = this.tokenizer.space(src)) {
|
|
1430
|
+
src = src.substring(token.raw.length);
|
|
1431
|
+
if (token.raw.length === 1 && tokens.length > 0) {
|
|
1432
|
+
// if there's a single \n as a spacer, it's terminating the last line,
|
|
1433
|
+
// so move it there so that we don't get unecessary paragraph tags
|
|
1434
|
+
tokens[tokens.length - 1].raw += '\n';
|
|
1435
|
+
}
|
|
1436
|
+
else {
|
|
1437
|
+
tokens.push(token);
|
|
1438
|
+
}
|
|
1439
|
+
continue;
|
|
1440
|
+
}
|
|
1441
|
+
// code
|
|
1442
|
+
if (token = this.tokenizer.code(src)) {
|
|
1443
|
+
src = src.substring(token.raw.length);
|
|
1444
|
+
lastToken = tokens[tokens.length - 1];
|
|
1445
|
+
// An indented code block cannot interrupt a paragraph.
|
|
1446
|
+
if (lastToken && (lastToken.type === 'paragraph' || lastToken.type === 'text')) {
|
|
1447
|
+
lastToken.raw += '\n' + token.raw;
|
|
1448
|
+
lastToken.text += '\n' + token.text;
|
|
1449
|
+
this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
|
|
1450
|
+
}
|
|
1451
|
+
else {
|
|
1452
|
+
tokens.push(token);
|
|
1453
|
+
}
|
|
1454
|
+
continue;
|
|
1455
|
+
}
|
|
1456
|
+
// fences
|
|
1457
|
+
if (token = this.tokenizer.fences(src)) {
|
|
1458
|
+
src = src.substring(token.raw.length);
|
|
1459
|
+
tokens.push(token);
|
|
1460
|
+
continue;
|
|
1461
|
+
}
|
|
1462
|
+
// heading
|
|
1463
|
+
if (token = this.tokenizer.heading(src)) {
|
|
1464
|
+
src = src.substring(token.raw.length);
|
|
1465
|
+
tokens.push(token);
|
|
1466
|
+
continue;
|
|
1467
|
+
}
|
|
1468
|
+
// hr
|
|
1469
|
+
if (token = this.tokenizer.hr(src)) {
|
|
1470
|
+
src = src.substring(token.raw.length);
|
|
1471
|
+
tokens.push(token);
|
|
1472
|
+
continue;
|
|
1473
|
+
}
|
|
1474
|
+
// blockquote
|
|
1475
|
+
if (token = this.tokenizer.blockquote(src)) {
|
|
1476
|
+
src = src.substring(token.raw.length);
|
|
1477
|
+
tokens.push(token);
|
|
1478
|
+
continue;
|
|
1479
|
+
}
|
|
1480
|
+
// list
|
|
1481
|
+
if (token = this.tokenizer.list(src)) {
|
|
1482
|
+
src = src.substring(token.raw.length);
|
|
1483
|
+
tokens.push(token);
|
|
1484
|
+
continue;
|
|
1485
|
+
}
|
|
1486
|
+
// html
|
|
1487
|
+
if (token = this.tokenizer.html(src)) {
|
|
1488
|
+
src = src.substring(token.raw.length);
|
|
1489
|
+
tokens.push(token);
|
|
1490
|
+
continue;
|
|
1491
|
+
}
|
|
1492
|
+
// def
|
|
1493
|
+
if (token = this.tokenizer.def(src)) {
|
|
1494
|
+
src = src.substring(token.raw.length);
|
|
1495
|
+
lastToken = tokens[tokens.length - 1];
|
|
1496
|
+
if (lastToken && (lastToken.type === 'paragraph' || lastToken.type === 'text')) {
|
|
1497
|
+
lastToken.raw += '\n' + token.raw;
|
|
1498
|
+
lastToken.text += '\n' + token.raw;
|
|
1499
|
+
this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
|
|
1500
|
+
}
|
|
1501
|
+
else if (!this.tokens.links[token.tag]) {
|
|
1502
|
+
this.tokens.links[token.tag] = {
|
|
1503
|
+
href: token.href,
|
|
1504
|
+
title: token.title
|
|
1505
|
+
};
|
|
1506
|
+
}
|
|
1507
|
+
continue;
|
|
1508
|
+
}
|
|
1509
|
+
// table (gfm)
|
|
1510
|
+
if (token = this.tokenizer.table(src)) {
|
|
1511
|
+
src = src.substring(token.raw.length);
|
|
1512
|
+
tokens.push(token);
|
|
1513
|
+
continue;
|
|
1514
|
+
}
|
|
1515
|
+
// lheading
|
|
1516
|
+
if (token = this.tokenizer.lheading(src)) {
|
|
1517
|
+
src = src.substring(token.raw.length);
|
|
1518
|
+
tokens.push(token);
|
|
1519
|
+
continue;
|
|
1520
|
+
}
|
|
1521
|
+
// top-level paragraph
|
|
1522
|
+
// prevent paragraph consuming extensions by clipping 'src' to extension start
|
|
1523
|
+
cutSrc = src;
|
|
1524
|
+
if (this.options.extensions && this.options.extensions.startBlock) {
|
|
1525
|
+
let startIndex = Infinity;
|
|
1526
|
+
const tempSrc = src.slice(1);
|
|
1527
|
+
let tempStart;
|
|
1528
|
+
this.options.extensions.startBlock.forEach((getStartIndex) => {
|
|
1529
|
+
tempStart = getStartIndex.call({ lexer: this }, tempSrc);
|
|
1530
|
+
if (typeof tempStart === 'number' && tempStart >= 0) {
|
|
1531
|
+
startIndex = Math.min(startIndex, tempStart);
|
|
1532
|
+
}
|
|
1533
|
+
});
|
|
1534
|
+
if (startIndex < Infinity && startIndex >= 0) {
|
|
1535
|
+
cutSrc = src.substring(0, startIndex + 1);
|
|
1536
|
+
}
|
|
1537
|
+
}
|
|
1538
|
+
if (this.state.top && (token = this.tokenizer.paragraph(cutSrc))) {
|
|
1539
|
+
lastToken = tokens[tokens.length - 1];
|
|
1540
|
+
if (lastParagraphClipped && lastToken.type === 'paragraph') {
|
|
1541
|
+
lastToken.raw += '\n' + token.raw;
|
|
1542
|
+
lastToken.text += '\n' + token.text;
|
|
1543
|
+
this.inlineQueue.pop();
|
|
1544
|
+
this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
|
|
1545
|
+
}
|
|
1546
|
+
else {
|
|
1547
|
+
tokens.push(token);
|
|
1548
|
+
}
|
|
1549
|
+
lastParagraphClipped = (cutSrc.length !== src.length);
|
|
1550
|
+
src = src.substring(token.raw.length);
|
|
1551
|
+
continue;
|
|
1552
|
+
}
|
|
1553
|
+
// text
|
|
1554
|
+
if (token = this.tokenizer.text(src)) {
|
|
1555
|
+
src = src.substring(token.raw.length);
|
|
1556
|
+
lastToken = tokens[tokens.length - 1];
|
|
1557
|
+
if (lastToken && lastToken.type === 'text') {
|
|
1558
|
+
lastToken.raw += '\n' + token.raw;
|
|
1559
|
+
lastToken.text += '\n' + token.text;
|
|
1560
|
+
this.inlineQueue.pop();
|
|
1561
|
+
this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
|
|
1562
|
+
}
|
|
1563
|
+
else {
|
|
1564
|
+
tokens.push(token);
|
|
1565
|
+
}
|
|
1566
|
+
continue;
|
|
1567
|
+
}
|
|
1568
|
+
if (src) {
|
|
1569
|
+
const errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0);
|
|
1570
|
+
if (this.options.silent) {
|
|
1571
|
+
console.error(errMsg);
|
|
1572
|
+
break;
|
|
1573
|
+
}
|
|
1574
|
+
else {
|
|
1575
|
+
throw new Error(errMsg);
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
}
|
|
1579
|
+
this.state.top = true;
|
|
1580
|
+
return tokens;
|
|
1581
|
+
}
|
|
1582
|
+
inline(src, tokens = []) {
|
|
1583
|
+
this.inlineQueue.push({ src, tokens });
|
|
1584
|
+
return tokens;
|
|
1585
|
+
}
|
|
1586
|
+
/**
|
|
1587
|
+
* Lexing/Compiling
|
|
1588
|
+
*/
|
|
1589
|
+
inlineTokens(src, tokens = []) {
|
|
1590
|
+
let token, lastToken, cutSrc;
|
|
1591
|
+
// String with links masked to avoid interference with em and strong
|
|
1592
|
+
let maskedSrc = src;
|
|
1593
|
+
let match;
|
|
1594
|
+
let keepPrevChar, prevChar;
|
|
1595
|
+
// Mask out reflinks
|
|
1596
|
+
if (this.tokens.links) {
|
|
1597
|
+
const links = Object.keys(this.tokens.links);
|
|
1598
|
+
if (links.length > 0) {
|
|
1599
|
+
while ((match = this.tokenizer.rules.inline.reflinkSearch.exec(maskedSrc)) != null) {
|
|
1600
|
+
if (links.includes(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) {
|
|
1601
|
+
maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex);
|
|
1602
|
+
}
|
|
1603
|
+
}
|
|
1604
|
+
}
|
|
1605
|
+
}
|
|
1606
|
+
// Mask out other blocks
|
|
1607
|
+
while ((match = this.tokenizer.rules.inline.blockSkip.exec(maskedSrc)) != null) {
|
|
1608
|
+
maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);
|
|
1609
|
+
}
|
|
1610
|
+
// Mask out escaped characters
|
|
1611
|
+
while ((match = this.tokenizer.rules.inline.anyPunctuation.exec(maskedSrc)) != null) {
|
|
1612
|
+
maskedSrc = maskedSrc.slice(0, match.index) + '++' + maskedSrc.slice(this.tokenizer.rules.inline.anyPunctuation.lastIndex);
|
|
1613
|
+
}
|
|
1614
|
+
while (src) {
|
|
1615
|
+
if (!keepPrevChar) {
|
|
1616
|
+
prevChar = '';
|
|
1617
|
+
}
|
|
1618
|
+
keepPrevChar = false;
|
|
1619
|
+
// extensions
|
|
1620
|
+
if (this.options.extensions
|
|
1621
|
+
&& this.options.extensions.inline
|
|
1622
|
+
&& this.options.extensions.inline.some((extTokenizer) => {
|
|
1623
|
+
if (token = extTokenizer.call({ lexer: this }, src, tokens)) {
|
|
1624
|
+
src = src.substring(token.raw.length);
|
|
1625
|
+
tokens.push(token);
|
|
1626
|
+
return true;
|
|
1627
|
+
}
|
|
1628
|
+
return false;
|
|
1629
|
+
})) {
|
|
1630
|
+
continue;
|
|
1631
|
+
}
|
|
1632
|
+
// escape
|
|
1633
|
+
if (token = this.tokenizer.escape(src)) {
|
|
1634
|
+
src = src.substring(token.raw.length);
|
|
1635
|
+
tokens.push(token);
|
|
1636
|
+
continue;
|
|
1637
|
+
}
|
|
1638
|
+
// tag
|
|
1639
|
+
if (token = this.tokenizer.tag(src)) {
|
|
1640
|
+
src = src.substring(token.raw.length);
|
|
1641
|
+
lastToken = tokens[tokens.length - 1];
|
|
1642
|
+
if (lastToken && token.type === 'text' && lastToken.type === 'text') {
|
|
1643
|
+
lastToken.raw += token.raw;
|
|
1644
|
+
lastToken.text += token.text;
|
|
1645
|
+
}
|
|
1646
|
+
else {
|
|
1647
|
+
tokens.push(token);
|
|
1648
|
+
}
|
|
1649
|
+
continue;
|
|
1650
|
+
}
|
|
1651
|
+
// link
|
|
1652
|
+
if (token = this.tokenizer.link(src)) {
|
|
1653
|
+
src = src.substring(token.raw.length);
|
|
1654
|
+
tokens.push(token);
|
|
1655
|
+
continue;
|
|
1656
|
+
}
|
|
1657
|
+
// reflink, nolink
|
|
1658
|
+
if (token = this.tokenizer.reflink(src, this.tokens.links)) {
|
|
1659
|
+
src = src.substring(token.raw.length);
|
|
1660
|
+
lastToken = tokens[tokens.length - 1];
|
|
1661
|
+
if (lastToken && token.type === 'text' && lastToken.type === 'text') {
|
|
1662
|
+
lastToken.raw += token.raw;
|
|
1663
|
+
lastToken.text += token.text;
|
|
1664
|
+
}
|
|
1665
|
+
else {
|
|
1666
|
+
tokens.push(token);
|
|
1667
|
+
}
|
|
1668
|
+
continue;
|
|
1669
|
+
}
|
|
1670
|
+
// em & strong
|
|
1671
|
+
if (token = this.tokenizer.emStrong(src, maskedSrc, prevChar)) {
|
|
1672
|
+
src = src.substring(token.raw.length);
|
|
1673
|
+
tokens.push(token);
|
|
1674
|
+
continue;
|
|
1675
|
+
}
|
|
1676
|
+
// code
|
|
1677
|
+
if (token = this.tokenizer.codespan(src)) {
|
|
1678
|
+
src = src.substring(token.raw.length);
|
|
1679
|
+
tokens.push(token);
|
|
1680
|
+
continue;
|
|
1681
|
+
}
|
|
1682
|
+
// br
|
|
1683
|
+
if (token = this.tokenizer.br(src)) {
|
|
1684
|
+
src = src.substring(token.raw.length);
|
|
1685
|
+
tokens.push(token);
|
|
1686
|
+
continue;
|
|
1687
|
+
}
|
|
1688
|
+
// del (gfm)
|
|
1689
|
+
if (token = this.tokenizer.del(src)) {
|
|
1690
|
+
src = src.substring(token.raw.length);
|
|
1691
|
+
tokens.push(token);
|
|
1692
|
+
continue;
|
|
1693
|
+
}
|
|
1694
|
+
// autolink
|
|
1695
|
+
if (token = this.tokenizer.autolink(src, mangle)) {
|
|
1696
|
+
src = src.substring(token.raw.length);
|
|
1697
|
+
tokens.push(token);
|
|
1698
|
+
continue;
|
|
1699
|
+
}
|
|
1700
|
+
// url (gfm)
|
|
1701
|
+
if (!this.state.inLink && (token = this.tokenizer.url(src, mangle))) {
|
|
1702
|
+
src = src.substring(token.raw.length);
|
|
1703
|
+
tokens.push(token);
|
|
1704
|
+
continue;
|
|
1705
|
+
}
|
|
1706
|
+
// text
|
|
1707
|
+
// prevent inlineText consuming extensions by clipping 'src' to extension start
|
|
1708
|
+
cutSrc = src;
|
|
1709
|
+
if (this.options.extensions && this.options.extensions.startInline) {
|
|
1710
|
+
let startIndex = Infinity;
|
|
1711
|
+
const tempSrc = src.slice(1);
|
|
1712
|
+
let tempStart;
|
|
1713
|
+
this.options.extensions.startInline.forEach((getStartIndex) => {
|
|
1714
|
+
tempStart = getStartIndex.call({ lexer: this }, tempSrc);
|
|
1715
|
+
if (typeof tempStart === 'number' && tempStart >= 0) {
|
|
1716
|
+
startIndex = Math.min(startIndex, tempStart);
|
|
1717
|
+
}
|
|
1718
|
+
});
|
|
1719
|
+
if (startIndex < Infinity && startIndex >= 0) {
|
|
1720
|
+
cutSrc = src.substring(0, startIndex + 1);
|
|
1721
|
+
}
|
|
1722
|
+
}
|
|
1723
|
+
if (token = this.tokenizer.inlineText(cutSrc, smartypants)) {
|
|
1724
|
+
src = src.substring(token.raw.length);
|
|
1725
|
+
if (token.raw.slice(-1) !== '_') { // Track prevChar before string of ____ started
|
|
1726
|
+
prevChar = token.raw.slice(-1);
|
|
1727
|
+
}
|
|
1728
|
+
keepPrevChar = true;
|
|
1729
|
+
lastToken = tokens[tokens.length - 1];
|
|
1730
|
+
if (lastToken && lastToken.type === 'text') {
|
|
1731
|
+
lastToken.raw += token.raw;
|
|
1732
|
+
lastToken.text += token.text;
|
|
1733
|
+
}
|
|
1734
|
+
else {
|
|
1735
|
+
tokens.push(token);
|
|
1736
|
+
}
|
|
1737
|
+
continue;
|
|
1738
|
+
}
|
|
1739
|
+
if (src) {
|
|
1740
|
+
const errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0);
|
|
1741
|
+
if (this.options.silent) {
|
|
1742
|
+
console.error(errMsg);
|
|
1743
|
+
break;
|
|
1744
|
+
}
|
|
1745
|
+
else {
|
|
1746
|
+
throw new Error(errMsg);
|
|
1747
|
+
}
|
|
1748
|
+
}
|
|
1749
|
+
}
|
|
1750
|
+
return tokens;
|
|
1751
|
+
}
|
|
1752
|
+
}
|
|
1618
1753
|
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1754
|
+
/**
|
|
1755
|
+
* Renderer
|
|
1756
|
+
*/
|
|
1757
|
+
class _Renderer {
|
|
1758
|
+
options;
|
|
1759
|
+
constructor(options) {
|
|
1760
|
+
this.options = options || exports.defaults;
|
|
1761
|
+
}
|
|
1762
|
+
code(code, infostring, escaped) {
|
|
1763
|
+
const lang = (infostring || '').match(/\S*/)[0];
|
|
1764
|
+
if (this.options.highlight) {
|
|
1765
|
+
const out = this.options.highlight(code, lang);
|
|
1766
|
+
if (out != null && out !== code) {
|
|
1767
|
+
escaped = true;
|
|
1768
|
+
code = out;
|
|
1769
|
+
}
|
|
1770
|
+
}
|
|
1771
|
+
code = code.replace(/\n$/, '') + '\n';
|
|
1772
|
+
if (!lang) {
|
|
1773
|
+
return '<pre><code>'
|
|
1774
|
+
+ (escaped ? code : escape(code, true))
|
|
1775
|
+
+ '</code></pre>\n';
|
|
1776
|
+
}
|
|
1777
|
+
return '<pre><code class="'
|
|
1778
|
+
+ this.options.langPrefix
|
|
1779
|
+
+ escape(lang)
|
|
1780
|
+
+ '">'
|
|
1781
|
+
+ (escaped ? code : escape(code, true))
|
|
1782
|
+
+ '</code></pre>\n';
|
|
1783
|
+
}
|
|
1784
|
+
blockquote(quote) {
|
|
1785
|
+
return `<blockquote>\n${quote}</blockquote>\n`;
|
|
1786
|
+
}
|
|
1787
|
+
html(html, block) {
|
|
1788
|
+
return html;
|
|
1789
|
+
}
|
|
1790
|
+
heading(text, level, raw, slugger) {
|
|
1791
|
+
if (this.options.headerIds) {
|
|
1792
|
+
const id = this.options.headerPrefix + slugger.slug(raw);
|
|
1793
|
+
return `<h${level} id="${id}">${text}</h${level}>\n`;
|
|
1794
|
+
}
|
|
1795
|
+
// ignore IDs
|
|
1796
|
+
return `<h${level}>${text}</h${level}>\n`;
|
|
1797
|
+
}
|
|
1798
|
+
hr() {
|
|
1799
|
+
return this.options.xhtml ? '<hr/>\n' : '<hr>\n';
|
|
1800
|
+
}
|
|
1801
|
+
list(body, ordered, start) {
|
|
1802
|
+
const type = ordered ? 'ol' : 'ul', startatt = (ordered && start !== 1) ? (' start="' + start + '"') : '';
|
|
1803
|
+
return '<' + type + startatt + '>\n' + body + '</' + type + '>\n';
|
|
1804
|
+
}
|
|
1805
|
+
listitem(text, task, checked) {
|
|
1806
|
+
return `<li>${text}</li>\n`;
|
|
1807
|
+
}
|
|
1808
|
+
checkbox(checked) {
|
|
1809
|
+
return '<input '
|
|
1810
|
+
+ (checked ? 'checked="" ' : '')
|
|
1811
|
+
+ 'disabled="" type="checkbox"'
|
|
1812
|
+
+ (this.options.xhtml ? ' /' : '')
|
|
1813
|
+
+ '> ';
|
|
1814
|
+
}
|
|
1815
|
+
paragraph(text) {
|
|
1816
|
+
return `<p>${text}</p>\n`;
|
|
1817
|
+
}
|
|
1818
|
+
table(header, body) {
|
|
1819
|
+
if (body)
|
|
1820
|
+
body = `<tbody>${body}</tbody>`;
|
|
1821
|
+
return '<table>\n'
|
|
1822
|
+
+ '<thead>\n'
|
|
1823
|
+
+ header
|
|
1824
|
+
+ '</thead>\n'
|
|
1825
|
+
+ body
|
|
1826
|
+
+ '</table>\n';
|
|
1827
|
+
}
|
|
1828
|
+
tablerow(content) {
|
|
1829
|
+
return `<tr>\n${content}</tr>\n`;
|
|
1830
|
+
}
|
|
1831
|
+
tablecell(content, flags) {
|
|
1832
|
+
const type = flags.header ? 'th' : 'td';
|
|
1833
|
+
const tag = flags.align
|
|
1834
|
+
? `<${type} align="${flags.align}">`
|
|
1835
|
+
: `<${type}>`;
|
|
1836
|
+
return tag + content + `</${type}>\n`;
|
|
1837
|
+
}
|
|
1838
|
+
/**
|
|
1839
|
+
* span level renderer
|
|
1840
|
+
*/
|
|
1841
|
+
strong(text) {
|
|
1842
|
+
return `<strong>${text}</strong>`;
|
|
1843
|
+
}
|
|
1844
|
+
em(text) {
|
|
1845
|
+
return `<em>${text}</em>`;
|
|
1846
|
+
}
|
|
1847
|
+
codespan(text) {
|
|
1848
|
+
return `<code>${text}</code>`;
|
|
1849
|
+
}
|
|
1850
|
+
br() {
|
|
1851
|
+
return this.options.xhtml ? '<br/>' : '<br>';
|
|
1852
|
+
}
|
|
1853
|
+
del(text) {
|
|
1854
|
+
return `<del>${text}</del>`;
|
|
1855
|
+
}
|
|
1856
|
+
link(href, title, text) {
|
|
1857
|
+
href = cleanUrl(this.options.sanitize, this.options.baseUrl, href);
|
|
1858
|
+
if (href === null) {
|
|
1859
|
+
return text;
|
|
1860
|
+
}
|
|
1861
|
+
let out = '<a href="' + href + '"';
|
|
1862
|
+
if (title) {
|
|
1863
|
+
out += ' title="' + title + '"';
|
|
1864
|
+
}
|
|
1865
|
+
out += '>' + text + '</a>';
|
|
1866
|
+
return out;
|
|
1867
|
+
}
|
|
1868
|
+
image(href, title, text) {
|
|
1869
|
+
href = cleanUrl(this.options.sanitize, this.options.baseUrl, href);
|
|
1870
|
+
if (href === null) {
|
|
1871
|
+
return text;
|
|
1872
|
+
}
|
|
1873
|
+
let out = `<img src="${href}" alt="${text}"`;
|
|
1874
|
+
if (title) {
|
|
1875
|
+
out += ` title="${title}"`;
|
|
1876
|
+
}
|
|
1877
|
+
out += this.options.xhtml ? '/>' : '>';
|
|
1878
|
+
return out;
|
|
1879
|
+
}
|
|
1880
|
+
text(text) {
|
|
1881
|
+
return text;
|
|
1882
|
+
}
|
|
1883
|
+
}
|
|
1654
1884
|
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
this.slugger = new _Slugger();
|
|
1664
|
-
}
|
|
1665
|
-
/**
|
|
1666
|
-
* Static Parse Method
|
|
1667
|
-
*/
|
|
1668
|
-
static parse(tokens, options2) {
|
|
1669
|
-
const parser2 = new __Parser(options2);
|
|
1670
|
-
return parser2.parse(tokens);
|
|
1671
|
-
}
|
|
1672
|
-
/**
|
|
1673
|
-
* Static Parse Inline Method
|
|
1674
|
-
*/
|
|
1675
|
-
static parseInline(tokens, options2) {
|
|
1676
|
-
const parser2 = new __Parser(options2);
|
|
1677
|
-
return parser2.parseInline(tokens);
|
|
1678
|
-
}
|
|
1679
|
-
/**
|
|
1680
|
-
* Parse Loop
|
|
1681
|
-
*/
|
|
1682
|
-
parse(tokens, top = true) {
|
|
1683
|
-
let out = "", i, j, k, l2, l3, row, cell, header, body, token, ordered, start, loose, itemBody, item, checked, task, checkbox, ret;
|
|
1684
|
-
const l = tokens.length;
|
|
1685
|
-
for (i = 0; i < l; i++) {
|
|
1686
|
-
token = tokens[i];
|
|
1687
|
-
if (this.options.extensions && this.options.extensions.renderers && this.options.extensions.renderers[token.type]) {
|
|
1688
|
-
ret = this.options.extensions.renderers[token.type].call({ parser: this }, token);
|
|
1689
|
-
if (ret !== false || !["space", "hr", "heading", "code", "table", "blockquote", "list", "html", "paragraph", "text"].includes(token.type)) {
|
|
1690
|
-
out += ret || "";
|
|
1691
|
-
continue;
|
|
1692
|
-
}
|
|
1693
|
-
}
|
|
1694
|
-
switch (token.type) {
|
|
1695
|
-
case "space": {
|
|
1696
|
-
continue;
|
|
1697
|
-
}
|
|
1698
|
-
case "hr": {
|
|
1699
|
-
out += this.renderer.hr();
|
|
1700
|
-
continue;
|
|
1701
|
-
}
|
|
1702
|
-
case "heading": {
|
|
1703
|
-
out += this.renderer.heading(
|
|
1704
|
-
this.parseInline(token.tokens),
|
|
1705
|
-
token.depth,
|
|
1706
|
-
unescape(this.parseInline(token.tokens, this.textRenderer)),
|
|
1707
|
-
this.slugger
|
|
1708
|
-
);
|
|
1709
|
-
continue;
|
|
1710
|
-
}
|
|
1711
|
-
case "code": {
|
|
1712
|
-
out += this.renderer.code(
|
|
1713
|
-
token.text,
|
|
1714
|
-
token.lang,
|
|
1715
|
-
!!token.escaped
|
|
1716
|
-
);
|
|
1717
|
-
continue;
|
|
1718
|
-
}
|
|
1719
|
-
case "table": {
|
|
1720
|
-
header = "";
|
|
1721
|
-
cell = "";
|
|
1722
|
-
l2 = token.header.length;
|
|
1723
|
-
for (j = 0; j < l2; j++) {
|
|
1724
|
-
cell += this.renderer.tablecell(
|
|
1725
|
-
this.parseInline(token.header[j].tokens),
|
|
1726
|
-
{ header: true, align: token.align[j] }
|
|
1727
|
-
);
|
|
1728
|
-
}
|
|
1729
|
-
header += this.renderer.tablerow(cell);
|
|
1730
|
-
body = "";
|
|
1731
|
-
l2 = token.rows.length;
|
|
1732
|
-
for (j = 0; j < l2; j++) {
|
|
1733
|
-
row = token.rows[j];
|
|
1734
|
-
cell = "";
|
|
1735
|
-
l3 = row.length;
|
|
1736
|
-
for (k = 0; k < l3; k++) {
|
|
1737
|
-
cell += this.renderer.tablecell(
|
|
1738
|
-
this.parseInline(row[k].tokens),
|
|
1739
|
-
{ header: false, align: token.align[k] }
|
|
1740
|
-
);
|
|
1741
|
-
}
|
|
1742
|
-
body += this.renderer.tablerow(cell);
|
|
1743
|
-
}
|
|
1744
|
-
out += this.renderer.table(header, body);
|
|
1745
|
-
continue;
|
|
1746
|
-
}
|
|
1747
|
-
case "blockquote": {
|
|
1748
|
-
body = this.parse(token.tokens);
|
|
1749
|
-
out += this.renderer.blockquote(body);
|
|
1750
|
-
continue;
|
|
1751
|
-
}
|
|
1752
|
-
case "list": {
|
|
1753
|
-
ordered = token.ordered;
|
|
1754
|
-
start = token.start;
|
|
1755
|
-
loose = token.loose;
|
|
1756
|
-
l2 = token.items.length;
|
|
1757
|
-
body = "";
|
|
1758
|
-
for (j = 0; j < l2; j++) {
|
|
1759
|
-
item = token.items[j];
|
|
1760
|
-
checked = item.checked;
|
|
1761
|
-
task = item.task;
|
|
1762
|
-
itemBody = "";
|
|
1763
|
-
if (item.task) {
|
|
1764
|
-
checkbox = this.renderer.checkbox(!!checked);
|
|
1765
|
-
if (loose) {
|
|
1766
|
-
if (item.tokens.length > 0 && item.tokens[0].type === "paragraph") {
|
|
1767
|
-
item.tokens[0].text = checkbox + " " + item.tokens[0].text;
|
|
1768
|
-
if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === "text") {
|
|
1769
|
-
item.tokens[0].tokens[0].text = checkbox + " " + item.tokens[0].tokens[0].text;
|
|
1770
|
-
}
|
|
1771
|
-
} else {
|
|
1772
|
-
item.tokens.unshift({
|
|
1773
|
-
type: "text",
|
|
1774
|
-
text: checkbox
|
|
1775
|
-
});
|
|
1776
|
-
}
|
|
1777
|
-
} else {
|
|
1778
|
-
itemBody += checkbox;
|
|
1779
|
-
}
|
|
1780
|
-
}
|
|
1781
|
-
itemBody += this.parse(item.tokens, loose);
|
|
1782
|
-
body += this.renderer.listitem(itemBody, task, !!checked);
|
|
1783
|
-
}
|
|
1784
|
-
out += this.renderer.list(body, ordered, start);
|
|
1785
|
-
continue;
|
|
1786
|
-
}
|
|
1787
|
-
case "html": {
|
|
1788
|
-
out += this.renderer.html(token.text, token.block);
|
|
1789
|
-
continue;
|
|
1790
|
-
}
|
|
1791
|
-
case "paragraph": {
|
|
1792
|
-
out += this.renderer.paragraph(this.parseInline(token.tokens));
|
|
1793
|
-
continue;
|
|
1794
|
-
}
|
|
1795
|
-
case "text": {
|
|
1796
|
-
body = token.tokens ? this.parseInline(token.tokens) : token.text;
|
|
1797
|
-
while (i + 1 < l && tokens[i + 1].type === "text") {
|
|
1798
|
-
token = tokens[++i];
|
|
1799
|
-
body += "\n" + (token.tokens ? this.parseInline(token.tokens) : token.text);
|
|
1800
|
-
}
|
|
1801
|
-
out += top ? this.renderer.paragraph(body) : body;
|
|
1802
|
-
continue;
|
|
1803
|
-
}
|
|
1804
|
-
default: {
|
|
1805
|
-
const errMsg = 'Token with "' + token.type + '" type was not found.';
|
|
1806
|
-
if (this.options.silent) {
|
|
1807
|
-
console.error(errMsg);
|
|
1808
|
-
return "";
|
|
1809
|
-
} else {
|
|
1810
|
-
throw new Error(errMsg);
|
|
1811
|
-
}
|
|
1812
|
-
}
|
|
1813
|
-
}
|
|
1885
|
+
/**
|
|
1886
|
+
* TextRenderer
|
|
1887
|
+
* returns only the textual part of the token
|
|
1888
|
+
*/
|
|
1889
|
+
class _TextRenderer {
|
|
1890
|
+
// no need for block level renderers
|
|
1891
|
+
strong(text) {
|
|
1892
|
+
return text;
|
|
1814
1893
|
}
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
/**
|
|
1818
|
-
* Parse Inline Tokens
|
|
1819
|
-
*/
|
|
1820
|
-
parseInline(tokens, renderer) {
|
|
1821
|
-
renderer = renderer || this.renderer;
|
|
1822
|
-
let out = "", i, token, ret;
|
|
1823
|
-
const l = tokens.length;
|
|
1824
|
-
for (i = 0; i < l; i++) {
|
|
1825
|
-
token = tokens[i];
|
|
1826
|
-
if (this.options.extensions && this.options.extensions.renderers && this.options.extensions.renderers[token.type]) {
|
|
1827
|
-
ret = this.options.extensions.renderers[token.type].call({ parser: this }, token);
|
|
1828
|
-
if (ret !== false || !["escape", "html", "link", "image", "strong", "em", "codespan", "br", "del", "text"].includes(token.type)) {
|
|
1829
|
-
out += ret || "";
|
|
1830
|
-
continue;
|
|
1831
|
-
}
|
|
1832
|
-
}
|
|
1833
|
-
switch (token.type) {
|
|
1834
|
-
case "escape": {
|
|
1835
|
-
out += renderer.text(token.text);
|
|
1836
|
-
break;
|
|
1837
|
-
}
|
|
1838
|
-
case "html": {
|
|
1839
|
-
out += renderer.html(token.text);
|
|
1840
|
-
break;
|
|
1841
|
-
}
|
|
1842
|
-
case "link": {
|
|
1843
|
-
out += renderer.link(token.href, token.title, this.parseInline(token.tokens, renderer));
|
|
1844
|
-
break;
|
|
1845
|
-
}
|
|
1846
|
-
case "image": {
|
|
1847
|
-
out += renderer.image(token.href, token.title, token.text);
|
|
1848
|
-
break;
|
|
1849
|
-
}
|
|
1850
|
-
case "strong": {
|
|
1851
|
-
out += renderer.strong(this.parseInline(token.tokens, renderer));
|
|
1852
|
-
break;
|
|
1853
|
-
}
|
|
1854
|
-
case "em": {
|
|
1855
|
-
out += renderer.em(this.parseInline(token.tokens, renderer));
|
|
1856
|
-
break;
|
|
1857
|
-
}
|
|
1858
|
-
case "codespan": {
|
|
1859
|
-
out += renderer.codespan(token.text);
|
|
1860
|
-
break;
|
|
1861
|
-
}
|
|
1862
|
-
case "br": {
|
|
1863
|
-
out += renderer.br();
|
|
1864
|
-
break;
|
|
1865
|
-
}
|
|
1866
|
-
case "del": {
|
|
1867
|
-
out += renderer.del(this.parseInline(token.tokens, renderer));
|
|
1868
|
-
break;
|
|
1869
|
-
}
|
|
1870
|
-
case "text": {
|
|
1871
|
-
out += renderer.text(token.text);
|
|
1872
|
-
break;
|
|
1873
|
-
}
|
|
1874
|
-
default: {
|
|
1875
|
-
const errMsg = 'Token with "' + token.type + '" type was not found.';
|
|
1876
|
-
if (this.options.silent) {
|
|
1877
|
-
console.error(errMsg);
|
|
1878
|
-
return "";
|
|
1879
|
-
} else {
|
|
1880
|
-
throw new Error(errMsg);
|
|
1881
|
-
}
|
|
1882
|
-
}
|
|
1883
|
-
}
|
|
1894
|
+
em(text) {
|
|
1895
|
+
return text;
|
|
1884
1896
|
}
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
}
|
|
1897
|
+
codespan(text) {
|
|
1898
|
+
return text;
|
|
1899
|
+
}
|
|
1900
|
+
del(text) {
|
|
1901
|
+
return text;
|
|
1902
|
+
}
|
|
1903
|
+
html(text) {
|
|
1904
|
+
return text;
|
|
1905
|
+
}
|
|
1906
|
+
text(text) {
|
|
1907
|
+
return text;
|
|
1908
|
+
}
|
|
1909
|
+
link(href, title, text) {
|
|
1910
|
+
return '' + text;
|
|
1911
|
+
}
|
|
1912
|
+
image(href, title, text) {
|
|
1913
|
+
return '' + text;
|
|
1914
|
+
}
|
|
1915
|
+
br() {
|
|
1916
|
+
return '';
|
|
1917
|
+
}
|
|
1918
|
+
}
|
|
1888
1919
|
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
}
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1920
|
+
/**
|
|
1921
|
+
* Slugger generates header id
|
|
1922
|
+
*/
|
|
1923
|
+
class _Slugger {
|
|
1924
|
+
seen;
|
|
1925
|
+
constructor() {
|
|
1926
|
+
this.seen = {};
|
|
1927
|
+
}
|
|
1928
|
+
serialize(value) {
|
|
1929
|
+
return value
|
|
1930
|
+
.toLowerCase()
|
|
1931
|
+
.trim()
|
|
1932
|
+
// remove html tags
|
|
1933
|
+
.replace(/<[!\/a-z].*?>/ig, '')
|
|
1934
|
+
// remove unwanted chars
|
|
1935
|
+
.replace(/[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~]/g, '')
|
|
1936
|
+
.replace(/\s/g, '-');
|
|
1937
|
+
}
|
|
1938
|
+
/**
|
|
1939
|
+
* Finds the next safe (unique) slug to use
|
|
1940
|
+
*/
|
|
1941
|
+
getNextSafeSlug(originalSlug, isDryRun) {
|
|
1942
|
+
let slug = originalSlug;
|
|
1943
|
+
let occurenceAccumulator = 0;
|
|
1944
|
+
if (this.seen.hasOwnProperty(slug)) {
|
|
1945
|
+
occurenceAccumulator = this.seen[originalSlug];
|
|
1946
|
+
do {
|
|
1947
|
+
occurenceAccumulator++;
|
|
1948
|
+
slug = originalSlug + '-' + occurenceAccumulator;
|
|
1949
|
+
} while (this.seen.hasOwnProperty(slug));
|
|
1950
|
+
}
|
|
1951
|
+
if (!isDryRun) {
|
|
1952
|
+
this.seen[originalSlug] = occurenceAccumulator;
|
|
1953
|
+
this.seen[slug] = 0;
|
|
1954
|
+
}
|
|
1955
|
+
return slug;
|
|
1956
|
+
}
|
|
1957
|
+
/**
|
|
1958
|
+
* Convert string to unique id
|
|
1959
|
+
*/
|
|
1960
|
+
slug(value, options = {}) {
|
|
1961
|
+
const slug = this.serialize(value);
|
|
1962
|
+
return this.getNextSafeSlug(slug, options.dryrun);
|
|
1963
|
+
}
|
|
1964
|
+
}
|
|
1911
1965
|
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
if (ret === false) {
|
|
2070
|
-
ret = prevHook.apply(hooks, args2);
|
|
2071
|
-
}
|
|
2072
|
-
return ret;
|
|
2073
|
-
};
|
|
2074
|
-
}
|
|
2075
|
-
}
|
|
2076
|
-
opts.hooks = hooks;
|
|
2077
|
-
}
|
|
2078
|
-
if (pack.walkTokens) {
|
|
2079
|
-
const walkTokens2 = this.defaults.walkTokens;
|
|
2080
|
-
opts.walkTokens = function(token) {
|
|
2081
|
-
let values = [];
|
|
2082
|
-
values.push(pack.walkTokens.call(this, token));
|
|
2083
|
-
if (walkTokens2) {
|
|
2084
|
-
values = values.concat(walkTokens2.call(this, token));
|
|
2085
|
-
}
|
|
2086
|
-
return values;
|
|
2087
|
-
};
|
|
2088
|
-
}
|
|
2089
|
-
this.defaults = { ...this.defaults, ...opts };
|
|
2090
|
-
});
|
|
2091
|
-
return this;
|
|
2092
|
-
}
|
|
2093
|
-
setOptions(opt) {
|
|
2094
|
-
this.defaults = { ...this.defaults, ...opt };
|
|
2095
|
-
return this;
|
|
2096
|
-
}
|
|
2097
|
-
};
|
|
2098
|
-
_parseMarkdown = new WeakSet();
|
|
2099
|
-
parseMarkdown_fn = function(lexer2, parser2) {
|
|
2100
|
-
return (src, optOrCallback, callback) => {
|
|
2101
|
-
if (typeof optOrCallback === "function") {
|
|
2102
|
-
callback = optOrCallback;
|
|
2103
|
-
optOrCallback = null;
|
|
2104
|
-
}
|
|
2105
|
-
const origOpt = { ...optOrCallback };
|
|
2106
|
-
const opt = { ...this.defaults, ...origOpt };
|
|
2107
|
-
const throwError = __privateMethod(this, _onError, onError_fn).call(this, !!opt.silent, !!opt.async, callback);
|
|
2108
|
-
if (typeof src === "undefined" || src === null) {
|
|
2109
|
-
return throwError(new Error("marked(): input parameter is undefined or null"));
|
|
2110
|
-
}
|
|
2111
|
-
if (typeof src !== "string") {
|
|
2112
|
-
return throwError(new Error("marked(): input parameter is of type " + Object.prototype.toString.call(src) + ", string expected"));
|
|
2113
|
-
}
|
|
2114
|
-
checkDeprecations(opt, callback);
|
|
2115
|
-
if (opt.hooks) {
|
|
2116
|
-
opt.hooks.options = opt;
|
|
1966
|
+
/**
|
|
1967
|
+
* Parsing & Compiling
|
|
1968
|
+
*/
|
|
1969
|
+
class _Parser {
|
|
1970
|
+
options;
|
|
1971
|
+
renderer;
|
|
1972
|
+
textRenderer;
|
|
1973
|
+
slugger;
|
|
1974
|
+
constructor(options) {
|
|
1975
|
+
this.options = options || exports.defaults;
|
|
1976
|
+
this.options.renderer = this.options.renderer || new _Renderer();
|
|
1977
|
+
this.renderer = this.options.renderer;
|
|
1978
|
+
this.renderer.options = this.options;
|
|
1979
|
+
this.textRenderer = new _TextRenderer();
|
|
1980
|
+
this.slugger = new _Slugger();
|
|
1981
|
+
}
|
|
1982
|
+
/**
|
|
1983
|
+
* Static Parse Method
|
|
1984
|
+
*/
|
|
1985
|
+
static parse(tokens, options) {
|
|
1986
|
+
const parser = new _Parser(options);
|
|
1987
|
+
return parser.parse(tokens);
|
|
1988
|
+
}
|
|
1989
|
+
/**
|
|
1990
|
+
* Static Parse Inline Method
|
|
1991
|
+
*/
|
|
1992
|
+
static parseInline(tokens, options) {
|
|
1993
|
+
const parser = new _Parser(options);
|
|
1994
|
+
return parser.parseInline(tokens);
|
|
1995
|
+
}
|
|
1996
|
+
/**
|
|
1997
|
+
* Parse Loop
|
|
1998
|
+
*/
|
|
1999
|
+
parse(tokens, top = true) {
|
|
2000
|
+
let out = '', i, j, k, l2, l3, row, cell, header, body, token, ordered, start, loose, itemBody, item, checked, task, checkbox, ret;
|
|
2001
|
+
const l = tokens.length;
|
|
2002
|
+
for (i = 0; i < l; i++) {
|
|
2003
|
+
token = tokens[i];
|
|
2004
|
+
// Run any renderer extensions
|
|
2005
|
+
if (this.options.extensions && this.options.extensions.renderers && this.options.extensions.renderers[token.type]) {
|
|
2006
|
+
ret = this.options.extensions.renderers[token.type].call({ parser: this }, token);
|
|
2007
|
+
if (ret !== false || !['space', 'hr', 'heading', 'code', 'table', 'blockquote', 'list', 'html', 'paragraph', 'text'].includes(token.type)) {
|
|
2008
|
+
out += ret || '';
|
|
2009
|
+
continue;
|
|
2010
|
+
}
|
|
2011
|
+
}
|
|
2012
|
+
switch (token.type) {
|
|
2013
|
+
case 'space': {
|
|
2014
|
+
continue;
|
|
2015
|
+
}
|
|
2016
|
+
case 'hr': {
|
|
2017
|
+
out += this.renderer.hr();
|
|
2018
|
+
continue;
|
|
2019
|
+
}
|
|
2020
|
+
case 'heading': {
|
|
2021
|
+
out += this.renderer.heading(this.parseInline(token.tokens), token.depth, unescape(this.parseInline(token.tokens, this.textRenderer)), this.slugger);
|
|
2022
|
+
continue;
|
|
2023
|
+
}
|
|
2024
|
+
case 'code': {
|
|
2025
|
+
out += this.renderer.code(token.text, token.lang, !!token.escaped);
|
|
2026
|
+
continue;
|
|
2027
|
+
}
|
|
2028
|
+
case 'table': {
|
|
2029
|
+
header = '';
|
|
2030
|
+
// header
|
|
2031
|
+
cell = '';
|
|
2032
|
+
l2 = token.header.length;
|
|
2033
|
+
for (j = 0; j < l2; j++) {
|
|
2034
|
+
cell += this.renderer.tablecell(this.parseInline(token.header[j].tokens), { header: true, align: token.align[j] });
|
|
2035
|
+
}
|
|
2036
|
+
header += this.renderer.tablerow(cell);
|
|
2037
|
+
body = '';
|
|
2038
|
+
l2 = token.rows.length;
|
|
2039
|
+
for (j = 0; j < l2; j++) {
|
|
2040
|
+
row = token.rows[j];
|
|
2041
|
+
cell = '';
|
|
2042
|
+
l3 = row.length;
|
|
2043
|
+
for (k = 0; k < l3; k++) {
|
|
2044
|
+
cell += this.renderer.tablecell(this.parseInline(row[k].tokens), { header: false, align: token.align[k] });
|
|
2045
|
+
}
|
|
2046
|
+
body += this.renderer.tablerow(cell);
|
|
2047
|
+
}
|
|
2048
|
+
out += this.renderer.table(header, body);
|
|
2049
|
+
continue;
|
|
2050
|
+
}
|
|
2051
|
+
case 'blockquote': {
|
|
2052
|
+
body = this.parse(token.tokens);
|
|
2053
|
+
out += this.renderer.blockquote(body);
|
|
2054
|
+
continue;
|
|
2055
|
+
}
|
|
2056
|
+
case 'list': {
|
|
2057
|
+
ordered = token.ordered;
|
|
2058
|
+
start = token.start;
|
|
2059
|
+
loose = token.loose;
|
|
2060
|
+
l2 = token.items.length;
|
|
2061
|
+
body = '';
|
|
2062
|
+
for (j = 0; j < l2; j++) {
|
|
2063
|
+
item = token.items[j];
|
|
2064
|
+
checked = item.checked;
|
|
2065
|
+
task = item.task;
|
|
2066
|
+
itemBody = '';
|
|
2067
|
+
if (item.task) {
|
|
2068
|
+
checkbox = this.renderer.checkbox(!!checked);
|
|
2069
|
+
if (loose) {
|
|
2070
|
+
if (item.tokens.length > 0 && item.tokens[0].type === 'paragraph') {
|
|
2071
|
+
item.tokens[0].text = checkbox + ' ' + item.tokens[0].text;
|
|
2072
|
+
if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') {
|
|
2073
|
+
item.tokens[0].tokens[0].text = checkbox + ' ' + item.tokens[0].tokens[0].text;
|
|
2074
|
+
}
|
|
2075
|
+
}
|
|
2076
|
+
else {
|
|
2077
|
+
item.tokens.unshift({
|
|
2078
|
+
type: 'text',
|
|
2079
|
+
text: checkbox
|
|
2080
|
+
});
|
|
2081
|
+
}
|
|
2082
|
+
}
|
|
2083
|
+
else {
|
|
2084
|
+
itemBody += checkbox;
|
|
2085
|
+
}
|
|
2086
|
+
}
|
|
2087
|
+
itemBody += this.parse(item.tokens, loose);
|
|
2088
|
+
body += this.renderer.listitem(itemBody, task, !!checked);
|
|
2089
|
+
}
|
|
2090
|
+
out += this.renderer.list(body, ordered, start);
|
|
2091
|
+
continue;
|
|
2092
|
+
}
|
|
2093
|
+
case 'html': {
|
|
2094
|
+
out += this.renderer.html(token.text, token.block);
|
|
2095
|
+
continue;
|
|
2096
|
+
}
|
|
2097
|
+
case 'paragraph': {
|
|
2098
|
+
out += this.renderer.paragraph(this.parseInline(token.tokens));
|
|
2099
|
+
continue;
|
|
2100
|
+
}
|
|
2101
|
+
case 'text': {
|
|
2102
|
+
body = token.tokens ? this.parseInline(token.tokens) : token.text;
|
|
2103
|
+
while (i + 1 < l && tokens[i + 1].type === 'text') {
|
|
2104
|
+
token = tokens[++i];
|
|
2105
|
+
body += '\n' + (token.tokens ? this.parseInline(token.tokens) : token.text);
|
|
2106
|
+
}
|
|
2107
|
+
out += top ? this.renderer.paragraph(body) : body;
|
|
2108
|
+
continue;
|
|
2109
|
+
}
|
|
2110
|
+
default: {
|
|
2111
|
+
const errMsg = 'Token with "' + token.type + '" type was not found.';
|
|
2112
|
+
if (this.options.silent) {
|
|
2113
|
+
console.error(errMsg);
|
|
2114
|
+
return '';
|
|
2115
|
+
}
|
|
2116
|
+
else {
|
|
2117
|
+
throw new Error(errMsg);
|
|
2118
|
+
}
|
|
2119
|
+
}
|
|
2120
|
+
}
|
|
2121
|
+
}
|
|
2122
|
+
return out;
|
|
2117
2123
|
}
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2124
|
+
/**
|
|
2125
|
+
* Parse Inline Tokens
|
|
2126
|
+
*/
|
|
2127
|
+
parseInline(tokens, renderer) {
|
|
2128
|
+
renderer = renderer || this.renderer;
|
|
2129
|
+
let out = '', i, token, ret;
|
|
2130
|
+
const l = tokens.length;
|
|
2131
|
+
for (i = 0; i < l; i++) {
|
|
2132
|
+
token = tokens[i];
|
|
2133
|
+
// Run any renderer extensions
|
|
2134
|
+
if (this.options.extensions && this.options.extensions.renderers && this.options.extensions.renderers[token.type]) {
|
|
2135
|
+
ret = this.options.extensions.renderers[token.type].call({ parser: this }, token);
|
|
2136
|
+
if (ret !== false || !['escape', 'html', 'link', 'image', 'strong', 'em', 'codespan', 'br', 'del', 'text'].includes(token.type)) {
|
|
2137
|
+
out += ret || '';
|
|
2138
|
+
continue;
|
|
2139
|
+
}
|
|
2140
|
+
}
|
|
2141
|
+
switch (token.type) {
|
|
2142
|
+
case 'escape': {
|
|
2143
|
+
out += renderer.text(token.text);
|
|
2144
|
+
break;
|
|
2145
|
+
}
|
|
2146
|
+
case 'html': {
|
|
2147
|
+
out += renderer.html(token.text);
|
|
2148
|
+
break;
|
|
2149
|
+
}
|
|
2150
|
+
case 'link': {
|
|
2151
|
+
out += renderer.link(token.href, token.title, this.parseInline(token.tokens, renderer));
|
|
2152
|
+
break;
|
|
2153
|
+
}
|
|
2154
|
+
case 'image': {
|
|
2155
|
+
out += renderer.image(token.href, token.title, token.text);
|
|
2156
|
+
break;
|
|
2157
|
+
}
|
|
2158
|
+
case 'strong': {
|
|
2159
|
+
out += renderer.strong(this.parseInline(token.tokens, renderer));
|
|
2160
|
+
break;
|
|
2161
|
+
}
|
|
2162
|
+
case 'em': {
|
|
2163
|
+
out += renderer.em(this.parseInline(token.tokens, renderer));
|
|
2164
|
+
break;
|
|
2165
|
+
}
|
|
2166
|
+
case 'codespan': {
|
|
2167
|
+
out += renderer.codespan(token.text);
|
|
2168
|
+
break;
|
|
2169
|
+
}
|
|
2170
|
+
case 'br': {
|
|
2171
|
+
out += renderer.br();
|
|
2172
|
+
break;
|
|
2173
|
+
}
|
|
2174
|
+
case 'del': {
|
|
2175
|
+
out += renderer.del(this.parseInline(token.tokens, renderer));
|
|
2176
|
+
break;
|
|
2177
|
+
}
|
|
2178
|
+
case 'text': {
|
|
2179
|
+
out += renderer.text(token.text);
|
|
2180
|
+
break;
|
|
2181
|
+
}
|
|
2182
|
+
default: {
|
|
2183
|
+
const errMsg = 'Token with "' + token.type + '" type was not found.';
|
|
2184
|
+
if (this.options.silent) {
|
|
2185
|
+
console.error(errMsg);
|
|
2186
|
+
return '';
|
|
2187
|
+
}
|
|
2188
|
+
else {
|
|
2189
|
+
throw new Error(errMsg);
|
|
2190
|
+
}
|
|
2191
|
+
}
|
|
2192
|
+
}
|
|
2172
2193
|
}
|
|
2173
|
-
|
|
2174
|
-
if (pending === 0) {
|
|
2175
|
-
done();
|
|
2176
|
-
}
|
|
2177
|
-
return;
|
|
2194
|
+
return out;
|
|
2178
2195
|
}
|
|
2179
|
-
|
|
2180
|
-
|
|
2196
|
+
}
|
|
2197
|
+
|
|
2198
|
+
class _Hooks {
|
|
2199
|
+
options;
|
|
2200
|
+
constructor(options) {
|
|
2201
|
+
this.options = options || exports.defaults;
|
|
2202
|
+
}
|
|
2203
|
+
static passThroughHooks = new Set([
|
|
2204
|
+
'preprocess',
|
|
2205
|
+
'postprocess'
|
|
2206
|
+
]);
|
|
2207
|
+
/**
|
|
2208
|
+
* Process markdown before marked
|
|
2209
|
+
*/
|
|
2210
|
+
preprocess(markdown) {
|
|
2211
|
+
return markdown;
|
|
2212
|
+
}
|
|
2213
|
+
/**
|
|
2214
|
+
* Process HTML after marked is finished
|
|
2215
|
+
*/
|
|
2216
|
+
postprocess(html) {
|
|
2217
|
+
return html;
|
|
2181
2218
|
}
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2219
|
+
}
|
|
2220
|
+
|
|
2221
|
+
class Marked {
|
|
2222
|
+
defaults = _getDefaults();
|
|
2223
|
+
options = this.setOptions;
|
|
2224
|
+
parse = this.#parseMarkdown(_Lexer.lex, _Parser.parse);
|
|
2225
|
+
parseInline = this.#parseMarkdown(_Lexer.lexInline, _Parser.parseInline);
|
|
2226
|
+
Parser = _Parser;
|
|
2227
|
+
parser = _Parser.parse;
|
|
2228
|
+
Renderer = _Renderer;
|
|
2229
|
+
TextRenderer = _TextRenderer;
|
|
2230
|
+
Lexer = _Lexer;
|
|
2231
|
+
lexer = _Lexer.lex;
|
|
2232
|
+
Tokenizer = _Tokenizer;
|
|
2233
|
+
Slugger = _Slugger;
|
|
2234
|
+
Hooks = _Hooks;
|
|
2235
|
+
constructor(...args) {
|
|
2236
|
+
this.use(...args);
|
|
2237
|
+
}
|
|
2238
|
+
/**
|
|
2239
|
+
* Run callback for every token
|
|
2240
|
+
*/
|
|
2241
|
+
walkTokens(tokens, callback) {
|
|
2242
|
+
let values = [];
|
|
2243
|
+
for (const token of tokens) {
|
|
2244
|
+
values = values.concat(callback.call(this, token));
|
|
2245
|
+
switch (token.type) {
|
|
2246
|
+
case 'table': {
|
|
2247
|
+
for (const cell of token.header) {
|
|
2248
|
+
values = values.concat(this.walkTokens(cell.tokens, callback));
|
|
2249
|
+
}
|
|
2250
|
+
for (const row of token.rows) {
|
|
2251
|
+
for (const cell of row) {
|
|
2252
|
+
values = values.concat(this.walkTokens(cell.tokens, callback));
|
|
2253
|
+
}
|
|
2254
|
+
}
|
|
2255
|
+
break;
|
|
2256
|
+
}
|
|
2257
|
+
case 'list': {
|
|
2258
|
+
values = values.concat(this.walkTokens(token.items, callback));
|
|
2259
|
+
break;
|
|
2260
|
+
}
|
|
2261
|
+
default: {
|
|
2262
|
+
if (this.defaults.extensions && this.defaults.extensions.childTokens && this.defaults.extensions.childTokens[token.type]) { // Walk any extensions
|
|
2263
|
+
this.defaults.extensions.childTokens[token.type].forEach((childTokens) => {
|
|
2264
|
+
// @ts-expect-error we assume token[childToken] is an array of tokens but we can't be sure
|
|
2265
|
+
values = values.concat(this.walkTokens(token[childTokens], callback));
|
|
2266
|
+
});
|
|
2267
|
+
}
|
|
2268
|
+
else if (token.tokens) {
|
|
2269
|
+
values = values.concat(this.walkTokens(token.tokens, callback));
|
|
2270
|
+
}
|
|
2271
|
+
}
|
|
2272
|
+
}
|
|
2273
|
+
}
|
|
2274
|
+
return values;
|
|
2275
|
+
}
|
|
2276
|
+
use(...args) {
|
|
2277
|
+
const extensions = this.defaults.extensions || { renderers: {}, childTokens: {} };
|
|
2278
|
+
args.forEach((pack) => {
|
|
2279
|
+
// copy options to new object
|
|
2280
|
+
const opts = { ...pack };
|
|
2281
|
+
// set async to true if it was set to true before
|
|
2282
|
+
opts.async = this.defaults.async || opts.async || false;
|
|
2283
|
+
// ==-- Parse "addon" extensions --== //
|
|
2284
|
+
if (pack.extensions) {
|
|
2285
|
+
pack.extensions.forEach((ext) => {
|
|
2286
|
+
if (!ext.name) {
|
|
2287
|
+
throw new Error('extension name required');
|
|
2288
|
+
}
|
|
2289
|
+
if ('renderer' in ext) { // Renderer extensions
|
|
2290
|
+
const prevRenderer = extensions.renderers[ext.name];
|
|
2291
|
+
if (prevRenderer) {
|
|
2292
|
+
// Replace extension with func to run new extension but fall back if false
|
|
2293
|
+
extensions.renderers[ext.name] = function (...args) {
|
|
2294
|
+
let ret = ext.renderer.apply(this, args);
|
|
2295
|
+
if (ret === false) {
|
|
2296
|
+
ret = prevRenderer.apply(this, args);
|
|
2297
|
+
}
|
|
2298
|
+
return ret;
|
|
2299
|
+
};
|
|
2300
|
+
}
|
|
2301
|
+
else {
|
|
2302
|
+
extensions.renderers[ext.name] = ext.renderer;
|
|
2303
|
+
}
|
|
2304
|
+
}
|
|
2305
|
+
if ('tokenizer' in ext) { // Tokenizer Extensions
|
|
2306
|
+
if (!ext.level || (ext.level !== 'block' && ext.level !== 'inline')) {
|
|
2307
|
+
throw new Error("extension level must be 'block' or 'inline'");
|
|
2308
|
+
}
|
|
2309
|
+
if (extensions[ext.level]) {
|
|
2310
|
+
extensions[ext.level].unshift(ext.tokenizer);
|
|
2311
|
+
}
|
|
2312
|
+
else {
|
|
2313
|
+
extensions[ext.level] = [ext.tokenizer];
|
|
2314
|
+
}
|
|
2315
|
+
if (ext.start) { // Function to check for start of token
|
|
2316
|
+
if (ext.level === 'block') {
|
|
2317
|
+
if (extensions.startBlock) {
|
|
2318
|
+
extensions.startBlock.push(ext.start);
|
|
2319
|
+
}
|
|
2320
|
+
else {
|
|
2321
|
+
extensions.startBlock = [ext.start];
|
|
2322
|
+
}
|
|
2323
|
+
}
|
|
2324
|
+
else if (ext.level === 'inline') {
|
|
2325
|
+
if (extensions.startInline) {
|
|
2326
|
+
extensions.startInline.push(ext.start);
|
|
2327
|
+
}
|
|
2328
|
+
else {
|
|
2329
|
+
extensions.startInline = [ext.start];
|
|
2330
|
+
}
|
|
2331
|
+
}
|
|
2332
|
+
}
|
|
2333
|
+
}
|
|
2334
|
+
if ('childTokens' in ext && ext.childTokens) { // Child tokens to be visited by walkTokens
|
|
2335
|
+
extensions.childTokens[ext.name] = ext.childTokens;
|
|
2336
|
+
}
|
|
2337
|
+
});
|
|
2338
|
+
opts.extensions = extensions;
|
|
2339
|
+
}
|
|
2340
|
+
// ==-- Parse "overwrite" extensions --== //
|
|
2341
|
+
if (pack.renderer) {
|
|
2342
|
+
const renderer = this.defaults.renderer || new _Renderer(this.defaults);
|
|
2343
|
+
for (const prop in pack.renderer) {
|
|
2344
|
+
const rendererFunc = pack.renderer[prop];
|
|
2345
|
+
const rendererKey = prop;
|
|
2346
|
+
const prevRenderer = renderer[rendererKey];
|
|
2347
|
+
// Replace renderer with func to run extension, but fall back if false
|
|
2348
|
+
renderer[rendererKey] = (...args) => {
|
|
2349
|
+
let ret = rendererFunc.apply(renderer, args);
|
|
2350
|
+
if (ret === false) {
|
|
2351
|
+
ret = prevRenderer.apply(renderer, args);
|
|
2352
|
+
}
|
|
2353
|
+
return ret || '';
|
|
2354
|
+
};
|
|
2355
|
+
}
|
|
2356
|
+
opts.renderer = renderer;
|
|
2357
|
+
}
|
|
2358
|
+
if (pack.tokenizer) {
|
|
2359
|
+
const tokenizer = this.defaults.tokenizer || new _Tokenizer(this.defaults);
|
|
2360
|
+
for (const prop in pack.tokenizer) {
|
|
2361
|
+
const tokenizerFunc = pack.tokenizer[prop];
|
|
2362
|
+
const tokenizerKey = prop;
|
|
2363
|
+
const prevTokenizer = tokenizer[tokenizerKey];
|
|
2364
|
+
// Replace tokenizer with func to run extension, but fall back if false
|
|
2365
|
+
tokenizer[tokenizerKey] = (...args) => {
|
|
2366
|
+
let ret = tokenizerFunc.apply(tokenizer, args);
|
|
2367
|
+
if (ret === false) {
|
|
2368
|
+
ret = prevTokenizer.apply(tokenizer, args);
|
|
2369
|
+
}
|
|
2370
|
+
return ret;
|
|
2371
|
+
};
|
|
2372
|
+
}
|
|
2373
|
+
opts.tokenizer = tokenizer;
|
|
2374
|
+
}
|
|
2375
|
+
// ==-- Parse Hooks extensions --== //
|
|
2376
|
+
if (pack.hooks) {
|
|
2377
|
+
const hooks = this.defaults.hooks || new _Hooks();
|
|
2378
|
+
for (const prop in pack.hooks) {
|
|
2379
|
+
const hooksFunc = pack.hooks[prop];
|
|
2380
|
+
const hooksKey = prop;
|
|
2381
|
+
const prevHook = hooks[hooksKey];
|
|
2382
|
+
if (_Hooks.passThroughHooks.has(prop)) {
|
|
2383
|
+
hooks[hooksKey] = (arg) => {
|
|
2384
|
+
if (this.defaults.async) {
|
|
2385
|
+
return Promise.resolve(hooksFunc.call(hooks, arg)).then(ret => {
|
|
2386
|
+
return prevHook.call(hooks, ret);
|
|
2387
|
+
});
|
|
2388
|
+
}
|
|
2389
|
+
const ret = hooksFunc.call(hooks, arg);
|
|
2390
|
+
return prevHook.call(hooks, ret);
|
|
2391
|
+
};
|
|
2392
|
+
}
|
|
2393
|
+
else {
|
|
2394
|
+
hooks[hooksKey] = (...args) => {
|
|
2395
|
+
let ret = hooksFunc.apply(hooks, args);
|
|
2396
|
+
if (ret === false) {
|
|
2397
|
+
ret = prevHook.apply(hooks, args);
|
|
2398
|
+
}
|
|
2399
|
+
return ret;
|
|
2400
|
+
};
|
|
2401
|
+
}
|
|
2402
|
+
}
|
|
2403
|
+
opts.hooks = hooks;
|
|
2404
|
+
}
|
|
2405
|
+
// ==-- Parse WalkTokens extensions --== //
|
|
2406
|
+
if (pack.walkTokens) {
|
|
2407
|
+
const walkTokens = this.defaults.walkTokens;
|
|
2408
|
+
opts.walkTokens = function (token) {
|
|
2409
|
+
let values = [];
|
|
2410
|
+
values.push(pack.walkTokens.call(this, token));
|
|
2411
|
+
if (walkTokens) {
|
|
2412
|
+
values = values.concat(walkTokens.call(this, token));
|
|
2413
|
+
}
|
|
2414
|
+
return values;
|
|
2415
|
+
};
|
|
2416
|
+
}
|
|
2417
|
+
this.defaults = { ...this.defaults, ...opts };
|
|
2418
|
+
});
|
|
2419
|
+
return this;
|
|
2214
2420
|
}
|
|
2215
|
-
|
|
2216
|
-
|
|
2421
|
+
setOptions(opt) {
|
|
2422
|
+
this.defaults = { ...this.defaults, ...opt };
|
|
2423
|
+
return this;
|
|
2217
2424
|
}
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2425
|
+
#parseMarkdown(lexer, parser) {
|
|
2426
|
+
return (src, optOrCallback, callback) => {
|
|
2427
|
+
if (typeof optOrCallback === 'function') {
|
|
2428
|
+
callback = optOrCallback;
|
|
2429
|
+
optOrCallback = null;
|
|
2430
|
+
}
|
|
2431
|
+
const origOpt = { ...optOrCallback };
|
|
2432
|
+
const opt = { ...this.defaults, ...origOpt };
|
|
2433
|
+
const throwError = this.#onError(!!opt.silent, !!opt.async, callback);
|
|
2434
|
+
// throw error in case of non string input
|
|
2435
|
+
if (typeof src === 'undefined' || src === null) {
|
|
2436
|
+
return throwError(new Error('marked(): input parameter is undefined or null'));
|
|
2437
|
+
}
|
|
2438
|
+
if (typeof src !== 'string') {
|
|
2439
|
+
return throwError(new Error('marked(): input parameter is of type '
|
|
2440
|
+
+ Object.prototype.toString.call(src) + ', string expected'));
|
|
2441
|
+
}
|
|
2442
|
+
checkDeprecations(opt, callback);
|
|
2443
|
+
if (opt.hooks) {
|
|
2444
|
+
opt.hooks.options = opt;
|
|
2445
|
+
}
|
|
2446
|
+
if (callback) {
|
|
2447
|
+
const highlight = opt.highlight;
|
|
2448
|
+
let tokens;
|
|
2449
|
+
try {
|
|
2450
|
+
if (opt.hooks) {
|
|
2451
|
+
src = opt.hooks.preprocess(src);
|
|
2452
|
+
}
|
|
2453
|
+
tokens = lexer(src, opt);
|
|
2454
|
+
}
|
|
2455
|
+
catch (e) {
|
|
2456
|
+
return throwError(e);
|
|
2457
|
+
}
|
|
2458
|
+
const done = (err) => {
|
|
2459
|
+
let out;
|
|
2460
|
+
if (!err) {
|
|
2461
|
+
try {
|
|
2462
|
+
if (opt.walkTokens) {
|
|
2463
|
+
this.walkTokens(tokens, opt.walkTokens);
|
|
2464
|
+
}
|
|
2465
|
+
out = parser(tokens, opt);
|
|
2466
|
+
if (opt.hooks) {
|
|
2467
|
+
out = opt.hooks.postprocess(out);
|
|
2468
|
+
}
|
|
2469
|
+
}
|
|
2470
|
+
catch (e) {
|
|
2471
|
+
err = e;
|
|
2472
|
+
}
|
|
2473
|
+
}
|
|
2474
|
+
opt.highlight = highlight;
|
|
2475
|
+
return err
|
|
2476
|
+
? throwError(err)
|
|
2477
|
+
: callback(null, out);
|
|
2478
|
+
};
|
|
2479
|
+
if (!highlight || highlight.length < 3) {
|
|
2480
|
+
return done();
|
|
2481
|
+
}
|
|
2482
|
+
delete opt.highlight;
|
|
2483
|
+
if (!tokens.length)
|
|
2484
|
+
return done();
|
|
2485
|
+
let pending = 0;
|
|
2486
|
+
this.walkTokens(tokens, (token) => {
|
|
2487
|
+
if (token.type === 'code') {
|
|
2488
|
+
pending++;
|
|
2489
|
+
setTimeout(() => {
|
|
2490
|
+
highlight(token.text, token.lang, (err, code) => {
|
|
2491
|
+
if (err) {
|
|
2492
|
+
return done(err);
|
|
2493
|
+
}
|
|
2494
|
+
if (code != null && code !== token.text) {
|
|
2495
|
+
token.text = code;
|
|
2496
|
+
token.escaped = true;
|
|
2497
|
+
}
|
|
2498
|
+
pending--;
|
|
2499
|
+
if (pending === 0) {
|
|
2500
|
+
done();
|
|
2501
|
+
}
|
|
2502
|
+
});
|
|
2503
|
+
}, 0);
|
|
2504
|
+
}
|
|
2505
|
+
});
|
|
2506
|
+
if (pending === 0) {
|
|
2507
|
+
done();
|
|
2508
|
+
}
|
|
2509
|
+
return;
|
|
2510
|
+
}
|
|
2511
|
+
if (opt.async) {
|
|
2512
|
+
return Promise.resolve(opt.hooks ? opt.hooks.preprocess(src) : src)
|
|
2513
|
+
.then(src => lexer(src, opt))
|
|
2514
|
+
.then(tokens => opt.walkTokens ? Promise.all(this.walkTokens(tokens, opt.walkTokens)).then(() => tokens) : tokens)
|
|
2515
|
+
.then(tokens => parser(tokens, opt))
|
|
2516
|
+
.then(html => opt.hooks ? opt.hooks.postprocess(html) : html)
|
|
2517
|
+
.catch(throwError);
|
|
2518
|
+
}
|
|
2519
|
+
try {
|
|
2520
|
+
if (opt.hooks) {
|
|
2521
|
+
src = opt.hooks.preprocess(src);
|
|
2522
|
+
}
|
|
2523
|
+
const tokens = lexer(src, opt);
|
|
2524
|
+
if (opt.walkTokens) {
|
|
2525
|
+
this.walkTokens(tokens, opt.walkTokens);
|
|
2526
|
+
}
|
|
2527
|
+
let html = parser(tokens, opt);
|
|
2528
|
+
if (opt.hooks) {
|
|
2529
|
+
html = opt.hooks.postprocess(html);
|
|
2530
|
+
}
|
|
2531
|
+
return html;
|
|
2532
|
+
}
|
|
2533
|
+
catch (e) {
|
|
2534
|
+
return throwError(e);
|
|
2535
|
+
}
|
|
2536
|
+
};
|
|
2221
2537
|
}
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2538
|
+
#onError(silent, async, callback) {
|
|
2539
|
+
return (e) => {
|
|
2540
|
+
e.message += '\nPlease report this to https://github.com/markedjs/marked.';
|
|
2541
|
+
if (silent) {
|
|
2542
|
+
const msg = '<p>An error occurred:</p><pre>'
|
|
2543
|
+
+ escape(e.message + '', true)
|
|
2544
|
+
+ '</pre>';
|
|
2545
|
+
if (async) {
|
|
2546
|
+
return Promise.resolve(msg);
|
|
2547
|
+
}
|
|
2548
|
+
if (callback) {
|
|
2549
|
+
callback(null, msg);
|
|
2550
|
+
return;
|
|
2551
|
+
}
|
|
2552
|
+
return msg;
|
|
2553
|
+
}
|
|
2554
|
+
if (async) {
|
|
2555
|
+
return Promise.reject(e);
|
|
2556
|
+
}
|
|
2557
|
+
if (callback) {
|
|
2558
|
+
callback(e);
|
|
2559
|
+
return;
|
|
2560
|
+
}
|
|
2561
|
+
throw e;
|
|
2562
|
+
};
|
|
2563
|
+
}
|
|
2564
|
+
}
|
|
2225
2565
|
|
|
2226
|
-
|
|
2227
|
-
var markedInstance = new Marked();
|
|
2566
|
+
const markedInstance = new Marked();
|
|
2228
2567
|
function marked(src, opt, callback) {
|
|
2229
|
-
|
|
2568
|
+
return markedInstance.parse(src, opt, callback);
|
|
2230
2569
|
}
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2570
|
+
/**
|
|
2571
|
+
* Sets the default options.
|
|
2572
|
+
*
|
|
2573
|
+
* @param options Hash of options
|
|
2574
|
+
*/
|
|
2575
|
+
marked.options =
|
|
2576
|
+
marked.setOptions = function (options) {
|
|
2577
|
+
markedInstance.setOptions(options);
|
|
2578
|
+
marked.defaults = markedInstance.defaults;
|
|
2579
|
+
changeDefaults(marked.defaults);
|
|
2580
|
+
return marked;
|
|
2581
|
+
};
|
|
2582
|
+
/**
|
|
2583
|
+
* Gets the original marked default options.
|
|
2584
|
+
*/
|
|
2237
2585
|
marked.getDefaults = _getDefaults;
|
|
2238
|
-
marked.defaults =
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2586
|
+
marked.defaults = exports.defaults;
|
|
2587
|
+
/**
|
|
2588
|
+
* Use Extension
|
|
2589
|
+
*/
|
|
2590
|
+
marked.use = function (...args) {
|
|
2591
|
+
markedInstance.use(...args);
|
|
2592
|
+
marked.defaults = markedInstance.defaults;
|
|
2593
|
+
changeDefaults(marked.defaults);
|
|
2594
|
+
return marked;
|
|
2244
2595
|
};
|
|
2245
|
-
|
|
2246
|
-
|
|
2596
|
+
/**
|
|
2597
|
+
* Run callback for every token
|
|
2598
|
+
*/
|
|
2599
|
+
marked.walkTokens = function (tokens, callback) {
|
|
2600
|
+
return markedInstance.walkTokens(tokens, callback);
|
|
2247
2601
|
};
|
|
2602
|
+
/**
|
|
2603
|
+
* Compiles markdown to HTML without enclosing `p` tag.
|
|
2604
|
+
*
|
|
2605
|
+
* @param src String of markdown source to be compiled
|
|
2606
|
+
* @param options Hash of options
|
|
2607
|
+
* @return String of compiled HTML
|
|
2608
|
+
*/
|
|
2248
2609
|
marked.parseInline = markedInstance.parseInline;
|
|
2610
|
+
/**
|
|
2611
|
+
* Expose
|
|
2612
|
+
*/
|
|
2249
2613
|
marked.Parser = _Parser;
|
|
2250
2614
|
marked.parser = _Parser.parse;
|
|
2251
2615
|
marked.Renderer = _Renderer;
|
|
2252
2616
|
marked.TextRenderer = _TextRenderer;
|
|
2253
|
-
marked.Lexer =
|
|
2254
|
-
marked.lexer =
|
|
2617
|
+
marked.Lexer = _Lexer;
|
|
2618
|
+
marked.lexer = _Lexer.lex;
|
|
2255
2619
|
marked.Tokenizer = _Tokenizer;
|
|
2256
2620
|
marked.Slugger = _Slugger;
|
|
2257
2621
|
marked.Hooks = _Hooks;
|
|
2258
2622
|
marked.parse = marked;
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
walkTokens
|
|
2288
|
-
});
|
|
2289
|
-
//# sourceMappingURL=marked.cjs.map
|
|
2623
|
+
const options = marked.options;
|
|
2624
|
+
const setOptions = marked.setOptions;
|
|
2625
|
+
const use = marked.use;
|
|
2626
|
+
const walkTokens = marked.walkTokens;
|
|
2627
|
+
const parseInline = marked.parseInline;
|
|
2628
|
+
const parse = marked;
|
|
2629
|
+
const parser = _Parser.parse;
|
|
2630
|
+
const lexer = _Lexer.lex;
|
|
2631
|
+
|
|
2632
|
+
exports.Hooks = _Hooks;
|
|
2633
|
+
exports.Lexer = _Lexer;
|
|
2634
|
+
exports.Marked = Marked;
|
|
2635
|
+
exports.Parser = _Parser;
|
|
2636
|
+
exports.Renderer = _Renderer;
|
|
2637
|
+
exports.Slugger = _Slugger;
|
|
2638
|
+
exports.TextRenderer = _TextRenderer;
|
|
2639
|
+
exports.Tokenizer = _Tokenizer;
|
|
2640
|
+
exports.getDefaults = _getDefaults;
|
|
2641
|
+
exports.lexer = lexer;
|
|
2642
|
+
exports.marked = marked;
|
|
2643
|
+
exports.options = options;
|
|
2644
|
+
exports.parse = parse;
|
|
2645
|
+
exports.parseInline = parseInline;
|
|
2646
|
+
exports.parser = parser;
|
|
2647
|
+
exports.setOptions = setOptions;
|
|
2648
|
+
exports.use = use;
|
|
2649
|
+
exports.walkTokens = walkTokens;
|
|
2650
|
+
//# sourceMappingURL=marked.cjs.map
|