diff --git a/src/lib/editor/markdown.ts b/src/lib/editor/markdown.ts index abcbe4c..4a003ce 100644 --- a/src/lib/editor/markdown.ts +++ b/src/lib/editor/markdown.ts @@ -82,7 +82,9 @@ export function joinListItemBlocks(blocks: readonly ListItemBlock[]): string { const parts: string[] = []; blocks.forEach((block, index) => { if (block.isList) { - parts.push(block.text.replace(/\n$/, '').split('\n').map((line) => ' ' + line).join('\n')); + // A serialized list ends with a blank line; left in, it became an indented blank line here + // and an extra empty line after the outer list. + parts.push(block.text.replace(/\n+$/, '').split('\n').map((line) => (line ? ' ' + line : line)).join('\n')); } else if (index === 0) { parts.push(block.text); } else { diff --git a/tests/markdown-roundtrip.test.mjs b/tests/markdown-roundtrip.test.mjs index f56a919..7131299 100644 --- a/tests/markdown-roundtrip.test.mjs +++ b/tests/markdown-roundtrip.test.mjs @@ -521,6 +521,10 @@ const item = (text, isList = false) => ({ text, isList }); test('blocks after a list item\'s first paragraph stay inside the item, separated by a blank line', () => { assert.equal(joinListItemBlocks([item('only')]), 'only\n'); + // A nested list arrives ending in a blank line; it must not leave an indented blank line behind (issue #277). + const list = (text) => ({ text, isList: true }); + assert.equal(joinListItemBlocks([item('parent'), list('- a\n- b\n\n')]), 'parent\n - a\n - b\n'); + assert.equal(joinListItemBlocks([item('parent'), list('- a\n - deep\n\n')]), 'parent\n - a\n - deep\n'); assert.equal(joinListItemBlocks([item('first'), item('second\n')]), 'first\n\n second\n'); assert.equal( joinListItemBlocks([item('step'), item('```sh\nrun\n\n\nagain\n```\n'), item('after\n')]),