stream-markdown-parser 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +281 -47
- package/README.zh-CN.md +279 -44
- package/dist/index.d.ts +305 -323
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2203 -1671
- package/dist/index.js.map +1 -0
- package/package.json +33 -31
package/dist/index.js
CHANGED
|
@@ -1,1726 +1,2258 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
loading: !0,
|
|
16
|
-
meta: void 0
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
type: "thead_open",
|
|
20
|
-
tag: "thead",
|
|
21
|
-
attrs: void 0,
|
|
22
|
-
block: !0,
|
|
23
|
-
level: 1,
|
|
24
|
-
children: void 0
|
|
25
|
-
},
|
|
26
|
-
{
|
|
27
|
-
type: "tr_open",
|
|
28
|
-
tag: "tr",
|
|
29
|
-
attrs: void 0,
|
|
30
|
-
block: !0,
|
|
31
|
-
level: 2,
|
|
32
|
-
children: void 0
|
|
33
|
-
}
|
|
34
|
-
];
|
|
1
|
+
import MarkdownIt from "markdown-it";
|
|
2
|
+
import { full } from "markdown-it-emoji";
|
|
3
|
+
import markdownItFootnote from "markdown-it-footnote";
|
|
4
|
+
import markdownItIns from "markdown-it-ins";
|
|
5
|
+
import markdownItMark from "markdown-it-mark";
|
|
6
|
+
import markdownItSub from "markdown-it-sub";
|
|
7
|
+
import markdownItSup from "markdown-it-sup";
|
|
8
|
+
import * as markdownItCheckbox from "markdown-it-task-checkbox";
|
|
9
|
+
import markdownItContainer from "markdown-it-container";
|
|
10
|
+
|
|
11
|
+
//#region src/config.ts
|
|
12
|
+
let defaultMathOptions;
|
|
13
|
+
function setDefaultMathOptions(opts) {
|
|
14
|
+
defaultMathOptions = opts;
|
|
35
15
|
}
|
|
36
|
-
function
|
|
37
|
-
|
|
38
|
-
{
|
|
39
|
-
type: "tr_close",
|
|
40
|
-
tag: "tr",
|
|
41
|
-
attrs: void 0,
|
|
42
|
-
block: !0,
|
|
43
|
-
level: 2,
|
|
44
|
-
children: void 0
|
|
45
|
-
},
|
|
46
|
-
{
|
|
47
|
-
type: "thead_close",
|
|
48
|
-
tag: "thead",
|
|
49
|
-
attrs: void 0,
|
|
50
|
-
block: !0,
|
|
51
|
-
level: 1,
|
|
52
|
-
children: void 0
|
|
53
|
-
},
|
|
54
|
-
{
|
|
55
|
-
type: "table_close",
|
|
56
|
-
tag: "table",
|
|
57
|
-
attrs: void 0,
|
|
58
|
-
map: void 0,
|
|
59
|
-
children: void 0,
|
|
60
|
-
content: "",
|
|
61
|
-
markup: "",
|
|
62
|
-
info: "",
|
|
63
|
-
level: 0,
|
|
64
|
-
meta: void 0
|
|
65
|
-
}
|
|
66
|
-
];
|
|
16
|
+
function getDefaultMathOptions() {
|
|
17
|
+
return defaultMathOptions;
|
|
67
18
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
19
|
+
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/plugins/containers.ts
|
|
22
|
+
function applyContainers(md) {
|
|
23
|
+
[
|
|
24
|
+
"admonition",
|
|
25
|
+
"info",
|
|
26
|
+
"warning",
|
|
27
|
+
"error",
|
|
28
|
+
"tip",
|
|
29
|
+
"danger",
|
|
30
|
+
"note",
|
|
31
|
+
"caution"
|
|
32
|
+
].forEach((name) => {
|
|
33
|
+
md.use(markdownItContainer, name, { render(tokens, idx) {
|
|
34
|
+
if (tokens[idx].nesting === 1) return `<div class="vmr-container vmr-container-${name}">`;
|
|
35
|
+
else return "</div>\n";
|
|
36
|
+
} });
|
|
37
|
+
});
|
|
38
|
+
md.block.ruler.before("fence", "vmr_container_fallback", (state, startLine, endLine, silent) => {
|
|
39
|
+
const startPos = state.bMarks[startLine] + state.tShift[startLine];
|
|
40
|
+
const lineMax = state.eMarks[startLine];
|
|
41
|
+
const markerMatch = state.src.slice(startPos, lineMax).match(/^:::\s*(\w+)/);
|
|
42
|
+
if (!markerMatch) return false;
|
|
43
|
+
if (silent) return true;
|
|
44
|
+
const name = markerMatch[1];
|
|
45
|
+
let nextLine = startLine + 1;
|
|
46
|
+
let found = false;
|
|
47
|
+
while (nextLine <= endLine) {
|
|
48
|
+
const sPos = state.bMarks[nextLine] + state.tShift[nextLine];
|
|
49
|
+
const ePos = state.eMarks[nextLine];
|
|
50
|
+
if (state.src.slice(sPos, ePos).trim() === ":::") {
|
|
51
|
+
found = true;
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
nextLine++;
|
|
55
|
+
}
|
|
56
|
+
if (!found) return false;
|
|
57
|
+
state.push("vmr_container_open", "div", 1).attrSet("class", `vmr-container vmr-container-${name}`);
|
|
58
|
+
const contentLines = [];
|
|
59
|
+
for (let i = startLine + 1; i < nextLine; i++) {
|
|
60
|
+
const sPos = state.bMarks[i] + state.tShift[i];
|
|
61
|
+
const ePos = state.eMarks[i];
|
|
62
|
+
contentLines.push(state.src.slice(sPos, ePos));
|
|
63
|
+
}
|
|
64
|
+
state.push("paragraph_open", "p", 1);
|
|
65
|
+
const inlineToken = state.push("inline", "", 0);
|
|
66
|
+
inlineToken.content = contentLines.join("\n");
|
|
67
|
+
inlineToken.map = [startLine + 1, nextLine];
|
|
68
|
+
inlineToken.children = [];
|
|
69
|
+
state.md.inline.parse(inlineToken.content, state.md, state.env, inlineToken.children);
|
|
70
|
+
state.push("paragraph_close", "p", -1);
|
|
71
|
+
state.push("vmr_container_close", "div", -1);
|
|
72
|
+
state.line = nextLine + 1;
|
|
73
|
+
return true;
|
|
74
|
+
});
|
|
100
75
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
76
|
+
|
|
77
|
+
//#endregion
|
|
78
|
+
//#region src/findMatchingClose.ts
|
|
79
|
+
function findMatchingClose(src, startIdx, open, close) {
|
|
80
|
+
const len = src.length;
|
|
81
|
+
if (open === "$$" && close === "$$") {
|
|
82
|
+
let i$1 = startIdx;
|
|
83
|
+
while (i$1 < len - 1) {
|
|
84
|
+
if (src[i$1] === "$" && src[i$1 + 1] === "$") {
|
|
85
|
+
let k = i$1 - 1;
|
|
86
|
+
let backslashes = 0;
|
|
87
|
+
while (k >= 0 && src[k] === "\\") {
|
|
88
|
+
backslashes++;
|
|
89
|
+
k--;
|
|
90
|
+
}
|
|
91
|
+
if (backslashes % 2 === 0) return i$1;
|
|
92
|
+
}
|
|
93
|
+
i$1++;
|
|
94
|
+
}
|
|
95
|
+
return -1;
|
|
96
|
+
}
|
|
97
|
+
const openChar = open[open.length - 1];
|
|
98
|
+
const closeSeq = close;
|
|
99
|
+
let depth = 0;
|
|
100
|
+
let i = startIdx;
|
|
101
|
+
while (i < len) {
|
|
102
|
+
if (src.slice(i, i + closeSeq.length) === closeSeq) {
|
|
103
|
+
let k = i - 1;
|
|
104
|
+
let backslashes = 0;
|
|
105
|
+
while (k >= 0 && src[k] === "\\") {
|
|
106
|
+
backslashes++;
|
|
107
|
+
k--;
|
|
108
|
+
}
|
|
109
|
+
if (backslashes % 2 === 0) {
|
|
110
|
+
if (depth === 0) return i;
|
|
111
|
+
depth--;
|
|
112
|
+
i += closeSeq.length;
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const ch = src[i];
|
|
117
|
+
if (ch === "\\") {
|
|
118
|
+
i += 2;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (ch === openChar) depth++;
|
|
122
|
+
else if (ch === closeSeq[closeSeq.length - 1]) {
|
|
123
|
+
if (depth > 0) depth--;
|
|
124
|
+
}
|
|
125
|
+
i++;
|
|
126
|
+
}
|
|
127
|
+
return -1;
|
|
125
128
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
129
|
+
var findMatchingClose_default = findMatchingClose;
|
|
130
|
+
|
|
131
|
+
//#endregion
|
|
132
|
+
//#region src/plugins/isMathLike.ts
|
|
133
|
+
const TEX_BRACE_COMMANDS = [
|
|
134
|
+
"mathbf",
|
|
135
|
+
"boldsymbol",
|
|
136
|
+
"mathbb",
|
|
137
|
+
"mathcal",
|
|
138
|
+
"mathfrak",
|
|
139
|
+
"mathrm",
|
|
140
|
+
"mathit",
|
|
141
|
+
"mathsf",
|
|
142
|
+
"vec",
|
|
143
|
+
"hat",
|
|
144
|
+
"bar",
|
|
145
|
+
"tilde",
|
|
146
|
+
"overline",
|
|
147
|
+
"underline",
|
|
148
|
+
"mathscr",
|
|
149
|
+
"mathnormal",
|
|
150
|
+
"operatorname",
|
|
151
|
+
"mathbf*"
|
|
152
|
+
];
|
|
153
|
+
const ESCAPED_TEX_BRACE_COMMANDS = TEX_BRACE_COMMANDS.map((c) => c.replace(/[.*+?^${}()|[\\]"\]/g, "\\$&")).join("|");
|
|
154
|
+
const TEX_CMD_RE = /\\[a-z]+/i;
|
|
155
|
+
const PREFIX_CLASS = "(?:\\\\|\\u0008)";
|
|
156
|
+
const TEX_CMD_WITH_BRACES_RE = new RegExp(`${PREFIX_CLASS}(?:${ESCAPED_TEX_BRACE_COMMANDS})\\s*\\{[^}]+\\}`, "i");
|
|
157
|
+
const TEX_BRACE_CMD_START_RE = new RegExp(`(?:${PREFIX_CLASS})?(?:${ESCAPED_TEX_BRACE_COMMANDS})\s*\{`, "i");
|
|
158
|
+
const TEX_SPECIFIC_RE = /\\(?:text|frac|left|right|times)/;
|
|
159
|
+
const OPS_RE = /* @__PURE__ */ new RegExp("(?<!\\+)\\+(?!\\+)|[=\\-*/^<>]|\\\\times|\\\\pm|\\\\cdot|\\\\le|\\\\ge|\\\\neq");
|
|
160
|
+
const FUNC_CALL_RE = /[A-Z]+\s*\([^)]+\)/i;
|
|
161
|
+
const WORDS_RE = /\b(?:sin|cos|tan|log|ln|exp|sqrt|frac|sum|lim|int|prod)\b/;
|
|
162
|
+
const DATE_TIME_RE = /\b\d{4}\/\d{1,2}\/\d{1,2}(?:[ T]\d{1,2}:\d{2}(?::\d{2})?)?\b/;
|
|
163
|
+
function isMathLike(s) {
|
|
164
|
+
if (!s) return false;
|
|
165
|
+
const norm = s.replace(/\u0008/g, "\\b");
|
|
166
|
+
const stripped = norm.trim();
|
|
167
|
+
if (DATE_TIME_RE.test(stripped)) return false;
|
|
168
|
+
if (stripped.length > 2e3) return true;
|
|
169
|
+
if (/[./]\s*\D|\D\s*[./]/.test(s)) return false;
|
|
170
|
+
const texCmd = TEX_CMD_RE.test(norm);
|
|
171
|
+
const texCmdWithBraces = TEX_CMD_WITH_BRACES_RE.test(norm);
|
|
172
|
+
const texBraceStart = TEX_BRACE_CMD_START_RE.test(norm);
|
|
173
|
+
const texSpecific = TEX_SPECIFIC_RE.test(norm);
|
|
174
|
+
const superSub = /(?:^|[^\w\\])(?:[A-Z]|\\[A-Z]+)_(?:\{[^}]+\}|[A-Z0-9\\])/i.test(norm) || /(?:^|[^\w\\])(?:[A-Z]|\\[A-Z]+)\^(?:\{[^}]+\}|[A-Z0-9\\])/i.test(norm);
|
|
175
|
+
const ops = OPS_RE.test(norm);
|
|
176
|
+
const funcCall = FUNC_CALL_RE.test(norm);
|
|
177
|
+
const words = WORDS_RE.test(norm);
|
|
178
|
+
return texCmd || texCmdWithBraces || texBraceStart || texSpecific || superSub || ops || funcCall || words;
|
|
132
179
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
180
|
+
|
|
181
|
+
//#endregion
|
|
182
|
+
//#region src/plugins/math.ts
|
|
183
|
+
const KATEX_COMMANDS = [
|
|
184
|
+
"ldots",
|
|
185
|
+
"cdots",
|
|
186
|
+
"quad",
|
|
187
|
+
"in",
|
|
188
|
+
"infty",
|
|
189
|
+
"perp",
|
|
190
|
+
"mid",
|
|
191
|
+
"operatorname",
|
|
192
|
+
"to",
|
|
193
|
+
"rightarrow",
|
|
194
|
+
"leftarrow",
|
|
195
|
+
"math",
|
|
196
|
+
"mathrm",
|
|
197
|
+
"mathbf",
|
|
198
|
+
"mathit",
|
|
199
|
+
"mathbb",
|
|
200
|
+
"mathcal",
|
|
201
|
+
"mathfrak",
|
|
202
|
+
"alpha",
|
|
203
|
+
"beta",
|
|
204
|
+
"gamma",
|
|
205
|
+
"delta",
|
|
206
|
+
"epsilon",
|
|
207
|
+
"lambda",
|
|
208
|
+
"sum",
|
|
209
|
+
"prod",
|
|
210
|
+
"int",
|
|
211
|
+
"sqrt",
|
|
212
|
+
"fbox",
|
|
213
|
+
"boxed",
|
|
214
|
+
"color",
|
|
215
|
+
"rule",
|
|
216
|
+
"edef",
|
|
217
|
+
"fcolorbox",
|
|
218
|
+
"hline",
|
|
219
|
+
"hdashline",
|
|
220
|
+
"cdot",
|
|
221
|
+
"times",
|
|
222
|
+
"pm",
|
|
223
|
+
"le",
|
|
224
|
+
"ge",
|
|
225
|
+
"neq",
|
|
226
|
+
"sin",
|
|
227
|
+
"cos",
|
|
228
|
+
"tan",
|
|
229
|
+
"log",
|
|
230
|
+
"ln",
|
|
231
|
+
"exp",
|
|
232
|
+
"lim",
|
|
233
|
+
"frac",
|
|
234
|
+
"text",
|
|
235
|
+
"left",
|
|
236
|
+
"right",
|
|
237
|
+
"times"
|
|
238
|
+
];
|
|
239
|
+
const ESCAPED_KATEX_COMMANDS = KATEX_COMMANDS.slice().sort((a, b) => b.length - a.length).map((c) => c.replace(/[.*+?^${}()|[\\]\\\]/g, "\\$&")).join("|");
|
|
240
|
+
const CONTROL_CHARS_CLASS = "[ \r\b\f\v]";
|
|
241
|
+
const CONTROL_MAP = {
|
|
242
|
+
" ": "t",
|
|
243
|
+
"\r": "r",
|
|
244
|
+
"\b": "b",
|
|
245
|
+
"\f": "f",
|
|
246
|
+
"\v": "v"
|
|
247
|
+
};
|
|
248
|
+
function countUnescapedStrong(s) {
|
|
249
|
+
const re = /(^|[^\\])(__|\*\*)/g;
|
|
250
|
+
let c = 0;
|
|
251
|
+
while (re.exec(s) !== null) c++;
|
|
252
|
+
return c;
|
|
139
253
|
}
|
|
140
|
-
function
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
254
|
+
function normalizeStandaloneBackslashT(s, opts) {
|
|
255
|
+
const commands = opts?.commands ?? KATEX_COMMANDS;
|
|
256
|
+
const escapeExclamation = opts?.escapeExclamation ?? true;
|
|
257
|
+
const useDefault = opts?.commands == null;
|
|
258
|
+
let re;
|
|
259
|
+
if (useDefault) re = new RegExp(`${CONTROL_CHARS_CLASS}|(?<!\\\\|\\w)(${ESCAPED_KATEX_COMMANDS})\\b`, "g");
|
|
260
|
+
else {
|
|
261
|
+
const commandPattern = `(?:${commands.slice().sort((a, b) => b.length - a.length).map((c) => c.replace(/[.*+?^${}()|[\\]\\"\]/g, "\\$&")).join("|")})`;
|
|
262
|
+
re = new RegExp(`${CONTROL_CHARS_CLASS}|(?<!\\\\|\\w)(${commandPattern})\\b`, "g");
|
|
263
|
+
}
|
|
264
|
+
let out = s.replace(re, (m, cmd) => {
|
|
265
|
+
if (CONTROL_MAP[m] !== void 0) return `\\${CONTROL_MAP[m]}`;
|
|
266
|
+
if (cmd && commands.includes(cmd)) return `\\${cmd}`;
|
|
267
|
+
return m;
|
|
268
|
+
});
|
|
269
|
+
if (escapeExclamation) out = out.replace(/(^|[^\\])!/g, "$1\\!");
|
|
270
|
+
const braceEscaped = useDefault ? [ESCAPED_TEX_BRACE_COMMANDS, ESCAPED_KATEX_COMMANDS].filter(Boolean).join("|") : [commands.map((c) => c.replace(/[.*+?^${}()|[\\]\\\]/g, "\\$&")).join("|"), ESCAPED_TEX_BRACE_COMMANDS].filter(Boolean).join("|");
|
|
271
|
+
let result = out;
|
|
272
|
+
if (braceEscaped) {
|
|
273
|
+
const braceCmdRe = new RegExp(`(^|[^\\\\])(${braceEscaped})\\s*\\{`, "g");
|
|
274
|
+
result = result.replace(braceCmdRe, (_m, p1, p2) => `${p1}\\${p2}{`);
|
|
275
|
+
}
|
|
276
|
+
result = result.replace(/span\{([^}]+)\}/, "span\\{$1\\}").replace(/\\operatorname\{span\}\{((?:[^{}]|\{[^}]*\})+)\}/, "\\operatorname{span}\\{$1\\}");
|
|
277
|
+
return result;
|
|
147
278
|
}
|
|
148
|
-
function
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
279
|
+
function applyMath(md, mathOpts) {
|
|
280
|
+
const mathInline = (state, silent) => {
|
|
281
|
+
if (/^\*[^*]+/.test(state.src)) return false;
|
|
282
|
+
const delimiters = [
|
|
283
|
+
["$$", "$$"],
|
|
284
|
+
["\\(", "\\)"],
|
|
285
|
+
["(", ")"]
|
|
286
|
+
];
|
|
287
|
+
let searchPos = 0;
|
|
288
|
+
let preMathPos = 0;
|
|
289
|
+
for (const [open, close] of delimiters) {
|
|
290
|
+
const src = state.src;
|
|
291
|
+
let foundAny = false;
|
|
292
|
+
const pushText = (text) => {
|
|
293
|
+
if (text === "undefined" || text == null) text = "";
|
|
294
|
+
if (text === "\\") {
|
|
295
|
+
state.pos = state.pos + text.length;
|
|
296
|
+
searchPos = state.pos;
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
if (text === "\\)" || text === "\\(") {
|
|
300
|
+
const t$1 = state.push("text_special", "", 0);
|
|
301
|
+
t$1.content = text === "\\)" ? ")" : "(";
|
|
302
|
+
t$1.markup = text;
|
|
303
|
+
state.pos = state.pos + text.length;
|
|
304
|
+
searchPos = state.pos;
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
if (!text) return;
|
|
308
|
+
const t = state.push("text", "", 0);
|
|
309
|
+
t.content = text;
|
|
310
|
+
state.pos = state.pos + text.length;
|
|
311
|
+
searchPos = state.pos;
|
|
312
|
+
};
|
|
313
|
+
while (true) {
|
|
314
|
+
if (searchPos >= src.length) break;
|
|
315
|
+
const index = src.indexOf(open, searchPos);
|
|
316
|
+
if (index === -1) break;
|
|
317
|
+
if (index > 0) {
|
|
318
|
+
let i = index - 1;
|
|
319
|
+
while (i >= 0 && src[i] === " ") i--;
|
|
320
|
+
if (i >= 0 && src[i] === "]") return false;
|
|
321
|
+
}
|
|
322
|
+
const endIdx = findMatchingClose_default(src, index + open.length, open, close);
|
|
323
|
+
if (endIdx === -1) {
|
|
324
|
+
const content$1 = src.slice(index + open.length);
|
|
325
|
+
if (isMathLike(content$1)) {
|
|
326
|
+
searchPos = index + open.length;
|
|
327
|
+
foundAny = true;
|
|
328
|
+
if (!silent) {
|
|
329
|
+
state.pending = "";
|
|
330
|
+
const isStrongPrefix = countUnescapedStrong(preMathPos ? src.slice(preMathPos, searchPos) : src.slice(0, searchPos)) % 2 === 1;
|
|
331
|
+
if (preMathPos) pushText(src.slice(preMathPos, searchPos));
|
|
332
|
+
else pushText(src.slice(0, searchPos));
|
|
333
|
+
if (isStrongPrefix) {
|
|
334
|
+
const strongToken = state.push("strong_open", "", 0);
|
|
335
|
+
strongToken.markup = src.slice(0, index + 2);
|
|
336
|
+
const token = state.push("math_inline", "math", 0);
|
|
337
|
+
token.content = normalizeStandaloneBackslashT(content$1, mathOpts);
|
|
338
|
+
token.markup = open === "$$" ? "$$" : open === "\\(" ? "\\(\\)" : open === "$" ? "$" : "()";
|
|
339
|
+
token.raw = `${open}${content$1}${close}`;
|
|
340
|
+
token.loading = true;
|
|
341
|
+
strongToken.content = content$1;
|
|
342
|
+
state.push("strong_close", "", 0);
|
|
343
|
+
} else {
|
|
344
|
+
const token = state.push("math_inline", "math", 0);
|
|
345
|
+
token.content = normalizeStandaloneBackslashT(content$1, mathOpts);
|
|
346
|
+
token.markup = open === "$$" ? "$$" : open === "\\(" ? "\\(\\)" : open === "$" ? "$" : "()";
|
|
347
|
+
token.raw = `${open}${content$1}${close}`;
|
|
348
|
+
token.loading = true;
|
|
349
|
+
}
|
|
350
|
+
state.pos = src.length;
|
|
351
|
+
}
|
|
352
|
+
searchPos = src.length;
|
|
353
|
+
preMathPos = searchPos;
|
|
354
|
+
}
|
|
355
|
+
break;
|
|
356
|
+
}
|
|
357
|
+
const content = src.slice(index + open.length, endIdx);
|
|
358
|
+
if (!isMathLike(content)) {
|
|
359
|
+
searchPos = endIdx + close.length;
|
|
360
|
+
const text = src.slice(state.pos, searchPos);
|
|
361
|
+
if (!state.pending) pushText(text);
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
foundAny = true;
|
|
365
|
+
if (!silent) {
|
|
366
|
+
const before = src.slice(0, index);
|
|
367
|
+
let toPushBefore = src.slice(0, searchPos) ? src.slice(preMathPos, index) : before;
|
|
368
|
+
const isStrongPrefix = countUnescapedStrong(toPushBefore) % 2 === 1;
|
|
369
|
+
if (index !== state.pos && isStrongPrefix) toPushBefore = state.pending + src.slice(state.pos, index);
|
|
370
|
+
if (state.pending !== toPushBefore) {
|
|
371
|
+
state.pending = "";
|
|
372
|
+
if (isStrongPrefix) {
|
|
373
|
+
const _match = toPushBefore.match(/(\*+)/);
|
|
374
|
+
const after = toPushBefore.slice(_match.index + _match[0].length);
|
|
375
|
+
pushText(toPushBefore.slice(0, _match.index));
|
|
376
|
+
const strongToken = state.push("strong_open", "", 0);
|
|
377
|
+
strongToken.markup = _match[0];
|
|
378
|
+
const textToken = state.push("text", "", 0);
|
|
379
|
+
textToken.content = after;
|
|
380
|
+
state.push("strong_close", "", 0);
|
|
381
|
+
} else pushText(toPushBefore);
|
|
382
|
+
}
|
|
383
|
+
if (isStrongPrefix) {
|
|
384
|
+
const strongToken = state.push("strong_open", "", 0);
|
|
385
|
+
strongToken.markup = "**";
|
|
386
|
+
const token = state.push("math_inline", "math", 0);
|
|
387
|
+
token.content = normalizeStandaloneBackslashT(content, mathOpts);
|
|
388
|
+
token.markup = open === "$$" ? "$$" : open === "\\(" ? "\\(\\)" : open === "$" ? "$" : "()";
|
|
389
|
+
token.raw = `${open}${content}${close}`;
|
|
390
|
+
token.loading = false;
|
|
391
|
+
const raw = src.slice(endIdx + close.length);
|
|
392
|
+
const isBeforeClose = raw.startsWith("*");
|
|
393
|
+
if (isBeforeClose) state.push("strong_close", "", 0);
|
|
394
|
+
if (raw) {
|
|
395
|
+
const textContentToken = state.push("text", "", 0);
|
|
396
|
+
textContentToken.content = (raw == null ? "" : String(raw)).replace(/^\*+/, "");
|
|
397
|
+
}
|
|
398
|
+
if (!isBeforeClose) state.push("strong_close", "", 0);
|
|
399
|
+
state.pos = src.length;
|
|
400
|
+
searchPos = src.length;
|
|
401
|
+
preMathPos = searchPos;
|
|
402
|
+
continue;
|
|
403
|
+
} else {
|
|
404
|
+
const token = state.push("math_inline", "math", 0);
|
|
405
|
+
token.content = normalizeStandaloneBackslashT(content, mathOpts);
|
|
406
|
+
token.markup = open === "$$" ? "$$" : open === "\\(" ? "\\(\\)" : open === "$" ? "$" : "()";
|
|
407
|
+
token.raw = `${open}${content}${close}`;
|
|
408
|
+
token.loading = false;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
searchPos = endIdx + close.length;
|
|
412
|
+
preMathPos = searchPos;
|
|
413
|
+
state.pos = searchPos;
|
|
414
|
+
}
|
|
415
|
+
if (foundAny) {
|
|
416
|
+
if (!silent) {
|
|
417
|
+
if (searchPos < src.length) pushText(src.slice(searchPos));
|
|
418
|
+
state.pos = src.length;
|
|
419
|
+
} else state.pos = searchPos;
|
|
420
|
+
return true;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
return false;
|
|
424
|
+
};
|
|
425
|
+
const mathBlock = (state, startLine, endLine, silent) => {
|
|
426
|
+
const delimiters = [
|
|
427
|
+
["\\[", "\\]"],
|
|
428
|
+
["[", "]"],
|
|
429
|
+
["$$", "$$"]
|
|
430
|
+
];
|
|
431
|
+
const startPos = state.bMarks[startLine] + state.tShift[startLine];
|
|
432
|
+
const lineText = state.src.slice(startPos, state.eMarks[startLine]).trim();
|
|
433
|
+
let matched = false;
|
|
434
|
+
let openDelim = "";
|
|
435
|
+
let closeDelim = "";
|
|
436
|
+
for (const [open, close] of delimiters) if (lineText === open || lineText.startsWith(open)) if (open.includes("[")) {
|
|
437
|
+
if (lineText.replace("\\", "") === "[") {
|
|
438
|
+
if (startLine + 1 < endLine) {
|
|
439
|
+
matched = true;
|
|
440
|
+
openDelim = open;
|
|
441
|
+
closeDelim = close;
|
|
442
|
+
break;
|
|
443
|
+
}
|
|
444
|
+
continue;
|
|
445
|
+
}
|
|
446
|
+
} else {
|
|
447
|
+
matched = true;
|
|
448
|
+
openDelim = open;
|
|
449
|
+
closeDelim = close;
|
|
450
|
+
break;
|
|
451
|
+
}
|
|
452
|
+
if (!matched) return false;
|
|
453
|
+
if (silent) return true;
|
|
454
|
+
if (lineText.includes(closeDelim) && lineText.indexOf(closeDelim) > openDelim.length) {
|
|
455
|
+
const startDelimIndex = lineText.indexOf(openDelim);
|
|
456
|
+
const endDelimIndex = lineText.indexOf(closeDelim, startDelimIndex + openDelim.length);
|
|
457
|
+
const content$1 = lineText.slice(startDelimIndex + openDelim.length, endDelimIndex);
|
|
458
|
+
const token$1 = state.push("math_block", "math", 0);
|
|
459
|
+
token$1.content = normalizeStandaloneBackslashT(content$1);
|
|
460
|
+
token$1.markup = openDelim === "$$" ? "$$" : openDelim === "[" ? "[]" : "\\[\\]";
|
|
461
|
+
token$1.map = [startLine, startLine + 1];
|
|
462
|
+
token$1.raw = `${openDelim}${content$1}${closeDelim}`;
|
|
463
|
+
token$1.block = true;
|
|
464
|
+
token$1.loading = false;
|
|
465
|
+
state.line = startLine + 1;
|
|
466
|
+
return true;
|
|
467
|
+
}
|
|
468
|
+
let nextLine = startLine;
|
|
469
|
+
let content = "";
|
|
470
|
+
let found = false;
|
|
471
|
+
const firstLineContent = lineText === openDelim ? "" : lineText.slice(openDelim.length);
|
|
472
|
+
if (firstLineContent.includes(closeDelim)) {
|
|
473
|
+
const endIndex = firstLineContent.indexOf(closeDelim);
|
|
474
|
+
content = firstLineContent.slice(0, endIndex);
|
|
475
|
+
found = true;
|
|
476
|
+
nextLine = startLine;
|
|
477
|
+
} else {
|
|
478
|
+
if (firstLineContent) content = firstLineContent;
|
|
479
|
+
for (nextLine = startLine + 1; nextLine < endLine; nextLine++) {
|
|
480
|
+
const lineStart = state.bMarks[nextLine] + state.tShift[nextLine];
|
|
481
|
+
const lineEnd = state.eMarks[nextLine];
|
|
482
|
+
const currentLine = state.src.slice(lineStart - 1, lineEnd);
|
|
483
|
+
if (currentLine.trim() === closeDelim) {
|
|
484
|
+
found = true;
|
|
485
|
+
break;
|
|
486
|
+
} else if (currentLine.includes(closeDelim)) {
|
|
487
|
+
found = true;
|
|
488
|
+
const endIndex = currentLine.indexOf(closeDelim);
|
|
489
|
+
content += (content ? "\n" : "") + currentLine.slice(0, endIndex);
|
|
490
|
+
break;
|
|
491
|
+
}
|
|
492
|
+
content += (content ? "\n" : "") + currentLine;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
const token = state.push("math_block", "math", 0);
|
|
496
|
+
token.content = normalizeStandaloneBackslashT(content);
|
|
497
|
+
token.markup = openDelim === "$$" ? "$$" : openDelim === "[" ? "[]" : "\\[\\]";
|
|
498
|
+
token.raw = `${openDelim}${content}${content.startsWith("\n") ? "\n" : ""}${closeDelim}`;
|
|
499
|
+
token.map = [startLine, nextLine + 1];
|
|
500
|
+
token.block = true;
|
|
501
|
+
token.loading = !found;
|
|
502
|
+
state.line = nextLine + 1;
|
|
503
|
+
return true;
|
|
504
|
+
};
|
|
505
|
+
md.inline.ruler.before("escape", "math", mathInline);
|
|
506
|
+
md.block.ruler.before("paragraph", "math_block", mathBlock, { alt: [
|
|
507
|
+
"paragraph",
|
|
508
|
+
"reference",
|
|
509
|
+
"blockquote",
|
|
510
|
+
"list"
|
|
511
|
+
] });
|
|
161
512
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
513
|
+
|
|
514
|
+
//#endregion
|
|
515
|
+
//#region src/renderers/index.ts
|
|
516
|
+
function applyRenderRules(md) {
|
|
517
|
+
const defaultImage = md.renderer.rules.image || function(tokens, idx, options, env, self) {
|
|
518
|
+
return self.renderToken(tokens, idx, options);
|
|
519
|
+
};
|
|
520
|
+
md.renderer.rules.image = (tokens, idx, options, env, self) => {
|
|
521
|
+
tokens[idx].attrSet?.("loading", "lazy");
|
|
522
|
+
return defaultImage(tokens, idx, options, env, self);
|
|
523
|
+
};
|
|
524
|
+
md.renderer.rules.fence = md.renderer.rules.fence || ((tokens, idx) => {
|
|
525
|
+
const token = tokens[idx];
|
|
526
|
+
const info = token.info ? token.info.trim() : "";
|
|
527
|
+
return `<pre class="${info ? `language-${md.utils.escapeHtml(info.split(/\s+/g)[0])}` : ""}"><code>${md.utils.escapeHtml(token.content)}</code></pre>`;
|
|
528
|
+
});
|
|
174
529
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
return {
|
|
193
|
-
type: "code_block",
|
|
194
|
-
language: r,
|
|
195
|
-
code: l || "",
|
|
196
|
-
raw: l || "",
|
|
197
|
-
diff: n,
|
|
198
|
-
loading: o === !0 ? !1 : o === !1 ? !0 : !c
|
|
199
|
-
};
|
|
530
|
+
|
|
531
|
+
//#endregion
|
|
532
|
+
//#region src/factory.ts
|
|
533
|
+
function factory(opts = {}) {
|
|
534
|
+
const md = new MarkdownIt({
|
|
535
|
+
html: true,
|
|
536
|
+
linkify: true,
|
|
537
|
+
typographer: true,
|
|
538
|
+
...opts.markdownItOptions ?? {}
|
|
539
|
+
});
|
|
540
|
+
if (opts.enableMath ?? true) applyMath(md, {
|
|
541
|
+
...getDefaultMathOptions() ?? {},
|
|
542
|
+
...opts.mathOptions ?? {}
|
|
543
|
+
});
|
|
544
|
+
if (opts.enableContainers ?? true) applyContainers(md);
|
|
545
|
+
applyRenderRules(md);
|
|
546
|
+
return md;
|
|
200
547
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
548
|
+
|
|
549
|
+
//#endregion
|
|
550
|
+
//#region src/parser/fixTableTokens.ts
|
|
551
|
+
function createStart() {
|
|
552
|
+
return [
|
|
553
|
+
{
|
|
554
|
+
type: "table_open",
|
|
555
|
+
tag: "table",
|
|
556
|
+
attrs: null,
|
|
557
|
+
map: null,
|
|
558
|
+
children: null,
|
|
559
|
+
content: "",
|
|
560
|
+
markup: "",
|
|
561
|
+
info: "",
|
|
562
|
+
level: 0,
|
|
563
|
+
loading: true,
|
|
564
|
+
meta: null
|
|
565
|
+
},
|
|
566
|
+
{
|
|
567
|
+
type: "thead_open",
|
|
568
|
+
tag: "thead",
|
|
569
|
+
attrs: null,
|
|
570
|
+
block: true,
|
|
571
|
+
level: 1,
|
|
572
|
+
children: null
|
|
573
|
+
},
|
|
574
|
+
{
|
|
575
|
+
type: "tr_open",
|
|
576
|
+
tag: "tr",
|
|
577
|
+
attrs: null,
|
|
578
|
+
block: true,
|
|
579
|
+
level: 2,
|
|
580
|
+
children: null
|
|
581
|
+
}
|
|
582
|
+
];
|
|
224
583
|
}
|
|
225
|
-
function
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
584
|
+
function createEnd() {
|
|
585
|
+
return [
|
|
586
|
+
{
|
|
587
|
+
type: "tr_close",
|
|
588
|
+
tag: "tr",
|
|
589
|
+
attrs: null,
|
|
590
|
+
block: true,
|
|
591
|
+
level: 2,
|
|
592
|
+
children: null
|
|
593
|
+
},
|
|
594
|
+
{
|
|
595
|
+
type: "thead_close",
|
|
596
|
+
tag: "thead",
|
|
597
|
+
attrs: null,
|
|
598
|
+
block: true,
|
|
599
|
+
level: 1,
|
|
600
|
+
children: null
|
|
601
|
+
},
|
|
602
|
+
{
|
|
603
|
+
type: "table_close",
|
|
604
|
+
tag: "table",
|
|
605
|
+
attrs: null,
|
|
606
|
+
map: null,
|
|
607
|
+
children: null,
|
|
608
|
+
content: "",
|
|
609
|
+
markup: "",
|
|
610
|
+
info: "",
|
|
611
|
+
level: 0,
|
|
612
|
+
meta: null
|
|
613
|
+
}
|
|
614
|
+
];
|
|
248
615
|
}
|
|
249
|
-
function
|
|
250
|
-
|
|
251
|
-
|
|
616
|
+
function createTh(text) {
|
|
617
|
+
return [
|
|
618
|
+
{
|
|
619
|
+
type: "th_open",
|
|
620
|
+
tag: "th",
|
|
621
|
+
attrs: null,
|
|
622
|
+
block: true,
|
|
623
|
+
level: 3,
|
|
624
|
+
children: null
|
|
625
|
+
},
|
|
626
|
+
{
|
|
627
|
+
type: "inline",
|
|
628
|
+
tag: "",
|
|
629
|
+
children: [{
|
|
630
|
+
tag: "",
|
|
631
|
+
type: "text",
|
|
632
|
+
block: false,
|
|
633
|
+
content: text,
|
|
634
|
+
children: null
|
|
635
|
+
}],
|
|
636
|
+
content: text,
|
|
637
|
+
level: 4,
|
|
638
|
+
attrs: null,
|
|
639
|
+
block: true
|
|
640
|
+
},
|
|
641
|
+
{
|
|
642
|
+
type: "th_close",
|
|
643
|
+
tag: "th",
|
|
644
|
+
attrs: null,
|
|
645
|
+
block: true,
|
|
646
|
+
level: 3,
|
|
647
|
+
children: null
|
|
648
|
+
}
|
|
649
|
+
];
|
|
252
650
|
}
|
|
253
|
-
function
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
content: "",
|
|
282
|
-
markup: "**",
|
|
283
|
-
info: "",
|
|
284
|
-
meta: null
|
|
285
|
-
}
|
|
286
|
-
], s = o.content?.slice(0, -1);
|
|
287
|
-
return s && t.unshift({
|
|
288
|
-
type: "text",
|
|
289
|
-
content: s,
|
|
290
|
-
raw: s
|
|
291
|
-
}), c.splice(i, l, ...t), c;
|
|
292
|
-
}
|
|
293
|
-
return c;
|
|
651
|
+
function fixTableTokens(tokens) {
|
|
652
|
+
const fixedTokens = [...tokens];
|
|
653
|
+
if (tokens.length < 3) return fixedTokens;
|
|
654
|
+
const i = tokens.length - 2;
|
|
655
|
+
const token = tokens[i];
|
|
656
|
+
if (token.type === "inline") {
|
|
657
|
+
if (/^\|(?:[^|\n]+\|?)+/.test(token.content)) {
|
|
658
|
+
const body = token.children[0].content.slice(1).split("|").map((i$1) => i$1.trim()).filter(Boolean).flatMap((i$1) => createTh(i$1));
|
|
659
|
+
const insert = [
|
|
660
|
+
...createStart(),
|
|
661
|
+
...body,
|
|
662
|
+
...createEnd()
|
|
663
|
+
];
|
|
664
|
+
fixedTokens.splice(i - 1, 3, ...insert);
|
|
665
|
+
} else if (/^\|(?:[^|\n]+\|)+\n\|:?-/.test(token.content)) {
|
|
666
|
+
const body = token.children[0].content.slice(1, -1).split("|").map((i$1) => i$1.trim()).flatMap((i$1) => createTh(i$1));
|
|
667
|
+
const insert = [
|
|
668
|
+
...createStart(),
|
|
669
|
+
...body,
|
|
670
|
+
...createEnd()
|
|
671
|
+
];
|
|
672
|
+
fixedTokens.splice(i - 1, 3, ...insert);
|
|
673
|
+
} else if (/^\|(?:[^|\n:]+\|)+\n\|:?$/.test(token.content)) {
|
|
674
|
+
token.content = token.content.slice(0, -2);
|
|
675
|
+
token.children.splice(2, 1);
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
return fixedTokens;
|
|
294
679
|
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
680
|
+
|
|
681
|
+
//#endregion
|
|
682
|
+
//#region src/parser/inline-parsers/checkbox-parser.ts
|
|
683
|
+
function parseCheckboxToken(token) {
|
|
684
|
+
return {
|
|
685
|
+
type: "checkbox",
|
|
686
|
+
checked: token.meta?.checked === true,
|
|
687
|
+
raw: token.meta?.checked ? "[x]" : "[ ]"
|
|
688
|
+
};
|
|
301
689
|
}
|
|
302
|
-
function
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
690
|
+
function parseCheckboxInputToken(token) {
|
|
691
|
+
return {
|
|
692
|
+
type: "checkbox_input",
|
|
693
|
+
checked: token.attrGet("checked") === "" || token.attrGet("checked") === "true",
|
|
694
|
+
raw: token.attrGet("checked") === "" || token.attrGet("checked") === "true" ? "[x]" : "[ ]"
|
|
695
|
+
};
|
|
308
696
|
}
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
raw: `==${o}==`
|
|
320
|
-
}, t = n < e.length ? n + 1 : e.length;
|
|
321
|
-
return { node: l, nextIndex: t };
|
|
697
|
+
|
|
698
|
+
//#endregion
|
|
699
|
+
//#region src/parser/inline-parsers/emoji-parser.ts
|
|
700
|
+
function parseEmojiToken(token) {
|
|
701
|
+
return {
|
|
702
|
+
type: "emoji",
|
|
703
|
+
name: token.content || "",
|
|
704
|
+
markup: token.markup || "",
|
|
705
|
+
raw: `:${token.content || ""}:`
|
|
706
|
+
};
|
|
322
707
|
}
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
708
|
+
|
|
709
|
+
//#endregion
|
|
710
|
+
//#region src/parser/inline-parsers/emphasis-parser.ts
|
|
711
|
+
function parseEmphasisToken(tokens, startIndex) {
|
|
712
|
+
const children = [];
|
|
713
|
+
let emText = "";
|
|
714
|
+
let i = startIndex + 1;
|
|
715
|
+
const innerTokens = [];
|
|
716
|
+
while (i < tokens.length && tokens[i].type !== "em_close") {
|
|
717
|
+
emText += tokens[i].content || "";
|
|
718
|
+
innerTokens.push(tokens[i]);
|
|
719
|
+
i++;
|
|
720
|
+
}
|
|
721
|
+
children.push(...parseInlineTokens(innerTokens));
|
|
722
|
+
return {
|
|
723
|
+
node: {
|
|
724
|
+
type: "emphasis",
|
|
725
|
+
children,
|
|
726
|
+
raw: `*${emText}*`
|
|
727
|
+
},
|
|
728
|
+
nextIndex: i < tokens.length ? i + 1 : tokens.length
|
|
729
|
+
};
|
|
332
730
|
}
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
731
|
+
|
|
732
|
+
//#endregion
|
|
733
|
+
//#region src/parser/inline-parsers/fence-parser.ts
|
|
734
|
+
function splitUnifiedDiff(content) {
|
|
735
|
+
const orig = [];
|
|
736
|
+
const updated = [];
|
|
737
|
+
for (const rawLine of content.split(/\r?\n/)) {
|
|
738
|
+
const line = rawLine;
|
|
739
|
+
if (/^(?:diff |index |--- |\+\+\+ |@@ )/.test(line)) continue;
|
|
740
|
+
if (line.startsWith("- ")) orig.push(` ${line.slice(1)}`);
|
|
741
|
+
else if (line.startsWith("+ ")) updated.push(` ${line.slice(1)}`);
|
|
742
|
+
else {
|
|
743
|
+
orig.push(line);
|
|
744
|
+
updated.push(line);
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
return {
|
|
748
|
+
original: orig.join("\n"),
|
|
749
|
+
updated: updated.join("\n")
|
|
750
|
+
};
|
|
339
751
|
}
|
|
340
|
-
function
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
752
|
+
function parseFenceToken(token) {
|
|
753
|
+
const hasMap = Array.isArray(token.map) && token.map.length === 2;
|
|
754
|
+
const meta = token.meta;
|
|
755
|
+
const closed = typeof meta?.closed === "boolean" ? meta.closed : void 0;
|
|
756
|
+
const diff = token.info?.startsWith("diff") || false;
|
|
757
|
+
const language = diff ? token.info?.split(" ")[1] || "" : token.info || "";
|
|
758
|
+
let content = token.content || "";
|
|
759
|
+
const trailingFenceLine = /\r?\n[ \t]*`+\s*$/;
|
|
760
|
+
if (trailingFenceLine.test(content)) content = content.replace(trailingFenceLine, "");
|
|
761
|
+
if (diff) {
|
|
762
|
+
const { original, updated } = splitUnifiedDiff(content);
|
|
763
|
+
return {
|
|
764
|
+
type: "code_block",
|
|
765
|
+
language,
|
|
766
|
+
code: updated || "",
|
|
767
|
+
raw: content,
|
|
768
|
+
diff,
|
|
769
|
+
loading: closed === true ? false : closed === false ? true : !hasMap,
|
|
770
|
+
originalCode: original,
|
|
771
|
+
updatedCode: updated
|
|
772
|
+
};
|
|
773
|
+
}
|
|
774
|
+
return {
|
|
775
|
+
type: "code_block",
|
|
776
|
+
language,
|
|
777
|
+
code: content || "",
|
|
778
|
+
raw: content || "",
|
|
779
|
+
diff,
|
|
780
|
+
loading: closed === true ? false : closed === false ? true : !hasMap
|
|
781
|
+
};
|
|
353
782
|
}
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
783
|
+
|
|
784
|
+
//#endregion
|
|
785
|
+
//#region src/parser/inline-parsers/fixLinkToken.ts
|
|
786
|
+
function fixLinkToken(tokens) {
|
|
787
|
+
if (tokens.length < 5) return tokens;
|
|
788
|
+
const first = tokens[tokens.length - 5];
|
|
789
|
+
if (first.type !== "text" && !first.content.endsWith("[")) return fixLinkTokens2(tokens);
|
|
790
|
+
if (tokens[tokens.length - 4].tag !== "em") return fixLinkTokens2(tokens);
|
|
791
|
+
const last = tokens[tokens.length - 1];
|
|
792
|
+
if (!last.content.startsWith("]")) return fixLinkTokens2(tokens);
|
|
793
|
+
const third = tokens[tokens.length - 3];
|
|
794
|
+
const href = last.content.replace(/^\]\(*/, "");
|
|
795
|
+
const loading = !last.content.includes(")");
|
|
796
|
+
first.content = first.content.replace(/\[$/, "");
|
|
797
|
+
tokens.splice(tokens.length - 3, 1, {
|
|
798
|
+
type: "link",
|
|
799
|
+
href,
|
|
800
|
+
text: third.content,
|
|
801
|
+
children: [{
|
|
802
|
+
type: "text",
|
|
803
|
+
content: third.content,
|
|
804
|
+
raw: third.content
|
|
805
|
+
}],
|
|
806
|
+
loading
|
|
807
|
+
});
|
|
808
|
+
tokens.splice(tokens.length - 1, 1);
|
|
809
|
+
return tokens;
|
|
370
810
|
}
|
|
371
|
-
function
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
811
|
+
function fixLinkTokens2(tokens) {
|
|
812
|
+
if (tokens.length < 8) return tokens;
|
|
813
|
+
let length = tokens.length;
|
|
814
|
+
let last = tokens[length - 1];
|
|
815
|
+
if (last.type !== "link_close") {
|
|
816
|
+
length--;
|
|
817
|
+
last = tokens[length - 1];
|
|
818
|
+
if (last.type !== "link_close") return tokens;
|
|
819
|
+
}
|
|
820
|
+
if (tokens[length - 7].type !== "em_open") return tokens;
|
|
821
|
+
const third = tokens[length - 6];
|
|
822
|
+
const first = tokens[length - 8];
|
|
823
|
+
let href = tokens[length - 2].content;
|
|
824
|
+
let count = 4;
|
|
825
|
+
if (length !== tokens.length) {
|
|
826
|
+
href += last.content || "";
|
|
827
|
+
count++;
|
|
828
|
+
}
|
|
829
|
+
tokens.splice(length - 4, count);
|
|
830
|
+
const content = third.content;
|
|
831
|
+
length -= 4;
|
|
832
|
+
first.content = first.content.replace(/\[$/, "");
|
|
833
|
+
tokens.splice(length - 2, 1, {
|
|
834
|
+
type: "link",
|
|
835
|
+
href,
|
|
836
|
+
text: content,
|
|
837
|
+
children: [{
|
|
838
|
+
type: "text",
|
|
839
|
+
content,
|
|
840
|
+
raw: content
|
|
841
|
+
}],
|
|
842
|
+
loading: true
|
|
843
|
+
});
|
|
844
|
+
return tokens;
|
|
378
845
|
}
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
846
|
+
|
|
847
|
+
//#endregion
|
|
848
|
+
//#region src/parser/inline-parsers/fixListItem.ts
|
|
849
|
+
function fixListItem(tokens) {
|
|
850
|
+
const last = tokens[tokens.length - 1];
|
|
851
|
+
if (/\d+\.\s*$/.test(last.content || "") && tokens[tokens.length - 2]?.tag === "br") tokens.splice(tokens.length - 1, 1);
|
|
852
|
+
return tokens;
|
|
385
853
|
}
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
854
|
+
|
|
855
|
+
//#endregion
|
|
856
|
+
//#region src/parser/inline-parsers/fixStrongTokens.ts
|
|
857
|
+
function fixStrongTokens(tokens) {
|
|
858
|
+
const fixedTokens = [...tokens];
|
|
859
|
+
if (tokens.length < 4) return fixedTokens;
|
|
860
|
+
const i = tokens.length - 4;
|
|
861
|
+
const token = tokens[i];
|
|
862
|
+
const nextToken = tokens[i + 1];
|
|
863
|
+
if (token.type === "text" && token.content?.endsWith("*") && nextToken.type === "em_open") {
|
|
864
|
+
const _nextToken = tokens[i + 2];
|
|
865
|
+
const count = _nextToken?.type === "text" ? 4 : 3;
|
|
866
|
+
const insert = [
|
|
867
|
+
{
|
|
868
|
+
type: "strong_open",
|
|
869
|
+
tag: "strong",
|
|
870
|
+
attrs: null,
|
|
871
|
+
map: null,
|
|
872
|
+
children: null,
|
|
873
|
+
content: "",
|
|
874
|
+
markup: "**",
|
|
875
|
+
info: "",
|
|
876
|
+
meta: null
|
|
877
|
+
},
|
|
878
|
+
{
|
|
879
|
+
type: "text",
|
|
880
|
+
content: _nextToken?.type === "text" ? _nextToken.content : ""
|
|
881
|
+
},
|
|
882
|
+
{
|
|
883
|
+
type: "strong_close",
|
|
884
|
+
tag: "strong",
|
|
885
|
+
attrs: null,
|
|
886
|
+
map: null,
|
|
887
|
+
children: null,
|
|
888
|
+
content: "",
|
|
889
|
+
markup: "**",
|
|
890
|
+
info: "",
|
|
891
|
+
meta: null
|
|
892
|
+
}
|
|
893
|
+
];
|
|
894
|
+
const beforeText = token.content?.slice(0, -1);
|
|
895
|
+
if (beforeText) insert.unshift({
|
|
896
|
+
type: "text",
|
|
897
|
+
content: beforeText,
|
|
898
|
+
raw: beforeText
|
|
899
|
+
});
|
|
900
|
+
fixedTokens.splice(i, count, ...insert);
|
|
901
|
+
return fixedTokens;
|
|
902
|
+
}
|
|
903
|
+
return fixedTokens;
|
|
399
904
|
}
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
children: o,
|
|
410
|
-
raw: `**${n}**`
|
|
411
|
-
}, s = r < e.length ? r + 1 : e.length;
|
|
412
|
-
return { node: t, nextIndex: s };
|
|
905
|
+
|
|
906
|
+
//#endregion
|
|
907
|
+
//#region src/parser/inline-parsers/footnote-ref-parser.ts
|
|
908
|
+
function parseFootnoteRefToken(token) {
|
|
909
|
+
return {
|
|
910
|
+
type: "footnote_reference",
|
|
911
|
+
id: token.meta?.label || "",
|
|
912
|
+
raw: `[^${token.meta?.label || ""}]`
|
|
913
|
+
};
|
|
413
914
|
}
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
type: "subscript",
|
|
423
|
-
children: i.length > 0 ? i : [
|
|
424
|
-
{
|
|
425
|
-
type: "text",
|
|
426
|
-
// Fallback to the collected inner text (e.g., "2" in H~2~O)
|
|
427
|
-
content: o || e[c].content || "",
|
|
428
|
-
raw: o || e[c].content || ""
|
|
429
|
-
}
|
|
430
|
-
],
|
|
431
|
-
raw: `~${o || e[c].content || ""}~`
|
|
432
|
-
}, t = n < e.length ? n + 1 : e.length;
|
|
433
|
-
return { node: l, nextIndex: t };
|
|
915
|
+
|
|
916
|
+
//#endregion
|
|
917
|
+
//#region src/parser/inline-parsers/hardbreak-parser.ts
|
|
918
|
+
function parseHardbreakToken() {
|
|
919
|
+
return {
|
|
920
|
+
type: "hardbreak",
|
|
921
|
+
raw: "\\\n"
|
|
922
|
+
};
|
|
434
923
|
}
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
924
|
+
|
|
925
|
+
//#endregion
|
|
926
|
+
//#region src/parser/inline-parsers/highlight-parser.ts
|
|
927
|
+
function parseHighlightToken(tokens, startIndex) {
|
|
928
|
+
const children = [];
|
|
929
|
+
let markText = "";
|
|
930
|
+
let i = startIndex + 1;
|
|
931
|
+
const innerTokens = [];
|
|
932
|
+
while (i < tokens.length && tokens[i].type !== "mark_close") {
|
|
933
|
+
markText += tokens[i].content || "";
|
|
934
|
+
innerTokens.push(tokens[i]);
|
|
935
|
+
i++;
|
|
936
|
+
}
|
|
937
|
+
children.push(...parseInlineTokens(innerTokens));
|
|
938
|
+
return {
|
|
939
|
+
node: {
|
|
940
|
+
type: "highlight",
|
|
941
|
+
children,
|
|
942
|
+
raw: `==${markText}==`
|
|
943
|
+
},
|
|
944
|
+
nextIndex: i < tokens.length ? i + 1 : tokens.length
|
|
945
|
+
};
|
|
455
946
|
}
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
947
|
+
|
|
948
|
+
//#endregion
|
|
949
|
+
//#region src/parser/inline-parsers/image-parser.ts
|
|
950
|
+
function parseImageToken(token, loading = false) {
|
|
951
|
+
return {
|
|
952
|
+
type: "image",
|
|
953
|
+
src: token.attrs?.find((attr) => attr[0] === "src")?.[1] || "",
|
|
954
|
+
alt: token.attrs?.find((attr) => attr[0] === "alt")?.[1] || "",
|
|
955
|
+
title: token.attrs?.find((attr) => attr[0] === "title")?.[1] || null,
|
|
956
|
+
raw: token.content || "",
|
|
957
|
+
loading
|
|
958
|
+
};
|
|
462
959
|
}
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
let t = l.content.replace(/\\/g, "") || "";
|
|
473
|
-
if (t === "`" || t === "|" || t === "$" || t === "1" || /^\*+$/.test(t) || /^\d$/.test(t)) {
|
|
474
|
-
r++;
|
|
475
|
-
break;
|
|
476
|
-
}
|
|
477
|
-
if (/[^\]]\s*\(\s*$/.test(t) && (t = t.replace(/\(\s*$/, "")), c?.startsWith("[") && i?.type === "list_item_open") {
|
|
478
|
-
const f = t.slice(1).match(/[^\s\]]/);
|
|
479
|
-
if (f === null) {
|
|
480
|
-
r++;
|
|
481
|
-
break;
|
|
482
|
-
}
|
|
483
|
-
if (f && /x/i.test(f[0]) || !f) {
|
|
484
|
-
const h = f ? f[0] === "x" || f[0] === "X" : !1;
|
|
485
|
-
o.push({
|
|
486
|
-
type: "checkbox_input",
|
|
487
|
-
checked: h,
|
|
488
|
-
raw: h ? "[x]" : "[ ]"
|
|
489
|
-
}), r++;
|
|
490
|
-
break;
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
if (/`[^`]*/.test(t)) {
|
|
494
|
-
n = null, o.push({
|
|
495
|
-
type: "inline_code",
|
|
496
|
-
code: t.replace(/`/g, ""),
|
|
497
|
-
raw: t || ""
|
|
498
|
-
}), r++;
|
|
499
|
-
break;
|
|
500
|
-
}
|
|
501
|
-
if (t === "[") {
|
|
502
|
-
r++;
|
|
503
|
-
break;
|
|
504
|
-
}
|
|
505
|
-
if (/[^~]*~{2,}[^~]+/.test(t)) {
|
|
506
|
-
const u = t.indexOf("~~") || 0, f = t.slice(0, u);
|
|
507
|
-
f && (n ? (n.content += f, n.raw += f) : (n = {
|
|
508
|
-
type: "text",
|
|
509
|
-
content: f || "",
|
|
510
|
-
raw: l.content || ""
|
|
511
|
-
}, o.push(n)));
|
|
512
|
-
const h = t.slice(u);
|
|
513
|
-
n = null;
|
|
514
|
-
const { node: g } = B([
|
|
515
|
-
{
|
|
516
|
-
type: "s_open",
|
|
517
|
-
tag: "s",
|
|
518
|
-
attrs: [],
|
|
519
|
-
map: void 0,
|
|
520
|
-
content: "",
|
|
521
|
-
markup: "*",
|
|
522
|
-
info: "",
|
|
523
|
-
meta: null
|
|
524
|
-
},
|
|
525
|
-
{
|
|
526
|
-
type: "text",
|
|
527
|
-
tag: "",
|
|
528
|
-
attrs: [],
|
|
529
|
-
map: void 0,
|
|
530
|
-
content: h.replace(/~/g, ""),
|
|
531
|
-
markup: "",
|
|
532
|
-
info: "",
|
|
533
|
-
meta: null
|
|
534
|
-
},
|
|
535
|
-
{
|
|
536
|
-
type: "s_close",
|
|
537
|
-
tag: "s",
|
|
538
|
-
attrs: [],
|
|
539
|
-
map: void 0,
|
|
540
|
-
content: "",
|
|
541
|
-
markup: "*",
|
|
542
|
-
info: "",
|
|
543
|
-
meta: null
|
|
544
|
-
}
|
|
545
|
-
], 0);
|
|
546
|
-
o.push(g), r++;
|
|
547
|
-
break;
|
|
548
|
-
}
|
|
549
|
-
if (/[^*]*\*\*[^*]+/.test(t)) {
|
|
550
|
-
const u = t.indexOf("*") || 0, f = t.slice(0, u);
|
|
551
|
-
f && (n ? (n.content += f, n.raw += f) : (n = {
|
|
552
|
-
type: "text",
|
|
553
|
-
content: f || "",
|
|
554
|
-
raw: l.content || ""
|
|
555
|
-
}, o.push(n)));
|
|
556
|
-
const h = t.slice(u);
|
|
557
|
-
n = null;
|
|
558
|
-
const { node: g } = W([
|
|
559
|
-
{
|
|
560
|
-
type: "strong_open",
|
|
561
|
-
tag: "strong",
|
|
562
|
-
content: "",
|
|
563
|
-
markup: "*",
|
|
564
|
-
info: "",
|
|
565
|
-
meta: null
|
|
566
|
-
},
|
|
567
|
-
{
|
|
568
|
-
type: "text",
|
|
569
|
-
tag: "",
|
|
570
|
-
content: h.replace(/\*/g, ""),
|
|
571
|
-
markup: "",
|
|
572
|
-
info: "",
|
|
573
|
-
meta: null
|
|
574
|
-
},
|
|
575
|
-
{
|
|
576
|
-
type: "strong_close",
|
|
577
|
-
tag: "strong",
|
|
578
|
-
content: "",
|
|
579
|
-
markup: "*",
|
|
580
|
-
info: "",
|
|
581
|
-
meta: null
|
|
582
|
-
}
|
|
583
|
-
], 0, c);
|
|
584
|
-
o.push(g), r++;
|
|
585
|
-
break;
|
|
586
|
-
} else if (/[^*]*\*[^*]+/.test(t)) {
|
|
587
|
-
const u = t.indexOf("*") || 0, f = t.slice(0, u);
|
|
588
|
-
f && (n ? (n.content += f, n.raw += f) : (n = {
|
|
589
|
-
type: "text",
|
|
590
|
-
content: f || "",
|
|
591
|
-
raw: l.content || ""
|
|
592
|
-
}, o.push(n)));
|
|
593
|
-
const h = t.slice(u);
|
|
594
|
-
n = null;
|
|
595
|
-
const { node: g } = D([
|
|
596
|
-
{
|
|
597
|
-
type: "em_open",
|
|
598
|
-
tag: "em",
|
|
599
|
-
content: "",
|
|
600
|
-
markup: "*",
|
|
601
|
-
info: "",
|
|
602
|
-
meta: null
|
|
603
|
-
},
|
|
604
|
-
{
|
|
605
|
-
type: "text",
|
|
606
|
-
tag: "",
|
|
607
|
-
content: h.replace(/\*/g, ""),
|
|
608
|
-
markup: "",
|
|
609
|
-
info: "",
|
|
610
|
-
meta: null
|
|
611
|
-
},
|
|
612
|
-
{
|
|
613
|
-
type: "em_close",
|
|
614
|
-
tag: "em",
|
|
615
|
-
content: "",
|
|
616
|
-
markup: "*",
|
|
617
|
-
info: "",
|
|
618
|
-
meta: null
|
|
619
|
-
}
|
|
620
|
-
], 0);
|
|
621
|
-
o.push(g), r++;
|
|
622
|
-
break;
|
|
623
|
-
}
|
|
624
|
-
const s = t.indexOf(";
|
|
639
|
-
if (f !== -1) {
|
|
640
|
-
o.push({
|
|
641
|
-
type: "text",
|
|
642
|
-
content: u,
|
|
643
|
-
raw: u
|
|
644
|
-
});
|
|
645
|
-
const h = t.slice(a + 1, f);
|
|
646
|
-
o.push({
|
|
647
|
-
type: "link",
|
|
648
|
-
href: "",
|
|
649
|
-
text: h,
|
|
650
|
-
children: [
|
|
651
|
-
{
|
|
652
|
-
type: "text",
|
|
653
|
-
content: h,
|
|
654
|
-
raw: h
|
|
655
|
-
}
|
|
656
|
-
],
|
|
657
|
-
loading: !0
|
|
658
|
-
}), r++;
|
|
659
|
-
break;
|
|
660
|
-
}
|
|
661
|
-
}
|
|
662
|
-
const p = e[r - 1];
|
|
663
|
-
if (n)
|
|
664
|
-
n.content += d.content.replace(/(\*+|\(|\\)$/, ""), n.raw += d.raw;
|
|
665
|
-
else {
|
|
666
|
-
const u = p?.tag === "br" && e[r - 2]?.content === "[";
|
|
667
|
-
d.content = d.content.replace(/(\*+|\(|\\)$/, ""), n = d, n.center = u, o.push(n);
|
|
668
|
-
}
|
|
669
|
-
r++;
|
|
670
|
-
break;
|
|
671
|
-
}
|
|
672
|
-
case "softbreak":
|
|
673
|
-
n && (n.content += `
|
|
674
|
-
`, n.raw += `
|
|
675
|
-
`), r++;
|
|
676
|
-
break;
|
|
677
|
-
case "code_inline":
|
|
678
|
-
n = null, o.push(me(l)), r++;
|
|
679
|
-
break;
|
|
680
|
-
case "link_open": {
|
|
681
|
-
n = null;
|
|
682
|
-
const t = l.attrs?.find((d) => d[0] === "href")?.[1];
|
|
683
|
-
if (c && t) {
|
|
684
|
-
const d = new RegExp(`\\(\\s*${t}\\s*\\)`), p = o.length > 0 ? o[o.length - 1] : null, u = !d.test(c);
|
|
685
|
-
if (u && p && new RegExp(`\\[${p.text}\\s*\\]\\(`).test(c)) {
|
|
686
|
-
const h = p?.text || p?.content?.slice(1, -1) || "";
|
|
687
|
-
o.splice(o.length - 1, 1, {
|
|
688
|
-
type: "link",
|
|
689
|
-
href: "",
|
|
690
|
-
text: h,
|
|
691
|
-
loading: u
|
|
692
|
-
}), r += 3, e[r]?.content === "." && r++;
|
|
693
|
-
break;
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
const { node: s, nextIndex: a } = ye(e, r);
|
|
697
|
-
r = a, s.loading = !1, o.push(s);
|
|
698
|
-
break;
|
|
699
|
-
}
|
|
700
|
-
case "image":
|
|
701
|
-
n = null, o.push(P(l)), r++;
|
|
702
|
-
break;
|
|
703
|
-
case "strong_open": {
|
|
704
|
-
n = null;
|
|
705
|
-
const { node: t, nextIndex: s } = W(e, r, l.content);
|
|
706
|
-
o.push(t), r = s;
|
|
707
|
-
break;
|
|
708
|
-
}
|
|
709
|
-
case "em_open": {
|
|
710
|
-
n = null;
|
|
711
|
-
const { node: t, nextIndex: s } = D(e, r);
|
|
712
|
-
o.push(t), r = s;
|
|
713
|
-
break;
|
|
714
|
-
}
|
|
715
|
-
case "s_open": {
|
|
716
|
-
n = null;
|
|
717
|
-
const { node: t, nextIndex: s } = B(e, r);
|
|
718
|
-
o.push(t), r = s;
|
|
719
|
-
break;
|
|
720
|
-
}
|
|
721
|
-
case "mark_open": {
|
|
722
|
-
n = null;
|
|
723
|
-
const { node: t, nextIndex: s } = ge(e, r);
|
|
724
|
-
o.push(t), r = s;
|
|
725
|
-
break;
|
|
726
|
-
}
|
|
727
|
-
case "ins_open": {
|
|
728
|
-
n = null;
|
|
729
|
-
const { node: t, nextIndex: s } = _e(e, r);
|
|
730
|
-
o.push(t), r = s;
|
|
731
|
-
break;
|
|
732
|
-
}
|
|
733
|
-
case "sub_open": {
|
|
734
|
-
n = null;
|
|
735
|
-
const { node: t, nextIndex: s } = xe(e, r);
|
|
736
|
-
o.push(t), r = s;
|
|
737
|
-
break;
|
|
738
|
-
}
|
|
739
|
-
case "sup_open": {
|
|
740
|
-
n = null;
|
|
741
|
-
const { node: t, nextIndex: s } = $e(e, r);
|
|
742
|
-
o.push(t), r = s;
|
|
743
|
-
break;
|
|
744
|
-
}
|
|
745
|
-
case "sub":
|
|
746
|
-
n = null, o.push({
|
|
747
|
-
type: "subscript",
|
|
748
|
-
children: [
|
|
749
|
-
{
|
|
750
|
-
type: "text",
|
|
751
|
-
content: l.content || "",
|
|
752
|
-
raw: l.content || ""
|
|
753
|
-
}
|
|
754
|
-
],
|
|
755
|
-
raw: `~${l.content || ""}~`
|
|
756
|
-
}), r++;
|
|
757
|
-
break;
|
|
758
|
-
case "sup":
|
|
759
|
-
n = null, o.push({
|
|
760
|
-
type: "superscript",
|
|
761
|
-
children: [
|
|
762
|
-
{
|
|
763
|
-
type: "text",
|
|
764
|
-
content: l.content || "",
|
|
765
|
-
raw: l.content || ""
|
|
766
|
-
}
|
|
767
|
-
],
|
|
768
|
-
raw: `^${l.content || ""}^`
|
|
769
|
-
}), r++;
|
|
770
|
-
break;
|
|
771
|
-
case "emoji": {
|
|
772
|
-
n = null;
|
|
773
|
-
const t = e[r - 1];
|
|
774
|
-
t?.type === "text" && /\|:-+/.test(t.content || "") ? o.push({
|
|
775
|
-
type: "text",
|
|
776
|
-
content: "",
|
|
777
|
-
raw: ""
|
|
778
|
-
}) : o.push(se(l)), r++;
|
|
779
|
-
break;
|
|
780
|
-
}
|
|
781
|
-
case "checkbox":
|
|
782
|
-
n = null, o.push(ie(l)), r++;
|
|
783
|
-
break;
|
|
784
|
-
case "checkbox_input":
|
|
785
|
-
n = null, o.push(le(l)), r++;
|
|
786
|
-
break;
|
|
787
|
-
case "footnote_ref":
|
|
788
|
-
n = null, o.push(de(l)), r++;
|
|
789
|
-
break;
|
|
790
|
-
case "hardbreak":
|
|
791
|
-
n = null, o.push(fe()), r++;
|
|
792
|
-
break;
|
|
793
|
-
case "fence": {
|
|
794
|
-
n = null, o.push(N(e[r])), r++;
|
|
795
|
-
break;
|
|
796
|
-
}
|
|
797
|
-
case "math_inline": {
|
|
798
|
-
n = null, o.push(be(l)), r++;
|
|
799
|
-
break;
|
|
800
|
-
}
|
|
801
|
-
case "reference": {
|
|
802
|
-
n = null, o.push(we(l)), r++;
|
|
803
|
-
break;
|
|
804
|
-
}
|
|
805
|
-
default:
|
|
806
|
-
o.push(l), n = null, r++;
|
|
807
|
-
break;
|
|
808
|
-
}
|
|
809
|
-
}
|
|
810
|
-
return o;
|
|
960
|
+
|
|
961
|
+
//#endregion
|
|
962
|
+
//#region src/parser/inline-parsers/inline-code-parser.ts
|
|
963
|
+
function parseInlineCodeToken(token) {
|
|
964
|
+
return {
|
|
965
|
+
type: "inline_code",
|
|
966
|
+
code: token.content || "",
|
|
967
|
+
raw: token.content || ""
|
|
968
|
+
};
|
|
811
969
|
}
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
970
|
+
|
|
971
|
+
//#endregion
|
|
972
|
+
//#region src/parser/inline-parsers/insert-parser.ts
|
|
973
|
+
function parseInsertToken(tokens, startIndex) {
|
|
974
|
+
const children = [];
|
|
975
|
+
let insText = "";
|
|
976
|
+
let i = startIndex + 1;
|
|
977
|
+
const innerTokens = [];
|
|
978
|
+
while (i < tokens.length && tokens[i].type !== "ins_close") {
|
|
979
|
+
insText += tokens[i].content || "";
|
|
980
|
+
innerTokens.push(tokens[i]);
|
|
981
|
+
i++;
|
|
982
|
+
}
|
|
983
|
+
children.push(...parseInlineTokens(innerTokens));
|
|
984
|
+
return {
|
|
985
|
+
node: {
|
|
986
|
+
type: "insert",
|
|
987
|
+
children,
|
|
988
|
+
raw: `++${insText}++`
|
|
989
|
+
},
|
|
990
|
+
nextIndex: i < tokens.length ? i + 1 : tokens.length
|
|
991
|
+
};
|
|
834
992
|
}
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
993
|
+
|
|
994
|
+
//#endregion
|
|
995
|
+
//#region src/parser/inline-parsers/link-parser.ts
|
|
996
|
+
function parseLinkToken(tokens, startIndex) {
|
|
997
|
+
const openToken = tokens[startIndex];
|
|
998
|
+
const href = openToken.attrs?.find((attr) => attr[0] === "href")?.[1] || "";
|
|
999
|
+
const title = openToken.attrs?.find((attr) => attr[0] === "title")?.[1] || null;
|
|
1000
|
+
let i = startIndex + 1;
|
|
1001
|
+
const linkTokens = [];
|
|
1002
|
+
const loading = true;
|
|
1003
|
+
while (i < tokens.length && tokens[i].type !== "link_close") {
|
|
1004
|
+
linkTokens.push(tokens[i]);
|
|
1005
|
+
i++;
|
|
1006
|
+
}
|
|
1007
|
+
const children = parseInlineTokens(linkTokens);
|
|
1008
|
+
const linkText = children.map((node) => {
|
|
1009
|
+
if ("content" in node) return node.content;
|
|
1010
|
+
return node.raw;
|
|
1011
|
+
}).join("");
|
|
1012
|
+
return {
|
|
1013
|
+
node: {
|
|
1014
|
+
type: "link",
|
|
1015
|
+
href,
|
|
1016
|
+
title,
|
|
1017
|
+
text: linkText,
|
|
1018
|
+
children,
|
|
1019
|
+
raw: `[${linkText}](${href}${title ? ` "${title}"` : ""})`,
|
|
1020
|
+
loading
|
|
1021
|
+
},
|
|
1022
|
+
nextIndex: i < tokens.length ? i + 1 : tokens.length
|
|
1023
|
+
};
|
|
848
1024
|
}
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
if (e[t].type === "paragraph_open") {
|
|
860
|
-
const s = e[t + 1];
|
|
861
|
-
r.push({
|
|
862
|
-
type: "paragraph",
|
|
863
|
-
children: b(s.children || [], s.content || ""),
|
|
864
|
-
raw: s.content || ""
|
|
865
|
-
}), t += 3;
|
|
866
|
-
} else
|
|
867
|
-
t++;
|
|
868
|
-
n.length > 0 && (i.push({
|
|
869
|
-
type: "definition_item",
|
|
870
|
-
term: n,
|
|
871
|
-
definition: r,
|
|
872
|
-
raw: `${n.map((s) => s.raw).join("")}: ${r.map((s) => s.raw).join(`
|
|
873
|
-
`)}`
|
|
874
|
-
}), n = []), o = t + 1;
|
|
875
|
-
} else
|
|
876
|
-
o++;
|
|
877
|
-
return [{
|
|
878
|
-
type: "definition_list",
|
|
879
|
-
items: i,
|
|
880
|
-
raw: i.map((t) => t.raw).join(`
|
|
881
|
-
`)
|
|
882
|
-
}, o + 1];
|
|
1025
|
+
|
|
1026
|
+
//#endregion
|
|
1027
|
+
//#region src/parser/inline-parsers/math-inline-parser.ts
|
|
1028
|
+
function parseMathInlineToken(token) {
|
|
1029
|
+
return {
|
|
1030
|
+
type: "math_inline",
|
|
1031
|
+
content: token.content || "",
|
|
1032
|
+
loading: !!token.loading,
|
|
1033
|
+
raw: token.raw
|
|
1034
|
+
};
|
|
883
1035
|
}
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
raw: t.content || ""
|
|
894
|
-
}), r += 3;
|
|
895
|
-
} else
|
|
896
|
-
r++;
|
|
897
|
-
return [{
|
|
898
|
-
type: "footnote",
|
|
899
|
-
id: o,
|
|
900
|
-
children: n,
|
|
901
|
-
raw: `[^${o}]: ${n.map((t) => t.raw).join(`
|
|
902
|
-
`)}`
|
|
903
|
-
}, r + 1];
|
|
1036
|
+
|
|
1037
|
+
//#endregion
|
|
1038
|
+
//#region src/parser/inline-parsers/reference-parser.ts
|
|
1039
|
+
function parseReferenceToken(token) {
|
|
1040
|
+
return {
|
|
1041
|
+
type: "reference",
|
|
1042
|
+
id: token.content || "",
|
|
1043
|
+
raw: token.markup || `[${token.content}]`
|
|
1044
|
+
};
|
|
904
1045
|
}
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
1046
|
+
|
|
1047
|
+
//#endregion
|
|
1048
|
+
//#region src/parser/inline-parsers/strikethrough-parser.ts
|
|
1049
|
+
function parseStrikethroughToken(tokens, startIndex) {
|
|
1050
|
+
const children = [];
|
|
1051
|
+
let sText = "";
|
|
1052
|
+
let i = startIndex + 1;
|
|
1053
|
+
const innerTokens = [];
|
|
1054
|
+
while (i < tokens.length && tokens[i].type !== "s_close") {
|
|
1055
|
+
sText += tokens[i].content || "";
|
|
1056
|
+
innerTokens.push(tokens[i]);
|
|
1057
|
+
i++;
|
|
1058
|
+
}
|
|
1059
|
+
children.push(...parseInlineTokens(innerTokens));
|
|
1060
|
+
return {
|
|
1061
|
+
node: {
|
|
1062
|
+
type: "strikethrough",
|
|
1063
|
+
children,
|
|
1064
|
+
raw: `~~${sText}~~`
|
|
1065
|
+
},
|
|
1066
|
+
nextIndex: i < tokens.length ? i + 1 : tokens.length
|
|
1067
|
+
};
|
|
914
1068
|
}
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
1069
|
+
|
|
1070
|
+
//#endregion
|
|
1071
|
+
//#region src/parser/inline-parsers/strong-parser.ts
|
|
1072
|
+
function parseStrongToken(tokens, startIndex, raw) {
|
|
1073
|
+
const children = [];
|
|
1074
|
+
let strongText = "";
|
|
1075
|
+
let i = startIndex + 1;
|
|
1076
|
+
const innerTokens = [];
|
|
1077
|
+
while (i < tokens.length && tokens[i].type !== "strong_close") {
|
|
1078
|
+
strongText += tokens[i].content || "";
|
|
1079
|
+
innerTokens.push(tokens[i]);
|
|
1080
|
+
i++;
|
|
1081
|
+
}
|
|
1082
|
+
children.push(...parseInlineTokens(innerTokens, raw));
|
|
1083
|
+
return {
|
|
1084
|
+
node: {
|
|
1085
|
+
type: "strong",
|
|
1086
|
+
children,
|
|
1087
|
+
raw: `**${strongText}**`
|
|
1088
|
+
},
|
|
1089
|
+
nextIndex: i < tokens.length ? i + 1 : tokens.length
|
|
1090
|
+
};
|
|
922
1091
|
}
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
type: "table_row",
|
|
950
|
-
cells: t,
|
|
951
|
-
raw: t.map((d) => d.raw).join("|")
|
|
952
|
-
};
|
|
953
|
-
r ? o = a : n.push(a), i = s + 1;
|
|
954
|
-
} else
|
|
955
|
-
i++;
|
|
956
|
-
return o || (o = {
|
|
957
|
-
type: "table_row",
|
|
958
|
-
cells: [],
|
|
959
|
-
raw: ""
|
|
960
|
-
}), [{
|
|
961
|
-
type: "table",
|
|
962
|
-
header: o,
|
|
963
|
-
rows: n,
|
|
964
|
-
loading: e[c].loading || !1,
|
|
965
|
-
raw: [o, ...n].map((t) => t.raw).join(`
|
|
966
|
-
`)
|
|
967
|
-
}, i + 1];
|
|
1092
|
+
|
|
1093
|
+
//#endregion
|
|
1094
|
+
//#region src/parser/inline-parsers/subscript-parser.ts
|
|
1095
|
+
function parseSubscriptToken(tokens, startIndex) {
|
|
1096
|
+
const children = [];
|
|
1097
|
+
let subText = "";
|
|
1098
|
+
let i = startIndex + 1;
|
|
1099
|
+
const innerTokens = [];
|
|
1100
|
+
while (i < tokens.length && tokens[i].type !== "sub_close") {
|
|
1101
|
+
subText += tokens[i].content || "";
|
|
1102
|
+
innerTokens.push(tokens[i]);
|
|
1103
|
+
i++;
|
|
1104
|
+
}
|
|
1105
|
+
children.push(...parseInlineTokens(innerTokens));
|
|
1106
|
+
return {
|
|
1107
|
+
node: {
|
|
1108
|
+
type: "subscript",
|
|
1109
|
+
children: children.length > 0 ? children : [{
|
|
1110
|
+
type: "text",
|
|
1111
|
+
content: subText || tokens[startIndex].content || "",
|
|
1112
|
+
raw: subText || tokens[startIndex].content || ""
|
|
1113
|
+
}],
|
|
1114
|
+
raw: `~${subText || tokens[startIndex].content || ""}~`
|
|
1115
|
+
},
|
|
1116
|
+
nextIndex: i < tokens.length ? i + 1 : tokens.length
|
|
1117
|
+
};
|
|
968
1118
|
}
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
1119
|
+
|
|
1120
|
+
//#endregion
|
|
1121
|
+
//#region src/parser/inline-parsers/superscript-parser.ts
|
|
1122
|
+
function parseSuperscriptToken(tokens, startIndex) {
|
|
1123
|
+
const children = [];
|
|
1124
|
+
let supText = "";
|
|
1125
|
+
let i = startIndex + 1;
|
|
1126
|
+
const innerTokens = [];
|
|
1127
|
+
while (i < tokens.length && tokens[i].type !== "sup_close") {
|
|
1128
|
+
supText += tokens[i].content || "";
|
|
1129
|
+
innerTokens.push(tokens[i]);
|
|
1130
|
+
i++;
|
|
1131
|
+
}
|
|
1132
|
+
children.push(...parseInlineTokens(innerTokens));
|
|
1133
|
+
return {
|
|
1134
|
+
node: {
|
|
1135
|
+
type: "superscript",
|
|
1136
|
+
children: children.length > 0 ? children : [{
|
|
1137
|
+
type: "text",
|
|
1138
|
+
content: supText || tokens[startIndex].content || "",
|
|
1139
|
+
raw: supText || tokens[startIndex].content || ""
|
|
1140
|
+
}],
|
|
1141
|
+
raw: `^${supText || tokens[startIndex].content || ""}^`
|
|
1142
|
+
},
|
|
1143
|
+
nextIndex: i < tokens.length ? i + 1 : tokens.length
|
|
1144
|
+
};
|
|
974
1145
|
}
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
const l = [];
|
|
985
|
-
let t = n + 1;
|
|
986
|
-
for (; t < e.length && e[t].type !== "list_item_close"; )
|
|
987
|
-
if (e[t].type === "paragraph_open") {
|
|
988
|
-
const s = e[t + 1], a = e[t - 1], d = s && typeof s.content == "string" ? s.content : "";
|
|
989
|
-
/\n\d+$/.test(d) && (s && (s.content = d.replace(/\n\d+$/, "")), s?.children && s.children.splice(-1, 1)), l.push({
|
|
990
|
-
type: "paragraph",
|
|
991
|
-
children: b(s?.children || [], s?.content || "", a),
|
|
992
|
-
raw: s?.content || ""
|
|
993
|
-
}), t += 3;
|
|
994
|
-
} else if (e[t].type === "blockquote_open") {
|
|
995
|
-
const [s, a] = Z(e, t);
|
|
996
|
-
l.push(s), t = a;
|
|
997
|
-
} else if (e[t].type === "bullet_list_open" || e[t].type === "ordered_list_open") {
|
|
998
|
-
if (e[t].markup === "*") {
|
|
999
|
-
t++;
|
|
1000
|
-
continue;
|
|
1001
|
-
}
|
|
1002
|
-
const [s, a] = Q(e, t);
|
|
1003
|
-
l.push(s), t = a;
|
|
1004
|
-
} else if (e[t].type === "code_block")
|
|
1005
|
-
l.push(A(e[t])), t += 1;
|
|
1006
|
-
else if (e[t].type === "fence")
|
|
1007
|
-
l.push(N(e[t])), t += 1;
|
|
1008
|
-
else if (e[t].type === "math_block")
|
|
1009
|
-
l.push(j(e[t])), t += 1;
|
|
1010
|
-
else if (e[t].type === "table_open") {
|
|
1011
|
-
const [s, a] = K(e, t);
|
|
1012
|
-
l.push(s), t = a;
|
|
1013
|
-
} else if (e[t].type === "dl_open") {
|
|
1014
|
-
const [s, a] = U(e, t);
|
|
1015
|
-
l.push(s), t = a;
|
|
1016
|
-
} else if (e[t].type === "footnote_open") {
|
|
1017
|
-
const [s, a] = G(e, t);
|
|
1018
|
-
l.push(s), t = a;
|
|
1019
|
-
} else if (e[t].type === "heading_open") {
|
|
1020
|
-
const s = z(e, t);
|
|
1021
|
-
l.push(s), t += 3;
|
|
1022
|
-
} else if (e[t].type === "hr")
|
|
1023
|
-
l.push(J()), t += 1;
|
|
1024
|
-
else if (e[t].type === "container_open") {
|
|
1025
|
-
const s = /^::: ?(warning|info|note|tip|danger|caution) ?(.*)$/.exec(
|
|
1026
|
-
e[t].info || ""
|
|
1027
|
-
);
|
|
1028
|
-
if (s) {
|
|
1029
|
-
const [a, d] = V(e, t, s);
|
|
1030
|
-
l.push(a), t = d;
|
|
1031
|
-
} else
|
|
1032
|
-
t += 1;
|
|
1033
|
-
} else
|
|
1034
|
-
t += 1;
|
|
1035
|
-
o.push({
|
|
1036
|
-
type: "list_item",
|
|
1037
|
-
children: l,
|
|
1038
|
-
raw: l.map((s) => s.raw).join("")
|
|
1039
|
-
}), n = t + 1;
|
|
1040
|
-
} else
|
|
1041
|
-
n += 1;
|
|
1042
|
-
return [{
|
|
1043
|
-
type: "list",
|
|
1044
|
-
ordered: i.type === "ordered_list_open",
|
|
1045
|
-
// markdown-it may include attrs like [['start','2']] on ordered_list_open
|
|
1046
|
-
start: (() => {
|
|
1047
|
-
if (i.attrs && i.attrs.length) {
|
|
1048
|
-
const l = i.attrs.find((t) => t[0] === "start");
|
|
1049
|
-
if (l)
|
|
1050
|
-
return Number(l[1]) || 1;
|
|
1051
|
-
}
|
|
1052
|
-
})(),
|
|
1053
|
-
items: o,
|
|
1054
|
-
raw: o.map((l) => l.raw).join(`
|
|
1055
|
-
`)
|
|
1056
|
-
}, n + 1];
|
|
1146
|
+
|
|
1147
|
+
//#endregion
|
|
1148
|
+
//#region src/parser/inline-parsers/text-parser.ts
|
|
1149
|
+
function parseTextToken(token) {
|
|
1150
|
+
return {
|
|
1151
|
+
type: "text",
|
|
1152
|
+
content: token.content || "",
|
|
1153
|
+
raw: token.content || ""
|
|
1154
|
+
};
|
|
1057
1155
|
}
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1156
|
+
|
|
1157
|
+
//#endregion
|
|
1158
|
+
//#region src/parser/inline-parsers/index.ts
|
|
1159
|
+
function parseInlineTokens(tokens, raw, pPreToken) {
|
|
1160
|
+
if (!tokens || tokens.length === 0) return [];
|
|
1161
|
+
const result = [];
|
|
1162
|
+
let currentTextNode = null;
|
|
1163
|
+
let i = 0;
|
|
1164
|
+
tokens = fixStrongTokens(tokens);
|
|
1165
|
+
tokens = fixListItem(tokens);
|
|
1166
|
+
tokens = fixLinkToken(tokens);
|
|
1167
|
+
while (i < tokens.length) {
|
|
1168
|
+
const token = tokens[i];
|
|
1169
|
+
switch (token.type) {
|
|
1170
|
+
case "text": {
|
|
1171
|
+
let content = token.content.replace(/\\/g, "") || "";
|
|
1172
|
+
if (content === "`" || content === "|" || content === "$" || content === "1" || /^\*+$/.test(content) || /^\d$/.test(content)) {
|
|
1173
|
+
i++;
|
|
1174
|
+
break;
|
|
1175
|
+
}
|
|
1176
|
+
if (/[^\]]\s*\(\s*$/.test(content)) content = content.replace(/\(\s*$/, "");
|
|
1177
|
+
if (raw?.startsWith("[") && pPreToken?.type === "list_item_open") {
|
|
1178
|
+
const w = content.slice(1).match(/[^\s\]]/);
|
|
1179
|
+
if (w === null) {
|
|
1180
|
+
i++;
|
|
1181
|
+
break;
|
|
1182
|
+
}
|
|
1183
|
+
if (w && /x/i.test(w[0]) || !w) {
|
|
1184
|
+
const checked = w ? w[0] === "x" || w[0] === "X" : false;
|
|
1185
|
+
result.push({
|
|
1186
|
+
type: "checkbox_input",
|
|
1187
|
+
checked,
|
|
1188
|
+
raw: checked ? "[x]" : "[ ]"
|
|
1189
|
+
});
|
|
1190
|
+
i++;
|
|
1191
|
+
break;
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
if (/`[^`]*/.test(content)) {
|
|
1195
|
+
currentTextNode = null;
|
|
1196
|
+
result.push({
|
|
1197
|
+
type: "inline_code",
|
|
1198
|
+
code: content.replace(/`/g, ""),
|
|
1199
|
+
raw: content || ""
|
|
1200
|
+
});
|
|
1201
|
+
i++;
|
|
1202
|
+
break;
|
|
1203
|
+
}
|
|
1204
|
+
if (content === "[") {
|
|
1205
|
+
i++;
|
|
1206
|
+
break;
|
|
1207
|
+
}
|
|
1208
|
+
if (/[^~]*~{2,}[^~]+/.test(content)) {
|
|
1209
|
+
const index = content.indexOf("~~") || 0;
|
|
1210
|
+
const _text = content.slice(0, index);
|
|
1211
|
+
if (_text) if (currentTextNode) {
|
|
1212
|
+
currentTextNode.content += _text;
|
|
1213
|
+
currentTextNode.raw += _text;
|
|
1214
|
+
} else {
|
|
1215
|
+
currentTextNode = {
|
|
1216
|
+
type: "text",
|
|
1217
|
+
content: _text || "",
|
|
1218
|
+
raw: token.content || ""
|
|
1219
|
+
};
|
|
1220
|
+
result.push(currentTextNode);
|
|
1221
|
+
}
|
|
1222
|
+
const strikethroughContent = content.slice(index);
|
|
1223
|
+
currentTextNode = null;
|
|
1224
|
+
const { node } = parseStrikethroughToken([
|
|
1225
|
+
{
|
|
1226
|
+
type: "s_open",
|
|
1227
|
+
tag: "s",
|
|
1228
|
+
content: "",
|
|
1229
|
+
markup: "*",
|
|
1230
|
+
info: "",
|
|
1231
|
+
meta: null
|
|
1232
|
+
},
|
|
1233
|
+
{
|
|
1234
|
+
type: "text",
|
|
1235
|
+
tag: "",
|
|
1236
|
+
content: strikethroughContent.replace(/~/g, ""),
|
|
1237
|
+
markup: "",
|
|
1238
|
+
info: "",
|
|
1239
|
+
meta: null
|
|
1240
|
+
},
|
|
1241
|
+
{
|
|
1242
|
+
type: "s_close",
|
|
1243
|
+
tag: "s",
|
|
1244
|
+
content: "",
|
|
1245
|
+
markup: "*",
|
|
1246
|
+
info: "",
|
|
1247
|
+
meta: null
|
|
1248
|
+
}
|
|
1249
|
+
], 0);
|
|
1250
|
+
result.push(node);
|
|
1251
|
+
i++;
|
|
1252
|
+
break;
|
|
1253
|
+
}
|
|
1254
|
+
if (/[^*]*\*\*[^*]+/.test(content)) {
|
|
1255
|
+
const index = content.indexOf("*") || 0;
|
|
1256
|
+
const _text = content.slice(0, index);
|
|
1257
|
+
if (_text) if (currentTextNode) {
|
|
1258
|
+
currentTextNode.content += _text;
|
|
1259
|
+
currentTextNode.raw += _text;
|
|
1260
|
+
} else {
|
|
1261
|
+
currentTextNode = {
|
|
1262
|
+
type: "text",
|
|
1263
|
+
content: _text || "",
|
|
1264
|
+
raw: token.content || ""
|
|
1265
|
+
};
|
|
1266
|
+
result.push(currentTextNode);
|
|
1267
|
+
}
|
|
1268
|
+
const strongContent = content.slice(index);
|
|
1269
|
+
currentTextNode = null;
|
|
1270
|
+
const { node } = parseStrongToken([
|
|
1271
|
+
{
|
|
1272
|
+
type: "strong_open",
|
|
1273
|
+
tag: "strong",
|
|
1274
|
+
content: "",
|
|
1275
|
+
markup: "*",
|
|
1276
|
+
info: "",
|
|
1277
|
+
meta: null
|
|
1278
|
+
},
|
|
1279
|
+
{
|
|
1280
|
+
type: "text",
|
|
1281
|
+
tag: "",
|
|
1282
|
+
content: strongContent.replace(/\*/g, ""),
|
|
1283
|
+
markup: "",
|
|
1284
|
+
info: "",
|
|
1285
|
+
meta: null
|
|
1286
|
+
},
|
|
1287
|
+
{
|
|
1288
|
+
type: "strong_close",
|
|
1289
|
+
tag: "strong",
|
|
1290
|
+
content: "",
|
|
1291
|
+
markup: "*",
|
|
1292
|
+
info: "",
|
|
1293
|
+
meta: null
|
|
1294
|
+
}
|
|
1295
|
+
], 0, raw);
|
|
1296
|
+
result.push(node);
|
|
1297
|
+
i++;
|
|
1298
|
+
break;
|
|
1299
|
+
} else if (/[^*]*\*[^*]+/.test(content)) {
|
|
1300
|
+
const index = content.indexOf("*") || 0;
|
|
1301
|
+
const _text = content.slice(0, index);
|
|
1302
|
+
if (_text) if (currentTextNode) {
|
|
1303
|
+
currentTextNode.content += _text;
|
|
1304
|
+
currentTextNode.raw += _text;
|
|
1305
|
+
} else {
|
|
1306
|
+
currentTextNode = {
|
|
1307
|
+
type: "text",
|
|
1308
|
+
content: _text || "",
|
|
1309
|
+
raw: token.content || ""
|
|
1310
|
+
};
|
|
1311
|
+
result.push(currentTextNode);
|
|
1312
|
+
}
|
|
1313
|
+
const emphasisContent = content.slice(index);
|
|
1314
|
+
currentTextNode = null;
|
|
1315
|
+
const { node } = parseEmphasisToken([
|
|
1316
|
+
{
|
|
1317
|
+
type: "em_open",
|
|
1318
|
+
tag: "em",
|
|
1319
|
+
content: "",
|
|
1320
|
+
markup: "*",
|
|
1321
|
+
info: "",
|
|
1322
|
+
meta: null
|
|
1323
|
+
},
|
|
1324
|
+
{
|
|
1325
|
+
type: "text",
|
|
1326
|
+
tag: "",
|
|
1327
|
+
content: emphasisContent.replace(/\*/g, ""),
|
|
1328
|
+
markup: "",
|
|
1329
|
+
info: "",
|
|
1330
|
+
meta: null
|
|
1331
|
+
},
|
|
1332
|
+
{
|
|
1333
|
+
type: "em_close",
|
|
1334
|
+
tag: "em",
|
|
1335
|
+
content: "",
|
|
1336
|
+
markup: "*",
|
|
1337
|
+
info: "",
|
|
1338
|
+
meta: null
|
|
1339
|
+
}
|
|
1340
|
+
], 0);
|
|
1341
|
+
result.push(node);
|
|
1342
|
+
i++;
|
|
1343
|
+
break;
|
|
1344
|
+
}
|
|
1345
|
+
const imageStart = content.indexOf(";
|
|
1369
|
+
if (linkEnd !== -1) {
|
|
1370
|
+
const text = content.slice(linkStart + 1, linkEnd);
|
|
1371
|
+
if (!/[[\]()]/.test(text)) {
|
|
1372
|
+
result.push({
|
|
1373
|
+
type: "text",
|
|
1374
|
+
content: textNodeContent,
|
|
1375
|
+
raw: textNodeContent
|
|
1376
|
+
});
|
|
1377
|
+
result.push({
|
|
1378
|
+
type: "link",
|
|
1379
|
+
href: "",
|
|
1380
|
+
text,
|
|
1381
|
+
children: [{
|
|
1382
|
+
type: "text",
|
|
1383
|
+
content: text,
|
|
1384
|
+
raw: text
|
|
1385
|
+
}],
|
|
1386
|
+
loading: true
|
|
1387
|
+
});
|
|
1388
|
+
i++;
|
|
1389
|
+
break;
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
const preToken = tokens[i - 1];
|
|
1394
|
+
if (currentTextNode) {
|
|
1395
|
+
currentTextNode.content += textNode.content.replace(/(\*+|\(|\\)$/, "");
|
|
1396
|
+
currentTextNode.raw += textNode.raw;
|
|
1397
|
+
} else {
|
|
1398
|
+
const maybeMath = preToken?.tag === "br" && tokens[i - 2]?.content === "[";
|
|
1399
|
+
textNode.content = textNode.content.replace(/(\*+|\(|\\)$/, "");
|
|
1400
|
+
currentTextNode = textNode;
|
|
1401
|
+
currentTextNode.center = maybeMath;
|
|
1402
|
+
result.push(currentTextNode);
|
|
1403
|
+
}
|
|
1404
|
+
i++;
|
|
1405
|
+
break;
|
|
1406
|
+
}
|
|
1407
|
+
case "softbreak":
|
|
1408
|
+
if (currentTextNode) {
|
|
1409
|
+
currentTextNode.content += "\n";
|
|
1410
|
+
currentTextNode.raw += "\n";
|
|
1411
|
+
}
|
|
1412
|
+
i++;
|
|
1413
|
+
break;
|
|
1414
|
+
case "code_inline":
|
|
1415
|
+
currentTextNode = null;
|
|
1416
|
+
result.push(parseInlineCodeToken(token));
|
|
1417
|
+
i++;
|
|
1418
|
+
break;
|
|
1419
|
+
case "link_open": {
|
|
1420
|
+
currentTextNode = null;
|
|
1421
|
+
const href = token.attrs?.find((attr) => attr[0] === "href")?.[1];
|
|
1422
|
+
if (raw && href) {
|
|
1423
|
+
const loadingMath = /* @__PURE__ */ new RegExp(`\\(\\s*${href}\\s*\\)`);
|
|
1424
|
+
const pre = result.length > 0 ? result[result.length - 1] : null;
|
|
1425
|
+
const loading = !loadingMath.test(raw);
|
|
1426
|
+
if (loading && pre) {
|
|
1427
|
+
if ((/* @__PURE__ */ new RegExp(`\\[${pre.text}\\s*\\]\\(`)).test(raw)) {
|
|
1428
|
+
const text = pre?.text || pre?.content?.slice(1, -1) || "";
|
|
1429
|
+
result.splice(result.length - 1, 1, {
|
|
1430
|
+
type: "link",
|
|
1431
|
+
href: "",
|
|
1432
|
+
text,
|
|
1433
|
+
loading
|
|
1434
|
+
});
|
|
1435
|
+
i += 3;
|
|
1436
|
+
if (tokens[i]?.content === ".") i++;
|
|
1437
|
+
break;
|
|
1438
|
+
}
|
|
1439
|
+
}
|
|
1440
|
+
}
|
|
1441
|
+
const { node, nextIndex } = parseLinkToken(tokens, i);
|
|
1442
|
+
i = nextIndex;
|
|
1443
|
+
node.loading = false;
|
|
1444
|
+
result.push(node);
|
|
1445
|
+
break;
|
|
1446
|
+
}
|
|
1447
|
+
case "image":
|
|
1448
|
+
currentTextNode = null;
|
|
1449
|
+
result.push(parseImageToken(token));
|
|
1450
|
+
i++;
|
|
1451
|
+
break;
|
|
1452
|
+
case "strong_open": {
|
|
1453
|
+
currentTextNode = null;
|
|
1454
|
+
const { node, nextIndex } = parseStrongToken(tokens, i, token.content);
|
|
1455
|
+
result.push(node);
|
|
1456
|
+
i = nextIndex;
|
|
1457
|
+
break;
|
|
1458
|
+
}
|
|
1459
|
+
case "em_open": {
|
|
1460
|
+
currentTextNode = null;
|
|
1461
|
+
const { node, nextIndex } = parseEmphasisToken(tokens, i);
|
|
1462
|
+
result.push(node);
|
|
1463
|
+
i = nextIndex;
|
|
1464
|
+
break;
|
|
1465
|
+
}
|
|
1466
|
+
case "s_open": {
|
|
1467
|
+
currentTextNode = null;
|
|
1468
|
+
const { node, nextIndex } = parseStrikethroughToken(tokens, i);
|
|
1469
|
+
result.push(node);
|
|
1470
|
+
i = nextIndex;
|
|
1471
|
+
break;
|
|
1472
|
+
}
|
|
1473
|
+
case "mark_open": {
|
|
1474
|
+
currentTextNode = null;
|
|
1475
|
+
const { node, nextIndex } = parseHighlightToken(tokens, i);
|
|
1476
|
+
result.push(node);
|
|
1477
|
+
i = nextIndex;
|
|
1478
|
+
break;
|
|
1479
|
+
}
|
|
1480
|
+
case "ins_open": {
|
|
1481
|
+
currentTextNode = null;
|
|
1482
|
+
const { node, nextIndex } = parseInsertToken(tokens, i);
|
|
1483
|
+
result.push(node);
|
|
1484
|
+
i = nextIndex;
|
|
1485
|
+
break;
|
|
1486
|
+
}
|
|
1487
|
+
case "sub_open": {
|
|
1488
|
+
currentTextNode = null;
|
|
1489
|
+
const { node, nextIndex } = parseSubscriptToken(tokens, i);
|
|
1490
|
+
result.push(node);
|
|
1491
|
+
i = nextIndex;
|
|
1492
|
+
break;
|
|
1493
|
+
}
|
|
1494
|
+
case "sup_open": {
|
|
1495
|
+
currentTextNode = null;
|
|
1496
|
+
const { node, nextIndex } = parseSuperscriptToken(tokens, i);
|
|
1497
|
+
result.push(node);
|
|
1498
|
+
i = nextIndex;
|
|
1499
|
+
break;
|
|
1500
|
+
}
|
|
1501
|
+
case "sub":
|
|
1502
|
+
currentTextNode = null;
|
|
1503
|
+
result.push({
|
|
1504
|
+
type: "subscript",
|
|
1505
|
+
children: [{
|
|
1506
|
+
type: "text",
|
|
1507
|
+
content: token.content || "",
|
|
1508
|
+
raw: token.content || ""
|
|
1509
|
+
}],
|
|
1510
|
+
raw: `~${token.content || ""}~`
|
|
1511
|
+
});
|
|
1512
|
+
i++;
|
|
1513
|
+
break;
|
|
1514
|
+
case "sup":
|
|
1515
|
+
currentTextNode = null;
|
|
1516
|
+
result.push({
|
|
1517
|
+
type: "superscript",
|
|
1518
|
+
children: [{
|
|
1519
|
+
type: "text",
|
|
1520
|
+
content: token.content || "",
|
|
1521
|
+
raw: token.content || ""
|
|
1522
|
+
}],
|
|
1523
|
+
raw: `^${token.content || ""}^`
|
|
1524
|
+
});
|
|
1525
|
+
i++;
|
|
1526
|
+
break;
|
|
1527
|
+
case "emoji": {
|
|
1528
|
+
currentTextNode = null;
|
|
1529
|
+
const preToken = tokens[i - 1];
|
|
1530
|
+
if (preToken?.type === "text" && /\|:-+/.test(preToken.content || "")) result.push({
|
|
1531
|
+
type: "text",
|
|
1532
|
+
content: "",
|
|
1533
|
+
raw: ""
|
|
1534
|
+
});
|
|
1535
|
+
else result.push(parseEmojiToken(token));
|
|
1536
|
+
i++;
|
|
1537
|
+
break;
|
|
1538
|
+
}
|
|
1539
|
+
case "checkbox":
|
|
1540
|
+
currentTextNode = null;
|
|
1541
|
+
result.push(parseCheckboxToken(token));
|
|
1542
|
+
i++;
|
|
1543
|
+
break;
|
|
1544
|
+
case "checkbox_input":
|
|
1545
|
+
currentTextNode = null;
|
|
1546
|
+
result.push(parseCheckboxInputToken(token));
|
|
1547
|
+
i++;
|
|
1548
|
+
break;
|
|
1549
|
+
case "footnote_ref":
|
|
1550
|
+
currentTextNode = null;
|
|
1551
|
+
result.push(parseFootnoteRefToken(token));
|
|
1552
|
+
i++;
|
|
1553
|
+
break;
|
|
1554
|
+
case "hardbreak":
|
|
1555
|
+
currentTextNode = null;
|
|
1556
|
+
result.push(parseHardbreakToken());
|
|
1557
|
+
i++;
|
|
1558
|
+
break;
|
|
1559
|
+
case "fence":
|
|
1560
|
+
currentTextNode = null;
|
|
1561
|
+
result.push(parseFenceToken(tokens[i]));
|
|
1562
|
+
i++;
|
|
1563
|
+
break;
|
|
1564
|
+
case "math_inline":
|
|
1565
|
+
currentTextNode = null;
|
|
1566
|
+
result.push(parseMathInlineToken(token));
|
|
1567
|
+
i++;
|
|
1568
|
+
break;
|
|
1569
|
+
case "reference":
|
|
1570
|
+
currentTextNode = null;
|
|
1571
|
+
result.push(parseReferenceToken(token));
|
|
1572
|
+
i++;
|
|
1573
|
+
break;
|
|
1574
|
+
default:
|
|
1575
|
+
result.push(token);
|
|
1576
|
+
currentTextNode = null;
|
|
1577
|
+
i++;
|
|
1578
|
+
break;
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
return result;
|
|
1106
1582
|
}
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
:::`
|
|
1132
|
-
}, l + 1];
|
|
1583
|
+
|
|
1584
|
+
//#endregion
|
|
1585
|
+
//#region src/parser/node-parsers/blockquote-parser.ts
|
|
1586
|
+
function parseBlockquote(tokens, index) {
|
|
1587
|
+
const blockquoteChildren = [];
|
|
1588
|
+
let j = index + 1;
|
|
1589
|
+
while (j < tokens.length && tokens[j].type !== "blockquote_close") if (tokens[j].type === "paragraph_open") {
|
|
1590
|
+
const contentToken = tokens[j + 1];
|
|
1591
|
+
blockquoteChildren.push({
|
|
1592
|
+
type: "paragraph",
|
|
1593
|
+
children: parseInlineTokens(contentToken.children || []),
|
|
1594
|
+
raw: contentToken.content || ""
|
|
1595
|
+
});
|
|
1596
|
+
j += 3;
|
|
1597
|
+
} else if (tokens[j].type === "bullet_list_open" || tokens[j].type === "ordered_list_open") {
|
|
1598
|
+
const [listNode, newIndex] = parseList(tokens, j);
|
|
1599
|
+
blockquoteChildren.push(listNode);
|
|
1600
|
+
j = newIndex;
|
|
1601
|
+
} else j++;
|
|
1602
|
+
return [{
|
|
1603
|
+
type: "blockquote",
|
|
1604
|
+
children: blockquoteChildren,
|
|
1605
|
+
raw: blockquoteChildren.map((child) => child.raw).join("\n")
|
|
1606
|
+
}, j + 1];
|
|
1133
1607
|
}
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
n || (n = o.charAt(0).toUpperCase() + o.slice(1));
|
|
1150
|
-
const l = [];
|
|
1151
|
-
let t = c + 1;
|
|
1152
|
-
const s = new RegExp(`^container_${o}_close$`);
|
|
1153
|
-
for (; t < e.length && e[t].type !== "container_close" && !s.test(e[t].type); )
|
|
1154
|
-
if (e[t].type === "paragraph_open") {
|
|
1155
|
-
const p = e[t + 1];
|
|
1156
|
-
if (p) {
|
|
1157
|
-
let u = -1;
|
|
1158
|
-
if (p.children)
|
|
1159
|
-
for (let h = p.children.length - 1; h >= 0; h--) {
|
|
1160
|
-
const g = p.children[h];
|
|
1161
|
-
if (g && g.type === "text" && /:+/.test(g.content || "")) {
|
|
1162
|
-
u = h;
|
|
1163
|
-
break;
|
|
1164
|
-
}
|
|
1165
|
-
}
|
|
1166
|
-
const f = u !== -1 && p.children ? p.children.slice(0, u) : p.children;
|
|
1167
|
-
l.push({
|
|
1168
|
-
type: "paragraph",
|
|
1169
|
-
children: b(f || []),
|
|
1170
|
-
raw: p && typeof p.content == "string" ? p.content.replace(/\n:+$/, "").replace(/\n\s*:::\s*$/, "") : ""
|
|
1171
|
-
});
|
|
1172
|
-
}
|
|
1173
|
-
t += 3;
|
|
1174
|
-
} else if (e[t].type === "bullet_list_open" || e[t].type === "ordered_list_open") {
|
|
1175
|
-
const [p, u] = E(e, t);
|
|
1176
|
-
l.push(p), t = u;
|
|
1177
|
-
} else
|
|
1178
|
-
t++;
|
|
1179
|
-
return [{
|
|
1180
|
-
type: "admonition",
|
|
1181
|
-
kind: o,
|
|
1182
|
-
title: n,
|
|
1183
|
-
children: l,
|
|
1184
|
-
raw: `:::${o} ${n}
|
|
1185
|
-
${l.map((p) => p.raw).join(`
|
|
1186
|
-
`)}
|
|
1187
|
-
:::`
|
|
1188
|
-
}, t + 1];
|
|
1608
|
+
|
|
1609
|
+
//#endregion
|
|
1610
|
+
//#region src/parser/node-parsers/code-block-parser.ts
|
|
1611
|
+
function parseCodeBlock(token) {
|
|
1612
|
+
if (token.info?.startsWith("diff")) return parseFenceToken(token);
|
|
1613
|
+
const match = token.content.match(/ type="application\/vnd\.ant\.([^"]+)"/);
|
|
1614
|
+
if (match?.[1]) token.content = token.content.replace(/<antArtifact[^>]*>/g, "").replace(/<\/antArtifact>/g, "");
|
|
1615
|
+
const hasMap = Array.isArray(token.map) && token.map.length === 2;
|
|
1616
|
+
return {
|
|
1617
|
+
type: "code_block",
|
|
1618
|
+
language: match ? match[1] : token.info || "",
|
|
1619
|
+
code: token.content || "",
|
|
1620
|
+
raw: token.content || "",
|
|
1621
|
+
loading: !hasMap
|
|
1622
|
+
};
|
|
1189
1623
|
}
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1624
|
+
|
|
1625
|
+
//#endregion
|
|
1626
|
+
//#region src/parser/node-parsers/definition-list-parser.ts
|
|
1627
|
+
function parseDefinitionList(tokens, index) {
|
|
1628
|
+
const items = [];
|
|
1629
|
+
let j = index + 1;
|
|
1630
|
+
let termNodes = [];
|
|
1631
|
+
let definitionNodes = [];
|
|
1632
|
+
while (j < tokens.length && tokens[j].type !== "dl_close") if (tokens[j].type === "dt_open") {
|
|
1633
|
+
const termToken = tokens[j + 1];
|
|
1634
|
+
termNodes = parseInlineTokens(termToken.children || []);
|
|
1635
|
+
j += 3;
|
|
1636
|
+
} else if (tokens[j].type === "dd_open") {
|
|
1637
|
+
let k = j + 1;
|
|
1638
|
+
definitionNodes = [];
|
|
1639
|
+
while (k < tokens.length && tokens[k].type !== "dd_close") if (tokens[k].type === "paragraph_open") {
|
|
1640
|
+
const contentToken = tokens[k + 1];
|
|
1641
|
+
definitionNodes.push({
|
|
1642
|
+
type: "paragraph",
|
|
1643
|
+
children: parseInlineTokens(contentToken.children || [], contentToken.content || ""),
|
|
1644
|
+
raw: contentToken.content || ""
|
|
1645
|
+
});
|
|
1646
|
+
k += 3;
|
|
1647
|
+
} else k++;
|
|
1648
|
+
if (termNodes.length > 0) {
|
|
1649
|
+
items.push({
|
|
1650
|
+
type: "definition_item",
|
|
1651
|
+
term: termNodes,
|
|
1652
|
+
definition: definitionNodes,
|
|
1653
|
+
raw: `${termNodes.map((term) => term.raw).join("")}: ${definitionNodes.map((def) => def.raw).join("\n")}`
|
|
1654
|
+
});
|
|
1655
|
+
termNodes = [];
|
|
1656
|
+
}
|
|
1657
|
+
j = k + 1;
|
|
1658
|
+
} else j++;
|
|
1659
|
+
return [{
|
|
1660
|
+
type: "definition_list",
|
|
1661
|
+
items,
|
|
1662
|
+
raw: items.map((item) => item.raw).join("\n")
|
|
1663
|
+
}, j + 1];
|
|
1196
1664
|
}
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1665
|
+
|
|
1666
|
+
//#endregion
|
|
1667
|
+
//#region src/parser/node-parsers/footnote-parser.ts
|
|
1668
|
+
function parseFootnote(tokens, index) {
|
|
1669
|
+
const id = tokens[index].meta?.label ?? 0;
|
|
1670
|
+
const footnoteChildren = [];
|
|
1671
|
+
let j = index + 1;
|
|
1672
|
+
while (j < tokens.length && tokens[j].type !== "footnote_close") if (tokens[j].type === "paragraph_open") {
|
|
1673
|
+
const contentToken = tokens[j + 1];
|
|
1674
|
+
footnoteChildren.push({
|
|
1675
|
+
type: "paragraph",
|
|
1676
|
+
children: parseInlineTokens(contentToken.children || []),
|
|
1677
|
+
raw: contentToken.content || ""
|
|
1678
|
+
});
|
|
1679
|
+
j += 3;
|
|
1680
|
+
} else j++;
|
|
1681
|
+
return [{
|
|
1682
|
+
type: "footnote",
|
|
1683
|
+
id,
|
|
1684
|
+
children: footnoteChildren,
|
|
1685
|
+
raw: `[^${id}]: ${footnoteChildren.map((child) => child.raw).join("\n")}`
|
|
1686
|
+
}, j + 1];
|
|
1204
1687
|
}
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1688
|
+
|
|
1689
|
+
//#endregion
|
|
1690
|
+
//#region src/parser/node-parsers/heading-parser.ts
|
|
1691
|
+
function parseHeading(tokens, index) {
|
|
1692
|
+
const token = tokens[index];
|
|
1693
|
+
const headingLevel = Number.parseInt(token.tag?.substring(1) || "1");
|
|
1694
|
+
const headingContentToken = tokens[index + 1];
|
|
1695
|
+
const headingContent = headingContentToken.content || "";
|
|
1696
|
+
return {
|
|
1697
|
+
type: "heading",
|
|
1698
|
+
level: headingLevel,
|
|
1699
|
+
text: headingContent,
|
|
1700
|
+
children: parseInlineTokens(headingContentToken.children || []),
|
|
1701
|
+
raw: headingContent
|
|
1702
|
+
};
|
|
1218
1703
|
}
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
case "container_note_open":
|
|
1230
|
-
case "container_tip_open":
|
|
1231
|
-
case "container_danger_open":
|
|
1232
|
-
case "container_caution_open":
|
|
1233
|
-
case "container_error_open": {
|
|
1234
|
-
const [n, r] = ve(e, i);
|
|
1235
|
-
c.push(n), i = r;
|
|
1236
|
-
break;
|
|
1237
|
-
}
|
|
1238
|
-
case "heading_open":
|
|
1239
|
-
c.push(z(e, i)), i += 3;
|
|
1240
|
-
break;
|
|
1241
|
-
case "paragraph_open":
|
|
1242
|
-
c.push(Ne(e, i)), i += 3;
|
|
1243
|
-
break;
|
|
1244
|
-
case "html_block":
|
|
1245
|
-
case "code_block":
|
|
1246
|
-
c.push(A(e[i])), i += 1;
|
|
1247
|
-
break;
|
|
1248
|
-
case "fence":
|
|
1249
|
-
c.push(N(e[i])), i += 1;
|
|
1250
|
-
break;
|
|
1251
|
-
case "bullet_list_open":
|
|
1252
|
-
case "ordered_list_open": {
|
|
1253
|
-
const [n, r] = E(e, i);
|
|
1254
|
-
c.push(n), i = r;
|
|
1255
|
-
break;
|
|
1256
|
-
}
|
|
1257
|
-
case "hr":
|
|
1258
|
-
c.push(J()), i += 1;
|
|
1259
|
-
break;
|
|
1260
|
-
case "blockquote_open": {
|
|
1261
|
-
const [n, r] = Z(e, i);
|
|
1262
|
-
c.push(n), i = r;
|
|
1263
|
-
break;
|
|
1264
|
-
}
|
|
1265
|
-
case "table_open": {
|
|
1266
|
-
const [n, r] = K(e, i);
|
|
1267
|
-
c.push(n), i = r;
|
|
1268
|
-
break;
|
|
1269
|
-
}
|
|
1270
|
-
case "dl_open": {
|
|
1271
|
-
const [n, r] = U(e, i);
|
|
1272
|
-
c.push(n), i = r;
|
|
1273
|
-
break;
|
|
1274
|
-
}
|
|
1275
|
-
case "footnote_open": {
|
|
1276
|
-
const [n, r] = G(e, i);
|
|
1277
|
-
c.push(n), i = r;
|
|
1278
|
-
break;
|
|
1279
|
-
}
|
|
1280
|
-
case "container_open": {
|
|
1281
|
-
const n = /^::: ?(warning|info|note|tip|danger|caution|error) ?(.*)$/.exec(
|
|
1282
|
-
o.info || ""
|
|
1283
|
-
);
|
|
1284
|
-
if (n) {
|
|
1285
|
-
const [r, l] = V(e, i, n);
|
|
1286
|
-
c.push(r), i = l;
|
|
1287
|
-
} else
|
|
1288
|
-
i += 1;
|
|
1289
|
-
break;
|
|
1290
|
-
}
|
|
1291
|
-
case "hardbreak":
|
|
1292
|
-
c.push(Ce()), i++;
|
|
1293
|
-
break;
|
|
1294
|
-
case "math_block":
|
|
1295
|
-
c.push(j(e[i])), i += 1;
|
|
1296
|
-
break;
|
|
1297
|
-
default:
|
|
1298
|
-
i += 1;
|
|
1299
|
-
break;
|
|
1300
|
-
}
|
|
1301
|
-
}
|
|
1302
|
-
return c;
|
|
1704
|
+
|
|
1705
|
+
//#endregion
|
|
1706
|
+
//#region src/parser/node-parsers/math-block-parser.ts
|
|
1707
|
+
function parseMathBlock(token) {
|
|
1708
|
+
return {
|
|
1709
|
+
type: "math_block",
|
|
1710
|
+
content: token.content || "",
|
|
1711
|
+
loading: !!token.loading,
|
|
1712
|
+
raw: token.raw || ""
|
|
1713
|
+
};
|
|
1303
1714
|
}
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1715
|
+
|
|
1716
|
+
//#endregion
|
|
1717
|
+
//#region src/parser/node-parsers/table-parser.ts
|
|
1718
|
+
function parseTable(tokens, index) {
|
|
1719
|
+
let j = index + 1;
|
|
1720
|
+
let headerRow = null;
|
|
1721
|
+
const rows = [];
|
|
1722
|
+
let isHeader = false;
|
|
1723
|
+
while (j < tokens.length && tokens[j].type !== "table_close") if (tokens[j].type === "thead_open") {
|
|
1724
|
+
isHeader = true;
|
|
1725
|
+
j++;
|
|
1726
|
+
} else if (tokens[j].type === "thead_close") {
|
|
1727
|
+
isHeader = false;
|
|
1728
|
+
j++;
|
|
1729
|
+
} else if (tokens[j].type === "tbody_open" || tokens[j].type === "tbody_close") j++;
|
|
1730
|
+
else if (tokens[j].type === "tr_open") {
|
|
1731
|
+
const cells = [];
|
|
1732
|
+
let k = j + 1;
|
|
1733
|
+
while (k < tokens.length && tokens[k].type !== "tr_close") if (tokens[k].type === "th_open" || tokens[k].type === "td_open") {
|
|
1734
|
+
const isHeaderCell = tokens[k].type === "th_open";
|
|
1735
|
+
const contentToken = tokens[k + 1];
|
|
1736
|
+
const content = contentToken.content || "";
|
|
1737
|
+
cells.push({
|
|
1738
|
+
type: "table_cell",
|
|
1739
|
+
header: isHeaderCell || isHeader,
|
|
1740
|
+
children: parseInlineTokens(contentToken.children || [], content),
|
|
1741
|
+
raw: content
|
|
1742
|
+
});
|
|
1743
|
+
k += 3;
|
|
1744
|
+
} else k++;
|
|
1745
|
+
const rowNode = {
|
|
1746
|
+
type: "table_row",
|
|
1747
|
+
cells,
|
|
1748
|
+
raw: cells.map((cell) => cell.raw).join("|")
|
|
1749
|
+
};
|
|
1750
|
+
if (isHeader) headerRow = rowNode;
|
|
1751
|
+
else rows.push(rowNode);
|
|
1752
|
+
j = k + 1;
|
|
1753
|
+
} else j++;
|
|
1754
|
+
if (!headerRow) headerRow = {
|
|
1755
|
+
type: "table_row",
|
|
1756
|
+
cells: [],
|
|
1757
|
+
raw: ""
|
|
1758
|
+
};
|
|
1759
|
+
return [{
|
|
1760
|
+
type: "table",
|
|
1761
|
+
header: headerRow,
|
|
1762
|
+
rows,
|
|
1763
|
+
loading: tokens[index].loading || false,
|
|
1764
|
+
raw: [headerRow, ...rows].map((row) => row.raw).join("\n")
|
|
1765
|
+
}, j + 1];
|
|
1307
1766
|
}
|
|
1308
|
-
|
|
1309
|
-
|
|
1767
|
+
|
|
1768
|
+
//#endregion
|
|
1769
|
+
//#region src/parser/node-parsers/thematic-break-parser.ts
|
|
1770
|
+
function parseThematicBreak() {
|
|
1771
|
+
return {
|
|
1772
|
+
type: "thematic_break",
|
|
1773
|
+
raw: "---"
|
|
1774
|
+
};
|
|
1310
1775
|
}
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1776
|
+
|
|
1777
|
+
//#endregion
|
|
1778
|
+
//#region src/parser/node-parsers/list-parser.ts
|
|
1779
|
+
function parseList(tokens, index) {
|
|
1780
|
+
const token = tokens[index];
|
|
1781
|
+
const listItems = [];
|
|
1782
|
+
let j = index + 1;
|
|
1783
|
+
while (j < tokens.length && tokens[j].type !== "bullet_list_close" && tokens[j].type !== "ordered_list_close") if (tokens[j].type === "list_item_open") {
|
|
1784
|
+
if (tokens[j].markup === "*") {
|
|
1785
|
+
j++;
|
|
1786
|
+
continue;
|
|
1787
|
+
}
|
|
1788
|
+
const itemChildren = [];
|
|
1789
|
+
let k = j + 1;
|
|
1790
|
+
while (k < tokens.length && tokens[k].type !== "list_item_close") if (tokens[k].type === "paragraph_open") {
|
|
1791
|
+
const contentToken = tokens[k + 1];
|
|
1792
|
+
const preToken = tokens[k - 1];
|
|
1793
|
+
if (/\n\d+$/.test(contentToken.content || "")) {
|
|
1794
|
+
contentToken.content = contentToken.content?.replace(/\n\d+$/, "");
|
|
1795
|
+
contentToken.children?.splice(-1, 1);
|
|
1796
|
+
}
|
|
1797
|
+
itemChildren.push({
|
|
1798
|
+
type: "paragraph",
|
|
1799
|
+
children: parseInlineTokens(contentToken.children || [], contentToken.content || "", preToken),
|
|
1800
|
+
raw: contentToken.content || ""
|
|
1801
|
+
});
|
|
1802
|
+
k += 3;
|
|
1803
|
+
} else if (tokens[k].type === "blockquote_open") {
|
|
1804
|
+
const [blockquoteNode, newIndex] = parseBlockquote(tokens, k);
|
|
1805
|
+
itemChildren.push(blockquoteNode);
|
|
1806
|
+
k = newIndex;
|
|
1807
|
+
} else if (tokens[k].type === "bullet_list_open" || tokens[k].type === "ordered_list_open") {
|
|
1808
|
+
if (tokens[k].markup === "*") {
|
|
1809
|
+
k++;
|
|
1810
|
+
continue;
|
|
1811
|
+
}
|
|
1812
|
+
const [nestedListNode, newIndex] = parseNestedList(tokens, k);
|
|
1813
|
+
itemChildren.push(nestedListNode);
|
|
1814
|
+
k = newIndex;
|
|
1815
|
+
} else if (tokens[k].type === "code_block") {
|
|
1816
|
+
itemChildren.push(parseCodeBlock(tokens[k]));
|
|
1817
|
+
k += 1;
|
|
1818
|
+
} else if (tokens[k].type === "fence") {
|
|
1819
|
+
itemChildren.push(parseFenceToken(tokens[k]));
|
|
1820
|
+
k += 1;
|
|
1821
|
+
} else if (tokens[k].type === "math_block") {
|
|
1822
|
+
itemChildren.push(parseMathBlock(tokens[k]));
|
|
1823
|
+
k += 1;
|
|
1824
|
+
} else if (tokens[k].type === "table_open") {
|
|
1825
|
+
const [tableNode, newIndex] = parseTable(tokens, k);
|
|
1826
|
+
itemChildren.push(tableNode);
|
|
1827
|
+
k = newIndex;
|
|
1828
|
+
} else if (tokens[k].type === "dl_open") {
|
|
1829
|
+
const [defListNode, newIndex] = parseDefinitionList(tokens, k);
|
|
1830
|
+
itemChildren.push(defListNode);
|
|
1831
|
+
k = newIndex;
|
|
1832
|
+
} else if (tokens[k].type === "footnote_open") {
|
|
1833
|
+
const [footnoteNode, newIndex] = parseFootnote(tokens, k);
|
|
1834
|
+
itemChildren.push(footnoteNode);
|
|
1835
|
+
k = newIndex;
|
|
1836
|
+
} else if (tokens[k].type === "heading_open") {
|
|
1837
|
+
const headingNode = parseHeading(tokens, k);
|
|
1838
|
+
itemChildren.push(headingNode);
|
|
1839
|
+
k += 3;
|
|
1840
|
+
} else if (tokens[k].type === "hr") {
|
|
1841
|
+
itemChildren.push(parseThematicBreak());
|
|
1842
|
+
k += 1;
|
|
1843
|
+
} else if (tokens[k].type === "container_open") {
|
|
1844
|
+
const match = /^::: ?(warning|info|note|tip|danger|caution) ?(.*)$/.exec(tokens[k].info || "");
|
|
1845
|
+
if (match) {
|
|
1846
|
+
const [admonitionNode, newIndex] = parseAdmonition(tokens, k, match);
|
|
1847
|
+
itemChildren.push(admonitionNode);
|
|
1848
|
+
k = newIndex;
|
|
1849
|
+
} else k += 1;
|
|
1850
|
+
} else k += 1;
|
|
1851
|
+
listItems.push({
|
|
1852
|
+
type: "list_item",
|
|
1853
|
+
children: itemChildren,
|
|
1854
|
+
raw: itemChildren.map((child) => child.raw).join("")
|
|
1855
|
+
});
|
|
1856
|
+
j = k + 1;
|
|
1857
|
+
} else j += 1;
|
|
1858
|
+
return [{
|
|
1859
|
+
type: "list",
|
|
1860
|
+
ordered: token.type === "ordered_list_open",
|
|
1861
|
+
start: (() => {
|
|
1862
|
+
if (token.attrs && token.attrs.length) {
|
|
1863
|
+
const found = token.attrs.find((a) => a[0] === "start");
|
|
1864
|
+
if (found) return Number(found[1]) || 1;
|
|
1865
|
+
}
|
|
1866
|
+
})(),
|
|
1867
|
+
items: listItems,
|
|
1868
|
+
raw: listItems.map((item) => item.raw).join("\n")
|
|
1869
|
+
}, j + 1];
|
|
1349
1870
|
}
|
|
1350
|
-
function
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1871
|
+
function parseNestedList(tokens, index) {
|
|
1872
|
+
const nestedToken = tokens[index];
|
|
1873
|
+
const nestedItems = [];
|
|
1874
|
+
let j = index + 1;
|
|
1875
|
+
while (j < tokens.length && tokens[j].type !== "bullet_list_close" && tokens[j].type !== "ordered_list_close") if (tokens[j].type === "list_item_open") {
|
|
1876
|
+
if (tokens[j].markup === "*") {
|
|
1877
|
+
j++;
|
|
1878
|
+
continue;
|
|
1879
|
+
}
|
|
1880
|
+
const itemChildren = [];
|
|
1881
|
+
let k = j + 1;
|
|
1882
|
+
while (k < tokens.length && tokens[k].type !== "list_item_close") if (tokens[k].type === "paragraph_open") {
|
|
1883
|
+
const contentToken = tokens[k + 1];
|
|
1884
|
+
const preToken = tokens[k - 1];
|
|
1885
|
+
itemChildren.push({
|
|
1886
|
+
type: "paragraph",
|
|
1887
|
+
children: parseInlineTokens(contentToken.children || [], contentToken.content || "", preToken),
|
|
1888
|
+
raw: contentToken.content || ""
|
|
1889
|
+
});
|
|
1890
|
+
k += 3;
|
|
1891
|
+
} else if (tokens[k].type === "bullet_list_open" || tokens[k].type === "ordered_list_open") {
|
|
1892
|
+
if (tokens[k].markup === "*") {
|
|
1893
|
+
k++;
|
|
1894
|
+
continue;
|
|
1895
|
+
}
|
|
1896
|
+
const [deeperNestedListNode, newIndex] = parseNestedList(tokens, k);
|
|
1897
|
+
itemChildren.push(deeperNestedListNode);
|
|
1898
|
+
k = newIndex;
|
|
1899
|
+
} else if (tokens[k].type === "code_block") {
|
|
1900
|
+
itemChildren.push(parseCodeBlock(tokens[k]));
|
|
1901
|
+
k += 1;
|
|
1902
|
+
} else if (tokens[k].type === "fence") {
|
|
1903
|
+
itemChildren.push(parseFenceToken(tokens[k]));
|
|
1904
|
+
k += 1;
|
|
1905
|
+
} else if (tokens[k].type === "math_block") {
|
|
1906
|
+
itemChildren.push(parseMathBlock(tokens[k]));
|
|
1907
|
+
k += 1;
|
|
1908
|
+
} else k += 1;
|
|
1909
|
+
nestedItems.push({
|
|
1910
|
+
type: "list_item",
|
|
1911
|
+
children: itemChildren,
|
|
1912
|
+
raw: itemChildren.map((child) => child.raw).join("")
|
|
1913
|
+
});
|
|
1914
|
+
j = k + 1;
|
|
1915
|
+
} else j += 1;
|
|
1916
|
+
return [{
|
|
1917
|
+
type: "list",
|
|
1918
|
+
ordered: nestedToken.type === "ordered_list_open",
|
|
1919
|
+
start: (() => {
|
|
1920
|
+
if (nestedToken.attrs && nestedToken.attrs.length) {
|
|
1921
|
+
const found = nestedToken.attrs.find((a) => a[0] === "start");
|
|
1922
|
+
if (found) return Number(found[1]) || 1;
|
|
1923
|
+
}
|
|
1924
|
+
})(),
|
|
1925
|
+
items: nestedItems,
|
|
1926
|
+
raw: nestedItems.map((item) => item.raw).join("\n")
|
|
1927
|
+
}, j + 1];
|
|
1400
1928
|
}
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
if (/[./]\s*\D|\D\s*[./]/.test(e))
|
|
1430
|
-
return !1;
|
|
1431
|
-
const o = je.test(c), n = Re.test(c), r = ke.test(c), l = Oe.test(c), t = /(?:^|[^\w\\])(?:[A-Z]|\\[A-Z]+)_(?:\{[^}]+\}|[A-Z0-9\\])/i, s = /(?:^|[^\w\\])(?:[A-Z]|\\[A-Z]+)\^(?:\{[^}]+\}|[A-Z0-9\\])/i, a = t.test(c) || s.test(c), d = De.test(c), p = Pe.test(c), u = Be.test(c);
|
|
1432
|
-
return o || n || r || l || a || d || p || u;
|
|
1929
|
+
|
|
1930
|
+
//#endregion
|
|
1931
|
+
//#region src/parser/node-parsers/admonition-parser.ts
|
|
1932
|
+
function parseAdmonition(tokens, index, match) {
|
|
1933
|
+
const kind = match[1] || "note";
|
|
1934
|
+
const title = match[2] || kind.charAt(0).toUpperCase() + kind.slice(1);
|
|
1935
|
+
const admonitionChildren = [];
|
|
1936
|
+
let j = index + 1;
|
|
1937
|
+
while (j < tokens.length && tokens[j].type !== "container_close") if (tokens[j].type === "paragraph_open") {
|
|
1938
|
+
const contentToken = tokens[j + 1];
|
|
1939
|
+
if (contentToken) admonitionChildren.push({
|
|
1940
|
+
type: "paragraph",
|
|
1941
|
+
children: parseInlineTokens(contentToken.children || []),
|
|
1942
|
+
raw: contentToken.content || ""
|
|
1943
|
+
});
|
|
1944
|
+
j += 3;
|
|
1945
|
+
} else if (tokens[j].type === "bullet_list_open" || tokens[j].type === "ordered_list_open") {
|
|
1946
|
+
const [listNode, newIndex] = parseList(tokens, j);
|
|
1947
|
+
admonitionChildren.push(listNode);
|
|
1948
|
+
j = newIndex;
|
|
1949
|
+
} else j++;
|
|
1950
|
+
return [{
|
|
1951
|
+
type: "admonition",
|
|
1952
|
+
kind,
|
|
1953
|
+
title,
|
|
1954
|
+
children: admonitionChildren,
|
|
1955
|
+
raw: `:::${kind} ${title}\n${admonitionChildren.map((child) => child.raw).join("\n")}\n:::`
|
|
1956
|
+
}, j + 1];
|
|
1433
1957
|
}
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
"text",
|
|
1486
|
-
"left",
|
|
1487
|
-
"right",
|
|
1488
|
-
"times"
|
|
1489
|
-
], L = ne.slice().sort((e, c) => c.length - e.length).map((e) => e.replace(/[.*+?^${}()|[\\]\\\]/g, "\\$&")).join("|"), H = "[ \r\b\f\v]", X = {
|
|
1490
|
-
" ": "t",
|
|
1491
|
-
"\r": "r",
|
|
1492
|
-
"\b": "b",
|
|
1493
|
-
"\f": "f",
|
|
1494
|
-
"\v": "v"
|
|
1495
|
-
};
|
|
1496
|
-
function F(e) {
|
|
1497
|
-
const c = /(^|[^\\])(__|\*\*)/g;
|
|
1498
|
-
let i = 0;
|
|
1499
|
-
for (; c.exec(e) !== null; )
|
|
1500
|
-
i++;
|
|
1501
|
-
return i;
|
|
1958
|
+
|
|
1959
|
+
//#endregion
|
|
1960
|
+
//#region src/parser/node-parsers/container-parser.ts
|
|
1961
|
+
function parseContainer(tokens, index) {
|
|
1962
|
+
const openToken = tokens[index];
|
|
1963
|
+
let kind = "note";
|
|
1964
|
+
let title = "";
|
|
1965
|
+
const typeMatch = openToken.type.match(/^container_(\w+)_open$/);
|
|
1966
|
+
if (typeMatch) {
|
|
1967
|
+
kind = typeMatch[1];
|
|
1968
|
+
const info = (openToken.info || "").trim();
|
|
1969
|
+
if (info && !info.startsWith(":::")) {
|
|
1970
|
+
const maybe = info.replace(/* @__PURE__ */ new RegExp(`^${kind}`), "").trim();
|
|
1971
|
+
if (maybe) title = maybe;
|
|
1972
|
+
}
|
|
1973
|
+
} else {
|
|
1974
|
+
const info = (openToken.info || "").trim();
|
|
1975
|
+
const match = /^:{1,3}\s*(warning|info|note|tip|danger|caution)\s*(.*)$/i.exec(info);
|
|
1976
|
+
if (match) {
|
|
1977
|
+
kind = match[1];
|
|
1978
|
+
title = match[2] || "";
|
|
1979
|
+
}
|
|
1980
|
+
}
|
|
1981
|
+
if (!title) title = kind.charAt(0).toUpperCase() + kind.slice(1);
|
|
1982
|
+
const children = [];
|
|
1983
|
+
let j = index + 1;
|
|
1984
|
+
const closeType = /* @__PURE__ */ new RegExp(`^container_${kind}_close$`);
|
|
1985
|
+
while (j < tokens.length && tokens[j].type !== "container_close" && !closeType.test(tokens[j].type)) if (tokens[j].type === "paragraph_open") {
|
|
1986
|
+
const contentToken = tokens[j + 1];
|
|
1987
|
+
if (contentToken) {
|
|
1988
|
+
const i = contentToken.children.findLastIndex((t) => t.type === "text" && /:+/.test(t.content));
|
|
1989
|
+
const _children = i !== -1 ? contentToken.children?.slice(0, i) : contentToken.children;
|
|
1990
|
+
children.push({
|
|
1991
|
+
type: "paragraph",
|
|
1992
|
+
children: parseInlineTokens(_children || []),
|
|
1993
|
+
raw: contentToken.content?.replace(/\n:+$/, "").replace(/\n\s*:::\s*$/, "") || ""
|
|
1994
|
+
});
|
|
1995
|
+
}
|
|
1996
|
+
j += 3;
|
|
1997
|
+
} else if (tokens[j].type === "bullet_list_open" || tokens[j].type === "ordered_list_open") {
|
|
1998
|
+
const [listNode, newIndex] = parseList(tokens, j);
|
|
1999
|
+
children.push(listNode);
|
|
2000
|
+
j = newIndex;
|
|
2001
|
+
} else j++;
|
|
2002
|
+
return [{
|
|
2003
|
+
type: "admonition",
|
|
2004
|
+
kind,
|
|
2005
|
+
title,
|
|
2006
|
+
children,
|
|
2007
|
+
raw: `:::${kind} ${title}\n${children.map((c) => c.raw).join("\n")}\n:::`
|
|
2008
|
+
}, j + 1];
|
|
1502
2009
|
}
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
}
|
|
1512
|
-
let l = e.replace(r, (a, d) => X[a] !== void 0 ? `\\${X[a]}` : d && i.includes(d) ? `\\${d}` : a);
|
|
1513
|
-
o && (l = l.replace(/(^|[^\\])!/g, "$1\\!"));
|
|
1514
|
-
const t = n ? [M, L].filter(Boolean).join("|") : [i.map((a) => a.replace(/[.*+?^${}()|[\\]\\\]/g, "\\$&")).join("|"), M].filter(Boolean).join("|");
|
|
1515
|
-
let s = l;
|
|
1516
|
-
if (t) {
|
|
1517
|
-
const a = new RegExp(`(^|[^\\\\])(${t})\\s*\\{`, "g");
|
|
1518
|
-
s = s.replace(a, (d, p, u) => `${p}\\${u}{`);
|
|
1519
|
-
}
|
|
1520
|
-
return s = s.replace(/span\{([^}]+)\}/, "span\\{$1\\}").replace(/\\operatorname\{span\}\{((?:[^{}]|\{[^}]*\})+)\}/, "\\operatorname{span}\\{$1\\}"), s;
|
|
2010
|
+
|
|
2011
|
+
//#endregion
|
|
2012
|
+
//#region src/parser/node-parsers/hardbreak-parser.ts
|
|
2013
|
+
function parseHardBreak() {
|
|
2014
|
+
return {
|
|
2015
|
+
type: "hardbreak",
|
|
2016
|
+
raw: "\\\n"
|
|
2017
|
+
};
|
|
1521
2018
|
}
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
const p = n.src;
|
|
1534
|
-
let u = !1;
|
|
1535
|
-
const f = (h) => {
|
|
1536
|
-
if ((h === "undefined" || h == null) && (h = ""), h === "\\") {
|
|
1537
|
-
n.pos = n.pos + h.length, t = n.pos;
|
|
1538
|
-
return;
|
|
1539
|
-
}
|
|
1540
|
-
if (h === "\\)" || h === "\\(") {
|
|
1541
|
-
const x = n.push("text_special", "", 0);
|
|
1542
|
-
x.content = h === "\\)" ? ")" : "(", x.markup = h, n.pos = n.pos + h.length, t = n.pos;
|
|
1543
|
-
return;
|
|
1544
|
-
}
|
|
1545
|
-
if (!h)
|
|
1546
|
-
return;
|
|
1547
|
-
const g = n.push("text", "", 0);
|
|
1548
|
-
g.content = h, n.pos = n.pos + h.length, t = n.pos;
|
|
1549
|
-
};
|
|
1550
|
-
for (; !(t >= p.length); ) {
|
|
1551
|
-
const h = p.indexOf(a, t);
|
|
1552
|
-
if (h === -1)
|
|
1553
|
-
break;
|
|
1554
|
-
if (h > 0) {
|
|
1555
|
-
let _ = h - 1;
|
|
1556
|
-
for (; _ >= 0 && p[_] === " "; )
|
|
1557
|
-
_--;
|
|
1558
|
-
if (_ >= 0 && p[_] === "]")
|
|
1559
|
-
return !1;
|
|
1560
|
-
}
|
|
1561
|
-
const g = Ee(p, h + a.length, a, d);
|
|
1562
|
-
if (g === -1) {
|
|
1563
|
-
const _ = p.slice(h + a.length);
|
|
1564
|
-
if (q(_)) {
|
|
1565
|
-
if (t = h + a.length, u = !0, !r) {
|
|
1566
|
-
n.pending = "";
|
|
1567
|
-
const T = s ? p.slice(s, t) : p.slice(0, t), y = F(T) % 2 === 1;
|
|
1568
|
-
if (f(s ? p.slice(s, t) : p.slice(0, t)), y) {
|
|
1569
|
-
const w = n.push("strong_open", "", 0);
|
|
1570
|
-
w.markup = p.slice(0, h + 2);
|
|
1571
|
-
const m = n.push("math_inline", "math", 0);
|
|
1572
|
-
m.content = C(_, c), m.markup = a === "$$" ? "$$" : a === "\\(" ? "\\(\\)" : a === "$" ? "$" : "()", m.raw = `${a}${_}${d}`, m.loading = !0, w.content = _, n.push("strong_close", "", 0);
|
|
1573
|
-
} else {
|
|
1574
|
-
const w = n.push("math_inline", "math", 0);
|
|
1575
|
-
w.content = C(_, c), w.markup = a === "$$" ? "$$" : a === "\\(" ? "\\(\\)" : a === "$" ? "$" : "()", w.raw = `${a}${_}${d}`, w.loading = !0;
|
|
1576
|
-
}
|
|
1577
|
-
n.pos = p.length;
|
|
1578
|
-
}
|
|
1579
|
-
t = p.length, s = t;
|
|
1580
|
-
}
|
|
1581
|
-
break;
|
|
1582
|
-
}
|
|
1583
|
-
const x = p.slice(h + a.length, g);
|
|
1584
|
-
if (!q(x)) {
|
|
1585
|
-
t = g + d.length;
|
|
1586
|
-
const _ = p.slice(n.pos, t);
|
|
1587
|
-
n.pending || f(_);
|
|
1588
|
-
continue;
|
|
1589
|
-
}
|
|
1590
|
-
if (u = !0, !r) {
|
|
1591
|
-
const _ = p.slice(0, h);
|
|
1592
|
-
let y = p.slice(0, t) ? p.slice(s, h) : _;
|
|
1593
|
-
const w = F(y) % 2 === 1;
|
|
1594
|
-
if (h !== n.pos && w && (y = n.pending + p.slice(n.pos, h)), n.pending !== y)
|
|
1595
|
-
if (n.pending = "", w) {
|
|
1596
|
-
const m = y.match(/(\*+)/), $ = y.slice(m.index + m[0].length);
|
|
1597
|
-
f(y.slice(0, m.index));
|
|
1598
|
-
const v = n.push("strong_open", "", 0);
|
|
1599
|
-
v.markup = m[0];
|
|
1600
|
-
const I = n.push("text", "", 0);
|
|
1601
|
-
I.content = $, n.push("strong_close", "", 0);
|
|
1602
|
-
} else
|
|
1603
|
-
f(y);
|
|
1604
|
-
if (w) {
|
|
1605
|
-
const m = n.push("strong_open", "", 0);
|
|
1606
|
-
m.markup = "**";
|
|
1607
|
-
const $ = n.push("math_inline", "math", 0);
|
|
1608
|
-
$.content = C(x, c), $.markup = a === "$$" ? "$$" : a === "\\(" ? "\\(\\)" : a === "$" ? "$" : "()", $.raw = `${a}${x}${d}`, $.loading = !1;
|
|
1609
|
-
const v = p.slice(g + d.length), I = v.startsWith("*");
|
|
1610
|
-
if (I && n.push("strong_close", "", 0), v) {
|
|
1611
|
-
const te = n.push("text", "", 0);
|
|
1612
|
-
te.content = (v == null ? "" : String(v)).replace(/^\*+/, "");
|
|
1613
|
-
}
|
|
1614
|
-
I || n.push("strong_close", "", 0), n.pos = p.length, t = p.length, s = t;
|
|
1615
|
-
continue;
|
|
1616
|
-
} else {
|
|
1617
|
-
const m = n.push("math_inline", "math", 0);
|
|
1618
|
-
m.content = C(x, c), m.markup = a === "$$" ? "$$" : a === "\\(" ? "\\(\\)" : a === "$" ? "$" : "()", m.raw = `${a}${x}${d}`, m.loading = !1;
|
|
1619
|
-
}
|
|
1620
|
-
}
|
|
1621
|
-
t = g + d.length, s = t, n.pos = t;
|
|
1622
|
-
}
|
|
1623
|
-
if (u)
|
|
1624
|
-
return r ? n.pos = t : (t < p.length && f(p.slice(t)), n.pos = p.length), !0;
|
|
1625
|
-
}
|
|
1626
|
-
return !1;
|
|
1627
|
-
}, o = (n, r, l, t) => {
|
|
1628
|
-
const s = [
|
|
1629
|
-
["\\[", "\\]"],
|
|
1630
|
-
["[", "]"],
|
|
1631
|
-
["$$", "$$"]
|
|
1632
|
-
], a = n.bMarks[r] + n.tShift[r], d = n.src.slice(a, n.eMarks[r]).trim();
|
|
1633
|
-
let p = !1, u = "", f = "";
|
|
1634
|
-
for (const [y, w] of s)
|
|
1635
|
-
if (d === y || d.startsWith(y))
|
|
1636
|
-
if (y.includes("[")) {
|
|
1637
|
-
if (d.replace("\\", "") === "[") {
|
|
1638
|
-
if (r + 1 < l) {
|
|
1639
|
-
p = !0, u = y, f = w;
|
|
1640
|
-
break;
|
|
1641
|
-
}
|
|
1642
|
-
continue;
|
|
1643
|
-
}
|
|
1644
|
-
} else {
|
|
1645
|
-
p = !0, u = y, f = w;
|
|
1646
|
-
break;
|
|
1647
|
-
}
|
|
1648
|
-
if (!p)
|
|
1649
|
-
return !1;
|
|
1650
|
-
if (t)
|
|
1651
|
-
return !0;
|
|
1652
|
-
if (d.includes(f) && d.indexOf(f) > u.length) {
|
|
1653
|
-
const y = d.indexOf(u), w = d.indexOf(
|
|
1654
|
-
f,
|
|
1655
|
-
y + u.length
|
|
1656
|
-
), m = d.slice(
|
|
1657
|
-
y + u.length,
|
|
1658
|
-
w
|
|
1659
|
-
), $ = n.push("math_block", "math", 0);
|
|
1660
|
-
return $.content = C(m), $.markup = u === "$$" ? "$$" : u === "[" ? "[]" : "\\[\\]", $.map = [r, r + 1], $.raw = `${u}${m}${f}`, $.block = !0, $.loading = !1, n.line = r + 1, !0;
|
|
1661
|
-
}
|
|
1662
|
-
let h = r, g = "", x = !1;
|
|
1663
|
-
const _ = d === u ? "" : d.slice(u.length);
|
|
1664
|
-
if (_.includes(f)) {
|
|
1665
|
-
const y = _.indexOf(f);
|
|
1666
|
-
g = _.slice(0, y), x = !0, h = r;
|
|
1667
|
-
} else
|
|
1668
|
-
for (_ && (g = _), h = r + 1; h < l; h++) {
|
|
1669
|
-
const y = n.bMarks[h] + n.tShift[h], w = n.eMarks[h], m = n.src.slice(y - 1, w);
|
|
1670
|
-
if (m.trim() === f) {
|
|
1671
|
-
x = !0;
|
|
1672
|
-
break;
|
|
1673
|
-
} else if (m.includes(f)) {
|
|
1674
|
-
x = !0;
|
|
1675
|
-
const $ = m.indexOf(f);
|
|
1676
|
-
g += (g ? `
|
|
1677
|
-
` : "") + m.slice(0, $);
|
|
1678
|
-
break;
|
|
1679
|
-
}
|
|
1680
|
-
g += (g ? `
|
|
1681
|
-
` : "") + m;
|
|
1682
|
-
}
|
|
1683
|
-
const T = n.push("math_block", "math", 0);
|
|
1684
|
-
return T.content = C(g), T.markup = u === "$$" ? "$$" : u === "[" ? "[]" : "\\[\\]", T.raw = `${u}${g}${g.startsWith(`
|
|
1685
|
-
`) ? `
|
|
1686
|
-
` : ""}${f}`, T.map = [r, h + 1], T.block = !0, T.loading = !x, n.line = h + 1, !0;
|
|
1687
|
-
};
|
|
1688
|
-
e.inline.ruler.before("escape", "math", i), e.block.ruler.before("paragraph", "math_block", o, {
|
|
1689
|
-
alt: ["paragraph", "reference", "blockquote", "list"]
|
|
1690
|
-
});
|
|
2019
|
+
|
|
2020
|
+
//#endregion
|
|
2021
|
+
//#region src/parser/node-parsers/paragraph-parser.ts
|
|
2022
|
+
function parseParagraph(tokens, index) {
|
|
2023
|
+
const paragraphContentToken = tokens[index + 1];
|
|
2024
|
+
const paragraphContent = paragraphContentToken.content || "";
|
|
2025
|
+
return {
|
|
2026
|
+
type: "paragraph",
|
|
2027
|
+
children: parseInlineTokens(paragraphContentToken.children || [], paragraphContent),
|
|
2028
|
+
raw: paragraphContent
|
|
2029
|
+
};
|
|
1691
2030
|
}
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
2031
|
+
|
|
2032
|
+
//#endregion
|
|
2033
|
+
//#region src/parser/index.ts
|
|
2034
|
+
function parseMarkdownToStructure(markdown, md, options = {}) {
|
|
2035
|
+
let safeMarkdown = (markdown ?? "").toString().replace(/([^\\])\right/g, "$1\\right");
|
|
2036
|
+
if (safeMarkdown.endsWith("- *")) safeMarkdown = safeMarkdown.replace(/- \*$/, "- \\*");
|
|
2037
|
+
if (/\n\s*-\s*$/.test(safeMarkdown)) safeMarkdown = safeMarkdown.replace(/\n\s*-\s*$/, "\n");
|
|
2038
|
+
else if (/\n[[(]\n*$/.test(safeMarkdown)) safeMarkdown = safeMarkdown.replace(/(\n\[|\n\()+\n*$/g, "\n");
|
|
2039
|
+
const tokens = md.parse(safeMarkdown, {});
|
|
2040
|
+
if (!tokens || !Array.isArray(tokens)) return [];
|
|
2041
|
+
const pre = options.preTransformTokens;
|
|
2042
|
+
const post = options.postTransformTokens;
|
|
2043
|
+
let transformedTokens = tokens;
|
|
2044
|
+
if (pre && typeof pre === "function") transformedTokens = pre(tokens) || tokens;
|
|
2045
|
+
let result = processTokens(transformedTokens);
|
|
2046
|
+
if (post && typeof post === "function") result = post(transformedTokens) || transformedTokens;
|
|
2047
|
+
return result;
|
|
1700
2048
|
}
|
|
1701
|
-
function
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
2049
|
+
function processTokens(tokens) {
|
|
2050
|
+
if (!tokens || !Array.isArray(tokens)) return [];
|
|
2051
|
+
const result = [];
|
|
2052
|
+
let i = 0;
|
|
2053
|
+
tokens = fixTableTokens(tokens);
|
|
2054
|
+
while (i < tokens.length) {
|
|
2055
|
+
const token = tokens[i];
|
|
2056
|
+
switch (token.type) {
|
|
2057
|
+
case "container_warning_open":
|
|
2058
|
+
case "container_info_open":
|
|
2059
|
+
case "container_note_open":
|
|
2060
|
+
case "container_tip_open":
|
|
2061
|
+
case "container_danger_open":
|
|
2062
|
+
case "container_caution_open":
|
|
2063
|
+
case "container_error_open": {
|
|
2064
|
+
const [warningNode, newIndex] = parseContainer(tokens, i);
|
|
2065
|
+
result.push(warningNode);
|
|
2066
|
+
i = newIndex;
|
|
2067
|
+
break;
|
|
2068
|
+
}
|
|
2069
|
+
case "heading_open":
|
|
2070
|
+
result.push(parseHeading(tokens, i));
|
|
2071
|
+
i += 3;
|
|
2072
|
+
break;
|
|
2073
|
+
case "paragraph_open":
|
|
2074
|
+
result.push(parseParagraph(tokens, i));
|
|
2075
|
+
i += 3;
|
|
2076
|
+
break;
|
|
2077
|
+
case "html_block":
|
|
2078
|
+
case "code_block":
|
|
2079
|
+
result.push(parseCodeBlock(tokens[i]));
|
|
2080
|
+
i += 1;
|
|
2081
|
+
break;
|
|
2082
|
+
case "fence":
|
|
2083
|
+
result.push(parseFenceToken(tokens[i]));
|
|
2084
|
+
i += 1;
|
|
2085
|
+
break;
|
|
2086
|
+
case "bullet_list_open":
|
|
2087
|
+
case "ordered_list_open": {
|
|
2088
|
+
const [listNode, newIndex] = parseList(tokens, i);
|
|
2089
|
+
result.push(listNode);
|
|
2090
|
+
i = newIndex;
|
|
2091
|
+
break;
|
|
2092
|
+
}
|
|
2093
|
+
case "hr":
|
|
2094
|
+
result.push(parseThematicBreak());
|
|
2095
|
+
i += 1;
|
|
2096
|
+
break;
|
|
2097
|
+
case "blockquote_open": {
|
|
2098
|
+
const [blockquoteNode, newIndex] = parseBlockquote(tokens, i);
|
|
2099
|
+
result.push(blockquoteNode);
|
|
2100
|
+
i = newIndex;
|
|
2101
|
+
break;
|
|
2102
|
+
}
|
|
2103
|
+
case "table_open": {
|
|
2104
|
+
const [tableNode, newIndex] = parseTable(tokens, i);
|
|
2105
|
+
result.push(tableNode);
|
|
2106
|
+
i = newIndex;
|
|
2107
|
+
break;
|
|
2108
|
+
}
|
|
2109
|
+
case "dl_open": {
|
|
2110
|
+
const [definitionListNode, newIndex] = parseDefinitionList(tokens, i);
|
|
2111
|
+
result.push(definitionListNode);
|
|
2112
|
+
i = newIndex;
|
|
2113
|
+
break;
|
|
2114
|
+
}
|
|
2115
|
+
case "footnote_open": {
|
|
2116
|
+
const [footnoteNode, newIndex] = parseFootnote(tokens, i);
|
|
2117
|
+
result.push(footnoteNode);
|
|
2118
|
+
i = newIndex;
|
|
2119
|
+
break;
|
|
2120
|
+
}
|
|
2121
|
+
case "container_open": {
|
|
2122
|
+
const match = /^::: ?(warning|info|note|tip|danger|caution|error) ?(.*)$/.exec(token.info || "");
|
|
2123
|
+
if (match) {
|
|
2124
|
+
const [admonitionNode, newIndex] = parseAdmonition(tokens, i, match);
|
|
2125
|
+
result.push(admonitionNode);
|
|
2126
|
+
i = newIndex;
|
|
2127
|
+
} else i += 1;
|
|
2128
|
+
break;
|
|
2129
|
+
}
|
|
2130
|
+
case "hardbreak":
|
|
2131
|
+
result.push(parseHardBreak());
|
|
2132
|
+
i++;
|
|
2133
|
+
break;
|
|
2134
|
+
case "math_block":
|
|
2135
|
+
result.push(parseMathBlock(tokens[i]));
|
|
2136
|
+
i += 1;
|
|
2137
|
+
break;
|
|
2138
|
+
default:
|
|
2139
|
+
i += 1;
|
|
2140
|
+
break;
|
|
2141
|
+
}
|
|
2142
|
+
}
|
|
2143
|
+
return result;
|
|
1713
2144
|
}
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
2145
|
+
|
|
2146
|
+
//#endregion
|
|
2147
|
+
//#region src/index.ts
|
|
2148
|
+
function getMarkdown(msgId = `editor-${Date.now()}`, options = {}) {
|
|
2149
|
+
const md = factory(options);
|
|
2150
|
+
const defaultTranslations = { "common.copy": "Copy" };
|
|
2151
|
+
let t;
|
|
2152
|
+
if (typeof options.i18n === "function") t = options.i18n;
|
|
2153
|
+
else if (options.i18n && typeof options.i18n === "object") {
|
|
2154
|
+
const i18nMap = options.i18n;
|
|
2155
|
+
t = (key) => i18nMap[key] ?? defaultTranslations[key] ?? key;
|
|
2156
|
+
} else t = (key) => defaultTranslations[key] ?? key;
|
|
2157
|
+
if (Array.isArray(options.plugin)) for (const p of options.plugin) if (Array.isArray(p)) md.use(p[0], p[1]);
|
|
2158
|
+
else md.use(p);
|
|
2159
|
+
if (Array.isArray(options.apply)) for (const fn of options.apply) try {
|
|
2160
|
+
fn(md);
|
|
2161
|
+
} catch (e) {
|
|
2162
|
+
console.error("[getMarkdown] apply function threw an error", e);
|
|
2163
|
+
}
|
|
2164
|
+
md.use(markdownItSub);
|
|
2165
|
+
md.use(markdownItSup);
|
|
2166
|
+
md.use(markdownItMark);
|
|
2167
|
+
md.use(full);
|
|
2168
|
+
const markdownItCheckboxPlugin = markdownItCheckbox.default ?? markdownItCheckbox;
|
|
2169
|
+
md.use(markdownItCheckboxPlugin);
|
|
2170
|
+
md.use(markdownItIns);
|
|
2171
|
+
md.use(markdownItFootnote);
|
|
2172
|
+
md.core.ruler.after("block", "mark_fence_closed", (state) => {
|
|
2173
|
+
const lines = state.src.split(/\r?\n/);
|
|
2174
|
+
for (const token of state.tokens) {
|
|
2175
|
+
if (token.type !== "fence" || !token.map || !token.markup) continue;
|
|
2176
|
+
const openLine = token.map[0];
|
|
2177
|
+
const endLine = token.map[1];
|
|
2178
|
+
const markup = token.markup;
|
|
2179
|
+
const marker = markup[0];
|
|
2180
|
+
const minLen = markup.length;
|
|
2181
|
+
const line = lines[Math.max(0, endLine - 1)] ?? "";
|
|
2182
|
+
let i = 0;
|
|
2183
|
+
while (i < line.length && (line[i] === " " || line[i] === " ")) i++;
|
|
2184
|
+
let count = 0;
|
|
2185
|
+
while (i + count < line.length && line[i + count] === marker) count++;
|
|
2186
|
+
let j = i + count;
|
|
2187
|
+
while (j < line.length && (line[j] === " " || line[j] === " ")) j++;
|
|
2188
|
+
const closed = endLine > openLine + 1 && count >= minLen && j === line.length;
|
|
2189
|
+
token.meta = token.meta || {};
|
|
2190
|
+
token.meta.unclosed = !closed;
|
|
2191
|
+
token.meta.closed = !!closed;
|
|
2192
|
+
}
|
|
2193
|
+
});
|
|
2194
|
+
const waveRule = (state, silent) => {
|
|
2195
|
+
const start = state.pos;
|
|
2196
|
+
if (state.src[start] !== "~") return false;
|
|
2197
|
+
const prevChar = state.src[start - 1];
|
|
2198
|
+
const nextChar = state.src[start + 1];
|
|
2199
|
+
if (/\d/.test(prevChar) && /\d/.test(nextChar)) {
|
|
2200
|
+
if (!silent) {
|
|
2201
|
+
const token = state.push("text", "", 0);
|
|
2202
|
+
token.content = "~";
|
|
2203
|
+
}
|
|
2204
|
+
state.pos += 1;
|
|
2205
|
+
return true;
|
|
2206
|
+
}
|
|
2207
|
+
return false;
|
|
2208
|
+
};
|
|
2209
|
+
md.inline.ruler.before("sub", "wave", waveRule);
|
|
2210
|
+
md.renderer.rules.fence = (tokens, idx) => {
|
|
2211
|
+
const token = tokens[idx];
|
|
2212
|
+
const info = token.info ? token.info.trim() : "";
|
|
2213
|
+
const str = token.content;
|
|
2214
|
+
const encodedCode = btoa(unescape(encodeURIComponent(str)));
|
|
2215
|
+
const language = info || "text";
|
|
2216
|
+
return `<div class="code-block" data-code="${encodedCode}" data-lang="${language}" id="${`editor-${msgId}-${idx}-${language}`}">
|
|
2217
|
+
<div class="code-header">
|
|
2218
|
+
<span class="code-lang">${language.toUpperCase()}</span>
|
|
2219
|
+
<button class="copy-button" data-code="${encodedCode}">${t("common.copy")}</button>
|
|
2220
|
+
</div>
|
|
2221
|
+
<div class="code-editor"></div>
|
|
2222
|
+
</div>`;
|
|
2223
|
+
};
|
|
2224
|
+
const referenceInline = (state, silent) => {
|
|
2225
|
+
if (state.src[state.pos] !== "[") return false;
|
|
2226
|
+
const match = /^\[(\d+)\]/.exec(state.src.slice(state.pos));
|
|
2227
|
+
if (!match) return false;
|
|
2228
|
+
if (!silent) {
|
|
2229
|
+
const id = match[1];
|
|
2230
|
+
const token = state.push("reference", "span", 0);
|
|
2231
|
+
token.content = id;
|
|
2232
|
+
token.markup = match[0];
|
|
2233
|
+
}
|
|
2234
|
+
state.pos += match[0].length;
|
|
2235
|
+
return true;
|
|
2236
|
+
};
|
|
2237
|
+
md.inline.ruler.before("escape", "reference", referenceInline);
|
|
2238
|
+
md.renderer.rules.reference = (tokens, idx) => {
|
|
2239
|
+
const id = tokens[idx].content;
|
|
2240
|
+
return `<span class="reference-link" data-reference-id="${id}" role="button" tabindex="0" title="Click to view reference">${id}</span>`;
|
|
2241
|
+
};
|
|
2242
|
+
return md;
|
|
2243
|
+
}
|
|
2244
|
+
function getCommonMarkdown() {
|
|
2245
|
+
return new MarkdownIt({
|
|
2246
|
+
html: true,
|
|
2247
|
+
linkify: true,
|
|
2248
|
+
typographer: true,
|
|
2249
|
+
breaks: false
|
|
2250
|
+
});
|
|
2251
|
+
}
|
|
2252
|
+
function renderMarkdown(md, content) {
|
|
2253
|
+
return md.render(content);
|
|
2254
|
+
}
|
|
2255
|
+
|
|
2256
|
+
//#endregion
|
|
2257
|
+
export { ESCAPED_TEX_BRACE_COMMANDS, KATEX_COMMANDS, TEX_BRACE_COMMANDS, applyContainers, applyMath, findMatchingClose, getCommonMarkdown, getMarkdown, isMathLike, normalizeStandaloneBackslashT, parseFenceToken, parseInlineTokens, parseMarkdownToStructure, processTokens, renderMarkdown, setDefaultMathOptions };
|
|
2258
|
+
//# sourceMappingURL=index.js.map
|