Skip to content

Commit fa1d5f6

Browse files
committed
fix(markdown): normalize non-finite render chunk limits
1 parent 1e5ccd1 commit fa1d5f6

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

‎src/markdown/render-aware-chunking.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,17 @@ describe("renderMarkdownIRChunksWithinLimit", () => {
7070

7171
expect(chunks.map((chunk) => chunk.source.text)).toEqual(["README.md", "<"]);
7272
});
73+
74+
it("normalizes non-finite limits before chunking", () => {
75+
const ir = markdownToIR("abc");
76+
const chunks = renderMarkdownIRChunksWithinLimit({
77+
ir,
78+
limit: Number.NaN,
79+
renderChunk: renderEscapedHtml,
80+
measureRendered: (rendered) => rendered.length,
81+
});
82+
83+
expect(chunks.map((chunk) => chunk.source.text)).toEqual(["a", "b", "c"]);
84+
expect(chunks.every((chunk) => chunk.rendered.length <= 1)).toBe(true);
85+
});
7386
});

‎src/markdown/render-aware-chunking.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { resolveIntegerOption } from "../shared/number-coercion.js";
12
import {
23
chunkMarkdownIR,
34
sliceMarkdownIR,
@@ -30,7 +31,7 @@ export function renderMarkdownIRChunksWithinLimit<TRendered>(
3031
return [];
3132
}
3233

33-
const normalizedLimit = Math.max(1, Math.floor(options.limit));
34+
const normalizedLimit = resolveIntegerOption(options.limit, 1, { min: 1 });
3435
const pending = chunkMarkdownIR(options.ir, normalizedLimit);
3536
const finalized: MarkdownIR[] = [];
3637

@@ -196,7 +197,7 @@ function splitMarkdownIRPreserveWhitespace(ir: MarkdownIR, limit: number): Markd
196197
return [];
197198
}
198199

199-
const normalizedLimit = Math.max(1, Math.floor(limit));
200+
const normalizedLimit = resolveIntegerOption(limit, 1, { min: 1 });
200201
if (normalizedLimit <= 0 || ir.text.length <= normalizedLimit) {
201202
return [ir];
202203
}

0 commit comments

Comments
 (0)