fix(markdown): normalize non-finite render chunk limits

This commit is contained in:
Peter Steinberger
2026-05-29 01:25:26 -04:00
parent 1e5ccd1ce8
commit fa1d5f6584
2 changed files with 16 additions and 2 deletions

View File

@@ -70,4 +70,17 @@ describe("renderMarkdownIRChunksWithinLimit", () => {
expect(chunks.map((chunk) => chunk.source.text)).toEqual(["README.md", "<"]);
});
it("normalizes non-finite limits before chunking", () => {
const ir = markdownToIR("abc");
const chunks = renderMarkdownIRChunksWithinLimit({
ir,
limit: Number.NaN,
renderChunk: renderEscapedHtml,
measureRendered: (rendered) => rendered.length,
});
expect(chunks.map((chunk) => chunk.source.text)).toEqual(["a", "b", "c"]);
expect(chunks.every((chunk) => chunk.rendered.length <= 1)).toBe(true);
});
});

View File

@@ -1,3 +1,4 @@
import { resolveIntegerOption } from "../shared/number-coercion.js";
import {
chunkMarkdownIR,
sliceMarkdownIR,
@@ -30,7 +31,7 @@ export function renderMarkdownIRChunksWithinLimit<TRendered>(
return [];
}
const normalizedLimit = Math.max(1, Math.floor(options.limit));
const normalizedLimit = resolveIntegerOption(options.limit, 1, { min: 1 });
const pending = chunkMarkdownIR(options.ir, normalizedLimit);
const finalized: MarkdownIR[] = [];
@@ -196,7 +197,7 @@ function splitMarkdownIRPreserveWhitespace(ir: MarkdownIR, limit: number): Markd
return [];
}
const normalizedLimit = Math.max(1, Math.floor(limit));
const normalizedLimit = resolveIntegerOption(limit, 1, { min: 1 });
if (normalizedLimit <= 0 || ir.text.length <= normalizedLimit) {
return [ir];
}