diff --git a/packages/shared/src/markdownChunks.test.ts b/packages/shared/src/markdownChunks.test.ts
index a5cf2a8017b3..8b4801ad7594 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",
+ "```",
+ "",
+ " |
",
+ "",
+ '',
+ "",
+ "- one
",
+ "",
+ "- two
",
+ "",
+ "
",
+ "",
+ "```html",
+ "",
+ "```",
+ "",
+ "after",
+ ].join("\n");
+ expect(starts(text)).toEqual(["',
+ "",
+ "Inside details",
+ "",
+ " ",
+ "",
"after",
].join("\n");
- expect(starts(text)).toEqual(["", 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 withoutComments(line).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 +71,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 +113,7 @@ export function splitMarkdown(text: string, targetChars = CHUNK_TARGET_CHARS): S
let afterDefinition = false;
let afterHeading = false;
let definitionChars = 0;
+ let openWrappers = 0;
const flush = () => {
chunks.push({ text: lines.join("\n"), lines: lines.length });
lines = [];
@@ -98,8 +126,10 @@ 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 >= WRAPPED_MAX_CHARS) openWrappers = 0;
const cut =
chars >= targetChars &&
+ openWrappers === 0 &&
(ATX_HEADING.test(line) ||
(afterBlankLine && BLOCK_START.test(line) && !ORDERED_ITEM.test(line)));
if (cut) flush();
@@ -114,6 +144,8 @@ export function splitMarkdown(text: string, targetChars = CHUNK_TARGET_CHARS): S
definitionChars += line.length + 1;
}
}
+ if (closes === closesAtBlankLine)
+ openWrappers = Math.max(0, openWrappers + openedWrappers(line));
first = false;
lines.push(line);
chars += line.length + 1;