diff --git a/.changeset/reuse-pending-list-marker.md b/.changeset/reuse-pending-list-marker.md new file mode 100644 index 0000000..50d85df --- /dev/null +++ b/.changeset/reuse-pending-list-marker.md @@ -0,0 +1,5 @@ +--- +"@tanstack/markdown": patch +--- + +Reuse pending list-marker matches while parsing list items. diff --git a/src/parser.ts b/src/parser.ts index f0b31ea..345c472 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -216,8 +216,10 @@ function createBlockParser( } function parseList(): ListNode | undefined { - const first = listMarker(current()) - if (!first) return undefined + let markerLine = current() + let markerValue = listMarker(markerLine) + if (!markerValue) return undefined + const first = markerValue const items: ListItemNode[] = [] const ordered = first.ordered @@ -225,7 +227,12 @@ function createBlockParser( let loose = false while (cursor < inputLines.length) { - const marker = listMarker(current()) + const line = current() + if (line !== markerLine) { + markerLine = line + markerValue = listMarker(line) + } + const marker = markerValue if (!marker || marker.marker !== first.marker || marker.indent !== baseIndent) break let firstLine = marker.content @@ -238,7 +245,9 @@ function createBlockParser( while (cursor < inputLines.length) { const line = current() - const nextMarker = listMarker(line) + markerLine = line + markerValue = listMarker(line) + const nextMarker = markerValue if (nextMarker && nextMarker.indent === baseIndent) break if (isBlank(line)) { let nextIndex = cursor @@ -246,7 +255,9 @@ function createBlockParser( const following = inputLines[nextIndex] if (following === undefined) break - const followingMarker = listMarker(following) + markerLine = following + markerValue = listMarker(following) + const followingMarker = markerValue if (followingMarker?.indent === baseIndent) { if (followingMarker.marker !== first.marker) break loose = true diff --git a/tests/audit-regressions.test.tsx b/tests/audit-regressions.test.tsx index 6cd67f0..6ceba4f 100644 --- a/tests/audit-regressions.test.tsx +++ b/tests/audit-regressions.test.tsx @@ -20,6 +20,21 @@ function equivalent(input: MarkdownInput, options: RenderOptions = {}) { } describe('audit regressions', () => { + it('observes extension edits to a pending list marker', () => { + let outerLines: string[] | undefined + const document = parseMarkdown('- first\n- second', { + extensions: [{ + name: 'edit-pending-list', + parseBlock({ lines }) { + if (lines[0] === '- first') outerLines = lines + if (lines[0] === 'first' && outerLines) outerLines[1] = '+ changed' + return undefined + }, + }], + }) + expect(equivalent(document)).toBe('