@lvce-editor/markdown-worker 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/markdownWorkerMain.js +3882 -0
- package/package.json +9 -0
|
@@ -0,0 +1,3882 @@
|
|
|
1
|
+
const normalizeLine = line => {
|
|
2
|
+
if (line.startsWith('Error: ')) {
|
|
3
|
+
return line.slice('Error: '.length);
|
|
4
|
+
}
|
|
5
|
+
if (line.startsWith('VError: ')) {
|
|
6
|
+
return line.slice('VError: '.length);
|
|
7
|
+
}
|
|
8
|
+
return line;
|
|
9
|
+
};
|
|
10
|
+
const getCombinedMessage = (error, message) => {
|
|
11
|
+
const stringifiedError = normalizeLine(`${error}`);
|
|
12
|
+
if (message) {
|
|
13
|
+
return `${message}: ${stringifiedError}`;
|
|
14
|
+
}
|
|
15
|
+
return stringifiedError;
|
|
16
|
+
};
|
|
17
|
+
const NewLine$2 = '\n';
|
|
18
|
+
const getNewLineIndex$1 = (string, startIndex = undefined) => {
|
|
19
|
+
return string.indexOf(NewLine$2, startIndex);
|
|
20
|
+
};
|
|
21
|
+
const mergeStacks = (parent, child) => {
|
|
22
|
+
if (!child) {
|
|
23
|
+
return parent;
|
|
24
|
+
}
|
|
25
|
+
const parentNewLineIndex = getNewLineIndex$1(parent);
|
|
26
|
+
const childNewLineIndex = getNewLineIndex$1(child);
|
|
27
|
+
if (childNewLineIndex === -1) {
|
|
28
|
+
return parent;
|
|
29
|
+
}
|
|
30
|
+
const parentFirstLine = parent.slice(0, parentNewLineIndex);
|
|
31
|
+
const childRest = child.slice(childNewLineIndex);
|
|
32
|
+
const childFirstLine = normalizeLine(child.slice(0, childNewLineIndex));
|
|
33
|
+
if (parentFirstLine.includes(childFirstLine)) {
|
|
34
|
+
return parentFirstLine + childRest;
|
|
35
|
+
}
|
|
36
|
+
return child;
|
|
37
|
+
};
|
|
38
|
+
class VError extends Error {
|
|
39
|
+
constructor(error, message) {
|
|
40
|
+
const combinedMessage = getCombinedMessage(error, message);
|
|
41
|
+
super(combinedMessage);
|
|
42
|
+
this.name = 'VError';
|
|
43
|
+
if (error instanceof Error) {
|
|
44
|
+
this.stack = mergeStacks(this.stack, error.stack);
|
|
45
|
+
}
|
|
46
|
+
if (error.codeFrame) {
|
|
47
|
+
// @ts-ignore
|
|
48
|
+
this.codeFrame = error.codeFrame;
|
|
49
|
+
}
|
|
50
|
+
if (error.code) {
|
|
51
|
+
// @ts-ignore
|
|
52
|
+
this.code = error.code;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
class AssertionError extends Error {
|
|
58
|
+
constructor(message) {
|
|
59
|
+
super(message);
|
|
60
|
+
this.name = 'AssertionError';
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const getType = value => {
|
|
64
|
+
switch (typeof value) {
|
|
65
|
+
case 'number':
|
|
66
|
+
return 'number';
|
|
67
|
+
case 'function':
|
|
68
|
+
return 'function';
|
|
69
|
+
case 'string':
|
|
70
|
+
return 'string';
|
|
71
|
+
case 'object':
|
|
72
|
+
if (value === null) {
|
|
73
|
+
return 'null';
|
|
74
|
+
}
|
|
75
|
+
if (Array.isArray(value)) {
|
|
76
|
+
return 'array';
|
|
77
|
+
}
|
|
78
|
+
return 'object';
|
|
79
|
+
case 'boolean':
|
|
80
|
+
return 'boolean';
|
|
81
|
+
default:
|
|
82
|
+
return 'unknown';
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
const array = value => {
|
|
86
|
+
const type = getType(value);
|
|
87
|
+
if (type !== 'array') {
|
|
88
|
+
throw new AssertionError('expected value to be of type array');
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
const string = value => {
|
|
92
|
+
const type = getType(value);
|
|
93
|
+
if (type !== 'string') {
|
|
94
|
+
throw new AssertionError('expected value to be of type string');
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const isMessagePort = value => {
|
|
99
|
+
return value && value instanceof MessagePort;
|
|
100
|
+
};
|
|
101
|
+
const isMessagePortMain = value => {
|
|
102
|
+
return value && value.constructor && value.constructor.name === 'MessagePortMain';
|
|
103
|
+
};
|
|
104
|
+
const isOffscreenCanvas = value => {
|
|
105
|
+
return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
|
|
106
|
+
};
|
|
107
|
+
const isInstanceOf = (value, constructorName) => {
|
|
108
|
+
return value?.constructor?.name === constructorName;
|
|
109
|
+
};
|
|
110
|
+
const isSocket = value => {
|
|
111
|
+
return isInstanceOf(value, 'Socket');
|
|
112
|
+
};
|
|
113
|
+
const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
|
|
114
|
+
const isTransferrable = value => {
|
|
115
|
+
for (const fn of transferrables) {
|
|
116
|
+
if (fn(value)) {
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return false;
|
|
121
|
+
};
|
|
122
|
+
const walkValue = (value, transferrables, isTransferrable) => {
|
|
123
|
+
if (!value) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
if (isTransferrable(value)) {
|
|
127
|
+
transferrables.push(value);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
if (Array.isArray(value)) {
|
|
131
|
+
for (const item of value) {
|
|
132
|
+
walkValue(item, transferrables, isTransferrable);
|
|
133
|
+
}
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (typeof value === 'object') {
|
|
137
|
+
for (const property of Object.values(value)) {
|
|
138
|
+
walkValue(property, transferrables, isTransferrable);
|
|
139
|
+
}
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
const getTransferrables = value => {
|
|
144
|
+
const transferrables = [];
|
|
145
|
+
walkValue(value, transferrables, isTransferrable);
|
|
146
|
+
return transferrables;
|
|
147
|
+
};
|
|
148
|
+
const attachEvents = that => {
|
|
149
|
+
const handleMessage = (...args) => {
|
|
150
|
+
const data = that.getData(...args);
|
|
151
|
+
that.dispatchEvent(new MessageEvent('message', {
|
|
152
|
+
data
|
|
153
|
+
}));
|
|
154
|
+
};
|
|
155
|
+
that.onMessage(handleMessage);
|
|
156
|
+
const handleClose = event => {
|
|
157
|
+
that.dispatchEvent(new Event('close'));
|
|
158
|
+
};
|
|
159
|
+
that.onClose(handleClose);
|
|
160
|
+
};
|
|
161
|
+
class Ipc extends EventTarget {
|
|
162
|
+
constructor(rawIpc) {
|
|
163
|
+
super();
|
|
164
|
+
this._rawIpc = rawIpc;
|
|
165
|
+
attachEvents(this);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
|
|
169
|
+
const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
|
|
170
|
+
const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND';
|
|
171
|
+
const NewLine$1 = '\n';
|
|
172
|
+
const joinLines$1 = lines => {
|
|
173
|
+
return lines.join(NewLine$1);
|
|
174
|
+
};
|
|
175
|
+
const RE_AT = /^\s+at/;
|
|
176
|
+
const RE_AT_PROMISE_INDEX = /^\s*at async Promise.all \(index \d+\)$/;
|
|
177
|
+
const isNormalStackLine = line => {
|
|
178
|
+
return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line);
|
|
179
|
+
};
|
|
180
|
+
const getDetails = lines => {
|
|
181
|
+
const index = lines.findIndex(isNormalStackLine);
|
|
182
|
+
if (index === -1) {
|
|
183
|
+
return {
|
|
184
|
+
actualMessage: joinLines$1(lines),
|
|
185
|
+
rest: []
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
let lastIndex = index - 1;
|
|
189
|
+
while (++lastIndex < lines.length) {
|
|
190
|
+
if (!isNormalStackLine(lines[lastIndex])) {
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return {
|
|
195
|
+
actualMessage: lines[index - 1],
|
|
196
|
+
rest: lines.slice(index, lastIndex)
|
|
197
|
+
};
|
|
198
|
+
};
|
|
199
|
+
const splitLines$1 = lines => {
|
|
200
|
+
return lines.split(NewLine$1);
|
|
201
|
+
};
|
|
202
|
+
const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
|
|
203
|
+
const RE_MESSAGE_CODE_BLOCK_END = /^\s* at/;
|
|
204
|
+
const isMessageCodeBlockStartIndex = line => {
|
|
205
|
+
return RE_MESSAGE_CODE_BLOCK_START.test(line);
|
|
206
|
+
};
|
|
207
|
+
const isMessageCodeBlockEndIndex = line => {
|
|
208
|
+
return RE_MESSAGE_CODE_BLOCK_END.test(line);
|
|
209
|
+
};
|
|
210
|
+
const getMessageCodeBlock = stderr => {
|
|
211
|
+
const lines = splitLines$1(stderr);
|
|
212
|
+
const startIndex = lines.findIndex(isMessageCodeBlockStartIndex);
|
|
213
|
+
const endIndex = startIndex + lines.slice(startIndex).findIndex(isMessageCodeBlockEndIndex, startIndex);
|
|
214
|
+
const relevantLines = lines.slice(startIndex, endIndex);
|
|
215
|
+
const relevantMessage = relevantLines.join(' ').slice('Error: '.length);
|
|
216
|
+
return relevantMessage;
|
|
217
|
+
};
|
|
218
|
+
const isModuleNotFoundMessage = line => {
|
|
219
|
+
return line.includes('[ERR_MODULE_NOT_FOUND]');
|
|
220
|
+
};
|
|
221
|
+
const getModuleNotFoundError = stderr => {
|
|
222
|
+
const lines = splitLines$1(stderr);
|
|
223
|
+
const messageIndex = lines.findIndex(isModuleNotFoundMessage);
|
|
224
|
+
const message = lines[messageIndex];
|
|
225
|
+
return {
|
|
226
|
+
message,
|
|
227
|
+
code: ERR_MODULE_NOT_FOUND
|
|
228
|
+
};
|
|
229
|
+
};
|
|
230
|
+
const isModuleNotFoundError = stderr => {
|
|
231
|
+
if (!stderr) {
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
return stderr.includes('ERR_MODULE_NOT_FOUND');
|
|
235
|
+
};
|
|
236
|
+
const isModulesSyntaxError = stderr => {
|
|
237
|
+
if (!stderr) {
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
return stderr.includes('SyntaxError: Cannot use import statement outside a module');
|
|
241
|
+
};
|
|
242
|
+
const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/;
|
|
243
|
+
const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/;
|
|
244
|
+
const isUnhelpfulNativeModuleError = stderr => {
|
|
245
|
+
return RE_NATIVE_MODULE_ERROR.test(stderr) && RE_NATIVE_MODULE_ERROR_2.test(stderr);
|
|
246
|
+
};
|
|
247
|
+
const getNativeModuleErrorMessage = stderr => {
|
|
248
|
+
const message = getMessageCodeBlock(stderr);
|
|
249
|
+
return {
|
|
250
|
+
message: `Incompatible native node module: ${message}`,
|
|
251
|
+
code: E_INCOMPATIBLE_NATIVE_MODULE
|
|
252
|
+
};
|
|
253
|
+
};
|
|
254
|
+
const getModuleSyntaxError = () => {
|
|
255
|
+
return {
|
|
256
|
+
message: `ES Modules are not supported in electron`,
|
|
257
|
+
code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON
|
|
258
|
+
};
|
|
259
|
+
};
|
|
260
|
+
const getHelpfulChildProcessError = (stdout, stderr) => {
|
|
261
|
+
if (isUnhelpfulNativeModuleError(stderr)) {
|
|
262
|
+
return getNativeModuleErrorMessage(stderr);
|
|
263
|
+
}
|
|
264
|
+
if (isModulesSyntaxError(stderr)) {
|
|
265
|
+
return getModuleSyntaxError();
|
|
266
|
+
}
|
|
267
|
+
if (isModuleNotFoundError(stderr)) {
|
|
268
|
+
return getModuleNotFoundError(stderr);
|
|
269
|
+
}
|
|
270
|
+
const lines = splitLines$1(stderr);
|
|
271
|
+
const {
|
|
272
|
+
actualMessage,
|
|
273
|
+
rest
|
|
274
|
+
} = getDetails(lines);
|
|
275
|
+
return {
|
|
276
|
+
message: actualMessage,
|
|
277
|
+
code: '',
|
|
278
|
+
stack: rest
|
|
279
|
+
};
|
|
280
|
+
};
|
|
281
|
+
class IpcError extends VError {
|
|
282
|
+
// @ts-ignore
|
|
283
|
+
constructor(betterMessage, stdout = '', stderr = '') {
|
|
284
|
+
if (stdout || stderr) {
|
|
285
|
+
// @ts-ignore
|
|
286
|
+
const {
|
|
287
|
+
message,
|
|
288
|
+
code,
|
|
289
|
+
stack
|
|
290
|
+
} = getHelpfulChildProcessError(stdout, stderr);
|
|
291
|
+
const cause = new Error(message);
|
|
292
|
+
// @ts-ignore
|
|
293
|
+
cause.code = code;
|
|
294
|
+
cause.stack = stack;
|
|
295
|
+
super(cause, betterMessage);
|
|
296
|
+
} else {
|
|
297
|
+
super(betterMessage);
|
|
298
|
+
}
|
|
299
|
+
// @ts-ignore
|
|
300
|
+
this.name = 'IpcError';
|
|
301
|
+
// @ts-ignore
|
|
302
|
+
this.stdout = stdout;
|
|
303
|
+
// @ts-ignore
|
|
304
|
+
this.stderr = stderr;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
const readyMessage = 'ready';
|
|
308
|
+
const getData$2 = event => {
|
|
309
|
+
return event.data;
|
|
310
|
+
};
|
|
311
|
+
const listen$7 = () => {
|
|
312
|
+
// @ts-ignore
|
|
313
|
+
if (typeof WorkerGlobalScope === 'undefined') {
|
|
314
|
+
throw new TypeError('module is not in web worker scope');
|
|
315
|
+
}
|
|
316
|
+
return globalThis;
|
|
317
|
+
};
|
|
318
|
+
const signal$8 = global => {
|
|
319
|
+
global.postMessage(readyMessage);
|
|
320
|
+
};
|
|
321
|
+
class IpcChildWithModuleWorker extends Ipc {
|
|
322
|
+
getData(event) {
|
|
323
|
+
return getData$2(event);
|
|
324
|
+
}
|
|
325
|
+
send(message) {
|
|
326
|
+
// @ts-ignore
|
|
327
|
+
this._rawIpc.postMessage(message);
|
|
328
|
+
}
|
|
329
|
+
sendAndTransfer(message) {
|
|
330
|
+
const transfer = getTransferrables(message);
|
|
331
|
+
// @ts-ignore
|
|
332
|
+
this._rawIpc.postMessage(message, transfer);
|
|
333
|
+
}
|
|
334
|
+
dispose() {
|
|
335
|
+
// ignore
|
|
336
|
+
}
|
|
337
|
+
onClose(callback) {
|
|
338
|
+
// ignore
|
|
339
|
+
}
|
|
340
|
+
onMessage(callback) {
|
|
341
|
+
this._rawIpc.addEventListener('message', callback);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
const wrap$f = global => {
|
|
345
|
+
return new IpcChildWithModuleWorker(global);
|
|
346
|
+
};
|
|
347
|
+
const withResolvers = () => {
|
|
348
|
+
let _resolve;
|
|
349
|
+
const promise = new Promise(resolve => {
|
|
350
|
+
_resolve = resolve;
|
|
351
|
+
});
|
|
352
|
+
return {
|
|
353
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
354
|
+
resolve: _resolve,
|
|
355
|
+
promise
|
|
356
|
+
};
|
|
357
|
+
};
|
|
358
|
+
const waitForFirstMessage = async port => {
|
|
359
|
+
const {
|
|
360
|
+
resolve,
|
|
361
|
+
promise
|
|
362
|
+
} = withResolvers();
|
|
363
|
+
port.addEventListener('message', resolve, {
|
|
364
|
+
once: true
|
|
365
|
+
});
|
|
366
|
+
const event = await promise;
|
|
367
|
+
// @ts-ignore
|
|
368
|
+
return event.data;
|
|
369
|
+
};
|
|
370
|
+
const listen$6 = async () => {
|
|
371
|
+
const parentIpcRaw = listen$7();
|
|
372
|
+
signal$8(parentIpcRaw);
|
|
373
|
+
const parentIpc = wrap$f(parentIpcRaw);
|
|
374
|
+
const firstMessage = await waitForFirstMessage(parentIpc);
|
|
375
|
+
if (firstMessage.method !== 'initialize') {
|
|
376
|
+
throw new IpcError('unexpected first message');
|
|
377
|
+
}
|
|
378
|
+
const type = firstMessage.params[0];
|
|
379
|
+
if (type === 'message-port') {
|
|
380
|
+
parentIpc.send({
|
|
381
|
+
jsonrpc: '2.0',
|
|
382
|
+
id: firstMessage.id,
|
|
383
|
+
result: null
|
|
384
|
+
});
|
|
385
|
+
parentIpc.dispose();
|
|
386
|
+
const port = firstMessage.params[1];
|
|
387
|
+
return port;
|
|
388
|
+
}
|
|
389
|
+
return globalThis;
|
|
390
|
+
};
|
|
391
|
+
class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
|
|
392
|
+
getData(event) {
|
|
393
|
+
return getData$2(event);
|
|
394
|
+
}
|
|
395
|
+
send(message) {
|
|
396
|
+
this._rawIpc.postMessage(message);
|
|
397
|
+
}
|
|
398
|
+
sendAndTransfer(message) {
|
|
399
|
+
const transfer = getTransferrables(message);
|
|
400
|
+
this._rawIpc.postMessage(message, transfer);
|
|
401
|
+
}
|
|
402
|
+
dispose() {
|
|
403
|
+
if (this._rawIpc.close) {
|
|
404
|
+
this._rawIpc.close();
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
onClose(callback) {
|
|
408
|
+
// ignore
|
|
409
|
+
}
|
|
410
|
+
onMessage(callback) {
|
|
411
|
+
this._rawIpc.addEventListener('message', callback);
|
|
412
|
+
this._rawIpc.start();
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
const wrap$e = port => {
|
|
416
|
+
return new IpcChildWithModuleWorkerAndMessagePort(port);
|
|
417
|
+
};
|
|
418
|
+
const IpcChildWithModuleWorkerAndMessagePort$1 = {
|
|
419
|
+
__proto__: null,
|
|
420
|
+
listen: listen$6,
|
|
421
|
+
wrap: wrap$e
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
const Two = '2.0';
|
|
425
|
+
const create$4 = (method, params) => {
|
|
426
|
+
return {
|
|
427
|
+
jsonrpc: Two,
|
|
428
|
+
method,
|
|
429
|
+
params
|
|
430
|
+
};
|
|
431
|
+
};
|
|
432
|
+
const callbacks = Object.create(null);
|
|
433
|
+
const set$1 = (id, fn) => {
|
|
434
|
+
callbacks[id] = fn;
|
|
435
|
+
};
|
|
436
|
+
const get = id => {
|
|
437
|
+
return callbacks[id];
|
|
438
|
+
};
|
|
439
|
+
const remove = id => {
|
|
440
|
+
delete callbacks[id];
|
|
441
|
+
};
|
|
442
|
+
let id = 0;
|
|
443
|
+
const create$3 = () => {
|
|
444
|
+
return ++id;
|
|
445
|
+
};
|
|
446
|
+
const registerPromise = () => {
|
|
447
|
+
const id = create$3();
|
|
448
|
+
const {
|
|
449
|
+
resolve,
|
|
450
|
+
promise
|
|
451
|
+
} = Promise.withResolvers();
|
|
452
|
+
set$1(id, resolve);
|
|
453
|
+
return {
|
|
454
|
+
id,
|
|
455
|
+
promise
|
|
456
|
+
};
|
|
457
|
+
};
|
|
458
|
+
const create$2 = (method, params) => {
|
|
459
|
+
const {
|
|
460
|
+
id,
|
|
461
|
+
promise
|
|
462
|
+
} = registerPromise();
|
|
463
|
+
const message = {
|
|
464
|
+
jsonrpc: Two,
|
|
465
|
+
method,
|
|
466
|
+
params,
|
|
467
|
+
id
|
|
468
|
+
};
|
|
469
|
+
return {
|
|
470
|
+
message,
|
|
471
|
+
promise
|
|
472
|
+
};
|
|
473
|
+
};
|
|
474
|
+
class JsonRpcError extends Error {
|
|
475
|
+
constructor(message) {
|
|
476
|
+
super(message);
|
|
477
|
+
this.name = 'JsonRpcError';
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
const NewLine = '\n';
|
|
481
|
+
const DomException = 'DOMException';
|
|
482
|
+
const ReferenceError$1 = 'ReferenceError';
|
|
483
|
+
const SyntaxError$1 = 'SyntaxError';
|
|
484
|
+
const TypeError$1 = 'TypeError';
|
|
485
|
+
const getErrorConstructor = (message, type) => {
|
|
486
|
+
if (type) {
|
|
487
|
+
switch (type) {
|
|
488
|
+
case DomException:
|
|
489
|
+
return DOMException;
|
|
490
|
+
case TypeError$1:
|
|
491
|
+
return TypeError;
|
|
492
|
+
case SyntaxError$1:
|
|
493
|
+
return SyntaxError;
|
|
494
|
+
case ReferenceError$1:
|
|
495
|
+
return ReferenceError;
|
|
496
|
+
default:
|
|
497
|
+
return Error;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
if (message.startsWith('TypeError: ')) {
|
|
501
|
+
return TypeError;
|
|
502
|
+
}
|
|
503
|
+
if (message.startsWith('SyntaxError: ')) {
|
|
504
|
+
return SyntaxError;
|
|
505
|
+
}
|
|
506
|
+
if (message.startsWith('ReferenceError: ')) {
|
|
507
|
+
return ReferenceError;
|
|
508
|
+
}
|
|
509
|
+
return Error;
|
|
510
|
+
};
|
|
511
|
+
const constructError = (message, type, name) => {
|
|
512
|
+
const ErrorConstructor = getErrorConstructor(message, type);
|
|
513
|
+
if (ErrorConstructor === DOMException && name) {
|
|
514
|
+
return new ErrorConstructor(message, name);
|
|
515
|
+
}
|
|
516
|
+
if (ErrorConstructor === Error) {
|
|
517
|
+
const error = new Error(message);
|
|
518
|
+
if (name && name !== 'VError') {
|
|
519
|
+
error.name = name;
|
|
520
|
+
}
|
|
521
|
+
return error;
|
|
522
|
+
}
|
|
523
|
+
return new ErrorConstructor(message);
|
|
524
|
+
};
|
|
525
|
+
const getNewLineIndex = (string, startIndex = undefined) => {
|
|
526
|
+
return string.indexOf(NewLine, startIndex);
|
|
527
|
+
};
|
|
528
|
+
const getParentStack = error => {
|
|
529
|
+
let parentStack = error.stack || error.data || error.message || '';
|
|
530
|
+
if (parentStack.startsWith(' at')) {
|
|
531
|
+
parentStack = error.message + NewLine + parentStack;
|
|
532
|
+
}
|
|
533
|
+
return parentStack;
|
|
534
|
+
};
|
|
535
|
+
const joinLines = lines => {
|
|
536
|
+
return lines.join(NewLine);
|
|
537
|
+
};
|
|
538
|
+
const MethodNotFound = -32601;
|
|
539
|
+
const Custom = -32001;
|
|
540
|
+
const splitLines = lines => {
|
|
541
|
+
return lines.split(NewLine);
|
|
542
|
+
};
|
|
543
|
+
const restoreJsonRpcError = error => {
|
|
544
|
+
if (error && error instanceof Error) {
|
|
545
|
+
return error;
|
|
546
|
+
}
|
|
547
|
+
const currentStack = joinLines(splitLines(new Error().stack || '').slice(1));
|
|
548
|
+
if (error && error.code && error.code === MethodNotFound) {
|
|
549
|
+
const restoredError = new JsonRpcError(error.message);
|
|
550
|
+
const parentStack = getParentStack(error);
|
|
551
|
+
restoredError.stack = parentStack + NewLine + currentStack;
|
|
552
|
+
return restoredError;
|
|
553
|
+
}
|
|
554
|
+
if (error && error.message) {
|
|
555
|
+
const restoredError = constructError(error.message, error.type, error.name);
|
|
556
|
+
if (error.data) {
|
|
557
|
+
if (error.data.stack && error.data.type && error.message) {
|
|
558
|
+
restoredError.stack = error.data.type + ': ' + error.message + NewLine + error.data.stack + NewLine + currentStack;
|
|
559
|
+
} else if (error.data.stack) {
|
|
560
|
+
restoredError.stack = error.data.stack;
|
|
561
|
+
}
|
|
562
|
+
if (error.data.codeFrame) {
|
|
563
|
+
// @ts-ignore
|
|
564
|
+
restoredError.codeFrame = error.data.codeFrame;
|
|
565
|
+
}
|
|
566
|
+
if (error.data.code) {
|
|
567
|
+
// @ts-ignore
|
|
568
|
+
restoredError.code = error.data.code;
|
|
569
|
+
}
|
|
570
|
+
if (error.data.type) {
|
|
571
|
+
// @ts-ignore
|
|
572
|
+
restoredError.name = error.data.type;
|
|
573
|
+
}
|
|
574
|
+
} else {
|
|
575
|
+
if (error.stack) {
|
|
576
|
+
const lowerStack = restoredError.stack || '';
|
|
577
|
+
// @ts-ignore
|
|
578
|
+
const indexNewLine = getNewLineIndex(lowerStack);
|
|
579
|
+
const parentStack = getParentStack(error);
|
|
580
|
+
// @ts-ignore
|
|
581
|
+
restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
|
|
582
|
+
}
|
|
583
|
+
if (error.codeFrame) {
|
|
584
|
+
// @ts-ignore
|
|
585
|
+
restoredError.codeFrame = error.codeFrame;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
return restoredError;
|
|
589
|
+
}
|
|
590
|
+
if (typeof error === 'string') {
|
|
591
|
+
return new Error(`JsonRpc Error: ${error}`);
|
|
592
|
+
}
|
|
593
|
+
return new Error(`JsonRpc Error: ${error}`);
|
|
594
|
+
};
|
|
595
|
+
const unwrapJsonRpcResult = responseMessage => {
|
|
596
|
+
if ('error' in responseMessage) {
|
|
597
|
+
const restoredError = restoreJsonRpcError(responseMessage.error);
|
|
598
|
+
throw restoredError;
|
|
599
|
+
}
|
|
600
|
+
if ('result' in responseMessage) {
|
|
601
|
+
return responseMessage.result;
|
|
602
|
+
}
|
|
603
|
+
throw new JsonRpcError('unexpected response message');
|
|
604
|
+
};
|
|
605
|
+
const warn = (...args) => {
|
|
606
|
+
console.warn(...args);
|
|
607
|
+
};
|
|
608
|
+
const resolve = (id, response) => {
|
|
609
|
+
const fn = get(id);
|
|
610
|
+
if (!fn) {
|
|
611
|
+
console.log(response);
|
|
612
|
+
warn(`callback ${id} may already be disposed`);
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
fn(response);
|
|
616
|
+
remove(id);
|
|
617
|
+
};
|
|
618
|
+
const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
|
|
619
|
+
const getErrorType = prettyError => {
|
|
620
|
+
if (prettyError && prettyError.type) {
|
|
621
|
+
return prettyError.type;
|
|
622
|
+
}
|
|
623
|
+
if (prettyError && prettyError.constructor && prettyError.constructor.name) {
|
|
624
|
+
return prettyError.constructor.name;
|
|
625
|
+
}
|
|
626
|
+
return undefined;
|
|
627
|
+
};
|
|
628
|
+
const getErrorProperty = (error, prettyError) => {
|
|
629
|
+
if (error && error.code === E_COMMAND_NOT_FOUND) {
|
|
630
|
+
return {
|
|
631
|
+
code: MethodNotFound,
|
|
632
|
+
message: error.message,
|
|
633
|
+
data: error.stack
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
return {
|
|
637
|
+
code: Custom,
|
|
638
|
+
message: prettyError.message,
|
|
639
|
+
data: {
|
|
640
|
+
stack: prettyError.stack,
|
|
641
|
+
codeFrame: prettyError.codeFrame,
|
|
642
|
+
type: getErrorType(prettyError),
|
|
643
|
+
code: prettyError.code,
|
|
644
|
+
name: prettyError.name
|
|
645
|
+
}
|
|
646
|
+
};
|
|
647
|
+
};
|
|
648
|
+
const create$1 = (message, error) => {
|
|
649
|
+
return {
|
|
650
|
+
jsonrpc: Two,
|
|
651
|
+
id: message.id,
|
|
652
|
+
error
|
|
653
|
+
};
|
|
654
|
+
};
|
|
655
|
+
const getErrorResponse = (message, error, preparePrettyError, logError) => {
|
|
656
|
+
const prettyError = preparePrettyError(error);
|
|
657
|
+
logError(error, prettyError);
|
|
658
|
+
const errorProperty = getErrorProperty(error, prettyError);
|
|
659
|
+
return create$1(message, errorProperty);
|
|
660
|
+
};
|
|
661
|
+
const create$5 = (message, result) => {
|
|
662
|
+
return {
|
|
663
|
+
jsonrpc: Two,
|
|
664
|
+
id: message.id,
|
|
665
|
+
result: result ?? null
|
|
666
|
+
};
|
|
667
|
+
};
|
|
668
|
+
const getSuccessResponse = (message, result) => {
|
|
669
|
+
const resultProperty = result ?? null;
|
|
670
|
+
return create$5(message, resultProperty);
|
|
671
|
+
};
|
|
672
|
+
const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
|
|
673
|
+
try {
|
|
674
|
+
const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
|
|
675
|
+
return getSuccessResponse(message, result);
|
|
676
|
+
} catch (error) {
|
|
677
|
+
return getErrorResponse(message, error, preparePrettyError, logError);
|
|
678
|
+
}
|
|
679
|
+
};
|
|
680
|
+
const defaultPreparePrettyError = error => {
|
|
681
|
+
return error;
|
|
682
|
+
};
|
|
683
|
+
const defaultLogError = () => {
|
|
684
|
+
// ignore
|
|
685
|
+
};
|
|
686
|
+
const defaultRequiresSocket = () => {
|
|
687
|
+
return false;
|
|
688
|
+
};
|
|
689
|
+
const defaultResolve = resolve;
|
|
690
|
+
|
|
691
|
+
// TODO maybe remove this in v6 or v7, only accept options object to simplify the code
|
|
692
|
+
const normalizeParams = args => {
|
|
693
|
+
if (args.length === 1) {
|
|
694
|
+
const options = args[0];
|
|
695
|
+
return {
|
|
696
|
+
ipc: options.ipc,
|
|
697
|
+
message: options.message,
|
|
698
|
+
execute: options.execute,
|
|
699
|
+
resolve: options.resolve || defaultResolve,
|
|
700
|
+
preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
|
|
701
|
+
logError: options.logError || defaultLogError,
|
|
702
|
+
requiresSocket: options.requiresSocket || defaultRequiresSocket
|
|
703
|
+
};
|
|
704
|
+
}
|
|
705
|
+
return {
|
|
706
|
+
ipc: args[0],
|
|
707
|
+
message: args[1],
|
|
708
|
+
execute: args[2],
|
|
709
|
+
resolve: args[3],
|
|
710
|
+
preparePrettyError: args[4],
|
|
711
|
+
logError: args[5],
|
|
712
|
+
requiresSocket: args[6]
|
|
713
|
+
};
|
|
714
|
+
};
|
|
715
|
+
const handleJsonRpcMessage = async (...args) => {
|
|
716
|
+
const options = normalizeParams(args);
|
|
717
|
+
const {
|
|
718
|
+
message,
|
|
719
|
+
ipc,
|
|
720
|
+
execute,
|
|
721
|
+
resolve,
|
|
722
|
+
preparePrettyError,
|
|
723
|
+
logError,
|
|
724
|
+
requiresSocket
|
|
725
|
+
} = options;
|
|
726
|
+
if ('id' in message) {
|
|
727
|
+
if ('method' in message) {
|
|
728
|
+
const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
|
729
|
+
try {
|
|
730
|
+
ipc.send(response);
|
|
731
|
+
} catch (error) {
|
|
732
|
+
const errorResponse = getErrorResponse(message, error, preparePrettyError, logError);
|
|
733
|
+
ipc.send(errorResponse);
|
|
734
|
+
}
|
|
735
|
+
return;
|
|
736
|
+
}
|
|
737
|
+
resolve(message.id, message);
|
|
738
|
+
return;
|
|
739
|
+
}
|
|
740
|
+
if ('method' in message) {
|
|
741
|
+
await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
|
742
|
+
return;
|
|
743
|
+
}
|
|
744
|
+
throw new JsonRpcError('unexpected message');
|
|
745
|
+
};
|
|
746
|
+
const invokeHelper = async (ipc, method, params, useSendAndTransfer) => {
|
|
747
|
+
const {
|
|
748
|
+
message,
|
|
749
|
+
promise
|
|
750
|
+
} = create$2(method, params);
|
|
751
|
+
if (useSendAndTransfer && ipc.sendAndTransfer) {
|
|
752
|
+
ipc.sendAndTransfer(message);
|
|
753
|
+
} else {
|
|
754
|
+
ipc.send(message);
|
|
755
|
+
}
|
|
756
|
+
const responseMessage = await promise;
|
|
757
|
+
return unwrapJsonRpcResult(responseMessage);
|
|
758
|
+
};
|
|
759
|
+
const send = (transport, method, ...params) => {
|
|
760
|
+
const message = create$4(method, params);
|
|
761
|
+
transport.send(message);
|
|
762
|
+
};
|
|
763
|
+
const invoke = (ipc, method, ...params) => {
|
|
764
|
+
return invokeHelper(ipc, method, params, false);
|
|
765
|
+
};
|
|
766
|
+
const invokeAndTransfer = (ipc, method, ...params) => {
|
|
767
|
+
return invokeHelper(ipc, method, params, true);
|
|
768
|
+
};
|
|
769
|
+
|
|
770
|
+
const commands = Object.create(null);
|
|
771
|
+
const register = commandMap => {
|
|
772
|
+
Object.assign(commands, commandMap);
|
|
773
|
+
};
|
|
774
|
+
const getCommand = key => {
|
|
775
|
+
return commands[key];
|
|
776
|
+
};
|
|
777
|
+
const execute = (command, ...args) => {
|
|
778
|
+
const fn = getCommand(command);
|
|
779
|
+
if (!fn) {
|
|
780
|
+
throw new Error(`command not found ${command}`);
|
|
781
|
+
}
|
|
782
|
+
return fn(...args);
|
|
783
|
+
};
|
|
784
|
+
|
|
785
|
+
const createRpc = ipc => {
|
|
786
|
+
const rpc = {
|
|
787
|
+
// @ts-ignore
|
|
788
|
+
ipc,
|
|
789
|
+
/**
|
|
790
|
+
* @deprecated
|
|
791
|
+
*/
|
|
792
|
+
send(method, ...params) {
|
|
793
|
+
send(ipc, method, ...params);
|
|
794
|
+
},
|
|
795
|
+
invoke(method, ...params) {
|
|
796
|
+
return invoke(ipc, method, ...params);
|
|
797
|
+
},
|
|
798
|
+
invokeAndTransfer(method, ...params) {
|
|
799
|
+
return invokeAndTransfer(ipc, method, ...params);
|
|
800
|
+
}
|
|
801
|
+
};
|
|
802
|
+
return rpc;
|
|
803
|
+
};
|
|
804
|
+
const requiresSocket = () => {
|
|
805
|
+
return false;
|
|
806
|
+
};
|
|
807
|
+
const preparePrettyError = error => {
|
|
808
|
+
return error;
|
|
809
|
+
};
|
|
810
|
+
const logError = () => {
|
|
811
|
+
// handled by renderer worker
|
|
812
|
+
};
|
|
813
|
+
const handleMessage = event => {
|
|
814
|
+
const actualRequiresSocket = event?.target?.requiresSocket || requiresSocket;
|
|
815
|
+
const actualExecute = event?.target?.execute || execute;
|
|
816
|
+
return handleJsonRpcMessage(event.target, event.data, actualExecute, resolve, preparePrettyError, logError, actualRequiresSocket);
|
|
817
|
+
};
|
|
818
|
+
const handleIpc = ipc => {
|
|
819
|
+
if ('addEventListener' in ipc) {
|
|
820
|
+
ipc.addEventListener('message', handleMessage);
|
|
821
|
+
} else if ('on' in ipc) {
|
|
822
|
+
// deprecated
|
|
823
|
+
ipc.on('message', handleMessage);
|
|
824
|
+
}
|
|
825
|
+
};
|
|
826
|
+
const listen$1 = async (module, options) => {
|
|
827
|
+
const rawIpc = await module.listen(options);
|
|
828
|
+
if (module.signal) {
|
|
829
|
+
module.signal(rawIpc);
|
|
830
|
+
}
|
|
831
|
+
const ipc = module.wrap(rawIpc);
|
|
832
|
+
return ipc;
|
|
833
|
+
};
|
|
834
|
+
const create = async ({
|
|
835
|
+
commandMap
|
|
836
|
+
}) => {
|
|
837
|
+
// TODO create a commandMap per rpc instance
|
|
838
|
+
register(commandMap);
|
|
839
|
+
const ipc = await listen$1(IpcChildWithModuleWorkerAndMessagePort$1);
|
|
840
|
+
handleIpc(ipc);
|
|
841
|
+
const rpc = createRpc(ipc);
|
|
842
|
+
return rpc;
|
|
843
|
+
};
|
|
844
|
+
const WebWorkerRpcClient = {
|
|
845
|
+
__proto__: null,
|
|
846
|
+
create
|
|
847
|
+
};
|
|
848
|
+
|
|
849
|
+
const allowedMarkdownAttributes = ['src', 'id', 'className', 'title', 'alt', 'href', 'target', 'rel'];
|
|
850
|
+
|
|
851
|
+
const Document = 'document';
|
|
852
|
+
|
|
853
|
+
const Markdown = 'Markdown';
|
|
854
|
+
|
|
855
|
+
const HandleReadmeContextMenu = 'handleReadmeContextMenu';
|
|
856
|
+
|
|
857
|
+
const getVirtualDomChildCount = markdownDom => {
|
|
858
|
+
const max = markdownDom.length - 1;
|
|
859
|
+
let stack = [];
|
|
860
|
+
for (let i = max; i >= 0; i--) {
|
|
861
|
+
const element = markdownDom[i];
|
|
862
|
+
if (element.childCount > 0) {
|
|
863
|
+
stack = stack.slice(element.childCount);
|
|
864
|
+
}
|
|
865
|
+
stack.unshift(element);
|
|
866
|
+
}
|
|
867
|
+
return stack.length;
|
|
868
|
+
};
|
|
869
|
+
|
|
870
|
+
const Div$1 = 'div';
|
|
871
|
+
const H1$1 = 'h1';
|
|
872
|
+
const H2$1 = 'h2';
|
|
873
|
+
const H3$1 = 'h3';
|
|
874
|
+
const H4$1 = 'h4';
|
|
875
|
+
const H5$1 = 'h5';
|
|
876
|
+
const Img$1 = 'img';
|
|
877
|
+
const Span$1 = 'span';
|
|
878
|
+
const Article$1 = 'article';
|
|
879
|
+
const Aside$1 = 'aside';
|
|
880
|
+
const Footer$1 = 'footer';
|
|
881
|
+
const Header$1 = 'header';
|
|
882
|
+
const Nav$1 = 'nav';
|
|
883
|
+
const Section$1 = 'section';
|
|
884
|
+
const Search$1 = 'search';
|
|
885
|
+
const Dd$1 = 'dd';
|
|
886
|
+
const Dl$1 = 'dl';
|
|
887
|
+
const Figcaption$1 = 'figcaption';
|
|
888
|
+
const Figure$1 = 'figure';
|
|
889
|
+
const Hr$1 = 'hr';
|
|
890
|
+
const Li$1 = 'li';
|
|
891
|
+
const Ol$1 = 'ol';
|
|
892
|
+
const P$1 = 'p';
|
|
893
|
+
const Pre$1 = 'pre';
|
|
894
|
+
const A$1 = 'a';
|
|
895
|
+
const Abbr$1 = 'abbr';
|
|
896
|
+
const Br$1 = 'br';
|
|
897
|
+
const Cite$1 = 'cite';
|
|
898
|
+
const Data$1 = 'data';
|
|
899
|
+
const Time$1 = 'time';
|
|
900
|
+
const Tfoot$1 = 'tfoot';
|
|
901
|
+
|
|
902
|
+
const A = 53;
|
|
903
|
+
const Abbr = 54;
|
|
904
|
+
const Article = 27;
|
|
905
|
+
const Aside = 28;
|
|
906
|
+
const Br = 55;
|
|
907
|
+
const Cite = 56;
|
|
908
|
+
const Data = 57;
|
|
909
|
+
const Dd = 43;
|
|
910
|
+
const Div = 4;
|
|
911
|
+
const Dl = 44;
|
|
912
|
+
const Figcaption = 45;
|
|
913
|
+
const Figure = 46;
|
|
914
|
+
const Footer = 29;
|
|
915
|
+
const H1 = 5;
|
|
916
|
+
const H2 = 22;
|
|
917
|
+
const H3 = 23;
|
|
918
|
+
const H4 = 24;
|
|
919
|
+
const H5 = 25;
|
|
920
|
+
const Header = 30;
|
|
921
|
+
const Hr = 47;
|
|
922
|
+
const Img = 17;
|
|
923
|
+
const Li = 48;
|
|
924
|
+
const Nav = 40;
|
|
925
|
+
const Ol = 49;
|
|
926
|
+
const P = 50;
|
|
927
|
+
const Pre = 51;
|
|
928
|
+
const Search = 42;
|
|
929
|
+
const Section = 41;
|
|
930
|
+
const Span = 8;
|
|
931
|
+
const Text$1 = 12;
|
|
932
|
+
const Tfoot = 59;
|
|
933
|
+
const Time = 58;
|
|
934
|
+
|
|
935
|
+
const getVirtualDomTag = text => {
|
|
936
|
+
switch (text) {
|
|
937
|
+
case H1$1:
|
|
938
|
+
return H1;
|
|
939
|
+
case H2$1:
|
|
940
|
+
return H2;
|
|
941
|
+
case H3$1:
|
|
942
|
+
return H3;
|
|
943
|
+
case H4$1:
|
|
944
|
+
return H4;
|
|
945
|
+
case H5$1:
|
|
946
|
+
return H5;
|
|
947
|
+
case Div$1:
|
|
948
|
+
return Div;
|
|
949
|
+
case Article$1:
|
|
950
|
+
return Article;
|
|
951
|
+
case Aside$1:
|
|
952
|
+
return Aside;
|
|
953
|
+
case Footer$1:
|
|
954
|
+
return Footer;
|
|
955
|
+
case Header$1:
|
|
956
|
+
return Header;
|
|
957
|
+
case Nav$1:
|
|
958
|
+
return Nav;
|
|
959
|
+
case Section$1:
|
|
960
|
+
return Section;
|
|
961
|
+
case Search$1:
|
|
962
|
+
return Search;
|
|
963
|
+
case Dd$1:
|
|
964
|
+
return Dd;
|
|
965
|
+
case Dl$1:
|
|
966
|
+
return Dl;
|
|
967
|
+
case Figcaption$1:
|
|
968
|
+
return Figcaption;
|
|
969
|
+
case Figure$1:
|
|
970
|
+
return Figure;
|
|
971
|
+
case Hr$1:
|
|
972
|
+
return Hr;
|
|
973
|
+
case Li$1:
|
|
974
|
+
return Li;
|
|
975
|
+
case Ol$1:
|
|
976
|
+
return Ol;
|
|
977
|
+
case P$1:
|
|
978
|
+
return P;
|
|
979
|
+
case Pre$1:
|
|
980
|
+
return Pre;
|
|
981
|
+
case A$1:
|
|
982
|
+
return A;
|
|
983
|
+
case Abbr$1:
|
|
984
|
+
return Abbr;
|
|
985
|
+
case Br$1:
|
|
986
|
+
return Br;
|
|
987
|
+
case Cite$1:
|
|
988
|
+
return Cite;
|
|
989
|
+
case Data$1:
|
|
990
|
+
return Data;
|
|
991
|
+
case Time$1:
|
|
992
|
+
return Time;
|
|
993
|
+
case Tfoot$1:
|
|
994
|
+
return Tfoot;
|
|
995
|
+
case Img$1:
|
|
996
|
+
return Img;
|
|
997
|
+
case Span$1:
|
|
998
|
+
return Span;
|
|
999
|
+
default:
|
|
1000
|
+
return Div;
|
|
1001
|
+
}
|
|
1002
|
+
};
|
|
1003
|
+
|
|
1004
|
+
const None = 0;
|
|
1005
|
+
const OpeningAngleBracket = 1;
|
|
1006
|
+
const ClosingAngleBracket = 2;
|
|
1007
|
+
const TagNameStart = 3;
|
|
1008
|
+
const TagNameEnd = 4;
|
|
1009
|
+
const Content = 5;
|
|
1010
|
+
const ClosingTagSlash = 6;
|
|
1011
|
+
const WhitespaceInsideOpeningTag = 7;
|
|
1012
|
+
const AttributeName = 8;
|
|
1013
|
+
const AttributeEqualSign = 9;
|
|
1014
|
+
const AttributeQuoteStart = 10;
|
|
1015
|
+
const AttributeValue = 11;
|
|
1016
|
+
const AttributeQuoteEnd = 12;
|
|
1017
|
+
const WhitespaceAfterClosingTagSlash = 13;
|
|
1018
|
+
const WhitespaceAfterOpeningTagOpenAngleBracket = 14;
|
|
1019
|
+
const ExclamationMark = 15;
|
|
1020
|
+
const Doctype = 16;
|
|
1021
|
+
const StartCommentDashes = 17;
|
|
1022
|
+
const Comment = 18;
|
|
1023
|
+
const EndCommentTag = 19;
|
|
1024
|
+
const Text = 20;
|
|
1025
|
+
const CommentStart = 21;
|
|
1026
|
+
|
|
1027
|
+
const isSelfClosingTag = tag => {
|
|
1028
|
+
switch (tag) {
|
|
1029
|
+
case Img$1:
|
|
1030
|
+
return true;
|
|
1031
|
+
default:
|
|
1032
|
+
return false;
|
|
1033
|
+
}
|
|
1034
|
+
};
|
|
1035
|
+
|
|
1036
|
+
const parseText = text => {
|
|
1037
|
+
return text.replaceAll('>', '>').replaceAll('<', '<').replaceAll('&', '&');
|
|
1038
|
+
};
|
|
1039
|
+
|
|
1040
|
+
class UnexpectedTokenError extends Error {
|
|
1041
|
+
constructor() {
|
|
1042
|
+
super('Unexpected token');
|
|
1043
|
+
this.name = 'UnexpectedTokenError';
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
const State = {
|
|
1048
|
+
TopLevelContent: 1,
|
|
1049
|
+
AfterOpeningAngleBracket: 2,
|
|
1050
|
+
InsideOpeningTag: 3,
|
|
1051
|
+
AfterClosingTagSlash: 4,
|
|
1052
|
+
AfterClosingTagName: 5,
|
|
1053
|
+
InsideOpeningTagAfterWhitespace: 6,
|
|
1054
|
+
AfterAttributeName: 7,
|
|
1055
|
+
AfterAttributeEqualSign: 8,
|
|
1056
|
+
InsideAttributeAfterDoubleQuote: 9,
|
|
1057
|
+
AfterAttributeValueInsideDoubleQuote: 10,
|
|
1058
|
+
AfterAttributeValueClosingQuote: 11,
|
|
1059
|
+
AfterExclamationMark: 16,
|
|
1060
|
+
InsideComment: 17
|
|
1061
|
+
};
|
|
1062
|
+
const RE_ANGLE_BRACKET_OPEN = /^</;
|
|
1063
|
+
const RE_ANGLE_BRACKET_OPEN_TAG = /^<(?![\s!%])/;
|
|
1064
|
+
const RE_ANGLE_BRACKET_CLOSE = /^>/;
|
|
1065
|
+
const RE_SLASH = /^\//;
|
|
1066
|
+
const RE_TAGNAME = /^[a-zA-Z\d$]+/;
|
|
1067
|
+
const RE_CONTENT = /^[^<>]+/;
|
|
1068
|
+
const RE_WHITESPACE = /^\s+/;
|
|
1069
|
+
const RE_ATTRIBUTE_NAME = /^[a-zA-Z\d-]+/;
|
|
1070
|
+
const RE_EQUAL_SIGN = /^=/;
|
|
1071
|
+
const RE_DOUBLE_QUOTE = /^"/;
|
|
1072
|
+
const RE_ATTRIBUTE_VALUE_INSIDE_DOUBLE_QUOTE = /^[^"\n]+/;
|
|
1073
|
+
const RE_TEXT = /^[^<>]+/;
|
|
1074
|
+
const RE_EXCLAMATION_MARK = /^!/;
|
|
1075
|
+
const RE_DASH_DASH = /^--/;
|
|
1076
|
+
const RE_DOCTYPE = /^doctype/i;
|
|
1077
|
+
const RE_BLOCK_COMMENT_CONTENT = /^[a-zA-Z\s]+/;
|
|
1078
|
+
const RE_COMMENT_END = /^-->/;
|
|
1079
|
+
const RE_TAG_TEXT = /^[^\s>]+/;
|
|
1080
|
+
const RE_ANY_TEXT = /^[^\n]+/;
|
|
1081
|
+
const RE_BLOCK_COMMENT_START = /^<!--/;
|
|
1082
|
+
const RE_SELF_CLOSING = /^\/>/;
|
|
1083
|
+
const tokenizeHtml = text => {
|
|
1084
|
+
string(text);
|
|
1085
|
+
let state = State.TopLevelContent;
|
|
1086
|
+
let index = 0;
|
|
1087
|
+
let next;
|
|
1088
|
+
const tokens = [];
|
|
1089
|
+
let token = None;
|
|
1090
|
+
while (index < text.length) {
|
|
1091
|
+
const part = text.slice(index);
|
|
1092
|
+
switch (state) {
|
|
1093
|
+
case State.TopLevelContent:
|
|
1094
|
+
if (next = part.match(RE_ANGLE_BRACKET_OPEN_TAG)) {
|
|
1095
|
+
token = OpeningAngleBracket;
|
|
1096
|
+
state = State.AfterOpeningAngleBracket;
|
|
1097
|
+
} else if (next = part.match(RE_CONTENT)) {
|
|
1098
|
+
token = Content;
|
|
1099
|
+
state = State.TopLevelContent;
|
|
1100
|
+
} else if (next = part.match(RE_BLOCK_COMMENT_START)) {
|
|
1101
|
+
token = CommentStart;
|
|
1102
|
+
state = State.InsideComment;
|
|
1103
|
+
} else if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
|
|
1104
|
+
token = Content;
|
|
1105
|
+
state = State.TopLevelContent;
|
|
1106
|
+
} else if (next = part.match(RE_ANGLE_BRACKET_OPEN)) {
|
|
1107
|
+
token = Text;
|
|
1108
|
+
state = State.TopLevelContent;
|
|
1109
|
+
} else {
|
|
1110
|
+
throw new UnexpectedTokenError();
|
|
1111
|
+
}
|
|
1112
|
+
break;
|
|
1113
|
+
case State.AfterOpeningAngleBracket:
|
|
1114
|
+
if (next = part.match(RE_TAGNAME)) {
|
|
1115
|
+
token = TagNameStart;
|
|
1116
|
+
state = State.InsideOpeningTag;
|
|
1117
|
+
} else if (next = part.match(RE_SLASH)) {
|
|
1118
|
+
token = ClosingTagSlash;
|
|
1119
|
+
state = State.AfterClosingTagSlash;
|
|
1120
|
+
} else if (next = part.match(RE_WHITESPACE)) {
|
|
1121
|
+
token = WhitespaceAfterOpeningTagOpenAngleBracket;
|
|
1122
|
+
state = State.TopLevelContent;
|
|
1123
|
+
} else if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
|
|
1124
|
+
token = ClosingAngleBracket;
|
|
1125
|
+
state = State.TopLevelContent;
|
|
1126
|
+
} else if (next = part.match(RE_EXCLAMATION_MARK)) {
|
|
1127
|
+
token = ExclamationMark;
|
|
1128
|
+
state = State.AfterExclamationMark;
|
|
1129
|
+
} else if (next = part.match(RE_ANY_TEXT)) {
|
|
1130
|
+
token = Text;
|
|
1131
|
+
state = State.TopLevelContent;
|
|
1132
|
+
} else {
|
|
1133
|
+
text.slice(index); // ?
|
|
1134
|
+
throw new UnexpectedTokenError();
|
|
1135
|
+
}
|
|
1136
|
+
break;
|
|
1137
|
+
case State.AfterExclamationMark:
|
|
1138
|
+
if (next = part.match(RE_DASH_DASH)) {
|
|
1139
|
+
token = StartCommentDashes;
|
|
1140
|
+
state = State.InsideComment;
|
|
1141
|
+
} else if (next = part.match(RE_DOCTYPE)) {
|
|
1142
|
+
token = Doctype;
|
|
1143
|
+
state = State.InsideOpeningTag;
|
|
1144
|
+
} else {
|
|
1145
|
+
text.slice(index); // ?
|
|
1146
|
+
throw new UnexpectedTokenError();
|
|
1147
|
+
}
|
|
1148
|
+
break;
|
|
1149
|
+
case State.InsideComment:
|
|
1150
|
+
if (next = part.match(RE_BLOCK_COMMENT_CONTENT)) {
|
|
1151
|
+
token = Comment;
|
|
1152
|
+
state = State.InsideComment;
|
|
1153
|
+
} else if (next = part.match(RE_COMMENT_END)) {
|
|
1154
|
+
token = EndCommentTag;
|
|
1155
|
+
state = State.TopLevelContent;
|
|
1156
|
+
} else {
|
|
1157
|
+
text.slice(index); // ?
|
|
1158
|
+
throw new UnexpectedTokenError();
|
|
1159
|
+
}
|
|
1160
|
+
break;
|
|
1161
|
+
case State.InsideOpeningTag:
|
|
1162
|
+
if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
|
|
1163
|
+
token = ClosingAngleBracket;
|
|
1164
|
+
state = State.TopLevelContent;
|
|
1165
|
+
} else if (next = part.match(RE_WHITESPACE)) {
|
|
1166
|
+
token = WhitespaceInsideOpeningTag;
|
|
1167
|
+
state = State.InsideOpeningTagAfterWhitespace;
|
|
1168
|
+
} else if (next = part.match(RE_TAG_TEXT)) {
|
|
1169
|
+
token = Text;
|
|
1170
|
+
state = State.TopLevelContent;
|
|
1171
|
+
} else {
|
|
1172
|
+
throw new UnexpectedTokenError();
|
|
1173
|
+
}
|
|
1174
|
+
break;
|
|
1175
|
+
case State.InsideOpeningTagAfterWhitespace:
|
|
1176
|
+
if (next = part.match(RE_ATTRIBUTE_NAME)) {
|
|
1177
|
+
token = AttributeName;
|
|
1178
|
+
state = State.AfterAttributeName;
|
|
1179
|
+
} else if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
|
|
1180
|
+
token = ClosingAngleBracket;
|
|
1181
|
+
state = State.TopLevelContent;
|
|
1182
|
+
} else if (next = part.match(RE_SELF_CLOSING)) {
|
|
1183
|
+
token = ClosingAngleBracket;
|
|
1184
|
+
state = State.TopLevelContent;
|
|
1185
|
+
} else if (next = part.match(RE_TEXT)) {
|
|
1186
|
+
token = AttributeName;
|
|
1187
|
+
state = State.AfterAttributeName;
|
|
1188
|
+
} else {
|
|
1189
|
+
text.slice(index).match(RE_TEXT); // ?
|
|
1190
|
+
text.slice(index); // ?
|
|
1191
|
+
throw new UnexpectedTokenError();
|
|
1192
|
+
}
|
|
1193
|
+
break;
|
|
1194
|
+
case State.AfterAttributeName:
|
|
1195
|
+
if (next = part.match(RE_EQUAL_SIGN)) {
|
|
1196
|
+
token = AttributeEqualSign;
|
|
1197
|
+
state = State.AfterAttributeEqualSign;
|
|
1198
|
+
} else if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
|
|
1199
|
+
token = ClosingAngleBracket;
|
|
1200
|
+
state = State.TopLevelContent;
|
|
1201
|
+
} else if (next = part.match(RE_WHITESPACE)) {
|
|
1202
|
+
token = WhitespaceInsideOpeningTag;
|
|
1203
|
+
state = State.InsideOpeningTagAfterWhitespace;
|
|
1204
|
+
} else if (next = part.match(RE_ANGLE_BRACKET_OPEN)) {
|
|
1205
|
+
token = OpeningAngleBracket;
|
|
1206
|
+
state = State.AfterOpeningAngleBracket;
|
|
1207
|
+
} else {
|
|
1208
|
+
text.slice(index); // ?
|
|
1209
|
+
throw new UnexpectedTokenError();
|
|
1210
|
+
}
|
|
1211
|
+
break;
|
|
1212
|
+
case State.AfterAttributeEqualSign:
|
|
1213
|
+
if (next = part.match(RE_DOUBLE_QUOTE)) {
|
|
1214
|
+
token = AttributeQuoteStart;
|
|
1215
|
+
state = State.InsideAttributeAfterDoubleQuote;
|
|
1216
|
+
} else if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
|
|
1217
|
+
token = ClosingAngleBracket;
|
|
1218
|
+
state = State.TopLevelContent;
|
|
1219
|
+
} else {
|
|
1220
|
+
throw new UnexpectedTokenError();
|
|
1221
|
+
}
|
|
1222
|
+
break;
|
|
1223
|
+
case State.InsideAttributeAfterDoubleQuote:
|
|
1224
|
+
if (next = text.slice(index).match(RE_ATTRIBUTE_VALUE_INSIDE_DOUBLE_QUOTE)) {
|
|
1225
|
+
token = AttributeValue;
|
|
1226
|
+
state = State.AfterAttributeValueInsideDoubleQuote;
|
|
1227
|
+
} else if (next = part.match(RE_DOUBLE_QUOTE)) {
|
|
1228
|
+
token = AttributeQuoteEnd;
|
|
1229
|
+
state = State.AfterAttributeValueClosingQuote;
|
|
1230
|
+
} else {
|
|
1231
|
+
throw new UnexpectedTokenError();
|
|
1232
|
+
}
|
|
1233
|
+
break;
|
|
1234
|
+
case State.AfterAttributeValueInsideDoubleQuote:
|
|
1235
|
+
if (next = part.match(RE_DOUBLE_QUOTE)) {
|
|
1236
|
+
token = AttributeQuoteEnd;
|
|
1237
|
+
state = State.AfterAttributeValueClosingQuote;
|
|
1238
|
+
} else {
|
|
1239
|
+
throw new UnexpectedTokenError();
|
|
1240
|
+
}
|
|
1241
|
+
break;
|
|
1242
|
+
case State.AfterAttributeValueClosingQuote:
|
|
1243
|
+
if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
|
|
1244
|
+
token = ClosingAngleBracket;
|
|
1245
|
+
state = State.TopLevelContent;
|
|
1246
|
+
} else if (next = part.match(RE_WHITESPACE)) {
|
|
1247
|
+
token = WhitespaceInsideOpeningTag;
|
|
1248
|
+
state = State.InsideOpeningTagAfterWhitespace;
|
|
1249
|
+
} else if (next = part.match(RE_SELF_CLOSING)) {
|
|
1250
|
+
token = ClosingAngleBracket;
|
|
1251
|
+
state = State.TopLevelContent;
|
|
1252
|
+
} else {
|
|
1253
|
+
throw new UnexpectedTokenError();
|
|
1254
|
+
}
|
|
1255
|
+
break;
|
|
1256
|
+
case State.AfterClosingTagSlash:
|
|
1257
|
+
if (next = part.match(RE_TAGNAME)) {
|
|
1258
|
+
token = TagNameEnd;
|
|
1259
|
+
state = State.AfterClosingTagName;
|
|
1260
|
+
} else if (next = part.match(RE_WHITESPACE)) {
|
|
1261
|
+
token = WhitespaceAfterClosingTagSlash;
|
|
1262
|
+
state = State.TopLevelContent;
|
|
1263
|
+
} else if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
|
|
1264
|
+
token = ClosingAngleBracket;
|
|
1265
|
+
state = State.TopLevelContent;
|
|
1266
|
+
} else {
|
|
1267
|
+
throw new UnexpectedTokenError();
|
|
1268
|
+
}
|
|
1269
|
+
break;
|
|
1270
|
+
case State.AfterClosingTagName:
|
|
1271
|
+
if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
|
|
1272
|
+
token = ClosingAngleBracket;
|
|
1273
|
+
state = State.TopLevelContent;
|
|
1274
|
+
} else if (next = part.match(RE_WHITESPACE)) {
|
|
1275
|
+
token = Content;
|
|
1276
|
+
state = State.TopLevelContent;
|
|
1277
|
+
} else {
|
|
1278
|
+
throw new UnexpectedTokenError();
|
|
1279
|
+
}
|
|
1280
|
+
break;
|
|
1281
|
+
default:
|
|
1282
|
+
throw new UnexpectedTokenError();
|
|
1283
|
+
}
|
|
1284
|
+
const tokenText = next[0];
|
|
1285
|
+
tokens.push({
|
|
1286
|
+
type: token,
|
|
1287
|
+
text: tokenText
|
|
1288
|
+
});
|
|
1289
|
+
index += tokenText.length;
|
|
1290
|
+
}
|
|
1291
|
+
return tokens;
|
|
1292
|
+
};
|
|
1293
|
+
|
|
1294
|
+
const text = data => {
|
|
1295
|
+
return {
|
|
1296
|
+
type: Text$1,
|
|
1297
|
+
text: data,
|
|
1298
|
+
childCount: 0
|
|
1299
|
+
};
|
|
1300
|
+
};
|
|
1301
|
+
|
|
1302
|
+
const parseHtml = (html, allowedAttributes) => {
|
|
1303
|
+
string(html);
|
|
1304
|
+
array(allowedAttributes);
|
|
1305
|
+
const tokens = tokenizeHtml(html);
|
|
1306
|
+
const dom = [];
|
|
1307
|
+
const root = {
|
|
1308
|
+
type: 0,
|
|
1309
|
+
childCount: 0
|
|
1310
|
+
};
|
|
1311
|
+
let current = root;
|
|
1312
|
+
const stack = [root];
|
|
1313
|
+
let attributeName = '';
|
|
1314
|
+
for (const token of tokens) {
|
|
1315
|
+
switch (token.type) {
|
|
1316
|
+
case TagNameStart:
|
|
1317
|
+
current.childCount++;
|
|
1318
|
+
current = {
|
|
1319
|
+
type: getVirtualDomTag(token.text),
|
|
1320
|
+
childCount: 0
|
|
1321
|
+
};
|
|
1322
|
+
dom.push(current);
|
|
1323
|
+
if (!isSelfClosingTag(token.text)) {
|
|
1324
|
+
stack.push(current);
|
|
1325
|
+
}
|
|
1326
|
+
break;
|
|
1327
|
+
case TagNameEnd:
|
|
1328
|
+
stack.pop();
|
|
1329
|
+
current = stack.at(-1) || root;
|
|
1330
|
+
break;
|
|
1331
|
+
case Content:
|
|
1332
|
+
current.childCount++;
|
|
1333
|
+
dom.push(text(parseText(token.text)));
|
|
1334
|
+
break;
|
|
1335
|
+
case AttributeName:
|
|
1336
|
+
attributeName = token.text;
|
|
1337
|
+
if (attributeName === 'class') {
|
|
1338
|
+
attributeName = 'className';
|
|
1339
|
+
}
|
|
1340
|
+
break;
|
|
1341
|
+
case AttributeValue:
|
|
1342
|
+
if (allowedAttributes.includes(attributeName)) {
|
|
1343
|
+
current[attributeName] = token.text;
|
|
1344
|
+
}
|
|
1345
|
+
attributeName = '';
|
|
1346
|
+
break;
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
return dom;
|
|
1350
|
+
};
|
|
1351
|
+
|
|
1352
|
+
const getMarkdownVirtualDom = html => {
|
|
1353
|
+
string(html);
|
|
1354
|
+
const childDom = parseHtml(html, allowedMarkdownAttributes);
|
|
1355
|
+
const markdownChildCount = getVirtualDomChildCount(childDom);
|
|
1356
|
+
return [{
|
|
1357
|
+
type: Div,
|
|
1358
|
+
className: Markdown,
|
|
1359
|
+
role: Document,
|
|
1360
|
+
onContextMenu: HandleReadmeContextMenu,
|
|
1361
|
+
childCount: markdownChildCount
|
|
1362
|
+
}, ...childDom];
|
|
1363
|
+
};
|
|
1364
|
+
|
|
1365
|
+
/**
|
|
1366
|
+
* marked v15.0.6 - a markdown parser
|
|
1367
|
+
* Copyright (c) 2011-2025, Christopher Jeffrey. (MIT Licensed)
|
|
1368
|
+
* https://github.com/markedjs/marked
|
|
1369
|
+
*/
|
|
1370
|
+
|
|
1371
|
+
/**
|
|
1372
|
+
* DO NOT EDIT THIS FILE
|
|
1373
|
+
* The code in this file is generated from files in ./src/
|
|
1374
|
+
*/
|
|
1375
|
+
|
|
1376
|
+
/**
|
|
1377
|
+
* Gets the original marked default options.
|
|
1378
|
+
*/
|
|
1379
|
+
function _getDefaults() {
|
|
1380
|
+
return {
|
|
1381
|
+
async: false,
|
|
1382
|
+
breaks: false,
|
|
1383
|
+
extensions: null,
|
|
1384
|
+
gfm: true,
|
|
1385
|
+
hooks: null,
|
|
1386
|
+
pedantic: false,
|
|
1387
|
+
renderer: null,
|
|
1388
|
+
silent: false,
|
|
1389
|
+
tokenizer: null,
|
|
1390
|
+
walkTokens: null
|
|
1391
|
+
};
|
|
1392
|
+
}
|
|
1393
|
+
let _defaults = _getDefaults();
|
|
1394
|
+
function changeDefaults(newDefaults) {
|
|
1395
|
+
_defaults = newDefaults;
|
|
1396
|
+
}
|
|
1397
|
+
const noopTest = {
|
|
1398
|
+
exec: () => null
|
|
1399
|
+
};
|
|
1400
|
+
function edit(regex, opt = '') {
|
|
1401
|
+
let source = typeof regex === 'string' ? regex : regex.source;
|
|
1402
|
+
const obj = {
|
|
1403
|
+
replace: (name, val) => {
|
|
1404
|
+
let valSource = typeof val === 'string' ? val : val.source;
|
|
1405
|
+
valSource = valSource.replace(other.caret, '$1');
|
|
1406
|
+
source = source.replace(name, valSource);
|
|
1407
|
+
return obj;
|
|
1408
|
+
},
|
|
1409
|
+
getRegex: () => {
|
|
1410
|
+
return new RegExp(source, opt);
|
|
1411
|
+
}
|
|
1412
|
+
};
|
|
1413
|
+
return obj;
|
|
1414
|
+
}
|
|
1415
|
+
const other = {
|
|
1416
|
+
codeRemoveIndent: /^(?: {1,4}| {0,3}\t)/gm,
|
|
1417
|
+
outputLinkReplace: /\\([\[\]])/g,
|
|
1418
|
+
indentCodeCompensation: /^(\s+)(?:```)/,
|
|
1419
|
+
beginningSpace: /^\s+/,
|
|
1420
|
+
endingHash: /#$/,
|
|
1421
|
+
startingSpaceChar: /^ /,
|
|
1422
|
+
endingSpaceChar: / $/,
|
|
1423
|
+
nonSpaceChar: /[^ ]/,
|
|
1424
|
+
newLineCharGlobal: /\n/g,
|
|
1425
|
+
tabCharGlobal: /\t/g,
|
|
1426
|
+
multipleSpaceGlobal: /\s+/g,
|
|
1427
|
+
blankLine: /^[ \t]*$/,
|
|
1428
|
+
doubleBlankLine: /\n[ \t]*\n[ \t]*$/,
|
|
1429
|
+
blockquoteStart: /^ {0,3}>/,
|
|
1430
|
+
blockquoteSetextReplace: /\n {0,3}((?:=+|-+) *)(?=\n|$)/g,
|
|
1431
|
+
blockquoteSetextReplace2: /^ {0,3}>[ \t]?/gm,
|
|
1432
|
+
listReplaceTabs: /^\t+/,
|
|
1433
|
+
listReplaceNesting: /^ {1,4}(?=( {4})*[^ ])/g,
|
|
1434
|
+
listIsTask: /^\[[ xX]\] /,
|
|
1435
|
+
listReplaceTask: /^\[[ xX]\] +/,
|
|
1436
|
+
anyLine: /\n.*\n/,
|
|
1437
|
+
hrefBrackets: /^<(.*)>$/,
|
|
1438
|
+
tableDelimiter: /[:|]/,
|
|
1439
|
+
tableAlignChars: /^\||\| *$/g,
|
|
1440
|
+
tableRowBlankLine: /\n[ \t]*$/,
|
|
1441
|
+
tableAlignRight: /^ *-+: *$/,
|
|
1442
|
+
tableAlignCenter: /^ *:-+: *$/,
|
|
1443
|
+
tableAlignLeft: /^ *:-+ *$/,
|
|
1444
|
+
startATag: /^<a /i,
|
|
1445
|
+
endATag: /^<\/a>/i,
|
|
1446
|
+
startPreScriptTag: /^<(pre|code|kbd|script)(\s|>)/i,
|
|
1447
|
+
endPreScriptTag: /^<\/(pre|code|kbd|script)(\s|>)/i,
|
|
1448
|
+
startAngleBracket: /^</,
|
|
1449
|
+
endAngleBracket: />$/,
|
|
1450
|
+
pedanticHrefTitle: /^([^'"]*[^\s])\s+(['"])(.*)\2/,
|
|
1451
|
+
unicodeAlphaNumeric: /[\p{L}\p{N}]/u,
|
|
1452
|
+
escapeTest: /[&<>"']/,
|
|
1453
|
+
escapeReplace: /[&<>"']/g,
|
|
1454
|
+
escapeTestNoEncode: /[<>"']|&(?!(#\d{1,7}|#[Xx][a-fA-F0-9]{1,6}|\w+);)/,
|
|
1455
|
+
escapeReplaceNoEncode: /[<>"']|&(?!(#\d{1,7}|#[Xx][a-fA-F0-9]{1,6}|\w+);)/g,
|
|
1456
|
+
unescapeTest: /&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/ig,
|
|
1457
|
+
caret: /(^|[^\[])\^/g,
|
|
1458
|
+
percentDecode: /%25/g,
|
|
1459
|
+
findPipe: /\|/g,
|
|
1460
|
+
splitPipe: / \|/,
|
|
1461
|
+
slashPipe: /\\\|/g,
|
|
1462
|
+
carriageReturn: /\r\n|\r/g,
|
|
1463
|
+
spaceLine: /^ +$/gm,
|
|
1464
|
+
notSpaceStart: /^\S*/,
|
|
1465
|
+
endingNewline: /\n$/,
|
|
1466
|
+
listItemRegex: bull => new RegExp(`^( {0,3}${bull})((?:[\t ][^\\n]*)?(?:\\n|$))`),
|
|
1467
|
+
nextBulletRegex: indent => new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:[*+-]|\\d{1,9}[.)])((?:[ \t][^\\n]*)?(?:\\n|$))`),
|
|
1468
|
+
hrRegex: indent => new RegExp(`^ {0,${Math.min(3, indent - 1)}}((?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$)`),
|
|
1469
|
+
fencesBeginRegex: indent => new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:\`\`\`|~~~)`),
|
|
1470
|
+
headingBeginRegex: indent => new RegExp(`^ {0,${Math.min(3, indent - 1)}}#`),
|
|
1471
|
+
htmlBeginRegex: indent => new RegExp(`^ {0,${Math.min(3, indent - 1)}}<(?:[a-z].*>|!--)`, 'i')
|
|
1472
|
+
};
|
|
1473
|
+
/**
|
|
1474
|
+
* Block-Level Grammar
|
|
1475
|
+
*/
|
|
1476
|
+
const newline = /^(?:[ \t]*(?:\n|$))+/;
|
|
1477
|
+
const blockCode = /^((?: {4}| {0,3}\t)[^\n]+(?:\n(?:[ \t]*(?:\n|$))*)?)+/;
|
|
1478
|
+
const fences = /^ {0,3}(`{3,}(?=[^`\n]*(?:\n|$))|~{3,})([^\n]*)(?:\n|$)(?:|([\s\S]*?)(?:\n|$))(?: {0,3}\1[~`]* *(?=\n|$)|$)/;
|
|
1479
|
+
const hr = /^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/;
|
|
1480
|
+
const heading = /^ {0,3}(#{1,6})(?=\s|$)(.*)(?:\n+|$)/;
|
|
1481
|
+
const bullet = /(?:[*+-]|\d{1,9}[.)])/;
|
|
1482
|
+
const lheading = edit(/^(?!bull |blockCode|fences|blockquote|heading|html)((?:.|\n(?!\s*?\n|bull |blockCode|fences|blockquote|heading|html))+?)\n {0,3}(=+|-+) *(?:\n+|$)/).replace(/bull/g, bullet) // lists can interrupt
|
|
1483
|
+
.replace(/blockCode/g, /(?: {4}| {0,3}\t)/) // indented code blocks can interrupt
|
|
1484
|
+
.replace(/fences/g, / {0,3}(?:`{3,}|~{3,})/) // fenced code blocks can interrupt
|
|
1485
|
+
.replace(/blockquote/g, / {0,3}>/) // blockquote can interrupt
|
|
1486
|
+
.replace(/heading/g, / {0,3}#{1,6}/) // ATX heading can interrupt
|
|
1487
|
+
.replace(/html/g, / {0,3}<[^\n>]+>\n/) // block html can interrupt
|
|
1488
|
+
.getRegex();
|
|
1489
|
+
const _paragraph = /^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html|table| +\n)[^\n]+)*)/;
|
|
1490
|
+
const blockText = /^[^\n]+/;
|
|
1491
|
+
const _blockLabel = /(?!\s*\])(?:\\.|[^\[\]\\])+/;
|
|
1492
|
+
const def = edit(/^ {0,3}\[(label)\]: *(?:\n[ \t]*)?([^<\s][^\s]*|<.*?>)(?:(?: +(?:\n[ \t]*)?| *\n[ \t]*)(title))? *(?:\n+|$)/).replace('label', _blockLabel).replace('title', /(?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))/).getRegex();
|
|
1493
|
+
const list = edit(/^( {0,3}bull)([ \t][^\n]+?)?(?:\n|$)/).replace(/bull/g, bullet).getRegex();
|
|
1494
|
+
const _tag = 'address|article|aside|base|basefont|blockquote|body|caption' + '|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption' + '|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe' + '|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option' + '|p|param|search|section|summary|table|tbody|td|tfoot|th|thead|title' + '|tr|track|ul';
|
|
1495
|
+
const _comment = /<!--(?:-?>|[\s\S]*?(?:-->|$))/;
|
|
1496
|
+
const html = edit('^ {0,3}(?:' // optional indentation
|
|
1497
|
+
+ '<(script|pre|style|textarea)[\\s>][\\s\\S]*?(?:</\\1>[^\\n]*\\n+|$)' // (1)
|
|
1498
|
+
+ '|comment[^\\n]*(\\n+|$)' // (2)
|
|
1499
|
+
+ '|<\\?[\\s\\S]*?(?:\\?>\\n*|$)' // (3)
|
|
1500
|
+
+ '|<![A-Z][\\s\\S]*?(?:>\\n*|$)' // (4)
|
|
1501
|
+
+ '|<!\\[CDATA\\[[\\s\\S]*?(?:\\]\\]>\\n*|$)' // (5)
|
|
1502
|
+
+ '|</?(tag)(?: +|\\n|/?>)[\\s\\S]*?(?:(?:\\n[ \t]*)+\\n|$)' // (6)
|
|
1503
|
+
+ '|<(?!script|pre|style|textarea)([a-z][\\w-]*)(?:attribute)*? */?>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n[ \t]*)+\\n|$)' // (7) open tag
|
|
1504
|
+
+ '|</(?!script|pre|style|textarea)[a-z][\\w-]*\\s*>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n[ \t]*)+\\n|$)' // (7) closing tag
|
|
1505
|
+
+ ')', 'i').replace('comment', _comment).replace('tag', _tag).replace('attribute', / +[a-zA-Z:_][\w.:-]*(?: *= *"[^"\n]*"| *= *'[^'\n]*'| *= *[^\s"'=<>`]+)?/).getRegex();
|
|
1506
|
+
const paragraph = edit(_paragraph).replace('hr', hr).replace('heading', ' {0,3}#{1,6}(?:\\s|$)').replace('|lheading', '') // setext headings don't interrupt commonmark paragraphs
|
|
1507
|
+
.replace('|table', '').replace('blockquote', ' {0,3}>').replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n').replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
|
|
1508
|
+
.replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)').replace('tag', _tag) // pars can be interrupted by type (6) html blocks
|
|
1509
|
+
.getRegex();
|
|
1510
|
+
const blockquote = edit(/^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/).replace('paragraph', paragraph).getRegex();
|
|
1511
|
+
/**
|
|
1512
|
+
* Normal Block Grammar
|
|
1513
|
+
*/
|
|
1514
|
+
const blockNormal = {
|
|
1515
|
+
blockquote,
|
|
1516
|
+
code: blockCode,
|
|
1517
|
+
def,
|
|
1518
|
+
fences,
|
|
1519
|
+
heading,
|
|
1520
|
+
hr,
|
|
1521
|
+
html,
|
|
1522
|
+
lheading,
|
|
1523
|
+
list,
|
|
1524
|
+
newline,
|
|
1525
|
+
paragraph,
|
|
1526
|
+
table: noopTest,
|
|
1527
|
+
text: blockText
|
|
1528
|
+
};
|
|
1529
|
+
/**
|
|
1530
|
+
* GFM Block Grammar
|
|
1531
|
+
*/
|
|
1532
|
+
const gfmTable = edit('^ *([^\\n ].*)\\n' // Header
|
|
1533
|
+
+ ' {0,3}((?:\\| *)?:?-+:? *(?:\\| *:?-+:? *)*(?:\\| *)?)' // Align
|
|
1534
|
+
+ '(?:\\n((?:(?! *\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)') // Cells
|
|
1535
|
+
.replace('hr', hr).replace('heading', ' {0,3}#{1,6}(?:\\s|$)').replace('blockquote', ' {0,3}>').replace('code', '(?: {4}| {0,3}\t)[^\\n]').replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n').replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
|
|
1536
|
+
.replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)').replace('tag', _tag) // tables can be interrupted by type (6) html blocks
|
|
1537
|
+
.getRegex();
|
|
1538
|
+
const blockGfm = {
|
|
1539
|
+
...blockNormal,
|
|
1540
|
+
table: gfmTable,
|
|
1541
|
+
paragraph: edit(_paragraph).replace('hr', hr).replace('heading', ' {0,3}#{1,6}(?:\\s|$)').replace('|lheading', '') // setext headings don't interrupt commonmark paragraphs
|
|
1542
|
+
.replace('table', gfmTable) // interrupt paragraphs with table
|
|
1543
|
+
.replace('blockquote', ' {0,3}>').replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n').replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
|
|
1544
|
+
.replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)').replace('tag', _tag) // pars can be interrupted by type (6) html blocks
|
|
1545
|
+
.getRegex()
|
|
1546
|
+
};
|
|
1547
|
+
/**
|
|
1548
|
+
* Pedantic grammar (original John Gruber's loose markdown specification)
|
|
1549
|
+
*/
|
|
1550
|
+
const blockPedantic = {
|
|
1551
|
+
...blockNormal,
|
|
1552
|
+
html: edit('^ *(?:comment *(?:\\n|\\s*$)' + '|<(tag)[\\s\\S]+?</\\1> *(?:\\n{2,}|\\s*$)' // closed tag
|
|
1553
|
+
+ '|<tag(?:"[^"]*"|\'[^\']*\'|\\s[^\'"/>\\s]*)*?/?> *(?:\\n{2,}|\\s*$))').replace('comment', _comment).replace(/tag/g, '(?!(?:' + 'a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub' + '|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)' + '\\b)\\w+(?!:|[^\\w\\s@]*@)\\b').getRegex(),
|
|
1554
|
+
def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +(["(][^\n]+[")]))? *(?:\n+|$)/,
|
|
1555
|
+
heading: /^(#{1,6})(.*)(?:\n+|$)/,
|
|
1556
|
+
fences: noopTest,
|
|
1557
|
+
// fences not supported
|
|
1558
|
+
lheading: /^(.+?)\n {0,3}(=+|-+) *(?:\n+|$)/,
|
|
1559
|
+
paragraph: edit(_paragraph).replace('hr', hr).replace('heading', ' *#{1,6} *[^\n]').replace('lheading', lheading).replace('|table', '').replace('blockquote', ' {0,3}>').replace('|fences', '').replace('|list', '').replace('|html', '').replace('|tag', '').getRegex()
|
|
1560
|
+
};
|
|
1561
|
+
/**
|
|
1562
|
+
* Inline-Level Grammar
|
|
1563
|
+
*/
|
|
1564
|
+
const escape$1 = /^\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/;
|
|
1565
|
+
const inlineCode = /^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/;
|
|
1566
|
+
const br = /^( {2,}|\\)\n(?!\s*$)/;
|
|
1567
|
+
const inlineText = /^(`+|[^`])(?:(?= {2,}\n)|[\s\S]*?(?:(?=[\\<!\[`*_]|\b_|$)|[^ ](?= {2,}\n)))/;
|
|
1568
|
+
// list of unicode punctuation marks, plus any missing characters from CommonMark spec
|
|
1569
|
+
const _punctuation = /[\p{P}\p{S}]/u;
|
|
1570
|
+
const _punctuationOrSpace = /[\s\p{P}\p{S}]/u;
|
|
1571
|
+
const _notPunctuationOrSpace = /[^\s\p{P}\p{S}]/u;
|
|
1572
|
+
const punctuation = edit(/^((?![*_])punctSpace)/, 'u').replace(/punctSpace/g, _punctuationOrSpace).getRegex();
|
|
1573
|
+
// GFM allows ~ inside strong and em for strikethrough
|
|
1574
|
+
const _punctuationGfmStrongEm = /(?!~)[\p{P}\p{S}]/u;
|
|
1575
|
+
const _punctuationOrSpaceGfmStrongEm = /(?!~)[\s\p{P}\p{S}]/u;
|
|
1576
|
+
const _notPunctuationOrSpaceGfmStrongEm = /(?:[^\s\p{P}\p{S}]|~)/u;
|
|
1577
|
+
// sequences em should skip over [title](link), `code`, <html>
|
|
1578
|
+
const blockSkip = /\[[^[\]]*?\]\((?:\\.|[^\\\(\)]|\((?:\\.|[^\\\(\)])*\))*\)|`[^`]*?`|<[^<>]*?>/g;
|
|
1579
|
+
const emStrongLDelimCore = /^(?:\*+(?:((?!\*)punct)|[^\s*]))|^_+(?:((?!_)punct)|([^\s_]))/;
|
|
1580
|
+
const emStrongLDelim = edit(emStrongLDelimCore, 'u').replace(/punct/g, _punctuation).getRegex();
|
|
1581
|
+
const emStrongLDelimGfm = edit(emStrongLDelimCore, 'u').replace(/punct/g, _punctuationGfmStrongEm).getRegex();
|
|
1582
|
+
const emStrongRDelimAstCore = '^[^_*]*?__[^_*]*?\\*[^_*]*?(?=__)' // Skip orphan inside strong
|
|
1583
|
+
+ '|[^*]+(?=[^*])' // Consume to delim
|
|
1584
|
+
+ '|(?!\\*)punct(\\*+)(?=[\\s]|$)' // (1) #*** can only be a Right Delimiter
|
|
1585
|
+
+ '|notPunctSpace(\\*+)(?!\\*)(?=punctSpace|$)' // (2) a***#, a*** can only be a Right Delimiter
|
|
1586
|
+
+ '|(?!\\*)punctSpace(\\*+)(?=notPunctSpace)' // (3) #***a, ***a can only be Left Delimiter
|
|
1587
|
+
+ '|[\\s](\\*+)(?!\\*)(?=punct)' // (4) ***# can only be Left Delimiter
|
|
1588
|
+
+ '|(?!\\*)punct(\\*+)(?!\\*)(?=punct)' // (5) #***# can be either Left or Right Delimiter
|
|
1589
|
+
+ '|notPunctSpace(\\*+)(?=notPunctSpace)'; // (6) a***a can be either Left or Right Delimiter
|
|
1590
|
+
const emStrongRDelimAst = edit(emStrongRDelimAstCore, 'gu').replace(/notPunctSpace/g, _notPunctuationOrSpace).replace(/punctSpace/g, _punctuationOrSpace).replace(/punct/g, _punctuation).getRegex();
|
|
1591
|
+
const emStrongRDelimAstGfm = edit(emStrongRDelimAstCore, 'gu').replace(/notPunctSpace/g, _notPunctuationOrSpaceGfmStrongEm).replace(/punctSpace/g, _punctuationOrSpaceGfmStrongEm).replace(/punct/g, _punctuationGfmStrongEm).getRegex();
|
|
1592
|
+
// (6) Not allowed for _
|
|
1593
|
+
const emStrongRDelimUnd = edit('^[^_*]*?\\*\\*[^_*]*?_[^_*]*?(?=\\*\\*)' // Skip orphan inside strong
|
|
1594
|
+
+ '|[^_]+(?=[^_])' // Consume to delim
|
|
1595
|
+
+ '|(?!_)punct(_+)(?=[\\s]|$)' // (1) #___ can only be a Right Delimiter
|
|
1596
|
+
+ '|notPunctSpace(_+)(?!_)(?=punctSpace|$)' // (2) a___#, a___ can only be a Right Delimiter
|
|
1597
|
+
+ '|(?!_)punctSpace(_+)(?=notPunctSpace)' // (3) #___a, ___a can only be Left Delimiter
|
|
1598
|
+
+ '|[\\s](_+)(?!_)(?=punct)' // (4) ___# can only be Left Delimiter
|
|
1599
|
+
+ '|(?!_)punct(_+)(?!_)(?=punct)', 'gu') // (5) #___# can be either Left or Right Delimiter
|
|
1600
|
+
.replace(/notPunctSpace/g, _notPunctuationOrSpace).replace(/punctSpace/g, _punctuationOrSpace).replace(/punct/g, _punctuation).getRegex();
|
|
1601
|
+
const anyPunctuation = edit(/\\(punct)/, 'gu').replace(/punct/g, _punctuation).getRegex();
|
|
1602
|
+
const autolink = edit(/^<(scheme:[^\s\x00-\x1f<>]*|email)>/).replace('scheme', /[a-zA-Z][a-zA-Z0-9+.-]{1,31}/).replace('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])?)+(?![-_])/).getRegex();
|
|
1603
|
+
const _inlineComment = edit(_comment).replace('(?:-->|$)', '-->').getRegex();
|
|
1604
|
+
const tag = edit('^comment' + '|^</[a-zA-Z][\\w:-]*\\s*>' // self-closing tag
|
|
1605
|
+
+ '|^<[a-zA-Z][\\w-]*(?:attribute)*?\\s*/?>' // open tag
|
|
1606
|
+
+ '|^<\\?[\\s\\S]*?\\?>' // processing instruction, e.g. <?php ?>
|
|
1607
|
+
+ '|^<![a-zA-Z]+\\s[\\s\\S]*?>' // declaration, e.g. <!DOCTYPE html>
|
|
1608
|
+
+ '|^<!\\[CDATA\\[[\\s\\S]*?\\]\\]>') // CDATA section
|
|
1609
|
+
.replace('comment', _inlineComment).replace('attribute', /\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?/).getRegex();
|
|
1610
|
+
const _inlineLabel = /(?:\[(?:\\.|[^\[\]\\])*\]|\\.|`[^`]*`|[^\[\]\\`])*?/;
|
|
1611
|
+
const link = edit(/^!?\[(label)\]\(\s*(href)(?:\s+(title))?\s*\)/).replace('label', _inlineLabel).replace('href', /<(?:\\.|[^\n<>\\])+>|[^\s\x00-\x1f]*/).replace('title', /"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/).getRegex();
|
|
1612
|
+
const reflink = edit(/^!?\[(label)\]\[(ref)\]/).replace('label', _inlineLabel).replace('ref', _blockLabel).getRegex();
|
|
1613
|
+
const nolink = edit(/^!?\[(ref)\](?:\[\])?/).replace('ref', _blockLabel).getRegex();
|
|
1614
|
+
const reflinkSearch = edit('reflink|nolink(?!\\()', 'g').replace('reflink', reflink).replace('nolink', nolink).getRegex();
|
|
1615
|
+
/**
|
|
1616
|
+
* Normal Inline Grammar
|
|
1617
|
+
*/
|
|
1618
|
+
const inlineNormal = {
|
|
1619
|
+
_backpedal: noopTest,
|
|
1620
|
+
// only used for GFM url
|
|
1621
|
+
anyPunctuation,
|
|
1622
|
+
autolink,
|
|
1623
|
+
blockSkip,
|
|
1624
|
+
br,
|
|
1625
|
+
code: inlineCode,
|
|
1626
|
+
del: noopTest,
|
|
1627
|
+
emStrongLDelim,
|
|
1628
|
+
emStrongRDelimAst,
|
|
1629
|
+
emStrongRDelimUnd,
|
|
1630
|
+
escape: escape$1,
|
|
1631
|
+
link,
|
|
1632
|
+
nolink,
|
|
1633
|
+
punctuation,
|
|
1634
|
+
reflink,
|
|
1635
|
+
reflinkSearch,
|
|
1636
|
+
tag,
|
|
1637
|
+
text: inlineText,
|
|
1638
|
+
url: noopTest
|
|
1639
|
+
};
|
|
1640
|
+
/**
|
|
1641
|
+
* Pedantic Inline Grammar
|
|
1642
|
+
*/
|
|
1643
|
+
const inlinePedantic = {
|
|
1644
|
+
...inlineNormal,
|
|
1645
|
+
link: edit(/^!?\[(label)\]\((.*?)\)/).replace('label', _inlineLabel).getRegex(),
|
|
1646
|
+
reflink: edit(/^!?\[(label)\]\s*\[([^\]]*)\]/).replace('label', _inlineLabel).getRegex()
|
|
1647
|
+
};
|
|
1648
|
+
/**
|
|
1649
|
+
* GFM Inline Grammar
|
|
1650
|
+
*/
|
|
1651
|
+
const inlineGfm = {
|
|
1652
|
+
...inlineNormal,
|
|
1653
|
+
emStrongRDelimAst: emStrongRDelimAstGfm,
|
|
1654
|
+
emStrongLDelim: emStrongLDelimGfm,
|
|
1655
|
+
url: edit(/^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/, 'i').replace('email', /[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/).getRegex(),
|
|
1656
|
+
_backpedal: /(?:[^?!.,:;*_'"~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_'"~)]+(?!$))+/,
|
|
1657
|
+
del: /^(~~?)(?=[^\s~])((?:\\.|[^\\])*?(?:\\.|[^\s~\\]))\1(?=[^~]|$)/,
|
|
1658
|
+
text: /^([`~]+|[^`~])(?:(?= {2,}\n)|(?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)|[\s\S]*?(?:(?=[\\<!\[`*~_]|\b_|https?:\/\/|ftp:\/\/|www\.|$)|[^ ](?= {2,}\n)|[^a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-](?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)))/
|
|
1659
|
+
};
|
|
1660
|
+
/**
|
|
1661
|
+
* GFM + Line Breaks Inline Grammar
|
|
1662
|
+
*/
|
|
1663
|
+
const inlineBreaks = {
|
|
1664
|
+
...inlineGfm,
|
|
1665
|
+
br: edit(br).replace('{2,}', '*').getRegex(),
|
|
1666
|
+
text: edit(inlineGfm.text).replace('\\b_', '\\b_| {2,}\\n').replace(/\{2,\}/g, '*').getRegex()
|
|
1667
|
+
};
|
|
1668
|
+
/**
|
|
1669
|
+
* exports
|
|
1670
|
+
*/
|
|
1671
|
+
const block = {
|
|
1672
|
+
normal: blockNormal,
|
|
1673
|
+
gfm: blockGfm,
|
|
1674
|
+
pedantic: blockPedantic
|
|
1675
|
+
};
|
|
1676
|
+
const inline = {
|
|
1677
|
+
normal: inlineNormal,
|
|
1678
|
+
gfm: inlineGfm,
|
|
1679
|
+
breaks: inlineBreaks,
|
|
1680
|
+
pedantic: inlinePedantic
|
|
1681
|
+
};
|
|
1682
|
+
|
|
1683
|
+
/**
|
|
1684
|
+
* Helpers
|
|
1685
|
+
*/
|
|
1686
|
+
const escapeReplacements = {
|
|
1687
|
+
'&': '&',
|
|
1688
|
+
'<': '<',
|
|
1689
|
+
'>': '>',
|
|
1690
|
+
'"': '"',
|
|
1691
|
+
"'": '''
|
|
1692
|
+
};
|
|
1693
|
+
const getEscapeReplacement = ch => escapeReplacements[ch];
|
|
1694
|
+
function escape(html, encode) {
|
|
1695
|
+
if (encode) {
|
|
1696
|
+
if (other.escapeTest.test(html)) {
|
|
1697
|
+
return html.replace(other.escapeReplace, getEscapeReplacement);
|
|
1698
|
+
}
|
|
1699
|
+
} else {
|
|
1700
|
+
if (other.escapeTestNoEncode.test(html)) {
|
|
1701
|
+
return html.replace(other.escapeReplaceNoEncode, getEscapeReplacement);
|
|
1702
|
+
}
|
|
1703
|
+
}
|
|
1704
|
+
return html;
|
|
1705
|
+
}
|
|
1706
|
+
function cleanUrl(href) {
|
|
1707
|
+
try {
|
|
1708
|
+
href = encodeURI(href).replace(other.percentDecode, '%');
|
|
1709
|
+
} catch {
|
|
1710
|
+
return null;
|
|
1711
|
+
}
|
|
1712
|
+
return href;
|
|
1713
|
+
}
|
|
1714
|
+
function splitCells(tableRow, count) {
|
|
1715
|
+
// ensure that every cell-delimiting pipe has a space
|
|
1716
|
+
// before it to distinguish it from an escaped pipe
|
|
1717
|
+
const row = tableRow.replace(other.findPipe, (match, offset, str) => {
|
|
1718
|
+
let escaped = false;
|
|
1719
|
+
let curr = offset;
|
|
1720
|
+
while (--curr >= 0 && str[curr] === '\\') escaped = !escaped;
|
|
1721
|
+
if (escaped) {
|
|
1722
|
+
// odd number of slashes means | is escaped
|
|
1723
|
+
// so we leave it alone
|
|
1724
|
+
return '|';
|
|
1725
|
+
} else {
|
|
1726
|
+
// add space before unescaped |
|
|
1727
|
+
return ' |';
|
|
1728
|
+
}
|
|
1729
|
+
}),
|
|
1730
|
+
cells = row.split(other.splitPipe);
|
|
1731
|
+
let i = 0;
|
|
1732
|
+
// First/last cell in a row cannot be empty if it has no leading/trailing pipe
|
|
1733
|
+
if (!cells[0].trim()) {
|
|
1734
|
+
cells.shift();
|
|
1735
|
+
}
|
|
1736
|
+
if (cells.length > 0 && !cells.at(-1)?.trim()) {
|
|
1737
|
+
cells.pop();
|
|
1738
|
+
}
|
|
1739
|
+
if (count) {
|
|
1740
|
+
if (cells.length > count) {
|
|
1741
|
+
cells.splice(count);
|
|
1742
|
+
} else {
|
|
1743
|
+
while (cells.length < count) cells.push('');
|
|
1744
|
+
}
|
|
1745
|
+
}
|
|
1746
|
+
for (; i < cells.length; i++) {
|
|
1747
|
+
// leading or trailing whitespace is ignored per the gfm spec
|
|
1748
|
+
cells[i] = cells[i].trim().replace(other.slashPipe, '|');
|
|
1749
|
+
}
|
|
1750
|
+
return cells;
|
|
1751
|
+
}
|
|
1752
|
+
/**
|
|
1753
|
+
* Remove trailing 'c's. Equivalent to str.replace(/c*$/, '').
|
|
1754
|
+
* /c*$/ is vulnerable to REDOS.
|
|
1755
|
+
*
|
|
1756
|
+
* @param str
|
|
1757
|
+
* @param c
|
|
1758
|
+
* @param invert Remove suffix of non-c chars instead. Default falsey.
|
|
1759
|
+
*/
|
|
1760
|
+
function rtrim(str, c, invert) {
|
|
1761
|
+
const l = str.length;
|
|
1762
|
+
if (l === 0) {
|
|
1763
|
+
return '';
|
|
1764
|
+
}
|
|
1765
|
+
// Length of suffix matching the invert condition.
|
|
1766
|
+
let suffLen = 0;
|
|
1767
|
+
// Step left until we fail to match the invert condition.
|
|
1768
|
+
while (suffLen < l) {
|
|
1769
|
+
const currChar = str.charAt(l - suffLen - 1);
|
|
1770
|
+
if (currChar === c && true) {
|
|
1771
|
+
suffLen++;
|
|
1772
|
+
} else {
|
|
1773
|
+
break;
|
|
1774
|
+
}
|
|
1775
|
+
}
|
|
1776
|
+
return str.slice(0, l - suffLen);
|
|
1777
|
+
}
|
|
1778
|
+
function findClosingBracket(str, b) {
|
|
1779
|
+
if (str.indexOf(b[1]) === -1) {
|
|
1780
|
+
return -1;
|
|
1781
|
+
}
|
|
1782
|
+
let level = 0;
|
|
1783
|
+
for (let i = 0; i < str.length; i++) {
|
|
1784
|
+
if (str[i] === '\\') {
|
|
1785
|
+
i++;
|
|
1786
|
+
} else if (str[i] === b[0]) {
|
|
1787
|
+
level++;
|
|
1788
|
+
} else if (str[i] === b[1]) {
|
|
1789
|
+
level--;
|
|
1790
|
+
if (level < 0) {
|
|
1791
|
+
return i;
|
|
1792
|
+
}
|
|
1793
|
+
}
|
|
1794
|
+
}
|
|
1795
|
+
return -1;
|
|
1796
|
+
}
|
|
1797
|
+
function outputLink(cap, link, raw, lexer, rules) {
|
|
1798
|
+
const href = link.href;
|
|
1799
|
+
const title = link.title || null;
|
|
1800
|
+
const text = cap[1].replace(rules.other.outputLinkReplace, '$1');
|
|
1801
|
+
if (cap[0].charAt(0) !== '!') {
|
|
1802
|
+
lexer.state.inLink = true;
|
|
1803
|
+
const token = {
|
|
1804
|
+
type: 'link',
|
|
1805
|
+
raw,
|
|
1806
|
+
href,
|
|
1807
|
+
title,
|
|
1808
|
+
text,
|
|
1809
|
+
tokens: lexer.inlineTokens(text)
|
|
1810
|
+
};
|
|
1811
|
+
lexer.state.inLink = false;
|
|
1812
|
+
return token;
|
|
1813
|
+
}
|
|
1814
|
+
return {
|
|
1815
|
+
type: 'image',
|
|
1816
|
+
raw,
|
|
1817
|
+
href,
|
|
1818
|
+
title,
|
|
1819
|
+
text
|
|
1820
|
+
};
|
|
1821
|
+
}
|
|
1822
|
+
function indentCodeCompensation(raw, text, rules) {
|
|
1823
|
+
const matchIndentToCode = raw.match(rules.other.indentCodeCompensation);
|
|
1824
|
+
if (matchIndentToCode === null) {
|
|
1825
|
+
return text;
|
|
1826
|
+
}
|
|
1827
|
+
const indentToCode = matchIndentToCode[1];
|
|
1828
|
+
return text.split('\n').map(node => {
|
|
1829
|
+
const matchIndentInNode = node.match(rules.other.beginningSpace);
|
|
1830
|
+
if (matchIndentInNode === null) {
|
|
1831
|
+
return node;
|
|
1832
|
+
}
|
|
1833
|
+
const [indentInNode] = matchIndentInNode;
|
|
1834
|
+
if (indentInNode.length >= indentToCode.length) {
|
|
1835
|
+
return node.slice(indentToCode.length);
|
|
1836
|
+
}
|
|
1837
|
+
return node;
|
|
1838
|
+
}).join('\n');
|
|
1839
|
+
}
|
|
1840
|
+
/**
|
|
1841
|
+
* Tokenizer
|
|
1842
|
+
*/
|
|
1843
|
+
class _Tokenizer {
|
|
1844
|
+
options;
|
|
1845
|
+
rules; // set by the lexer
|
|
1846
|
+
lexer; // set by the lexer
|
|
1847
|
+
constructor(options) {
|
|
1848
|
+
this.options = options || _defaults;
|
|
1849
|
+
}
|
|
1850
|
+
space(src) {
|
|
1851
|
+
const cap = this.rules.block.newline.exec(src);
|
|
1852
|
+
if (cap && cap[0].length > 0) {
|
|
1853
|
+
return {
|
|
1854
|
+
type: 'space',
|
|
1855
|
+
raw: cap[0]
|
|
1856
|
+
};
|
|
1857
|
+
}
|
|
1858
|
+
}
|
|
1859
|
+
code(src) {
|
|
1860
|
+
const cap = this.rules.block.code.exec(src);
|
|
1861
|
+
if (cap) {
|
|
1862
|
+
const text = cap[0].replace(this.rules.other.codeRemoveIndent, '');
|
|
1863
|
+
return {
|
|
1864
|
+
type: 'code',
|
|
1865
|
+
raw: cap[0],
|
|
1866
|
+
codeBlockStyle: 'indented',
|
|
1867
|
+
text: !this.options.pedantic ? rtrim(text, '\n') : text
|
|
1868
|
+
};
|
|
1869
|
+
}
|
|
1870
|
+
}
|
|
1871
|
+
fences(src) {
|
|
1872
|
+
const cap = this.rules.block.fences.exec(src);
|
|
1873
|
+
if (cap) {
|
|
1874
|
+
const raw = cap[0];
|
|
1875
|
+
const text = indentCodeCompensation(raw, cap[3] || '', this.rules);
|
|
1876
|
+
return {
|
|
1877
|
+
type: 'code',
|
|
1878
|
+
raw,
|
|
1879
|
+
lang: cap[2] ? cap[2].trim().replace(this.rules.inline.anyPunctuation, '$1') : cap[2],
|
|
1880
|
+
text
|
|
1881
|
+
};
|
|
1882
|
+
}
|
|
1883
|
+
}
|
|
1884
|
+
heading(src) {
|
|
1885
|
+
const cap = this.rules.block.heading.exec(src);
|
|
1886
|
+
if (cap) {
|
|
1887
|
+
let text = cap[2].trim();
|
|
1888
|
+
// remove trailing #s
|
|
1889
|
+
if (this.rules.other.endingHash.test(text)) {
|
|
1890
|
+
const trimmed = rtrim(text, '#');
|
|
1891
|
+
if (this.options.pedantic) {
|
|
1892
|
+
text = trimmed.trim();
|
|
1893
|
+
} else if (!trimmed || this.rules.other.endingSpaceChar.test(trimmed)) {
|
|
1894
|
+
// CommonMark requires space before trailing #s
|
|
1895
|
+
text = trimmed.trim();
|
|
1896
|
+
}
|
|
1897
|
+
}
|
|
1898
|
+
return {
|
|
1899
|
+
type: 'heading',
|
|
1900
|
+
raw: cap[0],
|
|
1901
|
+
depth: cap[1].length,
|
|
1902
|
+
text,
|
|
1903
|
+
tokens: this.lexer.inline(text)
|
|
1904
|
+
};
|
|
1905
|
+
}
|
|
1906
|
+
}
|
|
1907
|
+
hr(src) {
|
|
1908
|
+
const cap = this.rules.block.hr.exec(src);
|
|
1909
|
+
if (cap) {
|
|
1910
|
+
return {
|
|
1911
|
+
type: 'hr',
|
|
1912
|
+
raw: rtrim(cap[0], '\n')
|
|
1913
|
+
};
|
|
1914
|
+
}
|
|
1915
|
+
}
|
|
1916
|
+
blockquote(src) {
|
|
1917
|
+
const cap = this.rules.block.blockquote.exec(src);
|
|
1918
|
+
if (cap) {
|
|
1919
|
+
let lines = rtrim(cap[0], '\n').split('\n');
|
|
1920
|
+
let raw = '';
|
|
1921
|
+
let text = '';
|
|
1922
|
+
const tokens = [];
|
|
1923
|
+
while (lines.length > 0) {
|
|
1924
|
+
let inBlockquote = false;
|
|
1925
|
+
const currentLines = [];
|
|
1926
|
+
let i;
|
|
1927
|
+
for (i = 0; i < lines.length; i++) {
|
|
1928
|
+
// get lines up to a continuation
|
|
1929
|
+
if (this.rules.other.blockquoteStart.test(lines[i])) {
|
|
1930
|
+
currentLines.push(lines[i]);
|
|
1931
|
+
inBlockquote = true;
|
|
1932
|
+
} else if (!inBlockquote) {
|
|
1933
|
+
currentLines.push(lines[i]);
|
|
1934
|
+
} else {
|
|
1935
|
+
break;
|
|
1936
|
+
}
|
|
1937
|
+
}
|
|
1938
|
+
lines = lines.slice(i);
|
|
1939
|
+
const currentRaw = currentLines.join('\n');
|
|
1940
|
+
const currentText = currentRaw
|
|
1941
|
+
// precede setext continuation with 4 spaces so it isn't a setext
|
|
1942
|
+
.replace(this.rules.other.blockquoteSetextReplace, '\n $1').replace(this.rules.other.blockquoteSetextReplace2, '');
|
|
1943
|
+
raw = raw ? `${raw}\n${currentRaw}` : currentRaw;
|
|
1944
|
+
text = text ? `${text}\n${currentText}` : currentText;
|
|
1945
|
+
// parse blockquote lines as top level tokens
|
|
1946
|
+
// merge paragraphs if this is a continuation
|
|
1947
|
+
const top = this.lexer.state.top;
|
|
1948
|
+
this.lexer.state.top = true;
|
|
1949
|
+
this.lexer.blockTokens(currentText, tokens, true);
|
|
1950
|
+
this.lexer.state.top = top;
|
|
1951
|
+
// if there is no continuation then we are done
|
|
1952
|
+
if (lines.length === 0) {
|
|
1953
|
+
break;
|
|
1954
|
+
}
|
|
1955
|
+
const lastToken = tokens.at(-1);
|
|
1956
|
+
if (lastToken?.type === 'code') {
|
|
1957
|
+
// blockquote continuation cannot be preceded by a code block
|
|
1958
|
+
break;
|
|
1959
|
+
} else if (lastToken?.type === 'blockquote') {
|
|
1960
|
+
// include continuation in nested blockquote
|
|
1961
|
+
const oldToken = lastToken;
|
|
1962
|
+
const newText = oldToken.raw + '\n' + lines.join('\n');
|
|
1963
|
+
const newToken = this.blockquote(newText);
|
|
1964
|
+
tokens[tokens.length - 1] = newToken;
|
|
1965
|
+
raw = raw.substring(0, raw.length - oldToken.raw.length) + newToken.raw;
|
|
1966
|
+
text = text.substring(0, text.length - oldToken.text.length) + newToken.text;
|
|
1967
|
+
break;
|
|
1968
|
+
} else if (lastToken?.type === 'list') {
|
|
1969
|
+
// include continuation in nested list
|
|
1970
|
+
const oldToken = lastToken;
|
|
1971
|
+
const newText = oldToken.raw + '\n' + lines.join('\n');
|
|
1972
|
+
const newToken = this.list(newText);
|
|
1973
|
+
tokens[tokens.length - 1] = newToken;
|
|
1974
|
+
raw = raw.substring(0, raw.length - lastToken.raw.length) + newToken.raw;
|
|
1975
|
+
text = text.substring(0, text.length - oldToken.raw.length) + newToken.raw;
|
|
1976
|
+
lines = newText.substring(tokens.at(-1).raw.length).split('\n');
|
|
1977
|
+
continue;
|
|
1978
|
+
}
|
|
1979
|
+
}
|
|
1980
|
+
return {
|
|
1981
|
+
type: 'blockquote',
|
|
1982
|
+
raw,
|
|
1983
|
+
tokens,
|
|
1984
|
+
text
|
|
1985
|
+
};
|
|
1986
|
+
}
|
|
1987
|
+
}
|
|
1988
|
+
list(src) {
|
|
1989
|
+
let cap = this.rules.block.list.exec(src);
|
|
1990
|
+
if (cap) {
|
|
1991
|
+
let bull = cap[1].trim();
|
|
1992
|
+
const isordered = bull.length > 1;
|
|
1993
|
+
const list = {
|
|
1994
|
+
type: 'list',
|
|
1995
|
+
raw: '',
|
|
1996
|
+
ordered: isordered,
|
|
1997
|
+
start: isordered ? +bull.slice(0, -1) : '',
|
|
1998
|
+
loose: false,
|
|
1999
|
+
items: []
|
|
2000
|
+
};
|
|
2001
|
+
bull = isordered ? `\\d{1,9}\\${bull.slice(-1)}` : `\\${bull}`;
|
|
2002
|
+
if (this.options.pedantic) {
|
|
2003
|
+
bull = isordered ? bull : '[*+-]';
|
|
2004
|
+
}
|
|
2005
|
+
// Get next list item
|
|
2006
|
+
const itemRegex = this.rules.other.listItemRegex(bull);
|
|
2007
|
+
let endsWithBlankLine = false;
|
|
2008
|
+
// Check if current bullet point can start a new List Item
|
|
2009
|
+
while (src) {
|
|
2010
|
+
let endEarly = false;
|
|
2011
|
+
let raw = '';
|
|
2012
|
+
let itemContents = '';
|
|
2013
|
+
if (!(cap = itemRegex.exec(src))) {
|
|
2014
|
+
break;
|
|
2015
|
+
}
|
|
2016
|
+
if (this.rules.block.hr.test(src)) {
|
|
2017
|
+
// End list if bullet was actually HR (possibly move into itemRegex?)
|
|
2018
|
+
break;
|
|
2019
|
+
}
|
|
2020
|
+
raw = cap[0];
|
|
2021
|
+
src = src.substring(raw.length);
|
|
2022
|
+
let line = cap[2].split('\n', 1)[0].replace(this.rules.other.listReplaceTabs, t => ' '.repeat(3 * t.length));
|
|
2023
|
+
let nextLine = src.split('\n', 1)[0];
|
|
2024
|
+
let blankLine = !line.trim();
|
|
2025
|
+
let indent = 0;
|
|
2026
|
+
if (this.options.pedantic) {
|
|
2027
|
+
indent = 2;
|
|
2028
|
+
itemContents = line.trimStart();
|
|
2029
|
+
} else if (blankLine) {
|
|
2030
|
+
indent = cap[1].length + 1;
|
|
2031
|
+
} else {
|
|
2032
|
+
indent = cap[2].search(this.rules.other.nonSpaceChar); // Find first non-space char
|
|
2033
|
+
indent = indent > 4 ? 1 : indent; // Treat indented code blocks (> 4 spaces) as having only 1 indent
|
|
2034
|
+
itemContents = line.slice(indent);
|
|
2035
|
+
indent += cap[1].length;
|
|
2036
|
+
}
|
|
2037
|
+
if (blankLine && this.rules.other.blankLine.test(nextLine)) {
|
|
2038
|
+
// Items begin with at most one blank line
|
|
2039
|
+
raw += nextLine + '\n';
|
|
2040
|
+
src = src.substring(nextLine.length + 1);
|
|
2041
|
+
endEarly = true;
|
|
2042
|
+
}
|
|
2043
|
+
if (!endEarly) {
|
|
2044
|
+
const nextBulletRegex = this.rules.other.nextBulletRegex(indent);
|
|
2045
|
+
const hrRegex = this.rules.other.hrRegex(indent);
|
|
2046
|
+
const fencesBeginRegex = this.rules.other.fencesBeginRegex(indent);
|
|
2047
|
+
const headingBeginRegex = this.rules.other.headingBeginRegex(indent);
|
|
2048
|
+
const htmlBeginRegex = this.rules.other.htmlBeginRegex(indent);
|
|
2049
|
+
// Check if following lines should be included in List Item
|
|
2050
|
+
while (src) {
|
|
2051
|
+
const rawLine = src.split('\n', 1)[0];
|
|
2052
|
+
let nextLineWithoutTabs;
|
|
2053
|
+
nextLine = rawLine;
|
|
2054
|
+
// Re-align to follow commonmark nesting rules
|
|
2055
|
+
if (this.options.pedantic) {
|
|
2056
|
+
nextLine = nextLine.replace(this.rules.other.listReplaceNesting, ' ');
|
|
2057
|
+
nextLineWithoutTabs = nextLine;
|
|
2058
|
+
} else {
|
|
2059
|
+
nextLineWithoutTabs = nextLine.replace(this.rules.other.tabCharGlobal, ' ');
|
|
2060
|
+
}
|
|
2061
|
+
// End list item if found code fences
|
|
2062
|
+
if (fencesBeginRegex.test(nextLine)) {
|
|
2063
|
+
break;
|
|
2064
|
+
}
|
|
2065
|
+
// End list item if found start of new heading
|
|
2066
|
+
if (headingBeginRegex.test(nextLine)) {
|
|
2067
|
+
break;
|
|
2068
|
+
}
|
|
2069
|
+
// End list item if found start of html block
|
|
2070
|
+
if (htmlBeginRegex.test(nextLine)) {
|
|
2071
|
+
break;
|
|
2072
|
+
}
|
|
2073
|
+
// End list item if found start of new bullet
|
|
2074
|
+
if (nextBulletRegex.test(nextLine)) {
|
|
2075
|
+
break;
|
|
2076
|
+
}
|
|
2077
|
+
// Horizontal rule found
|
|
2078
|
+
if (hrRegex.test(nextLine)) {
|
|
2079
|
+
break;
|
|
2080
|
+
}
|
|
2081
|
+
if (nextLineWithoutTabs.search(this.rules.other.nonSpaceChar) >= indent || !nextLine.trim()) {
|
|
2082
|
+
// Dedent if possible
|
|
2083
|
+
itemContents += '\n' + nextLineWithoutTabs.slice(indent);
|
|
2084
|
+
} else {
|
|
2085
|
+
// not enough indentation
|
|
2086
|
+
if (blankLine) {
|
|
2087
|
+
break;
|
|
2088
|
+
}
|
|
2089
|
+
// paragraph continuation unless last line was a different block level element
|
|
2090
|
+
if (line.replace(this.rules.other.tabCharGlobal, ' ').search(this.rules.other.nonSpaceChar) >= 4) {
|
|
2091
|
+
// indented code block
|
|
2092
|
+
break;
|
|
2093
|
+
}
|
|
2094
|
+
if (fencesBeginRegex.test(line)) {
|
|
2095
|
+
break;
|
|
2096
|
+
}
|
|
2097
|
+
if (headingBeginRegex.test(line)) {
|
|
2098
|
+
break;
|
|
2099
|
+
}
|
|
2100
|
+
if (hrRegex.test(line)) {
|
|
2101
|
+
break;
|
|
2102
|
+
}
|
|
2103
|
+
itemContents += '\n' + nextLine;
|
|
2104
|
+
}
|
|
2105
|
+
if (!blankLine && !nextLine.trim()) {
|
|
2106
|
+
// Check if current line is blank
|
|
2107
|
+
blankLine = true;
|
|
2108
|
+
}
|
|
2109
|
+
raw += rawLine + '\n';
|
|
2110
|
+
src = src.substring(rawLine.length + 1);
|
|
2111
|
+
line = nextLineWithoutTabs.slice(indent);
|
|
2112
|
+
}
|
|
2113
|
+
}
|
|
2114
|
+
if (!list.loose) {
|
|
2115
|
+
// If the previous item ended with a blank line, the list is loose
|
|
2116
|
+
if (endsWithBlankLine) {
|
|
2117
|
+
list.loose = true;
|
|
2118
|
+
} else if (this.rules.other.doubleBlankLine.test(raw)) {
|
|
2119
|
+
endsWithBlankLine = true;
|
|
2120
|
+
}
|
|
2121
|
+
}
|
|
2122
|
+
let istask = null;
|
|
2123
|
+
let ischecked;
|
|
2124
|
+
// Check for task list items
|
|
2125
|
+
if (this.options.gfm) {
|
|
2126
|
+
istask = this.rules.other.listIsTask.exec(itemContents);
|
|
2127
|
+
if (istask) {
|
|
2128
|
+
ischecked = istask[0] !== '[ ] ';
|
|
2129
|
+
itemContents = itemContents.replace(this.rules.other.listReplaceTask, '');
|
|
2130
|
+
}
|
|
2131
|
+
}
|
|
2132
|
+
list.items.push({
|
|
2133
|
+
type: 'list_item',
|
|
2134
|
+
raw,
|
|
2135
|
+
task: !!istask,
|
|
2136
|
+
checked: ischecked,
|
|
2137
|
+
loose: false,
|
|
2138
|
+
text: itemContents,
|
|
2139
|
+
tokens: []
|
|
2140
|
+
});
|
|
2141
|
+
list.raw += raw;
|
|
2142
|
+
}
|
|
2143
|
+
// Do not consume newlines at end of final item. Alternatively, make itemRegex *start* with any newlines to simplify/speed up endsWithBlankLine logic
|
|
2144
|
+
const lastItem = list.items.at(-1);
|
|
2145
|
+
if (lastItem) {
|
|
2146
|
+
lastItem.raw = lastItem.raw.trimEnd();
|
|
2147
|
+
lastItem.text = lastItem.text.trimEnd();
|
|
2148
|
+
} else {
|
|
2149
|
+
// not a list since there were no items
|
|
2150
|
+
return;
|
|
2151
|
+
}
|
|
2152
|
+
list.raw = list.raw.trimEnd();
|
|
2153
|
+
// Item child tokens handled here at end because we needed to have the final item to trim it first
|
|
2154
|
+
for (let i = 0; i < list.items.length; i++) {
|
|
2155
|
+
this.lexer.state.top = false;
|
|
2156
|
+
list.items[i].tokens = this.lexer.blockTokens(list.items[i].text, []);
|
|
2157
|
+
if (!list.loose) {
|
|
2158
|
+
// Check if list should be loose
|
|
2159
|
+
const spacers = list.items[i].tokens.filter(t => t.type === 'space');
|
|
2160
|
+
const hasMultipleLineBreaks = spacers.length > 0 && spacers.some(t => this.rules.other.anyLine.test(t.raw));
|
|
2161
|
+
list.loose = hasMultipleLineBreaks;
|
|
2162
|
+
}
|
|
2163
|
+
}
|
|
2164
|
+
// Set all items to loose if list is loose
|
|
2165
|
+
if (list.loose) {
|
|
2166
|
+
for (let i = 0; i < list.items.length; i++) {
|
|
2167
|
+
list.items[i].loose = true;
|
|
2168
|
+
}
|
|
2169
|
+
}
|
|
2170
|
+
return list;
|
|
2171
|
+
}
|
|
2172
|
+
}
|
|
2173
|
+
html(src) {
|
|
2174
|
+
const cap = this.rules.block.html.exec(src);
|
|
2175
|
+
if (cap) {
|
|
2176
|
+
const token = {
|
|
2177
|
+
type: 'html',
|
|
2178
|
+
block: true,
|
|
2179
|
+
raw: cap[0],
|
|
2180
|
+
pre: cap[1] === 'pre' || cap[1] === 'script' || cap[1] === 'style',
|
|
2181
|
+
text: cap[0]
|
|
2182
|
+
};
|
|
2183
|
+
return token;
|
|
2184
|
+
}
|
|
2185
|
+
}
|
|
2186
|
+
def(src) {
|
|
2187
|
+
const cap = this.rules.block.def.exec(src);
|
|
2188
|
+
if (cap) {
|
|
2189
|
+
const tag = cap[1].toLowerCase().replace(this.rules.other.multipleSpaceGlobal, ' ');
|
|
2190
|
+
const href = cap[2] ? cap[2].replace(this.rules.other.hrefBrackets, '$1').replace(this.rules.inline.anyPunctuation, '$1') : '';
|
|
2191
|
+
const title = cap[3] ? cap[3].substring(1, cap[3].length - 1).replace(this.rules.inline.anyPunctuation, '$1') : cap[3];
|
|
2192
|
+
return {
|
|
2193
|
+
type: 'def',
|
|
2194
|
+
tag,
|
|
2195
|
+
raw: cap[0],
|
|
2196
|
+
href,
|
|
2197
|
+
title
|
|
2198
|
+
};
|
|
2199
|
+
}
|
|
2200
|
+
}
|
|
2201
|
+
table(src) {
|
|
2202
|
+
const cap = this.rules.block.table.exec(src);
|
|
2203
|
+
if (!cap) {
|
|
2204
|
+
return;
|
|
2205
|
+
}
|
|
2206
|
+
if (!this.rules.other.tableDelimiter.test(cap[2])) {
|
|
2207
|
+
// delimiter row must have a pipe (|) or colon (:) otherwise it is a setext heading
|
|
2208
|
+
return;
|
|
2209
|
+
}
|
|
2210
|
+
const headers = splitCells(cap[1]);
|
|
2211
|
+
const aligns = cap[2].replace(this.rules.other.tableAlignChars, '').split('|');
|
|
2212
|
+
const rows = cap[3]?.trim() ? cap[3].replace(this.rules.other.tableRowBlankLine, '').split('\n') : [];
|
|
2213
|
+
const item = {
|
|
2214
|
+
type: 'table',
|
|
2215
|
+
raw: cap[0],
|
|
2216
|
+
header: [],
|
|
2217
|
+
align: [],
|
|
2218
|
+
rows: []
|
|
2219
|
+
};
|
|
2220
|
+
if (headers.length !== aligns.length) {
|
|
2221
|
+
// header and align columns must be equal, rows can be different.
|
|
2222
|
+
return;
|
|
2223
|
+
}
|
|
2224
|
+
for (const align of aligns) {
|
|
2225
|
+
if (this.rules.other.tableAlignRight.test(align)) {
|
|
2226
|
+
item.align.push('right');
|
|
2227
|
+
} else if (this.rules.other.tableAlignCenter.test(align)) {
|
|
2228
|
+
item.align.push('center');
|
|
2229
|
+
} else if (this.rules.other.tableAlignLeft.test(align)) {
|
|
2230
|
+
item.align.push('left');
|
|
2231
|
+
} else {
|
|
2232
|
+
item.align.push(null);
|
|
2233
|
+
}
|
|
2234
|
+
}
|
|
2235
|
+
for (let i = 0; i < headers.length; i++) {
|
|
2236
|
+
item.header.push({
|
|
2237
|
+
text: headers[i],
|
|
2238
|
+
tokens: this.lexer.inline(headers[i]),
|
|
2239
|
+
header: true,
|
|
2240
|
+
align: item.align[i]
|
|
2241
|
+
});
|
|
2242
|
+
}
|
|
2243
|
+
for (const row of rows) {
|
|
2244
|
+
item.rows.push(splitCells(row, item.header.length).map((cell, i) => {
|
|
2245
|
+
return {
|
|
2246
|
+
text: cell,
|
|
2247
|
+
tokens: this.lexer.inline(cell),
|
|
2248
|
+
header: false,
|
|
2249
|
+
align: item.align[i]
|
|
2250
|
+
};
|
|
2251
|
+
}));
|
|
2252
|
+
}
|
|
2253
|
+
return item;
|
|
2254
|
+
}
|
|
2255
|
+
lheading(src) {
|
|
2256
|
+
const cap = this.rules.block.lheading.exec(src);
|
|
2257
|
+
if (cap) {
|
|
2258
|
+
return {
|
|
2259
|
+
type: 'heading',
|
|
2260
|
+
raw: cap[0],
|
|
2261
|
+
depth: cap[2].charAt(0) === '=' ? 1 : 2,
|
|
2262
|
+
text: cap[1],
|
|
2263
|
+
tokens: this.lexer.inline(cap[1])
|
|
2264
|
+
};
|
|
2265
|
+
}
|
|
2266
|
+
}
|
|
2267
|
+
paragraph(src) {
|
|
2268
|
+
const cap = this.rules.block.paragraph.exec(src);
|
|
2269
|
+
if (cap) {
|
|
2270
|
+
const text = cap[1].charAt(cap[1].length - 1) === '\n' ? cap[1].slice(0, -1) : cap[1];
|
|
2271
|
+
return {
|
|
2272
|
+
type: 'paragraph',
|
|
2273
|
+
raw: cap[0],
|
|
2274
|
+
text,
|
|
2275
|
+
tokens: this.lexer.inline(text)
|
|
2276
|
+
};
|
|
2277
|
+
}
|
|
2278
|
+
}
|
|
2279
|
+
text(src) {
|
|
2280
|
+
const cap = this.rules.block.text.exec(src);
|
|
2281
|
+
if (cap) {
|
|
2282
|
+
return {
|
|
2283
|
+
type: 'text',
|
|
2284
|
+
raw: cap[0],
|
|
2285
|
+
text: cap[0],
|
|
2286
|
+
tokens: this.lexer.inline(cap[0])
|
|
2287
|
+
};
|
|
2288
|
+
}
|
|
2289
|
+
}
|
|
2290
|
+
escape(src) {
|
|
2291
|
+
const cap = this.rules.inline.escape.exec(src);
|
|
2292
|
+
if (cap) {
|
|
2293
|
+
return {
|
|
2294
|
+
type: 'escape',
|
|
2295
|
+
raw: cap[0],
|
|
2296
|
+
text: cap[1]
|
|
2297
|
+
};
|
|
2298
|
+
}
|
|
2299
|
+
}
|
|
2300
|
+
tag(src) {
|
|
2301
|
+
const cap = this.rules.inline.tag.exec(src);
|
|
2302
|
+
if (cap) {
|
|
2303
|
+
if (!this.lexer.state.inLink && this.rules.other.startATag.test(cap[0])) {
|
|
2304
|
+
this.lexer.state.inLink = true;
|
|
2305
|
+
} else if (this.lexer.state.inLink && this.rules.other.endATag.test(cap[0])) {
|
|
2306
|
+
this.lexer.state.inLink = false;
|
|
2307
|
+
}
|
|
2308
|
+
if (!this.lexer.state.inRawBlock && this.rules.other.startPreScriptTag.test(cap[0])) {
|
|
2309
|
+
this.lexer.state.inRawBlock = true;
|
|
2310
|
+
} else if (this.lexer.state.inRawBlock && this.rules.other.endPreScriptTag.test(cap[0])) {
|
|
2311
|
+
this.lexer.state.inRawBlock = false;
|
|
2312
|
+
}
|
|
2313
|
+
return {
|
|
2314
|
+
type: 'html',
|
|
2315
|
+
raw: cap[0],
|
|
2316
|
+
inLink: this.lexer.state.inLink,
|
|
2317
|
+
inRawBlock: this.lexer.state.inRawBlock,
|
|
2318
|
+
block: false,
|
|
2319
|
+
text: cap[0]
|
|
2320
|
+
};
|
|
2321
|
+
}
|
|
2322
|
+
}
|
|
2323
|
+
link(src) {
|
|
2324
|
+
const cap = this.rules.inline.link.exec(src);
|
|
2325
|
+
if (cap) {
|
|
2326
|
+
const trimmedUrl = cap[2].trim();
|
|
2327
|
+
if (!this.options.pedantic && this.rules.other.startAngleBracket.test(trimmedUrl)) {
|
|
2328
|
+
// commonmark requires matching angle brackets
|
|
2329
|
+
if (!this.rules.other.endAngleBracket.test(trimmedUrl)) {
|
|
2330
|
+
return;
|
|
2331
|
+
}
|
|
2332
|
+
// ending angle bracket cannot be escaped
|
|
2333
|
+
const rtrimSlash = rtrim(trimmedUrl.slice(0, -1), '\\');
|
|
2334
|
+
if ((trimmedUrl.length - rtrimSlash.length) % 2 === 0) {
|
|
2335
|
+
return;
|
|
2336
|
+
}
|
|
2337
|
+
} else {
|
|
2338
|
+
// find closing parenthesis
|
|
2339
|
+
const lastParenIndex = findClosingBracket(cap[2], '()');
|
|
2340
|
+
if (lastParenIndex > -1) {
|
|
2341
|
+
const start = cap[0].indexOf('!') === 0 ? 5 : 4;
|
|
2342
|
+
const linkLen = start + cap[1].length + lastParenIndex;
|
|
2343
|
+
cap[2] = cap[2].substring(0, lastParenIndex);
|
|
2344
|
+
cap[0] = cap[0].substring(0, linkLen).trim();
|
|
2345
|
+
cap[3] = '';
|
|
2346
|
+
}
|
|
2347
|
+
}
|
|
2348
|
+
let href = cap[2];
|
|
2349
|
+
let title = '';
|
|
2350
|
+
if (this.options.pedantic) {
|
|
2351
|
+
// split pedantic href and title
|
|
2352
|
+
const link = this.rules.other.pedanticHrefTitle.exec(href);
|
|
2353
|
+
if (link) {
|
|
2354
|
+
href = link[1];
|
|
2355
|
+
title = link[3];
|
|
2356
|
+
}
|
|
2357
|
+
} else {
|
|
2358
|
+
title = cap[3] ? cap[3].slice(1, -1) : '';
|
|
2359
|
+
}
|
|
2360
|
+
href = href.trim();
|
|
2361
|
+
if (this.rules.other.startAngleBracket.test(href)) {
|
|
2362
|
+
if (this.options.pedantic && !this.rules.other.endAngleBracket.test(trimmedUrl)) {
|
|
2363
|
+
// pedantic allows starting angle bracket without ending angle bracket
|
|
2364
|
+
href = href.slice(1);
|
|
2365
|
+
} else {
|
|
2366
|
+
href = href.slice(1, -1);
|
|
2367
|
+
}
|
|
2368
|
+
}
|
|
2369
|
+
return outputLink(cap, {
|
|
2370
|
+
href: href ? href.replace(this.rules.inline.anyPunctuation, '$1') : href,
|
|
2371
|
+
title: title ? title.replace(this.rules.inline.anyPunctuation, '$1') : title
|
|
2372
|
+
}, cap[0], this.lexer, this.rules);
|
|
2373
|
+
}
|
|
2374
|
+
}
|
|
2375
|
+
reflink(src, links) {
|
|
2376
|
+
let cap;
|
|
2377
|
+
if ((cap = this.rules.inline.reflink.exec(src)) || (cap = this.rules.inline.nolink.exec(src))) {
|
|
2378
|
+
const linkString = (cap[2] || cap[1]).replace(this.rules.other.multipleSpaceGlobal, ' ');
|
|
2379
|
+
const link = links[linkString.toLowerCase()];
|
|
2380
|
+
if (!link) {
|
|
2381
|
+
const text = cap[0].charAt(0);
|
|
2382
|
+
return {
|
|
2383
|
+
type: 'text',
|
|
2384
|
+
raw: text,
|
|
2385
|
+
text
|
|
2386
|
+
};
|
|
2387
|
+
}
|
|
2388
|
+
return outputLink(cap, link, cap[0], this.lexer, this.rules);
|
|
2389
|
+
}
|
|
2390
|
+
}
|
|
2391
|
+
emStrong(src, maskedSrc, prevChar = '') {
|
|
2392
|
+
let match = this.rules.inline.emStrongLDelim.exec(src);
|
|
2393
|
+
if (!match) return;
|
|
2394
|
+
// _ can't be between two alphanumerics. \p{L}\p{N} includes non-english alphabet/numbers as well
|
|
2395
|
+
if (match[3] && prevChar.match(this.rules.other.unicodeAlphaNumeric)) return;
|
|
2396
|
+
const nextChar = match[1] || match[2] || '';
|
|
2397
|
+
if (!nextChar || !prevChar || this.rules.inline.punctuation.exec(prevChar)) {
|
|
2398
|
+
// unicode Regex counts emoji as 1 char; spread into array for proper count (used multiple times below)
|
|
2399
|
+
const lLength = [...match[0]].length - 1;
|
|
2400
|
+
let rDelim,
|
|
2401
|
+
rLength,
|
|
2402
|
+
delimTotal = lLength,
|
|
2403
|
+
midDelimTotal = 0;
|
|
2404
|
+
const endReg = match[0][0] === '*' ? this.rules.inline.emStrongRDelimAst : this.rules.inline.emStrongRDelimUnd;
|
|
2405
|
+
endReg.lastIndex = 0;
|
|
2406
|
+
// Clip maskedSrc to same section of string as src (move to lexer?)
|
|
2407
|
+
maskedSrc = maskedSrc.slice(-1 * src.length + lLength);
|
|
2408
|
+
while ((match = endReg.exec(maskedSrc)) != null) {
|
|
2409
|
+
rDelim = match[1] || match[2] || match[3] || match[4] || match[5] || match[6];
|
|
2410
|
+
if (!rDelim) continue; // skip single * in __abc*abc__
|
|
2411
|
+
rLength = [...rDelim].length;
|
|
2412
|
+
if (match[3] || match[4]) {
|
|
2413
|
+
// found another Left Delim
|
|
2414
|
+
delimTotal += rLength;
|
|
2415
|
+
continue;
|
|
2416
|
+
} else if (match[5] || match[6]) {
|
|
2417
|
+
// either Left or Right Delim
|
|
2418
|
+
if (lLength % 3 && !((lLength + rLength) % 3)) {
|
|
2419
|
+
midDelimTotal += rLength;
|
|
2420
|
+
continue; // CommonMark Emphasis Rules 9-10
|
|
2421
|
+
}
|
|
2422
|
+
}
|
|
2423
|
+
delimTotal -= rLength;
|
|
2424
|
+
if (delimTotal > 0) continue; // Haven't found enough closing delimiters
|
|
2425
|
+
// Remove extra characters. *a*** -> *a*
|
|
2426
|
+
rLength = Math.min(rLength, rLength + delimTotal + midDelimTotal);
|
|
2427
|
+
// char length can be >1 for unicode characters;
|
|
2428
|
+
const lastCharLength = [...match[0]][0].length;
|
|
2429
|
+
const raw = src.slice(0, lLength + match.index + lastCharLength + rLength);
|
|
2430
|
+
// Create `em` if smallest delimiter has odd char count. *a***
|
|
2431
|
+
if (Math.min(lLength, rLength) % 2) {
|
|
2432
|
+
const text = raw.slice(1, -1);
|
|
2433
|
+
return {
|
|
2434
|
+
type: 'em',
|
|
2435
|
+
raw,
|
|
2436
|
+
text,
|
|
2437
|
+
tokens: this.lexer.inlineTokens(text)
|
|
2438
|
+
};
|
|
2439
|
+
}
|
|
2440
|
+
// Create 'strong' if smallest delimiter has even char count. **a***
|
|
2441
|
+
const text = raw.slice(2, -2);
|
|
2442
|
+
return {
|
|
2443
|
+
type: 'strong',
|
|
2444
|
+
raw,
|
|
2445
|
+
text,
|
|
2446
|
+
tokens: this.lexer.inlineTokens(text)
|
|
2447
|
+
};
|
|
2448
|
+
}
|
|
2449
|
+
}
|
|
2450
|
+
}
|
|
2451
|
+
codespan(src) {
|
|
2452
|
+
const cap = this.rules.inline.code.exec(src);
|
|
2453
|
+
if (cap) {
|
|
2454
|
+
let text = cap[2].replace(this.rules.other.newLineCharGlobal, ' ');
|
|
2455
|
+
const hasNonSpaceChars = this.rules.other.nonSpaceChar.test(text);
|
|
2456
|
+
const hasSpaceCharsOnBothEnds = this.rules.other.startingSpaceChar.test(text) && this.rules.other.endingSpaceChar.test(text);
|
|
2457
|
+
if (hasNonSpaceChars && hasSpaceCharsOnBothEnds) {
|
|
2458
|
+
text = text.substring(1, text.length - 1);
|
|
2459
|
+
}
|
|
2460
|
+
return {
|
|
2461
|
+
type: 'codespan',
|
|
2462
|
+
raw: cap[0],
|
|
2463
|
+
text
|
|
2464
|
+
};
|
|
2465
|
+
}
|
|
2466
|
+
}
|
|
2467
|
+
br(src) {
|
|
2468
|
+
const cap = this.rules.inline.br.exec(src);
|
|
2469
|
+
if (cap) {
|
|
2470
|
+
return {
|
|
2471
|
+
type: 'br',
|
|
2472
|
+
raw: cap[0]
|
|
2473
|
+
};
|
|
2474
|
+
}
|
|
2475
|
+
}
|
|
2476
|
+
del(src) {
|
|
2477
|
+
const cap = this.rules.inline.del.exec(src);
|
|
2478
|
+
if (cap) {
|
|
2479
|
+
return {
|
|
2480
|
+
type: 'del',
|
|
2481
|
+
raw: cap[0],
|
|
2482
|
+
text: cap[2],
|
|
2483
|
+
tokens: this.lexer.inlineTokens(cap[2])
|
|
2484
|
+
};
|
|
2485
|
+
}
|
|
2486
|
+
}
|
|
2487
|
+
autolink(src) {
|
|
2488
|
+
const cap = this.rules.inline.autolink.exec(src);
|
|
2489
|
+
if (cap) {
|
|
2490
|
+
let text, href;
|
|
2491
|
+
if (cap[2] === '@') {
|
|
2492
|
+
text = cap[1];
|
|
2493
|
+
href = 'mailto:' + text;
|
|
2494
|
+
} else {
|
|
2495
|
+
text = cap[1];
|
|
2496
|
+
href = text;
|
|
2497
|
+
}
|
|
2498
|
+
return {
|
|
2499
|
+
type: 'link',
|
|
2500
|
+
raw: cap[0],
|
|
2501
|
+
text,
|
|
2502
|
+
href,
|
|
2503
|
+
tokens: [{
|
|
2504
|
+
type: 'text',
|
|
2505
|
+
raw: text,
|
|
2506
|
+
text
|
|
2507
|
+
}]
|
|
2508
|
+
};
|
|
2509
|
+
}
|
|
2510
|
+
}
|
|
2511
|
+
url(src) {
|
|
2512
|
+
let cap;
|
|
2513
|
+
if (cap = this.rules.inline.url.exec(src)) {
|
|
2514
|
+
let text, href;
|
|
2515
|
+
if (cap[2] === '@') {
|
|
2516
|
+
text = cap[0];
|
|
2517
|
+
href = 'mailto:' + text;
|
|
2518
|
+
} else {
|
|
2519
|
+
// do extended autolink path validation
|
|
2520
|
+
let prevCapZero;
|
|
2521
|
+
do {
|
|
2522
|
+
prevCapZero = cap[0];
|
|
2523
|
+
cap[0] = this.rules.inline._backpedal.exec(cap[0])?.[0] ?? '';
|
|
2524
|
+
} while (prevCapZero !== cap[0]);
|
|
2525
|
+
text = cap[0];
|
|
2526
|
+
if (cap[1] === 'www.') {
|
|
2527
|
+
href = 'http://' + cap[0];
|
|
2528
|
+
} else {
|
|
2529
|
+
href = cap[0];
|
|
2530
|
+
}
|
|
2531
|
+
}
|
|
2532
|
+
return {
|
|
2533
|
+
type: 'link',
|
|
2534
|
+
raw: cap[0],
|
|
2535
|
+
text,
|
|
2536
|
+
href,
|
|
2537
|
+
tokens: [{
|
|
2538
|
+
type: 'text',
|
|
2539
|
+
raw: text,
|
|
2540
|
+
text
|
|
2541
|
+
}]
|
|
2542
|
+
};
|
|
2543
|
+
}
|
|
2544
|
+
}
|
|
2545
|
+
inlineText(src) {
|
|
2546
|
+
const cap = this.rules.inline.text.exec(src);
|
|
2547
|
+
if (cap) {
|
|
2548
|
+
const escaped = this.lexer.state.inRawBlock;
|
|
2549
|
+
return {
|
|
2550
|
+
type: 'text',
|
|
2551
|
+
raw: cap[0],
|
|
2552
|
+
text: cap[0],
|
|
2553
|
+
escaped
|
|
2554
|
+
};
|
|
2555
|
+
}
|
|
2556
|
+
}
|
|
2557
|
+
}
|
|
2558
|
+
|
|
2559
|
+
/**
|
|
2560
|
+
* Block Lexer
|
|
2561
|
+
*/
|
|
2562
|
+
class _Lexer {
|
|
2563
|
+
tokens;
|
|
2564
|
+
options;
|
|
2565
|
+
state;
|
|
2566
|
+
tokenizer;
|
|
2567
|
+
inlineQueue;
|
|
2568
|
+
constructor(options) {
|
|
2569
|
+
// TokenList cannot be created in one go
|
|
2570
|
+
this.tokens = [];
|
|
2571
|
+
this.tokens.links = Object.create(null);
|
|
2572
|
+
this.options = options || _defaults;
|
|
2573
|
+
this.options.tokenizer = this.options.tokenizer || new _Tokenizer();
|
|
2574
|
+
this.tokenizer = this.options.tokenizer;
|
|
2575
|
+
this.tokenizer.options = this.options;
|
|
2576
|
+
this.tokenizer.lexer = this;
|
|
2577
|
+
this.inlineQueue = [];
|
|
2578
|
+
this.state = {
|
|
2579
|
+
inLink: false,
|
|
2580
|
+
inRawBlock: false,
|
|
2581
|
+
top: true
|
|
2582
|
+
};
|
|
2583
|
+
const rules = {
|
|
2584
|
+
other,
|
|
2585
|
+
block: block.normal,
|
|
2586
|
+
inline: inline.normal
|
|
2587
|
+
};
|
|
2588
|
+
if (this.options.pedantic) {
|
|
2589
|
+
rules.block = block.pedantic;
|
|
2590
|
+
rules.inline = inline.pedantic;
|
|
2591
|
+
} else if (this.options.gfm) {
|
|
2592
|
+
rules.block = block.gfm;
|
|
2593
|
+
if (this.options.breaks) {
|
|
2594
|
+
rules.inline = inline.breaks;
|
|
2595
|
+
} else {
|
|
2596
|
+
rules.inline = inline.gfm;
|
|
2597
|
+
}
|
|
2598
|
+
}
|
|
2599
|
+
this.tokenizer.rules = rules;
|
|
2600
|
+
}
|
|
2601
|
+
/**
|
|
2602
|
+
* Expose Rules
|
|
2603
|
+
*/
|
|
2604
|
+
static get rules() {
|
|
2605
|
+
return {
|
|
2606
|
+
block,
|
|
2607
|
+
inline
|
|
2608
|
+
};
|
|
2609
|
+
}
|
|
2610
|
+
/**
|
|
2611
|
+
* Static Lex Method
|
|
2612
|
+
*/
|
|
2613
|
+
static lex(src, options) {
|
|
2614
|
+
const lexer = new _Lexer(options);
|
|
2615
|
+
return lexer.lex(src);
|
|
2616
|
+
}
|
|
2617
|
+
/**
|
|
2618
|
+
* Static Lex Inline Method
|
|
2619
|
+
*/
|
|
2620
|
+
static lexInline(src, options) {
|
|
2621
|
+
const lexer = new _Lexer(options);
|
|
2622
|
+
return lexer.inlineTokens(src);
|
|
2623
|
+
}
|
|
2624
|
+
/**
|
|
2625
|
+
* Preprocessing
|
|
2626
|
+
*/
|
|
2627
|
+
lex(src) {
|
|
2628
|
+
src = src.replace(other.carriageReturn, '\n');
|
|
2629
|
+
this.blockTokens(src, this.tokens);
|
|
2630
|
+
for (let i = 0; i < this.inlineQueue.length; i++) {
|
|
2631
|
+
const next = this.inlineQueue[i];
|
|
2632
|
+
this.inlineTokens(next.src, next.tokens);
|
|
2633
|
+
}
|
|
2634
|
+
this.inlineQueue = [];
|
|
2635
|
+
return this.tokens;
|
|
2636
|
+
}
|
|
2637
|
+
blockTokens(src, tokens = [], lastParagraphClipped = false) {
|
|
2638
|
+
if (this.options.pedantic) {
|
|
2639
|
+
src = src.replace(other.tabCharGlobal, ' ').replace(other.spaceLine, '');
|
|
2640
|
+
}
|
|
2641
|
+
while (src) {
|
|
2642
|
+
let token;
|
|
2643
|
+
if (this.options.extensions?.block?.some(extTokenizer => {
|
|
2644
|
+
if (token = extTokenizer.call({
|
|
2645
|
+
lexer: this
|
|
2646
|
+
}, src, tokens)) {
|
|
2647
|
+
src = src.substring(token.raw.length);
|
|
2648
|
+
tokens.push(token);
|
|
2649
|
+
return true;
|
|
2650
|
+
}
|
|
2651
|
+
return false;
|
|
2652
|
+
})) {
|
|
2653
|
+
continue;
|
|
2654
|
+
}
|
|
2655
|
+
// newline
|
|
2656
|
+
if (token = this.tokenizer.space(src)) {
|
|
2657
|
+
src = src.substring(token.raw.length);
|
|
2658
|
+
const lastToken = tokens.at(-1);
|
|
2659
|
+
if (token.raw.length === 1 && lastToken !== undefined) {
|
|
2660
|
+
// if there's a single \n as a spacer, it's terminating the last line,
|
|
2661
|
+
// so move it there so that we don't get unnecessary paragraph tags
|
|
2662
|
+
lastToken.raw += '\n';
|
|
2663
|
+
} else {
|
|
2664
|
+
tokens.push(token);
|
|
2665
|
+
}
|
|
2666
|
+
continue;
|
|
2667
|
+
}
|
|
2668
|
+
// code
|
|
2669
|
+
if (token = this.tokenizer.code(src)) {
|
|
2670
|
+
src = src.substring(token.raw.length);
|
|
2671
|
+
const lastToken = tokens.at(-1);
|
|
2672
|
+
// An indented code block cannot interrupt a paragraph.
|
|
2673
|
+
if (lastToken?.type === 'paragraph' || lastToken?.type === 'text') {
|
|
2674
|
+
lastToken.raw += '\n' + token.raw;
|
|
2675
|
+
lastToken.text += '\n' + token.text;
|
|
2676
|
+
this.inlineQueue.at(-1).src = lastToken.text;
|
|
2677
|
+
} else {
|
|
2678
|
+
tokens.push(token);
|
|
2679
|
+
}
|
|
2680
|
+
continue;
|
|
2681
|
+
}
|
|
2682
|
+
// fences
|
|
2683
|
+
if (token = this.tokenizer.fences(src)) {
|
|
2684
|
+
src = src.substring(token.raw.length);
|
|
2685
|
+
tokens.push(token);
|
|
2686
|
+
continue;
|
|
2687
|
+
}
|
|
2688
|
+
// heading
|
|
2689
|
+
if (token = this.tokenizer.heading(src)) {
|
|
2690
|
+
src = src.substring(token.raw.length);
|
|
2691
|
+
tokens.push(token);
|
|
2692
|
+
continue;
|
|
2693
|
+
}
|
|
2694
|
+
// hr
|
|
2695
|
+
if (token = this.tokenizer.hr(src)) {
|
|
2696
|
+
src = src.substring(token.raw.length);
|
|
2697
|
+
tokens.push(token);
|
|
2698
|
+
continue;
|
|
2699
|
+
}
|
|
2700
|
+
// blockquote
|
|
2701
|
+
if (token = this.tokenizer.blockquote(src)) {
|
|
2702
|
+
src = src.substring(token.raw.length);
|
|
2703
|
+
tokens.push(token);
|
|
2704
|
+
continue;
|
|
2705
|
+
}
|
|
2706
|
+
// list
|
|
2707
|
+
if (token = this.tokenizer.list(src)) {
|
|
2708
|
+
src = src.substring(token.raw.length);
|
|
2709
|
+
tokens.push(token);
|
|
2710
|
+
continue;
|
|
2711
|
+
}
|
|
2712
|
+
// html
|
|
2713
|
+
if (token = this.tokenizer.html(src)) {
|
|
2714
|
+
src = src.substring(token.raw.length);
|
|
2715
|
+
tokens.push(token);
|
|
2716
|
+
continue;
|
|
2717
|
+
}
|
|
2718
|
+
// def
|
|
2719
|
+
if (token = this.tokenizer.def(src)) {
|
|
2720
|
+
src = src.substring(token.raw.length);
|
|
2721
|
+
const lastToken = tokens.at(-1);
|
|
2722
|
+
if (lastToken?.type === 'paragraph' || lastToken?.type === 'text') {
|
|
2723
|
+
lastToken.raw += '\n' + token.raw;
|
|
2724
|
+
lastToken.text += '\n' + token.raw;
|
|
2725
|
+
this.inlineQueue.at(-1).src = lastToken.text;
|
|
2726
|
+
} else if (!this.tokens.links[token.tag]) {
|
|
2727
|
+
this.tokens.links[token.tag] = {
|
|
2728
|
+
href: token.href,
|
|
2729
|
+
title: token.title
|
|
2730
|
+
};
|
|
2731
|
+
}
|
|
2732
|
+
continue;
|
|
2733
|
+
}
|
|
2734
|
+
// table (gfm)
|
|
2735
|
+
if (token = this.tokenizer.table(src)) {
|
|
2736
|
+
src = src.substring(token.raw.length);
|
|
2737
|
+
tokens.push(token);
|
|
2738
|
+
continue;
|
|
2739
|
+
}
|
|
2740
|
+
// lheading
|
|
2741
|
+
if (token = this.tokenizer.lheading(src)) {
|
|
2742
|
+
src = src.substring(token.raw.length);
|
|
2743
|
+
tokens.push(token);
|
|
2744
|
+
continue;
|
|
2745
|
+
}
|
|
2746
|
+
// top-level paragraph
|
|
2747
|
+
// prevent paragraph consuming extensions by clipping 'src' to extension start
|
|
2748
|
+
let cutSrc = src;
|
|
2749
|
+
if (this.options.extensions?.startBlock) {
|
|
2750
|
+
let startIndex = Infinity;
|
|
2751
|
+
const tempSrc = src.slice(1);
|
|
2752
|
+
let tempStart;
|
|
2753
|
+
this.options.extensions.startBlock.forEach(getStartIndex => {
|
|
2754
|
+
tempStart = getStartIndex.call({
|
|
2755
|
+
lexer: this
|
|
2756
|
+
}, tempSrc);
|
|
2757
|
+
if (typeof tempStart === 'number' && tempStart >= 0) {
|
|
2758
|
+
startIndex = Math.min(startIndex, tempStart);
|
|
2759
|
+
}
|
|
2760
|
+
});
|
|
2761
|
+
if (startIndex < Infinity && startIndex >= 0) {
|
|
2762
|
+
cutSrc = src.substring(0, startIndex + 1);
|
|
2763
|
+
}
|
|
2764
|
+
}
|
|
2765
|
+
if (this.state.top && (token = this.tokenizer.paragraph(cutSrc))) {
|
|
2766
|
+
const lastToken = tokens.at(-1);
|
|
2767
|
+
if (lastParagraphClipped && lastToken?.type === 'paragraph') {
|
|
2768
|
+
lastToken.raw += '\n' + token.raw;
|
|
2769
|
+
lastToken.text += '\n' + token.text;
|
|
2770
|
+
this.inlineQueue.pop();
|
|
2771
|
+
this.inlineQueue.at(-1).src = lastToken.text;
|
|
2772
|
+
} else {
|
|
2773
|
+
tokens.push(token);
|
|
2774
|
+
}
|
|
2775
|
+
lastParagraphClipped = cutSrc.length !== src.length;
|
|
2776
|
+
src = src.substring(token.raw.length);
|
|
2777
|
+
continue;
|
|
2778
|
+
}
|
|
2779
|
+
// text
|
|
2780
|
+
if (token = this.tokenizer.text(src)) {
|
|
2781
|
+
src = src.substring(token.raw.length);
|
|
2782
|
+
const lastToken = tokens.at(-1);
|
|
2783
|
+
if (lastToken?.type === 'text') {
|
|
2784
|
+
lastToken.raw += '\n' + token.raw;
|
|
2785
|
+
lastToken.text += '\n' + token.text;
|
|
2786
|
+
this.inlineQueue.pop();
|
|
2787
|
+
this.inlineQueue.at(-1).src = lastToken.text;
|
|
2788
|
+
} else {
|
|
2789
|
+
tokens.push(token);
|
|
2790
|
+
}
|
|
2791
|
+
continue;
|
|
2792
|
+
}
|
|
2793
|
+
if (src) {
|
|
2794
|
+
const errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0);
|
|
2795
|
+
if (this.options.silent) {
|
|
2796
|
+
console.error(errMsg);
|
|
2797
|
+
break;
|
|
2798
|
+
} else {
|
|
2799
|
+
throw new Error(errMsg);
|
|
2800
|
+
}
|
|
2801
|
+
}
|
|
2802
|
+
}
|
|
2803
|
+
this.state.top = true;
|
|
2804
|
+
return tokens;
|
|
2805
|
+
}
|
|
2806
|
+
inline(src, tokens = []) {
|
|
2807
|
+
this.inlineQueue.push({
|
|
2808
|
+
src,
|
|
2809
|
+
tokens
|
|
2810
|
+
});
|
|
2811
|
+
return tokens;
|
|
2812
|
+
}
|
|
2813
|
+
/**
|
|
2814
|
+
* Lexing/Compiling
|
|
2815
|
+
*/
|
|
2816
|
+
inlineTokens(src, tokens = []) {
|
|
2817
|
+
// String with links masked to avoid interference with em and strong
|
|
2818
|
+
let maskedSrc = src;
|
|
2819
|
+
let match = null;
|
|
2820
|
+
// Mask out reflinks
|
|
2821
|
+
if (this.tokens.links) {
|
|
2822
|
+
const links = Object.keys(this.tokens.links);
|
|
2823
|
+
if (links.length > 0) {
|
|
2824
|
+
while ((match = this.tokenizer.rules.inline.reflinkSearch.exec(maskedSrc)) != null) {
|
|
2825
|
+
if (links.includes(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) {
|
|
2826
|
+
maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex);
|
|
2827
|
+
}
|
|
2828
|
+
}
|
|
2829
|
+
}
|
|
2830
|
+
}
|
|
2831
|
+
// Mask out other blocks
|
|
2832
|
+
while ((match = this.tokenizer.rules.inline.blockSkip.exec(maskedSrc)) != null) {
|
|
2833
|
+
maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);
|
|
2834
|
+
}
|
|
2835
|
+
// Mask out escaped characters
|
|
2836
|
+
while ((match = this.tokenizer.rules.inline.anyPunctuation.exec(maskedSrc)) != null) {
|
|
2837
|
+
maskedSrc = maskedSrc.slice(0, match.index) + '++' + maskedSrc.slice(this.tokenizer.rules.inline.anyPunctuation.lastIndex);
|
|
2838
|
+
}
|
|
2839
|
+
let keepPrevChar = false;
|
|
2840
|
+
let prevChar = '';
|
|
2841
|
+
while (src) {
|
|
2842
|
+
if (!keepPrevChar) {
|
|
2843
|
+
prevChar = '';
|
|
2844
|
+
}
|
|
2845
|
+
keepPrevChar = false;
|
|
2846
|
+
let token;
|
|
2847
|
+
// extensions
|
|
2848
|
+
if (this.options.extensions?.inline?.some(extTokenizer => {
|
|
2849
|
+
if (token = extTokenizer.call({
|
|
2850
|
+
lexer: this
|
|
2851
|
+
}, src, tokens)) {
|
|
2852
|
+
src = src.substring(token.raw.length);
|
|
2853
|
+
tokens.push(token);
|
|
2854
|
+
return true;
|
|
2855
|
+
}
|
|
2856
|
+
return false;
|
|
2857
|
+
})) {
|
|
2858
|
+
continue;
|
|
2859
|
+
}
|
|
2860
|
+
// escape
|
|
2861
|
+
if (token = this.tokenizer.escape(src)) {
|
|
2862
|
+
src = src.substring(token.raw.length);
|
|
2863
|
+
tokens.push(token);
|
|
2864
|
+
continue;
|
|
2865
|
+
}
|
|
2866
|
+
// tag
|
|
2867
|
+
if (token = this.tokenizer.tag(src)) {
|
|
2868
|
+
src = src.substring(token.raw.length);
|
|
2869
|
+
tokens.push(token);
|
|
2870
|
+
continue;
|
|
2871
|
+
}
|
|
2872
|
+
// link
|
|
2873
|
+
if (token = this.tokenizer.link(src)) {
|
|
2874
|
+
src = src.substring(token.raw.length);
|
|
2875
|
+
tokens.push(token);
|
|
2876
|
+
continue;
|
|
2877
|
+
}
|
|
2878
|
+
// reflink, nolink
|
|
2879
|
+
if (token = this.tokenizer.reflink(src, this.tokens.links)) {
|
|
2880
|
+
src = src.substring(token.raw.length);
|
|
2881
|
+
const lastToken = tokens.at(-1);
|
|
2882
|
+
if (token.type === 'text' && lastToken?.type === 'text') {
|
|
2883
|
+
lastToken.raw += token.raw;
|
|
2884
|
+
lastToken.text += token.text;
|
|
2885
|
+
} else {
|
|
2886
|
+
tokens.push(token);
|
|
2887
|
+
}
|
|
2888
|
+
continue;
|
|
2889
|
+
}
|
|
2890
|
+
// em & strong
|
|
2891
|
+
if (token = this.tokenizer.emStrong(src, maskedSrc, prevChar)) {
|
|
2892
|
+
src = src.substring(token.raw.length);
|
|
2893
|
+
tokens.push(token);
|
|
2894
|
+
continue;
|
|
2895
|
+
}
|
|
2896
|
+
// code
|
|
2897
|
+
if (token = this.tokenizer.codespan(src)) {
|
|
2898
|
+
src = src.substring(token.raw.length);
|
|
2899
|
+
tokens.push(token);
|
|
2900
|
+
continue;
|
|
2901
|
+
}
|
|
2902
|
+
// br
|
|
2903
|
+
if (token = this.tokenizer.br(src)) {
|
|
2904
|
+
src = src.substring(token.raw.length);
|
|
2905
|
+
tokens.push(token);
|
|
2906
|
+
continue;
|
|
2907
|
+
}
|
|
2908
|
+
// del (gfm)
|
|
2909
|
+
if (token = this.tokenizer.del(src)) {
|
|
2910
|
+
src = src.substring(token.raw.length);
|
|
2911
|
+
tokens.push(token);
|
|
2912
|
+
continue;
|
|
2913
|
+
}
|
|
2914
|
+
// autolink
|
|
2915
|
+
if (token = this.tokenizer.autolink(src)) {
|
|
2916
|
+
src = src.substring(token.raw.length);
|
|
2917
|
+
tokens.push(token);
|
|
2918
|
+
continue;
|
|
2919
|
+
}
|
|
2920
|
+
// url (gfm)
|
|
2921
|
+
if (!this.state.inLink && (token = this.tokenizer.url(src))) {
|
|
2922
|
+
src = src.substring(token.raw.length);
|
|
2923
|
+
tokens.push(token);
|
|
2924
|
+
continue;
|
|
2925
|
+
}
|
|
2926
|
+
// text
|
|
2927
|
+
// prevent inlineText consuming extensions by clipping 'src' to extension start
|
|
2928
|
+
let cutSrc = src;
|
|
2929
|
+
if (this.options.extensions?.startInline) {
|
|
2930
|
+
let startIndex = Infinity;
|
|
2931
|
+
const tempSrc = src.slice(1);
|
|
2932
|
+
let tempStart;
|
|
2933
|
+
this.options.extensions.startInline.forEach(getStartIndex => {
|
|
2934
|
+
tempStart = getStartIndex.call({
|
|
2935
|
+
lexer: this
|
|
2936
|
+
}, tempSrc);
|
|
2937
|
+
if (typeof tempStart === 'number' && tempStart >= 0) {
|
|
2938
|
+
startIndex = Math.min(startIndex, tempStart);
|
|
2939
|
+
}
|
|
2940
|
+
});
|
|
2941
|
+
if (startIndex < Infinity && startIndex >= 0) {
|
|
2942
|
+
cutSrc = src.substring(0, startIndex + 1);
|
|
2943
|
+
}
|
|
2944
|
+
}
|
|
2945
|
+
if (token = this.tokenizer.inlineText(cutSrc)) {
|
|
2946
|
+
src = src.substring(token.raw.length);
|
|
2947
|
+
if (token.raw.slice(-1) !== '_') {
|
|
2948
|
+
// Track prevChar before string of ____ started
|
|
2949
|
+
prevChar = token.raw.slice(-1);
|
|
2950
|
+
}
|
|
2951
|
+
keepPrevChar = true;
|
|
2952
|
+
const lastToken = tokens.at(-1);
|
|
2953
|
+
if (lastToken?.type === 'text') {
|
|
2954
|
+
lastToken.raw += token.raw;
|
|
2955
|
+
lastToken.text += token.text;
|
|
2956
|
+
} else {
|
|
2957
|
+
tokens.push(token);
|
|
2958
|
+
}
|
|
2959
|
+
continue;
|
|
2960
|
+
}
|
|
2961
|
+
if (src) {
|
|
2962
|
+
const errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0);
|
|
2963
|
+
if (this.options.silent) {
|
|
2964
|
+
console.error(errMsg);
|
|
2965
|
+
break;
|
|
2966
|
+
} else {
|
|
2967
|
+
throw new Error(errMsg);
|
|
2968
|
+
}
|
|
2969
|
+
}
|
|
2970
|
+
}
|
|
2971
|
+
return tokens;
|
|
2972
|
+
}
|
|
2973
|
+
}
|
|
2974
|
+
|
|
2975
|
+
/**
|
|
2976
|
+
* Renderer
|
|
2977
|
+
*/
|
|
2978
|
+
class _Renderer {
|
|
2979
|
+
options;
|
|
2980
|
+
parser; // set by the parser
|
|
2981
|
+
constructor(options) {
|
|
2982
|
+
this.options = options || _defaults;
|
|
2983
|
+
}
|
|
2984
|
+
space(token) {
|
|
2985
|
+
return '';
|
|
2986
|
+
}
|
|
2987
|
+
code({
|
|
2988
|
+
text,
|
|
2989
|
+
lang,
|
|
2990
|
+
escaped
|
|
2991
|
+
}) {
|
|
2992
|
+
const langString = (lang || '').match(other.notSpaceStart)?.[0];
|
|
2993
|
+
const code = text.replace(other.endingNewline, '') + '\n';
|
|
2994
|
+
if (!langString) {
|
|
2995
|
+
return '<pre><code>' + (escaped ? code : escape(code, true)) + '</code></pre>\n';
|
|
2996
|
+
}
|
|
2997
|
+
return '<pre><code class="language-' + escape(langString) + '">' + (escaped ? code : escape(code, true)) + '</code></pre>\n';
|
|
2998
|
+
}
|
|
2999
|
+
blockquote({
|
|
3000
|
+
tokens
|
|
3001
|
+
}) {
|
|
3002
|
+
const body = this.parser.parse(tokens);
|
|
3003
|
+
return `<blockquote>\n${body}</blockquote>\n`;
|
|
3004
|
+
}
|
|
3005
|
+
html({
|
|
3006
|
+
text
|
|
3007
|
+
}) {
|
|
3008
|
+
return text;
|
|
3009
|
+
}
|
|
3010
|
+
heading({
|
|
3011
|
+
tokens,
|
|
3012
|
+
depth
|
|
3013
|
+
}) {
|
|
3014
|
+
return `<h${depth}>${this.parser.parseInline(tokens)}</h${depth}>\n`;
|
|
3015
|
+
}
|
|
3016
|
+
hr(token) {
|
|
3017
|
+
return '<hr>\n';
|
|
3018
|
+
}
|
|
3019
|
+
list(token) {
|
|
3020
|
+
const ordered = token.ordered;
|
|
3021
|
+
const start = token.start;
|
|
3022
|
+
let body = '';
|
|
3023
|
+
for (let j = 0; j < token.items.length; j++) {
|
|
3024
|
+
const item = token.items[j];
|
|
3025
|
+
body += this.listitem(item);
|
|
3026
|
+
}
|
|
3027
|
+
const type = ordered ? 'ol' : 'ul';
|
|
3028
|
+
const startAttr = ordered && start !== 1 ? ' start="' + start + '"' : '';
|
|
3029
|
+
return '<' + type + startAttr + '>\n' + body + '</' + type + '>\n';
|
|
3030
|
+
}
|
|
3031
|
+
listitem(item) {
|
|
3032
|
+
let itemBody = '';
|
|
3033
|
+
if (item.task) {
|
|
3034
|
+
const checkbox = this.checkbox({
|
|
3035
|
+
checked: !!item.checked
|
|
3036
|
+
});
|
|
3037
|
+
if (item.loose) {
|
|
3038
|
+
if (item.tokens[0]?.type === 'paragraph') {
|
|
3039
|
+
item.tokens[0].text = checkbox + ' ' + item.tokens[0].text;
|
|
3040
|
+
if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') {
|
|
3041
|
+
item.tokens[0].tokens[0].text = checkbox + ' ' + escape(item.tokens[0].tokens[0].text);
|
|
3042
|
+
item.tokens[0].tokens[0].escaped = true;
|
|
3043
|
+
}
|
|
3044
|
+
} else {
|
|
3045
|
+
item.tokens.unshift({
|
|
3046
|
+
type: 'text',
|
|
3047
|
+
raw: checkbox + ' ',
|
|
3048
|
+
text: checkbox + ' ',
|
|
3049
|
+
escaped: true
|
|
3050
|
+
});
|
|
3051
|
+
}
|
|
3052
|
+
} else {
|
|
3053
|
+
itemBody += checkbox + ' ';
|
|
3054
|
+
}
|
|
3055
|
+
}
|
|
3056
|
+
itemBody += this.parser.parse(item.tokens, !!item.loose);
|
|
3057
|
+
return `<li>${itemBody}</li>\n`;
|
|
3058
|
+
}
|
|
3059
|
+
checkbox({
|
|
3060
|
+
checked
|
|
3061
|
+
}) {
|
|
3062
|
+
return '<input ' + (checked ? 'checked="" ' : '') + 'disabled="" type="checkbox">';
|
|
3063
|
+
}
|
|
3064
|
+
paragraph({
|
|
3065
|
+
tokens
|
|
3066
|
+
}) {
|
|
3067
|
+
return `<p>${this.parser.parseInline(tokens)}</p>\n`;
|
|
3068
|
+
}
|
|
3069
|
+
table(token) {
|
|
3070
|
+
let header = '';
|
|
3071
|
+
// header
|
|
3072
|
+
let cell = '';
|
|
3073
|
+
for (let j = 0; j < token.header.length; j++) {
|
|
3074
|
+
cell += this.tablecell(token.header[j]);
|
|
3075
|
+
}
|
|
3076
|
+
header += this.tablerow({
|
|
3077
|
+
text: cell
|
|
3078
|
+
});
|
|
3079
|
+
let body = '';
|
|
3080
|
+
for (let j = 0; j < token.rows.length; j++) {
|
|
3081
|
+
const row = token.rows[j];
|
|
3082
|
+
cell = '';
|
|
3083
|
+
for (let k = 0; k < row.length; k++) {
|
|
3084
|
+
cell += this.tablecell(row[k]);
|
|
3085
|
+
}
|
|
3086
|
+
body += this.tablerow({
|
|
3087
|
+
text: cell
|
|
3088
|
+
});
|
|
3089
|
+
}
|
|
3090
|
+
if (body) body = `<tbody>${body}</tbody>`;
|
|
3091
|
+
return '<table>\n' + '<thead>\n' + header + '</thead>\n' + body + '</table>\n';
|
|
3092
|
+
}
|
|
3093
|
+
tablerow({
|
|
3094
|
+
text
|
|
3095
|
+
}) {
|
|
3096
|
+
return `<tr>\n${text}</tr>\n`;
|
|
3097
|
+
}
|
|
3098
|
+
tablecell(token) {
|
|
3099
|
+
const content = this.parser.parseInline(token.tokens);
|
|
3100
|
+
const type = token.header ? 'th' : 'td';
|
|
3101
|
+
const tag = token.align ? `<${type} align="${token.align}">` : `<${type}>`;
|
|
3102
|
+
return tag + content + `</${type}>\n`;
|
|
3103
|
+
}
|
|
3104
|
+
/**
|
|
3105
|
+
* span level renderer
|
|
3106
|
+
*/
|
|
3107
|
+
strong({
|
|
3108
|
+
tokens
|
|
3109
|
+
}) {
|
|
3110
|
+
return `<strong>${this.parser.parseInline(tokens)}</strong>`;
|
|
3111
|
+
}
|
|
3112
|
+
em({
|
|
3113
|
+
tokens
|
|
3114
|
+
}) {
|
|
3115
|
+
return `<em>${this.parser.parseInline(tokens)}</em>`;
|
|
3116
|
+
}
|
|
3117
|
+
codespan({
|
|
3118
|
+
text
|
|
3119
|
+
}) {
|
|
3120
|
+
return `<code>${escape(text, true)}</code>`;
|
|
3121
|
+
}
|
|
3122
|
+
br(token) {
|
|
3123
|
+
return '<br>';
|
|
3124
|
+
}
|
|
3125
|
+
del({
|
|
3126
|
+
tokens
|
|
3127
|
+
}) {
|
|
3128
|
+
return `<del>${this.parser.parseInline(tokens)}</del>`;
|
|
3129
|
+
}
|
|
3130
|
+
link({
|
|
3131
|
+
href,
|
|
3132
|
+
title,
|
|
3133
|
+
tokens
|
|
3134
|
+
}) {
|
|
3135
|
+
const text = this.parser.parseInline(tokens);
|
|
3136
|
+
const cleanHref = cleanUrl(href);
|
|
3137
|
+
if (cleanHref === null) {
|
|
3138
|
+
return text;
|
|
3139
|
+
}
|
|
3140
|
+
href = cleanHref;
|
|
3141
|
+
let out = '<a href="' + href + '"';
|
|
3142
|
+
if (title) {
|
|
3143
|
+
out += ' title="' + escape(title) + '"';
|
|
3144
|
+
}
|
|
3145
|
+
out += '>' + text + '</a>';
|
|
3146
|
+
return out;
|
|
3147
|
+
}
|
|
3148
|
+
image({
|
|
3149
|
+
href,
|
|
3150
|
+
title,
|
|
3151
|
+
text
|
|
3152
|
+
}) {
|
|
3153
|
+
const cleanHref = cleanUrl(href);
|
|
3154
|
+
if (cleanHref === null) {
|
|
3155
|
+
return escape(text);
|
|
3156
|
+
}
|
|
3157
|
+
href = cleanHref;
|
|
3158
|
+
let out = `<img src="${href}" alt="${text}"`;
|
|
3159
|
+
if (title) {
|
|
3160
|
+
out += ` title="${escape(title)}"`;
|
|
3161
|
+
}
|
|
3162
|
+
out += '>';
|
|
3163
|
+
return out;
|
|
3164
|
+
}
|
|
3165
|
+
text(token) {
|
|
3166
|
+
return 'tokens' in token && token.tokens ? this.parser.parseInline(token.tokens) : 'escaped' in token && token.escaped ? token.text : escape(token.text);
|
|
3167
|
+
}
|
|
3168
|
+
}
|
|
3169
|
+
|
|
3170
|
+
/**
|
|
3171
|
+
* TextRenderer
|
|
3172
|
+
* returns only the textual part of the token
|
|
3173
|
+
*/
|
|
3174
|
+
class _TextRenderer {
|
|
3175
|
+
// no need for block level renderers
|
|
3176
|
+
strong({
|
|
3177
|
+
text
|
|
3178
|
+
}) {
|
|
3179
|
+
return text;
|
|
3180
|
+
}
|
|
3181
|
+
em({
|
|
3182
|
+
text
|
|
3183
|
+
}) {
|
|
3184
|
+
return text;
|
|
3185
|
+
}
|
|
3186
|
+
codespan({
|
|
3187
|
+
text
|
|
3188
|
+
}) {
|
|
3189
|
+
return text;
|
|
3190
|
+
}
|
|
3191
|
+
del({
|
|
3192
|
+
text
|
|
3193
|
+
}) {
|
|
3194
|
+
return text;
|
|
3195
|
+
}
|
|
3196
|
+
html({
|
|
3197
|
+
text
|
|
3198
|
+
}) {
|
|
3199
|
+
return text;
|
|
3200
|
+
}
|
|
3201
|
+
text({
|
|
3202
|
+
text
|
|
3203
|
+
}) {
|
|
3204
|
+
return text;
|
|
3205
|
+
}
|
|
3206
|
+
link({
|
|
3207
|
+
text
|
|
3208
|
+
}) {
|
|
3209
|
+
return '' + text;
|
|
3210
|
+
}
|
|
3211
|
+
image({
|
|
3212
|
+
text
|
|
3213
|
+
}) {
|
|
3214
|
+
return '' + text;
|
|
3215
|
+
}
|
|
3216
|
+
br() {
|
|
3217
|
+
return '';
|
|
3218
|
+
}
|
|
3219
|
+
}
|
|
3220
|
+
|
|
3221
|
+
/**
|
|
3222
|
+
* Parsing & Compiling
|
|
3223
|
+
*/
|
|
3224
|
+
class _Parser {
|
|
3225
|
+
options;
|
|
3226
|
+
renderer;
|
|
3227
|
+
textRenderer;
|
|
3228
|
+
constructor(options) {
|
|
3229
|
+
this.options = options || _defaults;
|
|
3230
|
+
this.options.renderer = this.options.renderer || new _Renderer();
|
|
3231
|
+
this.renderer = this.options.renderer;
|
|
3232
|
+
this.renderer.options = this.options;
|
|
3233
|
+
this.renderer.parser = this;
|
|
3234
|
+
this.textRenderer = new _TextRenderer();
|
|
3235
|
+
}
|
|
3236
|
+
/**
|
|
3237
|
+
* Static Parse Method
|
|
3238
|
+
*/
|
|
3239
|
+
static parse(tokens, options) {
|
|
3240
|
+
const parser = new _Parser(options);
|
|
3241
|
+
return parser.parse(tokens);
|
|
3242
|
+
}
|
|
3243
|
+
/**
|
|
3244
|
+
* Static Parse Inline Method
|
|
3245
|
+
*/
|
|
3246
|
+
static parseInline(tokens, options) {
|
|
3247
|
+
const parser = new _Parser(options);
|
|
3248
|
+
return parser.parseInline(tokens);
|
|
3249
|
+
}
|
|
3250
|
+
/**
|
|
3251
|
+
* Parse Loop
|
|
3252
|
+
*/
|
|
3253
|
+
parse(tokens, top = true) {
|
|
3254
|
+
let out = '';
|
|
3255
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
3256
|
+
const anyToken = tokens[i];
|
|
3257
|
+
// Run any renderer extensions
|
|
3258
|
+
if (this.options.extensions?.renderers?.[anyToken.type]) {
|
|
3259
|
+
const genericToken = anyToken;
|
|
3260
|
+
const ret = this.options.extensions.renderers[genericToken.type].call({
|
|
3261
|
+
parser: this
|
|
3262
|
+
}, genericToken);
|
|
3263
|
+
if (ret !== false || !['space', 'hr', 'heading', 'code', 'table', 'blockquote', 'list', 'html', 'paragraph', 'text'].includes(genericToken.type)) {
|
|
3264
|
+
out += ret || '';
|
|
3265
|
+
continue;
|
|
3266
|
+
}
|
|
3267
|
+
}
|
|
3268
|
+
const token = anyToken;
|
|
3269
|
+
switch (token.type) {
|
|
3270
|
+
case 'space':
|
|
3271
|
+
{
|
|
3272
|
+
out += this.renderer.space(token);
|
|
3273
|
+
continue;
|
|
3274
|
+
}
|
|
3275
|
+
case 'hr':
|
|
3276
|
+
{
|
|
3277
|
+
out += this.renderer.hr(token);
|
|
3278
|
+
continue;
|
|
3279
|
+
}
|
|
3280
|
+
case 'heading':
|
|
3281
|
+
{
|
|
3282
|
+
out += this.renderer.heading(token);
|
|
3283
|
+
continue;
|
|
3284
|
+
}
|
|
3285
|
+
case 'code':
|
|
3286
|
+
{
|
|
3287
|
+
out += this.renderer.code(token);
|
|
3288
|
+
continue;
|
|
3289
|
+
}
|
|
3290
|
+
case 'table':
|
|
3291
|
+
{
|
|
3292
|
+
out += this.renderer.table(token);
|
|
3293
|
+
continue;
|
|
3294
|
+
}
|
|
3295
|
+
case 'blockquote':
|
|
3296
|
+
{
|
|
3297
|
+
out += this.renderer.blockquote(token);
|
|
3298
|
+
continue;
|
|
3299
|
+
}
|
|
3300
|
+
case 'list':
|
|
3301
|
+
{
|
|
3302
|
+
out += this.renderer.list(token);
|
|
3303
|
+
continue;
|
|
3304
|
+
}
|
|
3305
|
+
case 'html':
|
|
3306
|
+
{
|
|
3307
|
+
out += this.renderer.html(token);
|
|
3308
|
+
continue;
|
|
3309
|
+
}
|
|
3310
|
+
case 'paragraph':
|
|
3311
|
+
{
|
|
3312
|
+
out += this.renderer.paragraph(token);
|
|
3313
|
+
continue;
|
|
3314
|
+
}
|
|
3315
|
+
case 'text':
|
|
3316
|
+
{
|
|
3317
|
+
let textToken = token;
|
|
3318
|
+
let body = this.renderer.text(textToken);
|
|
3319
|
+
while (i + 1 < tokens.length && tokens[i + 1].type === 'text') {
|
|
3320
|
+
textToken = tokens[++i];
|
|
3321
|
+
body += '\n' + this.renderer.text(textToken);
|
|
3322
|
+
}
|
|
3323
|
+
if (top) {
|
|
3324
|
+
out += this.renderer.paragraph({
|
|
3325
|
+
type: 'paragraph',
|
|
3326
|
+
raw: body,
|
|
3327
|
+
text: body,
|
|
3328
|
+
tokens: [{
|
|
3329
|
+
type: 'text',
|
|
3330
|
+
raw: body,
|
|
3331
|
+
text: body,
|
|
3332
|
+
escaped: true
|
|
3333
|
+
}]
|
|
3334
|
+
});
|
|
3335
|
+
} else {
|
|
3336
|
+
out += body;
|
|
3337
|
+
}
|
|
3338
|
+
continue;
|
|
3339
|
+
}
|
|
3340
|
+
default:
|
|
3341
|
+
{
|
|
3342
|
+
const errMsg = 'Token with "' + token.type + '" type was not found.';
|
|
3343
|
+
if (this.options.silent) {
|
|
3344
|
+
console.error(errMsg);
|
|
3345
|
+
return '';
|
|
3346
|
+
} else {
|
|
3347
|
+
throw new Error(errMsg);
|
|
3348
|
+
}
|
|
3349
|
+
}
|
|
3350
|
+
}
|
|
3351
|
+
}
|
|
3352
|
+
return out;
|
|
3353
|
+
}
|
|
3354
|
+
/**
|
|
3355
|
+
* Parse Inline Tokens
|
|
3356
|
+
*/
|
|
3357
|
+
parseInline(tokens, renderer = this.renderer) {
|
|
3358
|
+
let out = '';
|
|
3359
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
3360
|
+
const anyToken = tokens[i];
|
|
3361
|
+
// Run any renderer extensions
|
|
3362
|
+
if (this.options.extensions?.renderers?.[anyToken.type]) {
|
|
3363
|
+
const ret = this.options.extensions.renderers[anyToken.type].call({
|
|
3364
|
+
parser: this
|
|
3365
|
+
}, anyToken);
|
|
3366
|
+
if (ret !== false || !['escape', 'html', 'link', 'image', 'strong', 'em', 'codespan', 'br', 'del', 'text'].includes(anyToken.type)) {
|
|
3367
|
+
out += ret || '';
|
|
3368
|
+
continue;
|
|
3369
|
+
}
|
|
3370
|
+
}
|
|
3371
|
+
const token = anyToken;
|
|
3372
|
+
switch (token.type) {
|
|
3373
|
+
case 'escape':
|
|
3374
|
+
{
|
|
3375
|
+
out += renderer.text(token);
|
|
3376
|
+
break;
|
|
3377
|
+
}
|
|
3378
|
+
case 'html':
|
|
3379
|
+
{
|
|
3380
|
+
out += renderer.html(token);
|
|
3381
|
+
break;
|
|
3382
|
+
}
|
|
3383
|
+
case 'link':
|
|
3384
|
+
{
|
|
3385
|
+
out += renderer.link(token);
|
|
3386
|
+
break;
|
|
3387
|
+
}
|
|
3388
|
+
case 'image':
|
|
3389
|
+
{
|
|
3390
|
+
out += renderer.image(token);
|
|
3391
|
+
break;
|
|
3392
|
+
}
|
|
3393
|
+
case 'strong':
|
|
3394
|
+
{
|
|
3395
|
+
out += renderer.strong(token);
|
|
3396
|
+
break;
|
|
3397
|
+
}
|
|
3398
|
+
case 'em':
|
|
3399
|
+
{
|
|
3400
|
+
out += renderer.em(token);
|
|
3401
|
+
break;
|
|
3402
|
+
}
|
|
3403
|
+
case 'codespan':
|
|
3404
|
+
{
|
|
3405
|
+
out += renderer.codespan(token);
|
|
3406
|
+
break;
|
|
3407
|
+
}
|
|
3408
|
+
case 'br':
|
|
3409
|
+
{
|
|
3410
|
+
out += renderer.br(token);
|
|
3411
|
+
break;
|
|
3412
|
+
}
|
|
3413
|
+
case 'del':
|
|
3414
|
+
{
|
|
3415
|
+
out += renderer.del(token);
|
|
3416
|
+
break;
|
|
3417
|
+
}
|
|
3418
|
+
case 'text':
|
|
3419
|
+
{
|
|
3420
|
+
out += renderer.text(token);
|
|
3421
|
+
break;
|
|
3422
|
+
}
|
|
3423
|
+
default:
|
|
3424
|
+
{
|
|
3425
|
+
const errMsg = 'Token with "' + token.type + '" type was not found.';
|
|
3426
|
+
if (this.options.silent) {
|
|
3427
|
+
console.error(errMsg);
|
|
3428
|
+
return '';
|
|
3429
|
+
} else {
|
|
3430
|
+
throw new Error(errMsg);
|
|
3431
|
+
}
|
|
3432
|
+
}
|
|
3433
|
+
}
|
|
3434
|
+
}
|
|
3435
|
+
return out;
|
|
3436
|
+
}
|
|
3437
|
+
}
|
|
3438
|
+
class _Hooks {
|
|
3439
|
+
options;
|
|
3440
|
+
block;
|
|
3441
|
+
constructor(options) {
|
|
3442
|
+
this.options = options || _defaults;
|
|
3443
|
+
}
|
|
3444
|
+
static passThroughHooks = new Set(['preprocess', 'postprocess', 'processAllTokens']);
|
|
3445
|
+
/**
|
|
3446
|
+
* Process markdown before marked
|
|
3447
|
+
*/
|
|
3448
|
+
preprocess(markdown) {
|
|
3449
|
+
return markdown;
|
|
3450
|
+
}
|
|
3451
|
+
/**
|
|
3452
|
+
* Process HTML after marked is finished
|
|
3453
|
+
*/
|
|
3454
|
+
postprocess(html) {
|
|
3455
|
+
return html;
|
|
3456
|
+
}
|
|
3457
|
+
/**
|
|
3458
|
+
* Process all tokens before walk tokens
|
|
3459
|
+
*/
|
|
3460
|
+
processAllTokens(tokens) {
|
|
3461
|
+
return tokens;
|
|
3462
|
+
}
|
|
3463
|
+
/**
|
|
3464
|
+
* Provide function to tokenize markdown
|
|
3465
|
+
*/
|
|
3466
|
+
provideLexer() {
|
|
3467
|
+
return this.block ? _Lexer.lex : _Lexer.lexInline;
|
|
3468
|
+
}
|
|
3469
|
+
/**
|
|
3470
|
+
* Provide function to parse tokens
|
|
3471
|
+
*/
|
|
3472
|
+
provideParser() {
|
|
3473
|
+
return this.block ? _Parser.parse : _Parser.parseInline;
|
|
3474
|
+
}
|
|
3475
|
+
}
|
|
3476
|
+
class Marked {
|
|
3477
|
+
defaults = _getDefaults();
|
|
3478
|
+
options = this.setOptions;
|
|
3479
|
+
parse = this.parseMarkdown(true);
|
|
3480
|
+
parseInline = this.parseMarkdown(false);
|
|
3481
|
+
Parser = _Parser;
|
|
3482
|
+
Renderer = _Renderer;
|
|
3483
|
+
TextRenderer = _TextRenderer;
|
|
3484
|
+
Lexer = _Lexer;
|
|
3485
|
+
Tokenizer = _Tokenizer;
|
|
3486
|
+
Hooks = _Hooks;
|
|
3487
|
+
constructor(...args) {
|
|
3488
|
+
this.use(...args);
|
|
3489
|
+
}
|
|
3490
|
+
/**
|
|
3491
|
+
* Run callback for every token
|
|
3492
|
+
*/
|
|
3493
|
+
walkTokens(tokens, callback) {
|
|
3494
|
+
let values = [];
|
|
3495
|
+
for (const token of tokens) {
|
|
3496
|
+
values = values.concat(callback.call(this, token));
|
|
3497
|
+
switch (token.type) {
|
|
3498
|
+
case 'table':
|
|
3499
|
+
{
|
|
3500
|
+
const tableToken = token;
|
|
3501
|
+
for (const cell of tableToken.header) {
|
|
3502
|
+
values = values.concat(this.walkTokens(cell.tokens, callback));
|
|
3503
|
+
}
|
|
3504
|
+
for (const row of tableToken.rows) {
|
|
3505
|
+
for (const cell of row) {
|
|
3506
|
+
values = values.concat(this.walkTokens(cell.tokens, callback));
|
|
3507
|
+
}
|
|
3508
|
+
}
|
|
3509
|
+
break;
|
|
3510
|
+
}
|
|
3511
|
+
case 'list':
|
|
3512
|
+
{
|
|
3513
|
+
const listToken = token;
|
|
3514
|
+
values = values.concat(this.walkTokens(listToken.items, callback));
|
|
3515
|
+
break;
|
|
3516
|
+
}
|
|
3517
|
+
default:
|
|
3518
|
+
{
|
|
3519
|
+
const genericToken = token;
|
|
3520
|
+
if (this.defaults.extensions?.childTokens?.[genericToken.type]) {
|
|
3521
|
+
this.defaults.extensions.childTokens[genericToken.type].forEach(childTokens => {
|
|
3522
|
+
const tokens = genericToken[childTokens].flat(Infinity);
|
|
3523
|
+
values = values.concat(this.walkTokens(tokens, callback));
|
|
3524
|
+
});
|
|
3525
|
+
} else if (genericToken.tokens) {
|
|
3526
|
+
values = values.concat(this.walkTokens(genericToken.tokens, callback));
|
|
3527
|
+
}
|
|
3528
|
+
}
|
|
3529
|
+
}
|
|
3530
|
+
}
|
|
3531
|
+
return values;
|
|
3532
|
+
}
|
|
3533
|
+
use(...args) {
|
|
3534
|
+
const extensions = this.defaults.extensions || {
|
|
3535
|
+
renderers: {},
|
|
3536
|
+
childTokens: {}
|
|
3537
|
+
};
|
|
3538
|
+
args.forEach(pack => {
|
|
3539
|
+
// copy options to new object
|
|
3540
|
+
const opts = {
|
|
3541
|
+
...pack
|
|
3542
|
+
};
|
|
3543
|
+
// set async to true if it was set to true before
|
|
3544
|
+
opts.async = this.defaults.async || opts.async || false;
|
|
3545
|
+
// ==-- Parse "addon" extensions --== //
|
|
3546
|
+
if (pack.extensions) {
|
|
3547
|
+
pack.extensions.forEach(ext => {
|
|
3548
|
+
if (!ext.name) {
|
|
3549
|
+
throw new Error('extension name required');
|
|
3550
|
+
}
|
|
3551
|
+
if ('renderer' in ext) {
|
|
3552
|
+
// Renderer extensions
|
|
3553
|
+
const prevRenderer = extensions.renderers[ext.name];
|
|
3554
|
+
if (prevRenderer) {
|
|
3555
|
+
// Replace extension with func to run new extension but fall back if false
|
|
3556
|
+
extensions.renderers[ext.name] = function (...args) {
|
|
3557
|
+
let ret = ext.renderer.apply(this, args);
|
|
3558
|
+
if (ret === false) {
|
|
3559
|
+
ret = prevRenderer.apply(this, args);
|
|
3560
|
+
}
|
|
3561
|
+
return ret;
|
|
3562
|
+
};
|
|
3563
|
+
} else {
|
|
3564
|
+
extensions.renderers[ext.name] = ext.renderer;
|
|
3565
|
+
}
|
|
3566
|
+
}
|
|
3567
|
+
if ('tokenizer' in ext) {
|
|
3568
|
+
// Tokenizer Extensions
|
|
3569
|
+
if (!ext.level || ext.level !== 'block' && ext.level !== 'inline') {
|
|
3570
|
+
throw new Error("extension level must be 'block' or 'inline'");
|
|
3571
|
+
}
|
|
3572
|
+
const extLevel = extensions[ext.level];
|
|
3573
|
+
if (extLevel) {
|
|
3574
|
+
extLevel.unshift(ext.tokenizer);
|
|
3575
|
+
} else {
|
|
3576
|
+
extensions[ext.level] = [ext.tokenizer];
|
|
3577
|
+
}
|
|
3578
|
+
if (ext.start) {
|
|
3579
|
+
// Function to check for start of token
|
|
3580
|
+
if (ext.level === 'block') {
|
|
3581
|
+
if (extensions.startBlock) {
|
|
3582
|
+
extensions.startBlock.push(ext.start);
|
|
3583
|
+
} else {
|
|
3584
|
+
extensions.startBlock = [ext.start];
|
|
3585
|
+
}
|
|
3586
|
+
} else if (ext.level === 'inline') {
|
|
3587
|
+
if (extensions.startInline) {
|
|
3588
|
+
extensions.startInline.push(ext.start);
|
|
3589
|
+
} else {
|
|
3590
|
+
extensions.startInline = [ext.start];
|
|
3591
|
+
}
|
|
3592
|
+
}
|
|
3593
|
+
}
|
|
3594
|
+
}
|
|
3595
|
+
if ('childTokens' in ext && ext.childTokens) {
|
|
3596
|
+
// Child tokens to be visited by walkTokens
|
|
3597
|
+
extensions.childTokens[ext.name] = ext.childTokens;
|
|
3598
|
+
}
|
|
3599
|
+
});
|
|
3600
|
+
opts.extensions = extensions;
|
|
3601
|
+
}
|
|
3602
|
+
// ==-- Parse "overwrite" extensions --== //
|
|
3603
|
+
if (pack.renderer) {
|
|
3604
|
+
const renderer = this.defaults.renderer || new _Renderer(this.defaults);
|
|
3605
|
+
for (const prop in pack.renderer) {
|
|
3606
|
+
if (!(prop in renderer)) {
|
|
3607
|
+
throw new Error(`renderer '${prop}' does not exist`);
|
|
3608
|
+
}
|
|
3609
|
+
if (['options', 'parser'].includes(prop)) {
|
|
3610
|
+
// ignore options property
|
|
3611
|
+
continue;
|
|
3612
|
+
}
|
|
3613
|
+
const rendererProp = prop;
|
|
3614
|
+
const rendererFunc = pack.renderer[rendererProp];
|
|
3615
|
+
const prevRenderer = renderer[rendererProp];
|
|
3616
|
+
// Replace renderer with func to run extension, but fall back if false
|
|
3617
|
+
renderer[rendererProp] = (...args) => {
|
|
3618
|
+
let ret = rendererFunc.apply(renderer, args);
|
|
3619
|
+
if (ret === false) {
|
|
3620
|
+
ret = prevRenderer.apply(renderer, args);
|
|
3621
|
+
}
|
|
3622
|
+
return ret || '';
|
|
3623
|
+
};
|
|
3624
|
+
}
|
|
3625
|
+
opts.renderer = renderer;
|
|
3626
|
+
}
|
|
3627
|
+
if (pack.tokenizer) {
|
|
3628
|
+
const tokenizer = this.defaults.tokenizer || new _Tokenizer(this.defaults);
|
|
3629
|
+
for (const prop in pack.tokenizer) {
|
|
3630
|
+
if (!(prop in tokenizer)) {
|
|
3631
|
+
throw new Error(`tokenizer '${prop}' does not exist`);
|
|
3632
|
+
}
|
|
3633
|
+
if (['options', 'rules', 'lexer'].includes(prop)) {
|
|
3634
|
+
// ignore options, rules, and lexer properties
|
|
3635
|
+
continue;
|
|
3636
|
+
}
|
|
3637
|
+
const tokenizerProp = prop;
|
|
3638
|
+
const tokenizerFunc = pack.tokenizer[tokenizerProp];
|
|
3639
|
+
const prevTokenizer = tokenizer[tokenizerProp];
|
|
3640
|
+
// Replace tokenizer with func to run extension, but fall back if false
|
|
3641
|
+
// @ts-expect-error cannot type tokenizer function dynamically
|
|
3642
|
+
tokenizer[tokenizerProp] = (...args) => {
|
|
3643
|
+
let ret = tokenizerFunc.apply(tokenizer, args);
|
|
3644
|
+
if (ret === false) {
|
|
3645
|
+
ret = prevTokenizer.apply(tokenizer, args);
|
|
3646
|
+
}
|
|
3647
|
+
return ret;
|
|
3648
|
+
};
|
|
3649
|
+
}
|
|
3650
|
+
opts.tokenizer = tokenizer;
|
|
3651
|
+
}
|
|
3652
|
+
// ==-- Parse Hooks extensions --== //
|
|
3653
|
+
if (pack.hooks) {
|
|
3654
|
+
const hooks = this.defaults.hooks || new _Hooks();
|
|
3655
|
+
for (const prop in pack.hooks) {
|
|
3656
|
+
if (!(prop in hooks)) {
|
|
3657
|
+
throw new Error(`hook '${prop}' does not exist`);
|
|
3658
|
+
}
|
|
3659
|
+
if (['options', 'block'].includes(prop)) {
|
|
3660
|
+
// ignore options and block properties
|
|
3661
|
+
continue;
|
|
3662
|
+
}
|
|
3663
|
+
const hooksProp = prop;
|
|
3664
|
+
const hooksFunc = pack.hooks[hooksProp];
|
|
3665
|
+
const prevHook = hooks[hooksProp];
|
|
3666
|
+
if (_Hooks.passThroughHooks.has(prop)) {
|
|
3667
|
+
// @ts-expect-error cannot type hook function dynamically
|
|
3668
|
+
hooks[hooksProp] = arg => {
|
|
3669
|
+
if (this.defaults.async) {
|
|
3670
|
+
return Promise.resolve(hooksFunc.call(hooks, arg)).then(ret => {
|
|
3671
|
+
return prevHook.call(hooks, ret);
|
|
3672
|
+
});
|
|
3673
|
+
}
|
|
3674
|
+
const ret = hooksFunc.call(hooks, arg);
|
|
3675
|
+
return prevHook.call(hooks, ret);
|
|
3676
|
+
};
|
|
3677
|
+
} else {
|
|
3678
|
+
// @ts-expect-error cannot type hook function dynamically
|
|
3679
|
+
hooks[hooksProp] = (...args) => {
|
|
3680
|
+
let ret = hooksFunc.apply(hooks, args);
|
|
3681
|
+
if (ret === false) {
|
|
3682
|
+
ret = prevHook.apply(hooks, args);
|
|
3683
|
+
}
|
|
3684
|
+
return ret;
|
|
3685
|
+
};
|
|
3686
|
+
}
|
|
3687
|
+
}
|
|
3688
|
+
opts.hooks = hooks;
|
|
3689
|
+
}
|
|
3690
|
+
// ==-- Parse WalkTokens extensions --== //
|
|
3691
|
+
if (pack.walkTokens) {
|
|
3692
|
+
const walkTokens = this.defaults.walkTokens;
|
|
3693
|
+
const packWalktokens = pack.walkTokens;
|
|
3694
|
+
opts.walkTokens = function (token) {
|
|
3695
|
+
let values = [];
|
|
3696
|
+
values.push(packWalktokens.call(this, token));
|
|
3697
|
+
if (walkTokens) {
|
|
3698
|
+
values = values.concat(walkTokens.call(this, token));
|
|
3699
|
+
}
|
|
3700
|
+
return values;
|
|
3701
|
+
};
|
|
3702
|
+
}
|
|
3703
|
+
this.defaults = {
|
|
3704
|
+
...this.defaults,
|
|
3705
|
+
...opts
|
|
3706
|
+
};
|
|
3707
|
+
});
|
|
3708
|
+
return this;
|
|
3709
|
+
}
|
|
3710
|
+
setOptions(opt) {
|
|
3711
|
+
this.defaults = {
|
|
3712
|
+
...this.defaults,
|
|
3713
|
+
...opt
|
|
3714
|
+
};
|
|
3715
|
+
return this;
|
|
3716
|
+
}
|
|
3717
|
+
lexer(src, options) {
|
|
3718
|
+
return _Lexer.lex(src, options ?? this.defaults);
|
|
3719
|
+
}
|
|
3720
|
+
parser(tokens, options) {
|
|
3721
|
+
return _Parser.parse(tokens, options ?? this.defaults);
|
|
3722
|
+
}
|
|
3723
|
+
parseMarkdown(blockType) {
|
|
3724
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
3725
|
+
const parse = (src, options) => {
|
|
3726
|
+
const origOpt = {
|
|
3727
|
+
...options
|
|
3728
|
+
};
|
|
3729
|
+
const opt = {
|
|
3730
|
+
...this.defaults,
|
|
3731
|
+
...origOpt
|
|
3732
|
+
};
|
|
3733
|
+
const throwError = this.onError(!!opt.silent, !!opt.async);
|
|
3734
|
+
// throw error if an extension set async to true but parse was called with async: false
|
|
3735
|
+
if (this.defaults.async === true && origOpt.async === false) {
|
|
3736
|
+
return throwError(new Error('marked(): The async option was set to true by an extension. Remove async: false from the parse options object to return a Promise.'));
|
|
3737
|
+
}
|
|
3738
|
+
// throw error in case of non string input
|
|
3739
|
+
if (typeof src === 'undefined' || src === null) {
|
|
3740
|
+
return throwError(new Error('marked(): input parameter is undefined or null'));
|
|
3741
|
+
}
|
|
3742
|
+
if (typeof src !== 'string') {
|
|
3743
|
+
return throwError(new Error('marked(): input parameter is of type ' + Object.prototype.toString.call(src) + ', string expected'));
|
|
3744
|
+
}
|
|
3745
|
+
if (opt.hooks) {
|
|
3746
|
+
opt.hooks.options = opt;
|
|
3747
|
+
opt.hooks.block = blockType;
|
|
3748
|
+
}
|
|
3749
|
+
const lexer = opt.hooks ? opt.hooks.provideLexer() : blockType ? _Lexer.lex : _Lexer.lexInline;
|
|
3750
|
+
const parser = opt.hooks ? opt.hooks.provideParser() : blockType ? _Parser.parse : _Parser.parseInline;
|
|
3751
|
+
if (opt.async) {
|
|
3752
|
+
return Promise.resolve(opt.hooks ? opt.hooks.preprocess(src) : src).then(src => lexer(src, opt)).then(tokens => opt.hooks ? opt.hooks.processAllTokens(tokens) : tokens).then(tokens => opt.walkTokens ? Promise.all(this.walkTokens(tokens, opt.walkTokens)).then(() => tokens) : tokens).then(tokens => parser(tokens, opt)).then(html => opt.hooks ? opt.hooks.postprocess(html) : html).catch(throwError);
|
|
3753
|
+
}
|
|
3754
|
+
try {
|
|
3755
|
+
if (opt.hooks) {
|
|
3756
|
+
src = opt.hooks.preprocess(src);
|
|
3757
|
+
}
|
|
3758
|
+
let tokens = lexer(src, opt);
|
|
3759
|
+
if (opt.hooks) {
|
|
3760
|
+
tokens = opt.hooks.processAllTokens(tokens);
|
|
3761
|
+
}
|
|
3762
|
+
if (opt.walkTokens) {
|
|
3763
|
+
this.walkTokens(tokens, opt.walkTokens);
|
|
3764
|
+
}
|
|
3765
|
+
let html = parser(tokens, opt);
|
|
3766
|
+
if (opt.hooks) {
|
|
3767
|
+
html = opt.hooks.postprocess(html);
|
|
3768
|
+
}
|
|
3769
|
+
return html;
|
|
3770
|
+
} catch (e) {
|
|
3771
|
+
return throwError(e);
|
|
3772
|
+
}
|
|
3773
|
+
};
|
|
3774
|
+
return parse;
|
|
3775
|
+
}
|
|
3776
|
+
onError(silent, async) {
|
|
3777
|
+
return e => {
|
|
3778
|
+
e.message += '\nPlease report this to https://github.com/markedjs/marked.';
|
|
3779
|
+
if (silent) {
|
|
3780
|
+
const msg = '<p>An error occurred:</p><pre>' + escape(e.message + '', true) + '</pre>';
|
|
3781
|
+
if (async) {
|
|
3782
|
+
return Promise.resolve(msg);
|
|
3783
|
+
}
|
|
3784
|
+
return msg;
|
|
3785
|
+
}
|
|
3786
|
+
if (async) {
|
|
3787
|
+
return Promise.reject(e);
|
|
3788
|
+
}
|
|
3789
|
+
throw e;
|
|
3790
|
+
};
|
|
3791
|
+
}
|
|
3792
|
+
}
|
|
3793
|
+
const markedInstance = new Marked();
|
|
3794
|
+
function marked(src, opt) {
|
|
3795
|
+
return markedInstance.parse(src, opt);
|
|
3796
|
+
}
|
|
3797
|
+
/**
|
|
3798
|
+
* Sets the default options.
|
|
3799
|
+
*
|
|
3800
|
+
* @param options Hash of options
|
|
3801
|
+
*/
|
|
3802
|
+
marked.options = marked.setOptions = function (options) {
|
|
3803
|
+
markedInstance.setOptions(options);
|
|
3804
|
+
marked.defaults = markedInstance.defaults;
|
|
3805
|
+
changeDefaults(marked.defaults);
|
|
3806
|
+
return marked;
|
|
3807
|
+
};
|
|
3808
|
+
/**
|
|
3809
|
+
* Gets the original marked default options.
|
|
3810
|
+
*/
|
|
3811
|
+
marked.getDefaults = _getDefaults;
|
|
3812
|
+
marked.defaults = _defaults;
|
|
3813
|
+
/**
|
|
3814
|
+
* Use Extension
|
|
3815
|
+
*/
|
|
3816
|
+
marked.use = function (...args) {
|
|
3817
|
+
markedInstance.use(...args);
|
|
3818
|
+
marked.defaults = markedInstance.defaults;
|
|
3819
|
+
changeDefaults(marked.defaults);
|
|
3820
|
+
return marked;
|
|
3821
|
+
};
|
|
3822
|
+
/**
|
|
3823
|
+
* Run callback for every token
|
|
3824
|
+
*/
|
|
3825
|
+
marked.walkTokens = function (tokens, callback) {
|
|
3826
|
+
return markedInstance.walkTokens(tokens, callback);
|
|
3827
|
+
};
|
|
3828
|
+
/**
|
|
3829
|
+
* Compiles markdown to HTML without enclosing `p` tag.
|
|
3830
|
+
*
|
|
3831
|
+
* @param src String of markdown source to be compiled
|
|
3832
|
+
* @param options Hash of options
|
|
3833
|
+
* @return String of compiled HTML
|
|
3834
|
+
*/
|
|
3835
|
+
marked.parseInline = markedInstance.parseInline;
|
|
3836
|
+
/**
|
|
3837
|
+
* Expose
|
|
3838
|
+
*/
|
|
3839
|
+
marked.Parser = _Parser;
|
|
3840
|
+
marked.parser = _Parser.parse;
|
|
3841
|
+
marked.Renderer = _Renderer;
|
|
3842
|
+
marked.TextRenderer = _TextRenderer;
|
|
3843
|
+
marked.Lexer = _Lexer;
|
|
3844
|
+
marked.lexer = _Lexer.lex;
|
|
3845
|
+
marked.Tokenizer = _Tokenizer;
|
|
3846
|
+
marked.Hooks = _Hooks;
|
|
3847
|
+
marked.parse = marked;
|
|
3848
|
+
|
|
3849
|
+
const renderMarkdown = async (markdown, options = {}) => {
|
|
3850
|
+
const html = await marked(markdown, {});
|
|
3851
|
+
return html;
|
|
3852
|
+
};
|
|
3853
|
+
|
|
3854
|
+
const terminate = () => {
|
|
3855
|
+
globalThis.close();
|
|
3856
|
+
};
|
|
3857
|
+
|
|
3858
|
+
const commandMap = {
|
|
3859
|
+
'Markdown.getMarkDownVirtualDom': getMarkdownVirtualDom,
|
|
3860
|
+
'Markdown.renderMarkdown': renderMarkdown,
|
|
3861
|
+
'Markdown.terminate': terminate
|
|
3862
|
+
};
|
|
3863
|
+
|
|
3864
|
+
const RendererWorker = 1;
|
|
3865
|
+
|
|
3866
|
+
const rpcs = Object.create(null);
|
|
3867
|
+
const set = (id, rpc) => {
|
|
3868
|
+
rpcs[id] = rpc;
|
|
3869
|
+
};
|
|
3870
|
+
|
|
3871
|
+
const listen = async () => {
|
|
3872
|
+
const rpc = await WebWorkerRpcClient.create({
|
|
3873
|
+
commandMap: commandMap
|
|
3874
|
+
});
|
|
3875
|
+
set(RendererWorker, rpc);
|
|
3876
|
+
};
|
|
3877
|
+
|
|
3878
|
+
const main = async () => {
|
|
3879
|
+
await listen();
|
|
3880
|
+
};
|
|
3881
|
+
|
|
3882
|
+
main();
|