Skip to content

Commit 9365ff3

Browse files
committed
fix: preserve list ordering
1 parent 7a2a528 commit 9365ff3

3 files changed

Lines changed: 60 additions & 7 deletions

File tree

src/splitter.test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,32 @@ End of list.`;
436436
expect(chunks[4]).toBe('End of list.');
437437
});
438438

439+
it('should preserve ordered list numbering when splitting', () => {
440+
const splitter = chunkdown({
441+
chunkSize: 50,
442+
maxOverflowRatio: 1.0,
443+
});
444+
const text = `Instructions:
445+
446+
1. First step with some content
447+
2. Second step with some content
448+
3. Third step with some content
449+
4. Fourth step with some content
450+
5. Fifth step with some content
451+
6. Sixth step with some content
452+
453+
End of instructions.`;
454+
const chunks = splitter.splitText(text);
455+
456+
// Find the chunks containing ordered list items
457+
const listChunks = chunks.filter((chunk) => /^\d+\./.test(chunk.trim()));
458+
459+
expect(listChunks.length).toBe(6);
460+
for (let i = 1; i < listChunks.length; i++) {
461+
expect(listChunks[i]).toMatch(new RegExp(`^[${i + 1}]\.`));
462+
}
463+
});
464+
439465
it('should keep tables together if possible', () => {
440466
const splitter = chunkdown({
441467
chunkSize: 50,
@@ -1077,7 +1103,7 @@ Here's a sentence with a footnote[^1].
10771103
// Code block in list item
10781104
const example = true;
10791105
\`\`\`",
1080-
"1. Third item with blockquote:
1106+
"3. Third item with blockquote:
10811107
> This is a blockquote inside a list item
10821108
> with multiple lines",
10831109
"### Table with Complex Content

src/splitter.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import type { Blockquote, List, Node, Root, RootContent, Table } from 'mdast';
1+
import type {
2+
Blockquote,
3+
List,
4+
ListItem,
5+
Node,
6+
Root,
7+
RootContent,
8+
Table,
9+
} from 'mdast';
210
import {
311
createHierarchicalAST,
412
flattenHierarchicalAST,
@@ -417,10 +425,21 @@ export const chunkdown = (options: ChunkdownOptions) => {
417425
// Helper to flush accumulated items
418426
const flushCurrentItems = () => {
419427
if (currentItems.length > 0) {
420-
const containerMarkdown = toMarkdown({
428+
const currentContainer = {
421429
...container,
422430
children: currentItems,
423-
} as TContainer);
431+
};
432+
433+
// For ordered lists, calculate the correct start number
434+
if (container.type === 'list' && container.ordered) {
435+
const originalStart = container.start || 1;
436+
const firstItemIndex = container.children.indexOf(
437+
currentItems[0] as ListItem,
438+
);
439+
(currentContainer as List).start = originalStart + firstItemIndex;
440+
}
441+
442+
const containerMarkdown = toMarkdown(currentContainer);
424443
chunks.push(containerMarkdown.trim());
425444
currentItems = [];
426445
currentSize = 0;

www/src/components/ASTVisualizer.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,21 @@ function TreeNode({
6767
}
6868
}, [collapseAll, depth]);
6969

70+
const extractTextFromNode = (node: Node): string => {
71+
if ('value' in node && typeof node.value === 'string') {
72+
return node.value;
73+
}
74+
if ('children' in node && Array.isArray(node.children)) {
75+
return node.children.map(extractTextFromNode).join('');
76+
}
77+
return '';
78+
};
79+
7080
const getNodeLabel = () => {
7181
if (isSection(node)) {
7282
// Handle Section node
7383
const headingText =
74-
node.heading?.children
75-
?.map((child) => ('value' in child ? child.value : ''))
76-
.join('') || '';
84+
node.heading?.children?.map(extractTextFromNode).join('') || '';
7785
const truncated =
7886
headingText.length > 40
7987
? `${headingText.substring(0, 40)}...`

0 commit comments

Comments
 (0)