stream-markdown-parser 0.0.20 → 0.0.21

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 CHANGED
@@ -181,8 +181,8 @@ function isTextToken(t) {
181
181
  }
182
182
  function fixLinkToken(tokens) {
183
183
  const tokensAny = tokens;
184
- tokens = fixLinkToken4(fixLinkToken3(tokens));
185
- if (tokens.length < 5) return tokens;
184
+ tokens = fixLinkToken4(fixLinkToken6(tokens));
185
+ if (tokens.length < 5) return fixLinkToken3(tokens);
186
186
  const first = tokens[tokens.length - 5];
187
187
  const firstAny = first;
188
188
  const firstContent = String(firstAny.content ?? "");
@@ -257,7 +257,7 @@ function fixLinkToken3(tokens) {
257
257
  const preLast = tokens[tokens.length - 2];
258
258
  const fixedTokens = [...tokens];
259
259
  if (!last) return tokens;
260
- if (last.type !== "text" || !last.content?.startsWith(")")) return tokens;
260
+ if (last.type !== "text" || !last.content?.startsWith(")")) return fixLinkToken5(tokens);
261
261
  if (preLast.type !== "link_close") return tokens;
262
262
  if (isTextToken(tokens[tokens.length - 5]) && String(tokens[tokens.length - 5].content ?? "").endsWith("(")) {
263
263
  const a = tokensAny[tokens.length - 5];
@@ -316,6 +316,152 @@ function fixLinkToken4(tokens) {
316
316
  }
317
317
  return fixedTokens;
318
318
  }
319
+ function fixLinkToken5(tokens) {
320
+ const firstToken = tokens[0];
321
+ const nextToken = tokens[1];
322
+ if (firstToken.type === "text" && firstToken.content?.endsWith("(") && nextToken.type === "link_open") {
323
+ const linkText = firstToken.content.match(/\[([^\]]+)\]/)?.[1] || "";
324
+ const href = tokens[2]?.content || "";
325
+ const newTokens = [{
326
+ type: "link",
327
+ loading: true,
328
+ href,
329
+ title: "",
330
+ text: linkText,
331
+ children: [{
332
+ type: "text",
333
+ content: linkText,
334
+ raw: linkText
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({
353
+ type: "text",
354
+ content: beforeText,
355
+ raw: beforeText
356
+ });
357
+ const text = match[1];
358
+ const type = emphasisMatch[1].length;
359
+ if (type === 1) replacerTokens.push({
360
+ type: "em_open",
361
+ tag: "em",
362
+ nesting: 1
363
+ });
364
+ else if (type === 2) replacerTokens.push({
365
+ type: "strong_open",
366
+ tag: "strong",
367
+ nesting: 1
368
+ });
369
+ else if (type === 3) {
370
+ replacerTokens.push({
371
+ type: "strong_open",
372
+ tag: "strong",
373
+ nesting: 1
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 = "";
385
+ }
386
+ 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
+ type: "em_close",
401
+ tag: "em",
402
+ nesting: -1
403
+ });
404
+ else if (type === 2) replacerTokens.push({
405
+ type: "strong_close",
406
+ tag: "strong",
407
+ nesting: -1
408
+ });
409
+ else if (type === 3) {
410
+ replacerTokens.push({
411
+ type: "em_close",
412
+ tag: "em",
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({
432
+ type: "text",
433
+ content: beforeText,
434
+ raw: beforeText
435
+ });
436
+ const text = match[1];
437
+ replacerTokens.push(...[{
438
+ type: "link",
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
+ }
460
+ i -= replacerTokens.length - 1;
461
+ }
462
+ }
463
+ return tokens;
464
+ }
319
465
 
320
466
  //#endregion
321
467
  //#region src/plugins/fixListItem.ts