stream-markdown-parser 0.0.21 → 0.0.23
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/dist/index.js +361 -261
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -96,19 +96,102 @@ const VOID_TAGS$1 = new Set([
|
|
|
96
96
|
function applyFixHtmlInlineTokens(md) {
|
|
97
97
|
md.core.ruler.push("fix_html_inline_tokens", (state) => {
|
|
98
98
|
const toks = state.tokens ?? [];
|
|
99
|
+
const tagStack = [];
|
|
99
100
|
for (let i = 0; i < toks.length; i++) {
|
|
100
101
|
const t = toks[i];
|
|
101
|
-
if (
|
|
102
|
-
|
|
103
|
-
t.
|
|
102
|
+
if (t.type === "html_block") {
|
|
103
|
+
const tag = t.content?.match(/<([^\s>/]+)/)?.[1] ?? "";
|
|
104
|
+
if (!/<\s*\/\s*[^\s>]+\s*>/.test(t.content || "")) tagStack.push([tag, i]);
|
|
105
|
+
else if (tagStack.length > 0 && tagStack[tagStack.length - 1][0] === tag) tagStack.pop();
|
|
106
|
+
continue;
|
|
107
|
+
} else if (tagStack.length > 0) {
|
|
108
|
+
if (t.type === "paragraph_open" || t.type === "paragraph_close") {
|
|
109
|
+
toks.splice(i, 1);
|
|
110
|
+
i--;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
const content = t.content || "";
|
|
114
|
+
const isClosingTag = (/* @__PURE__ */ new RegExp(`<\\s*\\/\\s*${tagStack[tagStack.length - 1][0]}\\s*>`)).test(content);
|
|
115
|
+
if (content) {
|
|
116
|
+
const [, openIndex] = tagStack[tagStack.length - 1];
|
|
117
|
+
const openToken = toks[openIndex];
|
|
118
|
+
openToken.content = `${openToken.content || ""}\n${content}`;
|
|
119
|
+
if (openToken.loading !== false) openToken.loading = !isClosingTag;
|
|
120
|
+
}
|
|
121
|
+
if (isClosingTag) tagStack.pop();
|
|
122
|
+
toks.splice(i, 1);
|
|
123
|
+
i--;
|
|
124
|
+
} else continue;
|
|
125
|
+
}
|
|
126
|
+
for (let i = 0; i < toks.length; i++) {
|
|
127
|
+
const t = toks[i];
|
|
128
|
+
if (t.type === "html_block") {
|
|
129
|
+
const tag = t.content?.match(/<([^\s>/]+)/)?.[1] ?? "";
|
|
130
|
+
if ([
|
|
131
|
+
"br",
|
|
132
|
+
"hr",
|
|
133
|
+
"img",
|
|
134
|
+
"input",
|
|
135
|
+
"link",
|
|
136
|
+
"meta",
|
|
137
|
+
"div",
|
|
138
|
+
"p",
|
|
139
|
+
"ul",
|
|
140
|
+
"li"
|
|
141
|
+
].includes(tag)) continue;
|
|
142
|
+
t.type = "inline";
|
|
143
|
+
const loading = t.content?.includes(`</${tag}>`) ? false : t.loading !== void 0 ? t.loading : true;
|
|
144
|
+
t.children = [{
|
|
145
|
+
type: "html_block",
|
|
146
|
+
content: t.content,
|
|
147
|
+
tag: t.content?.match(/<([^\s>/]+)/)?.[1] ?? "",
|
|
148
|
+
loading
|
|
149
|
+
}];
|
|
104
150
|
continue;
|
|
105
151
|
}
|
|
152
|
+
if (!t || t.type !== "inline") continue;
|
|
106
153
|
if (t.children.length === 2 && t.children[0].type === "html_inline") {
|
|
154
|
+
const tag = t.children[0].content?.match(/<([^\s>/]+)/)?.[1] ?? "";
|
|
155
|
+
if ([
|
|
156
|
+
"a",
|
|
157
|
+
"span",
|
|
158
|
+
"strong",
|
|
159
|
+
"em",
|
|
160
|
+
"b",
|
|
161
|
+
"i",
|
|
162
|
+
"u"
|
|
163
|
+
].includes(tag)) {
|
|
164
|
+
t.children[0].loading = true;
|
|
165
|
+
t.children[0].tag = tag;
|
|
166
|
+
t.children.push({
|
|
167
|
+
type: "html_inline",
|
|
168
|
+
tag,
|
|
169
|
+
loading: true,
|
|
170
|
+
content: `</${tag}>`
|
|
171
|
+
});
|
|
172
|
+
} else t.children = [{
|
|
173
|
+
type: "html_block",
|
|
174
|
+
loading: true,
|
|
175
|
+
tag,
|
|
176
|
+
content: t.children[0].content + t.children[1].content
|
|
177
|
+
}];
|
|
178
|
+
continue;
|
|
179
|
+
} else if (t.children.length === 3 && t.children[0].type === "html_inline" && t.children[2].type === "html_inline") {
|
|
180
|
+
const tag = t.children[0].content?.match(/<([^\s>/]+)/)?.[1] ?? "";
|
|
181
|
+
if ([
|
|
182
|
+
"a",
|
|
183
|
+
"span",
|
|
184
|
+
"strong",
|
|
185
|
+
"em",
|
|
186
|
+
"b",
|
|
187
|
+
"i",
|
|
188
|
+
"u"
|
|
189
|
+
].includes(tag)) continue;
|
|
107
190
|
t.children = [{
|
|
108
191
|
type: "html_block",
|
|
109
|
-
|
|
110
|
-
tag
|
|
111
|
-
|
|
192
|
+
loading: false,
|
|
193
|
+
tag,
|
|
194
|
+
content: t.children.map((ct) => ct.content).join("")
|
|
112
195
|
}];
|
|
113
196
|
continue;
|
|
114
197
|
}
|
|
@@ -176,288 +259,300 @@ function applyFixLinkTokens(md) {
|
|
|
176
259
|
}
|
|
177
260
|
});
|
|
178
261
|
}
|
|
179
|
-
function isTextToken(t) {
|
|
180
|
-
return !!t && t.type === "text" && typeof t.content === "string";
|
|
181
|
-
}
|
|
182
262
|
function fixLinkToken(tokens) {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
tokens.splice(length - 4, count);
|
|
236
|
-
const thirdAny = third;
|
|
237
|
-
const content = String(thirdAny.content ?? "");
|
|
238
|
-
length -= 4;
|
|
239
|
-
const firstAny = first;
|
|
240
|
-
tokensAny[length - 8].content = String(firstAny.content ?? "").replace(/\[$/, "");
|
|
241
|
-
tokens.splice(length - 2, 1, {
|
|
242
|
-
type: "link",
|
|
243
|
-
href,
|
|
244
|
-
text: content,
|
|
245
|
-
children: [{
|
|
246
|
-
type: "text",
|
|
247
|
-
content,
|
|
248
|
-
raw: content
|
|
249
|
-
}],
|
|
250
|
-
loading: true
|
|
251
|
-
});
|
|
252
|
-
return tokens;
|
|
253
|
-
}
|
|
254
|
-
function fixLinkToken3(tokens) {
|
|
255
|
-
const tokensAny = tokens;
|
|
256
|
-
const last = tokens[tokens.length - 1];
|
|
257
|
-
const preLast = tokens[tokens.length - 2];
|
|
258
|
-
const fixedTokens = [...tokens];
|
|
259
|
-
if (!last) return tokens;
|
|
260
|
-
if (last.type !== "text" || !last.content?.startsWith(")")) return fixLinkToken5(tokens);
|
|
261
|
-
if (preLast.type !== "link_close") return tokens;
|
|
262
|
-
if (isTextToken(tokens[tokens.length - 5]) && String(tokens[tokens.length - 5].content ?? "").endsWith("(")) {
|
|
263
|
-
const a = tokensAny[tokens.length - 5];
|
|
264
|
-
const b = tokensAny[tokens.length - 3];
|
|
265
|
-
const content = String(a.content ?? "") + String(b.content ?? "") + String(last.content ?? "");
|
|
266
|
-
fixedTokens.splice(tokens.length - 5, 5, {
|
|
267
|
-
type: "text",
|
|
268
|
-
content,
|
|
269
|
-
raw: content
|
|
270
|
-
});
|
|
271
|
-
} else {
|
|
272
|
-
const lc = (last.content ?? "").slice(1);
|
|
273
|
-
fixedTokens[fixedTokens.length - 1] = {
|
|
274
|
-
...last,
|
|
275
|
-
content: lc
|
|
276
|
-
};
|
|
277
|
-
}
|
|
278
|
-
return fixedTokens;
|
|
279
|
-
}
|
|
280
|
-
function fixLinkToken4(tokens) {
|
|
281
|
-
const tokensAny = tokens;
|
|
282
|
-
const fixedTokens = [...tokens];
|
|
283
|
-
for (let i = tokens.length - 1; i >= 3; i--) {
|
|
284
|
-
const token = tokens[i];
|
|
285
|
-
if (token && token.type === "link_close") {
|
|
286
|
-
if (tokens[i - 3]?.content?.endsWith("(")) {
|
|
287
|
-
const nextToken = tokens[i + 1];
|
|
288
|
-
if (nextToken && nextToken?.type === "text") {
|
|
289
|
-
if (tokens[i - 1].type === "text" && tokens[i - 3]?.type === "text") {
|
|
290
|
-
const nextTokenContent = String(nextToken.content ?? "");
|
|
291
|
-
const a = tokensAny[i - 3];
|
|
292
|
-
const b = tokensAny[i - 1];
|
|
293
|
-
const content = String(a.content ?? "") + String(b.content ?? "") + nextTokenContent;
|
|
294
|
-
fixedTokens.splice(i - 3, 5, {
|
|
263
|
+
if (tokens.length < 4) return tokens;
|
|
264
|
+
for (let i = 0; i <= tokens.length - 1; i++) {
|
|
265
|
+
if (!tokens[i]) break;
|
|
266
|
+
if (tokens[i]?.type === "text" && tokens[i].content?.endsWith("(") && tokens[i + 1]?.type === "link_open") {
|
|
267
|
+
const match = tokens[i].content.match(/\[([^\]]+)\]/);
|
|
268
|
+
if (match) {
|
|
269
|
+
let beforeText = tokens[i].content.slice(0, match.index);
|
|
270
|
+
const emphasisMatch = beforeText.match(/(\*+)$/);
|
|
271
|
+
const replacerTokens = [];
|
|
272
|
+
if (emphasisMatch) {
|
|
273
|
+
beforeText = beforeText.slice(0, emphasisMatch.index);
|
|
274
|
+
if (beforeText) replacerTokens.push({
|
|
275
|
+
type: "text",
|
|
276
|
+
content: beforeText,
|
|
277
|
+
raw: beforeText
|
|
278
|
+
});
|
|
279
|
+
const text = match[1];
|
|
280
|
+
const type = emphasisMatch[1].length;
|
|
281
|
+
if (type === 1) replacerTokens.push({
|
|
282
|
+
type: "em_open",
|
|
283
|
+
tag: "em",
|
|
284
|
+
nesting: 1
|
|
285
|
+
});
|
|
286
|
+
else if (type === 2) replacerTokens.push({
|
|
287
|
+
type: "strong_open",
|
|
288
|
+
tag: "strong",
|
|
289
|
+
nesting: 1
|
|
290
|
+
});
|
|
291
|
+
else if (type === 3) {
|
|
292
|
+
replacerTokens.push({
|
|
293
|
+
type: "strong_open",
|
|
294
|
+
tag: "strong",
|
|
295
|
+
nesting: 1
|
|
296
|
+
});
|
|
297
|
+
replacerTokens.push({
|
|
298
|
+
type: "em_open",
|
|
299
|
+
tag: "em",
|
|
300
|
+
nesting: 1
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
let href = tokens[i + 2]?.content || "";
|
|
304
|
+
if (tokens[i + 4]?.type === "text" && !tokens[i + 4].content?.startsWith(")")) {
|
|
305
|
+
href += tokens[i + 4]?.content || "";
|
|
306
|
+
tokens[i + 4].content = "";
|
|
307
|
+
}
|
|
308
|
+
replacerTokens.push({
|
|
309
|
+
type: "link",
|
|
310
|
+
loading: !tokens[i + 4]?.content?.startsWith(")"),
|
|
311
|
+
href,
|
|
312
|
+
title: "",
|
|
313
|
+
text,
|
|
314
|
+
children: [{
|
|
295
315
|
type: "text",
|
|
296
|
-
content,
|
|
297
|
-
raw:
|
|
316
|
+
content: text,
|
|
317
|
+
raw: text
|
|
318
|
+
}],
|
|
319
|
+
raw: String(`[${text}](${href})`)
|
|
320
|
+
});
|
|
321
|
+
if (type === 1) replacerTokens.push({
|
|
322
|
+
type: "em_close",
|
|
323
|
+
tag: "em",
|
|
324
|
+
nesting: -1
|
|
325
|
+
});
|
|
326
|
+
else if (type === 2) replacerTokens.push({
|
|
327
|
+
type: "strong_close",
|
|
328
|
+
tag: "strong",
|
|
329
|
+
nesting: -1
|
|
330
|
+
});
|
|
331
|
+
else if (type === 3) {
|
|
332
|
+
replacerTokens.push({
|
|
333
|
+
type: "em_close",
|
|
334
|
+
tag: "em",
|
|
335
|
+
nesting: -1
|
|
336
|
+
});
|
|
337
|
+
replacerTokens.push({
|
|
338
|
+
type: "strong_close",
|
|
339
|
+
tag: "strong",
|
|
340
|
+
nesting: -1
|
|
298
341
|
});
|
|
299
|
-
i -= 3;
|
|
300
342
|
}
|
|
343
|
+
if (tokens[i + 4]?.type === "text") {
|
|
344
|
+
const afterText = tokens[i + 4].content?.replace(/^\)\**/, "");
|
|
345
|
+
if (afterText) replacerTokens.push({
|
|
346
|
+
type: "text",
|
|
347
|
+
content: afterText,
|
|
348
|
+
raw: afterText
|
|
349
|
+
});
|
|
350
|
+
tokens.splice(i, 5, ...replacerTokens);
|
|
351
|
+
} else tokens.splice(i, 4, ...replacerTokens);
|
|
301
352
|
} else {
|
|
302
|
-
if (
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
353
|
+
if (beforeText) replacerTokens.push({
|
|
354
|
+
type: "text",
|
|
355
|
+
content: beforeText,
|
|
356
|
+
raw: beforeText
|
|
357
|
+
});
|
|
358
|
+
const text = match[1];
|
|
359
|
+
let href = tokens[i + 2]?.content || "";
|
|
360
|
+
if (tokens[i + 4]?.type === "text" && !tokens[i + 4].content?.startsWith(")")) {
|
|
361
|
+
href += tokens[i + 4]?.content || "";
|
|
362
|
+
tokens[i + 4].content = "";
|
|
363
|
+
}
|
|
364
|
+
replacerTokens.push(...[{
|
|
365
|
+
type: "link",
|
|
366
|
+
loading: !tokens[i + 4]?.content?.startsWith(")"),
|
|
367
|
+
href,
|
|
368
|
+
title: "",
|
|
369
|
+
text,
|
|
370
|
+
children: [{
|
|
371
|
+
type: "text",
|
|
372
|
+
content: text,
|
|
373
|
+
raw: text
|
|
374
|
+
}],
|
|
375
|
+
raw: String(`[${text}](${href})`)
|
|
376
|
+
}]);
|
|
377
|
+
if (tokens[i + 4]?.type === "text") {
|
|
378
|
+
const afterText = tokens[i + 4].content?.replace(/^\)/, "");
|
|
379
|
+
if (afterText) replacerTokens.push({
|
|
307
380
|
type: "text",
|
|
308
|
-
content,
|
|
309
|
-
raw:
|
|
381
|
+
content: afterText,
|
|
382
|
+
raw: afterText
|
|
310
383
|
});
|
|
384
|
+
tokens.splice(i, 5, ...replacerTokens);
|
|
385
|
+
} else tokens.splice(i, 4, ...replacerTokens);
|
|
386
|
+
}
|
|
387
|
+
i -= replacerTokens.length - 1;
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
} else if (tokens[i].type === "link_open" && tokens[i].markup === "linkify" && tokens[i - 1]?.type === "text" && tokens[i - 1].content?.endsWith("(")) {
|
|
391
|
+
if (tokens[i - 2]?.type === "link_close") {
|
|
392
|
+
const replacerTokens = [];
|
|
393
|
+
const text = tokens[i - 3].content || "";
|
|
394
|
+
let href = tokens[i].attrs?.find((attr) => attr[0] === "href")?.[1] || "";
|
|
395
|
+
if (tokens[i + 3]?.type === "text") {
|
|
396
|
+
const m = (tokens[i + 3]?.content ?? "").indexOf(")");
|
|
397
|
+
const loading = m === -1;
|
|
398
|
+
if (m === -1) {
|
|
399
|
+
href += tokens[i + 3]?.content?.slice(0, m) || "";
|
|
400
|
+
tokens[i + 3].content = "";
|
|
311
401
|
}
|
|
312
|
-
|
|
402
|
+
replacerTokens.push({
|
|
403
|
+
type: "link",
|
|
404
|
+
loading,
|
|
405
|
+
href,
|
|
406
|
+
title: "",
|
|
407
|
+
text,
|
|
408
|
+
children: [{
|
|
409
|
+
type: "text",
|
|
410
|
+
content: text,
|
|
411
|
+
raw: text
|
|
412
|
+
}],
|
|
413
|
+
raw: String(`[${text}](${href})`)
|
|
414
|
+
});
|
|
415
|
+
const afterText = tokens[i + 3].content?.replace(/^\)\**/, "");
|
|
416
|
+
if (afterText) replacerTokens.push({
|
|
417
|
+
type: "text",
|
|
418
|
+
content: afterText,
|
|
419
|
+
raw: afterText
|
|
420
|
+
});
|
|
421
|
+
tokens.splice(i - 4, 8, ...replacerTokens);
|
|
422
|
+
} else {
|
|
423
|
+
replacerTokens.push({
|
|
424
|
+
type: "link",
|
|
425
|
+
loading: true,
|
|
426
|
+
href,
|
|
427
|
+
title: "",
|
|
428
|
+
text,
|
|
429
|
+
children: [{
|
|
430
|
+
type: "text",
|
|
431
|
+
content: href,
|
|
432
|
+
raw: href
|
|
433
|
+
}],
|
|
434
|
+
raw: String(`[${text}](${href})`)
|
|
435
|
+
});
|
|
436
|
+
tokens.splice(i - 4, 7, ...replacerTokens);
|
|
313
437
|
}
|
|
438
|
+
continue;
|
|
314
439
|
}
|
|
315
440
|
}
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
raw: String(`[${linkText}](${href})`)
|
|
337
|
-
}];
|
|
338
|
-
tokens.splice(0, 4, ...newTokens);
|
|
339
|
-
}
|
|
340
|
-
return tokens;
|
|
341
|
-
}
|
|
342
|
-
function fixLinkToken6(tokens) {
|
|
343
|
-
if (tokens.length < 4) return tokens;
|
|
344
|
-
for (let i = 0; i <= tokens.length - 4; i++) if (tokens[i]?.type === "text" && tokens[i].content?.endsWith("(") && tokens[i + 1]?.type === "link_open") {
|
|
345
|
-
const match = tokens[i].content.match(/\[([^\]]+)\]/);
|
|
346
|
-
if (match) {
|
|
347
|
-
let beforeText = tokens[i].content.slice(0, match.index);
|
|
348
|
-
const emphasisMatch = beforeText.match(/(\*+)$/);
|
|
349
|
-
const replacerTokens = [];
|
|
350
|
-
if (emphasisMatch) {
|
|
351
|
-
beforeText = beforeText.slice(0, emphasisMatch.index);
|
|
352
|
-
if (beforeText) replacerTokens.push({
|
|
441
|
+
if (tokens[i].type === "link_close" && tokens[i].nesting === -1 && tokens[i + 1]?.type === "text" && tokens[i - 1]?.type === "text" && tokens[i + 2]?.type !== "link_open") {
|
|
442
|
+
tokens[i - 2].loading = true;
|
|
443
|
+
const text = tokens[i - 1].content || "";
|
|
444
|
+
let href = tokens[i - 2].attrs?.[0]?.[1] || "";
|
|
445
|
+
let count = 3;
|
|
446
|
+
if (tokens[i].markup === "linkify" && tokens[i + 1]?.type === "text") {
|
|
447
|
+
const m = (tokens[i + 1]?.content ?? "").indexOf(")");
|
|
448
|
+
if (m === -1) {
|
|
449
|
+
href += tokens[i + 1]?.content?.slice(0, m) || "";
|
|
450
|
+
tokens[i + 1].content = "";
|
|
451
|
+
}
|
|
452
|
+
count += 1;
|
|
453
|
+
}
|
|
454
|
+
tokens.splice(i - 2, count, {
|
|
455
|
+
type: "link",
|
|
456
|
+
loading: false,
|
|
457
|
+
href,
|
|
458
|
+
title: "",
|
|
459
|
+
text,
|
|
460
|
+
children: [{
|
|
353
461
|
type: "text",
|
|
354
|
-
content:
|
|
355
|
-
raw:
|
|
462
|
+
content: text,
|
|
463
|
+
raw: text
|
|
464
|
+
}],
|
|
465
|
+
raw: String(`[${text}](${href})`)
|
|
466
|
+
});
|
|
467
|
+
} else if (tokens[i].content?.startsWith("](") && tokens[i - 1].markup?.includes("*") && tokens[i - 4].type === "text" && tokens[i - 4].content?.endsWith("[")) {
|
|
468
|
+
const type = tokens[i - 1].markup.length;
|
|
469
|
+
const replacerTokens = [];
|
|
470
|
+
const beforeText = tokens[i - 4].content.slice(0, tokens[i - 4].content.length - 1 - type);
|
|
471
|
+
if (beforeText) replacerTokens.push({
|
|
472
|
+
type: "text",
|
|
473
|
+
content: beforeText,
|
|
474
|
+
raw: beforeText
|
|
475
|
+
});
|
|
476
|
+
if (type === 1) replacerTokens.push({
|
|
477
|
+
type: "em_open",
|
|
478
|
+
tag: "em",
|
|
479
|
+
nesting: 1
|
|
480
|
+
});
|
|
481
|
+
else if (type === 2) replacerTokens.push({
|
|
482
|
+
type: "strong_open",
|
|
483
|
+
tag: "strong",
|
|
484
|
+
nesting: 1
|
|
485
|
+
});
|
|
486
|
+
else if (type === 3) {
|
|
487
|
+
replacerTokens.push({
|
|
488
|
+
type: "strong_open",
|
|
489
|
+
tag: "strong",
|
|
490
|
+
nesting: 1
|
|
356
491
|
});
|
|
357
|
-
|
|
358
|
-
const type = emphasisMatch[1].length;
|
|
359
|
-
if (type === 1) replacerTokens.push({
|
|
492
|
+
replacerTokens.push({
|
|
360
493
|
type: "em_open",
|
|
361
494
|
tag: "em",
|
|
362
495
|
nesting: 1
|
|
363
496
|
});
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
});
|
|
375
|
-
replacerTokens.push({
|
|
376
|
-
type: "em_open",
|
|
377
|
-
tag: "em",
|
|
378
|
-
nesting: 1
|
|
379
|
-
});
|
|
380
|
-
}
|
|
381
|
-
let href = tokens[i + 2]?.content || "";
|
|
382
|
-
if (tokens[i + 4]?.type === "text" && !tokens[i + 4].content?.startsWith(")")) {
|
|
383
|
-
href += tokens[i + 4]?.content || "";
|
|
384
|
-
tokens[i + 4].content = "";
|
|
497
|
+
}
|
|
498
|
+
const text = tokens[i - 2].content || "";
|
|
499
|
+
let href = tokens[i].content.slice(2);
|
|
500
|
+
let loading = true;
|
|
501
|
+
if (tokens[i + 1]?.type === "text") {
|
|
502
|
+
const m = (tokens[i + 1]?.content ?? "").indexOf(")");
|
|
503
|
+
loading = m === -1;
|
|
504
|
+
if (m === -1) {
|
|
505
|
+
href += tokens[i + 1]?.content?.slice(0, m) || "";
|
|
506
|
+
tokens[i + 1].content = "";
|
|
385
507
|
}
|
|
508
|
+
}
|
|
509
|
+
replacerTokens.push({
|
|
510
|
+
type: "link",
|
|
511
|
+
loading,
|
|
512
|
+
href,
|
|
513
|
+
title: "",
|
|
514
|
+
text,
|
|
515
|
+
children: [{
|
|
516
|
+
type: "text",
|
|
517
|
+
content: text,
|
|
518
|
+
raw: text
|
|
519
|
+
}],
|
|
520
|
+
raw: String(`[${text}](${href})`)
|
|
521
|
+
});
|
|
522
|
+
if (type === 1) replacerTokens.push({
|
|
523
|
+
type: "em_close",
|
|
524
|
+
tag: "em",
|
|
525
|
+
nesting: -1
|
|
526
|
+
});
|
|
527
|
+
else if (type === 2) replacerTokens.push({
|
|
528
|
+
type: "strong_close",
|
|
529
|
+
tag: "strong",
|
|
530
|
+
nesting: -1
|
|
531
|
+
});
|
|
532
|
+
else if (type === 3) {
|
|
386
533
|
replacerTokens.push({
|
|
387
|
-
type: "link",
|
|
388
|
-
loading: !tokens[i + 4]?.content?.startsWith(")"),
|
|
389
|
-
href,
|
|
390
|
-
title: "",
|
|
391
|
-
text,
|
|
392
|
-
children: [{
|
|
393
|
-
type: "text",
|
|
394
|
-
content: text,
|
|
395
|
-
raw: text
|
|
396
|
-
}],
|
|
397
|
-
raw: String(`[${text}](${tokens[i + 2]?.content || ""})`)
|
|
398
|
-
});
|
|
399
|
-
if (type === 1) replacerTokens.push({
|
|
400
534
|
type: "em_close",
|
|
401
535
|
tag: "em",
|
|
402
536
|
nesting: -1
|
|
403
537
|
});
|
|
404
|
-
|
|
538
|
+
replacerTokens.push({
|
|
405
539
|
type: "strong_close",
|
|
406
540
|
tag: "strong",
|
|
407
541
|
nesting: -1
|
|
408
542
|
});
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
nesting: -1
|
|
414
|
-
});
|
|
415
|
-
replacerTokens.push({
|
|
416
|
-
type: "strong_close",
|
|
417
|
-
tag: "strong",
|
|
418
|
-
nesting: -1
|
|
419
|
-
});
|
|
420
|
-
}
|
|
421
|
-
if (tokens[i + 4]?.type === "text") {
|
|
422
|
-
const afterText = tokens[i + 4].content?.replace(/^\)\**/, "");
|
|
423
|
-
if (afterText) replacerTokens.push({
|
|
424
|
-
type: "text",
|
|
425
|
-
content: afterText,
|
|
426
|
-
raw: afterText
|
|
427
|
-
});
|
|
428
|
-
tokens.splice(i, 5, ...replacerTokens);
|
|
429
|
-
} else tokens.splice(i, 4, ...replacerTokens);
|
|
430
|
-
} else {
|
|
431
|
-
if (beforeText) replacerTokens.push({
|
|
543
|
+
}
|
|
544
|
+
if (tokens[i + 1]?.type === "text") {
|
|
545
|
+
const afterText = tokens[i + 1].content?.replace(/^\)\**/, "");
|
|
546
|
+
if (afterText) replacerTokens.push({
|
|
432
547
|
type: "text",
|
|
433
|
-
content:
|
|
434
|
-
raw:
|
|
548
|
+
content: afterText,
|
|
549
|
+
raw: afterText
|
|
435
550
|
});
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
loading: true,
|
|
440
|
-
href: tokens[i + 2]?.content || "",
|
|
441
|
-
title: "",
|
|
442
|
-
text,
|
|
443
|
-
children: [{
|
|
444
|
-
type: "text",
|
|
445
|
-
content: text,
|
|
446
|
-
raw: text
|
|
447
|
-
}],
|
|
448
|
-
raw: String(`[${text}](${tokens[i + 2]?.content || ""})`)
|
|
449
|
-
}]);
|
|
450
|
-
if (tokens[i + 4]?.type === "text") {
|
|
451
|
-
const afterText = tokens[i + 4].content?.replace(/^\)/, "");
|
|
452
|
-
if (afterText) replacerTokens.push({
|
|
453
|
-
type: "text",
|
|
454
|
-
content: afterText,
|
|
455
|
-
raw: afterText
|
|
456
|
-
});
|
|
457
|
-
tokens.splice(i, 5, ...replacerTokens);
|
|
458
|
-
} else tokens.splice(i, 4, ...replacerTokens);
|
|
459
|
-
}
|
|
551
|
+
tokens.splice(i - 4, 8, ...replacerTokens);
|
|
552
|
+
} else if (tokens[i + 1]?.type === "link_open") tokens.splice(i - 4, 10, ...replacerTokens);
|
|
553
|
+
else tokens.splice(i - 4, 7, ...replacerTokens);
|
|
460
554
|
i -= replacerTokens.length - 1;
|
|
555
|
+
continue;
|
|
461
556
|
}
|
|
462
557
|
}
|
|
463
558
|
return tokens;
|
|
@@ -505,9 +600,10 @@ function fixStrongTokens(tokens) {
|
|
|
505
600
|
if (tokens.length < 4) return fixedTokens;
|
|
506
601
|
const i = tokens.length - 4;
|
|
507
602
|
const token = tokens[i];
|
|
603
|
+
if (!token) return fixedTokens;
|
|
508
604
|
const nextToken = tokens[i + 1];
|
|
509
605
|
const tokenContent = String(token.content ?? "");
|
|
510
|
-
if (token.type === "link_open" && tokens[i - 1]
|
|
606
|
+
if (token.type === "link_open" && tokens[i - 1]?.type === "em_open" && tokens[i - 2]?.type === "text" && tokens[i - 2].content?.endsWith("*")) {
|
|
511
607
|
const textContent = String(tokens[i - 2].content ?? "").slice(0, -1);
|
|
512
608
|
const replaceTokens = [
|
|
513
609
|
{
|
|
@@ -2125,7 +2221,8 @@ function parseInlineTokens(tokens, raw, pPreToken) {
|
|
|
2125
2221
|
}
|
|
2126
2222
|
if (!nextToken && /[^\]]\s*\(\s*$/.test(content)) content = content.replace(/\(\s*$/, "");
|
|
2127
2223
|
if (handleCheckboxLike(content)) return;
|
|
2128
|
-
|
|
2224
|
+
const preToken = tokens[i - 1];
|
|
2225
|
+
if (content === "[" && !nextToken?.markup?.includes("*") || content === "]" && !preToken.markup?.includes("*")) {
|
|
2129
2226
|
i++;
|
|
2130
2227
|
return;
|
|
2131
2228
|
}
|
|
@@ -2137,7 +2234,6 @@ function parseInlineTokens(tokens, raw, pPreToken) {
|
|
|
2137
2234
|
content
|
|
2138
2235
|
});
|
|
2139
2236
|
if (handleInlineLinkContent(content, token)) return;
|
|
2140
|
-
const preToken = tokens[i - 1];
|
|
2141
2237
|
if (currentTextNode) {
|
|
2142
2238
|
currentTextNode.content += textNode.content.replace(/(\*+|\(|\\)$/, "");
|
|
2143
2239
|
currentTextNode.raw += textNode.raw;
|
|
@@ -2978,6 +3074,10 @@ function processTokens(tokens) {
|
|
|
2978
3074
|
result.push(parseMathBlock(tokens[i]));
|
|
2979
3075
|
i += 1;
|
|
2980
3076
|
break;
|
|
3077
|
+
case "inline":
|
|
3078
|
+
result.push(...parseInlineTokens(token.children || []));
|
|
3079
|
+
i += 1;
|
|
3080
|
+
break;
|
|
2981
3081
|
default:
|
|
2982
3082
|
i += 1;
|
|
2983
3083
|
break;
|