Skip to content

Commit b7ed5fb

Browse files
committed
fix: skip extra newline when merging Adjacent Markdown list items
1 parent 4031f7b commit b7ed5fb

2 files changed

Lines changed: 112 additions & 2 deletions

File tree

src/index.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,8 +801,24 @@ export function mergeAgentsFiles(agentsFiles: string[]): string {
801801
for (const [, section] of Object.entries(allSections)) {
802802
result.push(`${'#'.repeat(section.level)} ${section.title}`);
803803
result.push('');
804-
for (const content of section.contents) {
804+
for (let i = 0; i < section.contents.length; i++) {
805+
const content = section.contents[i];
806+
const nextContent = section.contents[i + 1];
807+
805808
result.push(content);
809+
810+
if (nextContent) {
811+
const currentLines = content.split('\n');
812+
const lastLine = currentLines[currentLines.length - 1];
813+
const nextFirstLine = nextContent.split('\n')[0];
814+
815+
// If both blocks are part of an unordered list (starting with '- '),
816+
// skip the newline to merge them.
817+
if (lastLine.startsWith('- ') && nextFirstLine.startsWith('- ')) {
818+
continue;
819+
}
820+
}
821+
806822
result.push('');
807823
}
808824
}

test/agents.test.ts

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'node:fs';
22
import path from 'node:path';
33
import { fileURLToPath } from 'node:url';
44
import { assert, beforeEach, expect, test } from '@rstest/core';
5-
import { create } from '../src';
5+
import { create, mergeAgentsFiles } from '../src';
66

77
const __dirname = path.dirname(fileURLToPath(import.meta.url));
88
const fixturesDir = path.join(__dirname, 'fixtures', 'agents-md');
@@ -233,3 +233,97 @@ test('should merge top-level sections from AGENTS.md files', async () => {
233233
"
234234
`);
235235
});
236+
237+
test('mergeAgentsFiles should handle unordered lists without extra newline', () => {
238+
const file1 = `
239+
## Tools
240+
241+
- item 1
242+
- item 2
243+
`;
244+
const file2 = `
245+
## Tools
246+
247+
- item 3
248+
`;
249+
250+
const merged = mergeAgentsFiles([file1, file2]);
251+
252+
expect(merged).toMatchInlineSnapshot(`
253+
"## Tools
254+
255+
- item 1
256+
- item 2
257+
- item 3"
258+
`);
259+
});
260+
261+
test('mergeAgentsFiles should still add newline for non-list content', () => {
262+
const file1 = `
263+
## Tools
264+
265+
Some intro text.
266+
`;
267+
const file2 = `
268+
## Tools
269+
270+
More text.
271+
`;
272+
273+
const merged = mergeAgentsFiles([file1, file2]);
274+
275+
expect(merged).toMatchInlineSnapshot(`
276+
"## Tools
277+
278+
Some intro text.
279+
280+
More text."
281+
`);
282+
});
283+
284+
test('mergeAgentsFiles should not skip newline for horizontal rules (---)', () => {
285+
const file1 = `
286+
## Tools
287+
288+
---
289+
`;
290+
const file2 = `
291+
## Tools
292+
293+
- item 1
294+
`;
295+
296+
const merged = mergeAgentsFiles([file1, file2]);
297+
298+
expect(merged).toMatchInlineSnapshot(`
299+
"## Tools
300+
301+
---
302+
303+
- item 1"
304+
`);
305+
});
306+
307+
test('mergeAgentsFiles should add newline when paragraph is followed by list', () => {
308+
const file1 = `
309+
## Tools
310+
311+
Some intro text.
312+
`;
313+
const file2 = `
314+
## Tools
315+
316+
- item 1
317+
`;
318+
319+
const merged = mergeAgentsFiles([file1, file2]);
320+
321+
// Revised requirement: Paragraph followed by list SHOULD have a newline.
322+
expect(merged).toMatchInlineSnapshot(`
323+
"## Tools
324+
325+
Some intro text.
326+
327+
- item 1"
328+
`);
329+
});

0 commit comments

Comments
 (0)