stream-markdown-parser 0.0.84 → 0.0.85

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
@@ -11780,6 +11780,16 @@ function applyMath(md, mathOpts) {
11780
11780
  const s = state;
11781
11781
  const strict = !!mathOpts?.strictDelimiters;
11782
11782
  const allowLoading = !s?.env?.__markstreamFinal;
11783
+ const preserveSpacesBeforeLineBreak = (src, start) => {
11784
+ let end = start;
11785
+ while (end < src.length && (src[end] === " " || src[end] === " ")) end++;
11786
+ if (end === start) return start;
11787
+ if (!(src[end] === "\n" || src[end] === "\r" && src[end + 1] === "\n")) return start;
11788
+ const text$1 = src.slice(start, end);
11789
+ const token = s.push("text", "", 0);
11790
+ token.content = text$1;
11791
+ return end;
11792
+ };
11783
11793
  if (/^\*[^*]+/.test(s.src)) return false;
11784
11794
  const delimiters = [
11785
11795
  ["$$", "$$"],
@@ -12062,11 +12072,11 @@ function applyMath(md, mathOpts) {
12062
12072
  token.loading = false;
12063
12073
  const isBeforeClose = src.slice(endIdx + close.length).startsWith(strongMarker);
12064
12074
  if (isBeforeClose) s.push("strong_close", "", 0);
12065
- s.pos = endIdx + close.length;
12075
+ s.pos = preserveSpacesBeforeLineBreak(src, endIdx + close.length);
12066
12076
  searchPos = s.pos;
12067
12077
  preMathPos = searchPos;
12068
12078
  if (!isBeforeClose) s.push("strong_close", "", 0);
12069
- continue;
12079
+ return true;
12070
12080
  } else {
12071
12081
  const token = s.push("math_inline", "math", 0);
12072
12082
  token.content = normalizeStandaloneBackslashT(content, mathOpts);
@@ -12075,9 +12085,10 @@ function applyMath(md, mathOpts) {
12075
12085
  token.loading = false;
12076
12086
  }
12077
12087
  }
12078
- searchPos = endIdx + close.length;
12088
+ searchPos = preserveSpacesBeforeLineBreak(src, endIdx + close.length);
12079
12089
  preMathPos = searchPos;
12080
12090
  s.pos = searchPos;
12091
+ return true;
12081
12092
  }
12082
12093
  if (foundAny) {
12083
12094
  if (!silent) {
@@ -12539,6 +12550,40 @@ function normalizeLinkAttrs$1(attrs, href, title) {
12539
12550
  function stringifyTokens(tokens) {
12540
12551
  return tokens.map(tokenToRaw).join("");
12541
12552
  }
12553
+ function normalizeStandardHtmlChildren(children) {
12554
+ const normalized = [];
12555
+ const pushText = (rawText) => {
12556
+ const text$1 = String(rawText ?? "");
12557
+ if (!text$1) return;
12558
+ const last = normalized[normalized.length - 1];
12559
+ if (last?.type === "text") {
12560
+ last.content = String(last.content ?? "") + text$1;
12561
+ last.raw = String(last.raw ?? "") + text$1;
12562
+ return;
12563
+ }
12564
+ normalized.push({
12565
+ type: "text",
12566
+ content: text$1,
12567
+ raw: text$1
12568
+ });
12569
+ };
12570
+ for (const child of children) {
12571
+ if (!child) continue;
12572
+ if (child.type === "reference" || child.type === "footnote_reference") {
12573
+ pushText(String(child.raw ?? ""));
12574
+ continue;
12575
+ }
12576
+ if (Array.isArray(child.children)) {
12577
+ normalized.push({
12578
+ ...child,
12579
+ children: normalizeStandardHtmlChildren(child.children ?? [])
12580
+ });
12581
+ continue;
12582
+ }
12583
+ normalized.push(child);
12584
+ }
12585
+ return normalized;
12586
+ }
12542
12587
  function findMatchingClosing(tokens, startIndex, tag) {
12543
12588
  let depth = 0;
12544
12589
  for (let idx = startIndex; idx < tokens.length; idx++) {
@@ -12623,9 +12668,9 @@ function parseHtmlInlineCodeToken(token, tokens, i, parseInlineTokens$1, raw, pP
12623
12668
  const titleAttr = getAttrValue$1(attrs$1, "title");
12624
12669
  const title = titleAttr == null ? null : String(titleAttr);
12625
12670
  const normalizedAttrs = normalizeLinkAttrs$1(attrs$1, href, title);
12626
- const children$1 = innerTokens.length ? parseInlineTokens$1(innerTokens, raw, pPreToken, options) : [];
12671
+ const normalizedChildren$1 = normalizeStandardHtmlChildren(innerTokens.length ? parseInlineTokens$1(innerTokens, raw, pPreToken, options) : []);
12627
12672
  const textContent = innerTokens.length ? stringifyTokens(innerTokens) : href || "";
12628
- if (!children$1.length && textContent) children$1.push({
12673
+ if (!normalizedChildren$1.length && textContent) normalizedChildren$1.push({
12629
12674
  type: "text",
12630
12675
  content: textContent,
12631
12676
  raw: textContent
@@ -12636,7 +12681,7 @@ function parseHtmlInlineCodeToken(token, tokens, i, parseInlineTokens$1, raw, pP
12636
12681
  title,
12637
12682
  text: textContent,
12638
12683
  attrs: normalizedAttrs,
12639
- children: children$1,
12684
+ children: normalizedChildren$1,
12640
12685
  loading: !fragment$1.closed,
12641
12686
  raw: fragment$1.html || code$1
12642
12687
  }, fragment$1.nextIndex];
@@ -12652,10 +12697,10 @@ function parseHtmlInlineCodeToken(token, tokens, i, parseInlineTokens$1, raw, pP
12652
12697
  const fragment = collectHtmlFragment(tokens, i, tag);
12653
12698
  if (tag === "p" || tag === "div") return [{
12654
12699
  type: "paragraph",
12655
- children: fragment.innerTokens.length ? parseInlineTokens$1(fragment.innerTokens, raw, pPreToken, options) : [],
12700
+ children: normalizeStandardHtmlChildren(fragment.innerTokens.length ? parseInlineTokens$1(fragment.innerTokens, raw, pPreToken, options) : []),
12656
12701
  raw: fragment.html
12657
12702
  }, fragment.nextIndex];
12658
- const children = fragment.innerTokens.length ? parseInlineTokens$1(fragment.innerTokens, raw, pPreToken, options) : [];
12703
+ const normalizedChildren = normalizeStandardHtmlChildren(fragment.innerTokens.length ? parseInlineTokens$1(fragment.innerTokens, raw, pPreToken, options) : []);
12659
12704
  let content = fragment.html || code$1;
12660
12705
  let loading = !fragment.closed;
12661
12706
  let autoClosed = false;
@@ -12688,7 +12733,7 @@ function parseHtmlInlineCodeToken(token, tokens, i, parseInlineTokens$1, raw, pP
12688
12733
  tag,
12689
12734
  attrs,
12690
12735
  content,
12691
- children,
12736
+ children: normalizedChildren,
12692
12737
  raw: content,
12693
12738
  loading,
12694
12739
  autoClosed
@@ -12980,7 +13025,7 @@ function parseTextToken(token) {
12980
13025
  const STRONG_PAIR_RE = /\*\*([\s\S]*?)\*\*/;
12981
13026
  const STRIKETHROUGH_RE = /[^~]*~{2,}[^~]+/;
12982
13027
  const HAS_STRONG_RE = /\*\*/;
12983
- const INLINE_REPARSE_MARKER_RE = /\*\*\*|___|\*\*|__|\*|_|~~/;
13028
+ const INLINE_REPARSE_MARKER_RE = /[[_*^~]/;
12984
13029
  const ESCAPED_PUNCTUATION_RE = /\\([\\()[\]`$|*_\-!])/g;
12985
13030
  const ESCAPABLE_PUNCTUATION = new Set([
12986
13031
  "\\",
@@ -13442,10 +13487,10 @@ function parseInlineTokens(tokens, raw, pPreToken, options) {
13442
13487
  }
13443
13488
  function tryReparseCollapsedInlineText(rawContent) {
13444
13489
  const md = options?.__markdownIt;
13445
- if (!md || !options?.final) return null;
13490
+ if (!md) return null;
13446
13491
  if (tokens.length <= 1 || !tokens.some((token) => token?.type === "math_inline")) return null;
13447
13492
  if (!INLINE_REPARSE_MARKER_RE.test(rawContent)) return null;
13448
- const reparsed = md.parseInline(rawContent, { __markstreamFinal: true });
13493
+ const reparsed = md.parseInline(rawContent, { __markstreamFinal: !!options?.final });
13449
13494
  if (!Array.isArray(reparsed) || reparsed.length === 0) return null;
13450
13495
  const children = (reparsed.find((token) => token?.type === "inline")?.children ?? []).filter((child) => !(child?.type === "text" && String(child.content ?? "") === ""));
13451
13496
  if (!children.length) return null;
@@ -16744,6 +16789,7 @@ function getMarkdown(msgId = `editor-${Date.now()}`, options = {}) {
16744
16789
  const token = s.push("reference", "span", 0);
16745
16790
  token.content = id;
16746
16791
  token.markup = match[0];
16792
+ token.raw = match[0];
16747
16793
  }
16748
16794
  s.pos += match[0].length;
16749
16795
  return true;