|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { parse } from '../src/parse' |
| 3 | + |
| 4 | +// Regression tests for https://github.com/comarkdown/comark/issues/322 |
| 5 | +// |
| 6 | +// `comark_block_yaml`, `comark_block_shorthand`, and `comark_block_slots` each |
| 7 | +// narrowed `state.lineMax` after matching and never restored it, unlike |
| 8 | +// `comark_block`. Since `skipEmptyLines` is bounded by `state.lineMax`, a |
| 9 | +// *second* consecutive blank line inside a nested component fell through it |
| 10 | +// unskipped, then failed the tokenizer's indent check: `comark_block_yaml`/ |
| 11 | +// `comark_block_shorthand` broke out of the loop and dropped every later |
| 12 | +// sibling; `comark_block_slots` emitted a spurious empty paragraph instead. |
| 13 | +// |
| 14 | +// The fixes restore (or stop touching) `state.lineMax` so a blank-line run of |
| 15 | +// any length is treated as a single separator, as in CommonMark. |
| 16 | +describe('nested component with a run of blank lines between siblings', () => { |
| 17 | + describe('YAML props fence (`---`/`---`)', () => { |
| 18 | + const build = (blanks: number) => |
| 19 | + `::outer\n ::mid\n ---\n columns: 3\n ---\n${['A', 'B', 'C'] |
| 20 | + .map((name) => ` ::${name}\n ---\n display: "flex"\n ---\n ::`) |
| 21 | + .join('\n' + '\n'.repeat(blanks))}\n ::\n::` |
| 22 | + |
| 23 | + const expected = [ |
| 24 | + [ |
| 25 | + 'outer', |
| 26 | + {}, |
| 27 | + ['mid', { columns: '3' }, ['a', { display: 'flex' }], ['b', { display: 'flex' }], ['c', { display: 'flex' }]], |
| 28 | + ], |
| 29 | + ] |
| 30 | + |
| 31 | + it.each([1, 2, 3, 7])('keeps every sibling with %i consecutive blank line(s) between them', async (blanks) => { |
| 32 | + const tree = await parse(build(blanks)) |
| 33 | + expect(tree.nodes).toEqual(expected) |
| 34 | + }) |
| 35 | + |
| 36 | + it('still closes the enclosing component at its own marker after a blank run (no over-absorption)', async () => { |
| 37 | + // `mid` must still close at its own `::`, not swallow `after` too. |
| 38 | + const src = '::outer\n ::mid\n ---\n columns: 3\n ---\n ::A\n ::\n\n\n ::\nafter\n::' |
| 39 | + const tree = await parse(src) |
| 40 | + expect(tree.nodes).toEqual([['outer', {}, ['mid', { columns: '3' }, ['a', {}]], ['p', {}, 'after']]]) |
| 41 | + }) |
| 42 | + }) |
| 43 | + |
| 44 | + describe('shorthand block (`:name[content]`)', () => { |
| 45 | + const build = (blanks: number) => |
| 46 | + `::outer\n :leaf1[hi]\n${'\n'.repeat(blanks)} ::leaf2\n ::\n${'\n'.repeat(blanks)} ::leaf3\n ::\n::` |
| 47 | + |
| 48 | + const expected = [['outer', {}, ['leaf1', {}, 'hi'], ['leaf2', {}], ['leaf3', {}]]] |
| 49 | + |
| 50 | + it.each([1, 2, 3, 7])('keeps every sibling with %i consecutive blank line(s) after it', async (blanks) => { |
| 51 | + const tree = await parse(build(blanks)) |
| 52 | + expect(tree.nodes).toEqual(expected) |
| 53 | + }) |
| 54 | + |
| 55 | + it('still closes the enclosing component at its own marker after a blank run (no over-absorption)', async () => { |
| 56 | + const src = '::outer\n :leaf1[hi]\n\n\nafter\n::' |
| 57 | + const tree = await parse(src) |
| 58 | + expect(tree.nodes).toEqual([['outer', {}, ['leaf1', {}, 'hi'], ['p', {}, 'after']]]) |
| 59 | + }) |
| 60 | + }) |
| 61 | + |
| 62 | + describe('template slot (`#slotname`)', () => { |
| 63 | + const build = (blanks: number) => |
| 64 | + `::outer\n #title\n Hello\n ::childA\n ::\n${'\n'.repeat(blanks)} ::childB\n ::\n::` |
| 65 | + |
| 66 | + const expected = [ |
| 67 | + ['outer', {}, ['template', { name: 'title' }, ['p', {}, 'Hello'], ['child-a', {}], ['child-b', {}]]], |
| 68 | + ] |
| 69 | + |
| 70 | + it.each([1, 2, 3, 7])( |
| 71 | + 'keeps every sibling with %i consecutive blank line(s) between them, with no spurious paragraph token', |
| 72 | + async (blanks) => { |
| 73 | + const tree = await parse(build(blanks)) |
| 74 | + expect(tree.nodes).toEqual(expected) |
| 75 | + } |
| 76 | + ) |
| 77 | + |
| 78 | + it('still terminates at the next `#slot` marker regardless of a preceding blank run', async () => { |
| 79 | + const src = '::outer\n #title\n Hello\n\n\n #footer\n Bye\n ::\n::' |
| 80 | + const tree = await parse(src) |
| 81 | + expect(tree.nodes).toEqual([ |
| 82 | + ['outer', {}, ['template', { name: 'title' }, 'Hello'], ['template', { name: 'footer' }, 'Bye']], |
| 83 | + ]) |
| 84 | + }) |
| 85 | + |
| 86 | + it('still terminates at the parent close after a blank run (no over-absorption)', async () => { |
| 87 | + const src = '::outer\n #title\n Hello\n\n\n ::\nafter\n::' |
| 88 | + const tree = await parse(src) |
| 89 | + expect(tree.nodes).toEqual([ |
| 90 | + ['outer', {}, ['template', { name: 'title' }, 'Hello']], |
| 91 | + ['p', {}, 'after'], |
| 92 | + ]) |
| 93 | + }) |
| 94 | + }) |
| 95 | +}) |
0 commit comments