From 0b049ee35eb002935cd5766d0a1a60e9a5dee2ab Mon Sep 17 00:00:00 2001 From: Marve10s Date: Mon, 23 Nov 2026 21:31:09 +0300 Subject: [PATCH 1/4] fix(shared): Markdown previews keep a long
or in one piece --- packages/shared/src/markdownChunks.test.ts | 42 +++++++++++++++++++++- packages/shared/src/markdownChunks.ts | 32 +++++++++++++++-- 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/packages/shared/src/markdownChunks.test.ts b/packages/shared/src/markdownChunks.test.ts index a5cf2a8017b3..123dd666eda8 100644 --- a/packages/shared/src/markdownChunks.test.ts +++ b/packages/shared/src/markdownChunks.test.ts @@ -76,9 +76,49 @@ describe("splitMarkdown", () => { "# literal", "", "", + "
", + "Answer", + "", + "Inside details", + "", + "
", + "", + "
", + "", + "```ts", + "inside a cell", + "```", + "", + "
", + "", + "```html", + "
", + "```", + "", + "after", + ].join("\n"); + expect(starts(text)).toEqual([ + "', + "", + "Inside details", + "", + "
", + "", "after", ].join("\n"); - expect(starts(text)).toEqual(["/g; +// A wrapper whose end tag never comes must not hold the rest of a large file in one piece. +const WRAPPED_MAX_CHARS = 32 * 1024; +// After a blank line, an HTML block can start a piece as a paragraph can. +const HTML_OPEN = /^<[A-Za-z]/; + +function openedWrappers(line: string): number { + let opened = 0; + for (const [, closing] of line.replaceAll(HTML_COMMENT, "").matchAll(WRAPPER_TAG)) { + opened += closing ? -1 : 1; + } + return opened; +} /** Returns how to recognize the end of a block that `line` opens, or null for none. */ function blockCloser(line: string): ((line: string) => boolean) | null { @@ -44,7 +62,7 @@ function blockCloser(line: string): ((line: string) => boolean) | null { return close.test(line.slice(opener.length)) ? null : (next) => close.test(next); } // Other HTML blocks (CommonMark types 6 and 7) run until a blank line. - return HTML_BLOCK_START.test(line) ? (next) => next.trim() === "" : null; + return HTML_BLOCK_START.test(line) ? closesAtBlankLine : null; } export interface MarkdownChunk { @@ -86,6 +104,8 @@ export function splitMarkdown(text: string, targetChars = CHUNK_TARGET_CHARS): S let afterDefinition = false; let afterHeading = false; let definitionChars = 0; + let openWrappers = 0; + let wrapperStart = 0; const flush = () => { chunks.push({ text: lines.join("\n"), lines: lines.length }); lines = []; @@ -98,10 +118,14 @@ export function splitMarkdown(text: string, targetChars = CHUNK_TARGET_CHARS): S let seen = 0; closes = (next) => FRONTMATTER_CLOSE.test(next) || ++seen >= FRONTMATTER_MAX_LINES; } else { + if (chars - wrapperStart >= WRAPPED_MAX_CHARS) openWrappers = 0; const cut = chars >= targetChars && + openWrappers === 0 && (ATX_HEADING.test(line) || - (afterBlankLine && BLOCK_START.test(line) && !ORDERED_ITEM.test(line))); + (afterBlankLine && + (BLOCK_START.test(line) || HTML_OPEN.test(line)) && + !ORDERED_ITEM.test(line))); if (cut) flush(); closes = blockCloser(line); // A definition cannot interrupt a paragraph, so it follows a blank line, a heading or @@ -114,6 +138,10 @@ export function splitMarkdown(text: string, targetChars = CHUNK_TARGET_CHARS): S definitionChars += line.length + 1; } } + if (closes === closesAtBlankLine) { + if (openWrappers === 0) wrapperStart = chars; + openWrappers = Math.max(0, openWrappers + openedWrappers(line)); + } first = false; lines.push(line); chars += line.length + 1; From bf0521d0fb81a2bf93893b7f1ddb599b46deb6bc Mon Sep 17 00:00:00 2001 From: Marve10s Date: Mon, 23 Nov 2026 21:38:11 +0300 Subject: [PATCH 2/4] fix(shared): only a
or starts a Markdown piece after a blank line --- packages/shared/src/markdownChunks.test.ts | 18 +++++++++--------- packages/shared/src/markdownChunks.ts | 7 ++++--- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/packages/shared/src/markdownChunks.test.ts b/packages/shared/src/markdownChunks.test.ts index 123dd666eda8..3fff02801537 100644 --- a/packages/shared/src/markdownChunks.test.ts +++ b/packages/shared/src/markdownChunks.test.ts @@ -91,21 +91,21 @@ describe("splitMarkdown", () => { "", "
", "", + '
    ', + "", + "
  1. one
  2. ", + "", + "
  3. two
  4. ", + "", + "
", + "", "```html", "
", "```", "", "after", ].join("\n"); - expect(starts(text)).toEqual([ - "', diff --git a/packages/shared/src/markdownChunks.ts b/packages/shared/src/markdownChunks.ts index f38b9fb4b2ee..0365a6c0ce47 100644 --- a/packages/shared/src/markdownChunks.ts +++ b/packages/shared/src/markdownChunks.ts @@ -31,8 +31,9 @@ const WRAPPER_TAG = /<(\/?)(?:details|table)(?=[\s/>]|$)/gi; const HTML_COMMENT = //g; // A wrapper whose end tag never comes must not hold the rest of a large file in one piece. const WRAPPED_MAX_CHARS = 32 * 1024; -// After a blank line, an HTML block can start a piece as a paragraph can. -const HTML_OPEN = /^<[A-Za-z]/; +// After a blank line, a
or can start a piece as a paragraph can. Other tags +// may be children of a raw HTML container, such as
  • in
      , so they never start one. +const WRAPPER_OPEN = /^<(?:details|table)(?=[\s/>]|$)/i; function openedWrappers(line: string): number { let opened = 0; @@ -124,7 +125,7 @@ export function splitMarkdown(text: string, targetChars = CHUNK_TARGET_CHARS): S openWrappers === 0 && (ATX_HEADING.test(line) || (afterBlankLine && - (BLOCK_START.test(line) || HTML_OPEN.test(line)) && + (BLOCK_START.test(line) || WRAPPER_OPEN.test(line)) && !ORDERED_ITEM.test(line))); if (cut) flush(); closes = blockCloser(line); From e039d245f4884082a3df98ae41ae480d848511d6 Mon Sep 17 00:00:00 2001 From: Marve10s Date: Mon, 23 Nov 2026 21:46:49 +0300 Subject: [PATCH 3/4] fix(shared): Markdown pieces never start at a new HTML cut point --- packages/shared/src/markdownChunks.test.ts | 2 +- packages/shared/src/markdownChunks.ts | 16 ++++------------ 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/packages/shared/src/markdownChunks.test.ts b/packages/shared/src/markdownChunks.test.ts index 3fff02801537..8b4801ad7594 100644 --- a/packages/shared/src/markdownChunks.test.ts +++ b/packages/shared/src/markdownChunks.test.ts @@ -105,7 +105,7 @@ describe("splitMarkdown", () => { "", "after", ].join("\n"); - expect(starts(text)).toEqual(["', diff --git a/packages/shared/src/markdownChunks.ts b/packages/shared/src/markdownChunks.ts index 0365a6c0ce47..14ce2b364278 100644 --- a/packages/shared/src/markdownChunks.ts +++ b/packages/shared/src/markdownChunks.ts @@ -29,11 +29,8 @@ const closesAtBlankLine = (next: string) => next.trim() === ""; // each HTML block, so a cut waits until the ones an HTML block opened are closed. const WRAPPER_TAG = /<(\/?)(?:details|table)(?=[\s/>]|$)/gi; const HTML_COMMENT = //g; -// A wrapper whose end tag never comes must not hold the rest of a large file in one piece. +// An end tag that never comes, or back-to-back wrappers, must not make one huge piece. const WRAPPED_MAX_CHARS = 32 * 1024; -// After a blank line, a
      or
  • can start a piece as a paragraph can. Other tags -// may be children of a raw HTML container, such as
  • in
      , so they never start one. -const WRAPPER_OPEN = /^<(?:details|table)(?=[\s/>]|$)/i; function openedWrappers(line: string): number { let opened = 0; @@ -106,7 +103,6 @@ export function splitMarkdown(text: string, targetChars = CHUNK_TARGET_CHARS): S let afterHeading = false; let definitionChars = 0; let openWrappers = 0; - let wrapperStart = 0; const flush = () => { chunks.push({ text: lines.join("\n"), lines: lines.length }); lines = []; @@ -119,14 +115,12 @@ export function splitMarkdown(text: string, targetChars = CHUNK_TARGET_CHARS): S let seen = 0; closes = (next) => FRONTMATTER_CLOSE.test(next) || ++seen >= FRONTMATTER_MAX_LINES; } else { - if (chars - wrapperStart >= WRAPPED_MAX_CHARS) openWrappers = 0; + if (chars >= WRAPPED_MAX_CHARS) openWrappers = 0; const cut = chars >= targetChars && openWrappers === 0 && (ATX_HEADING.test(line) || - (afterBlankLine && - (BLOCK_START.test(line) || WRAPPER_OPEN.test(line)) && - !ORDERED_ITEM.test(line))); + (afterBlankLine && BLOCK_START.test(line) && !ORDERED_ITEM.test(line))); if (cut) flush(); closes = blockCloser(line); // A definition cannot interrupt a paragraph, so it follows a blank line, a heading or @@ -139,10 +133,8 @@ export function splitMarkdown(text: string, targetChars = CHUNK_TARGET_CHARS): S definitionChars += line.length + 1; } } - if (closes === closesAtBlankLine) { - if (openWrappers === 0) wrapperStart = chars; + if (closes === closesAtBlankLine) openWrappers = Math.max(0, openWrappers + openedWrappers(line)); - } first = false; lines.push(line); chars += line.length + 1; From 0387c748c2e822597833f6680a1f9b84c2d0de39 Mon Sep 17 00:00:00 2001 From: Marve10s Date: Mon, 23 Nov 2026 21:54:32 +0300 Subject: [PATCH 4/4] fix(shared): strip HTML comments from a wrapper line in linear time --- packages/shared/src/markdownChunks.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/shared/src/markdownChunks.ts b/packages/shared/src/markdownChunks.ts index 14ce2b364278..e714e8ed5ed7 100644 --- a/packages/shared/src/markdownChunks.ts +++ b/packages/shared/src/markdownChunks.ts @@ -28,13 +28,24 @@ const closesAtBlankLine = (next: string) => next.trim() === ""; // A
      or
  • wraps the Markdown between its tags, across the blank lines that end // each HTML block, so a cut waits until the ones an HTML block opened are closed. const WRAPPER_TAG = /<(\/?)(?:details|table)(?=[\s/>]|$)/gi; -const HTML_COMMENT = //g; // An end tag that never comes, or back-to-back wrappers, must not make one huge piece. const WRAPPED_MAX_CHARS = 32 * 1024; +function withoutComments(line: string): string { + let kept = ""; + let at = 0; + for (let open = line.indexOf("", open + 4); + if (close === -1) break; + kept += line.slice(at, open); + at = close + 3; + } + return kept + line.slice(at); +} + function openedWrappers(line: string): number { let opened = 0; - for (const [, closing] of line.replaceAll(HTML_COMMENT, "").matchAll(WRAPPER_TAG)) { + for (const [, closing] of withoutComments(line).matchAll(WRAPPER_TAG)) { opened += closing ? -1 : 1; } return opened;