diff --git a/packages/comark/src/plugins/components.ts b/packages/comark/src/plugins/components.ts index 9e2abcf9..f57df3db 100644 --- a/packages/comark/src/plugins/components.ts +++ b/packages/comark/src/plugins/components.ts @@ -481,9 +481,11 @@ const markdownItInlineSpan: PluginSimple = (md) => { const close = findClosingBracket(state.src, start) const index = close === -1 ? state.src.length : close - // Don't match `[text](url)` or `[text][ref]` — let the link parser handle those + // Don't match `[text](url)` or `[text][ref]` — let the link parser handle those, + // except for adjacent footnote references like `x[^1][^2]`. const nextChar = state.src[index + 1] - if (nextChar === '(' || nextChar === '[') return false + const isAdjacentFootnote = state.src[start + 1] === '^' && nextChar === '[' && state.src[index + 2] === '^' + if (nextChar === '(' || (nextChar === '[' && !isAdjacentFootnote)) return false // Inside a link label, bare `[text]` stays literal (plain-markdown behavior, // e.g. `[[1] Document](#)`); only an explicit `[text]{attrs}` span matches diff --git a/packages/comark/test/footnotes.test.ts b/packages/comark/test/footnotes.test.ts index 7b017fb3..9aed4406 100644 --- a/packages/comark/test/footnotes.test.ts +++ b/packages/comark/test/footnotes.test.ts @@ -278,4 +278,23 @@ describe('footnotes stringify', () => { expect(result).toContain('[^note]') expect(result).toContain('[^note]: A note') }) + + it('should parse adjacent footnote references (#567)', async () => { + const md = `x[^1][^2] + +[^1]: First note +[^2]: Second note` + + const tree = await parseMarkdown(md, { plugins: [footnotes()] }) + const paragraph = tree.nodes[0] as any[] + expect(paragraph[0]).toBe('p') + expect(paragraph[2]).toBe('x') + expect(paragraph[3][0]).toBe('sup') + expect(paragraph[4][0]).toBe('sup') + + const section = tree.nodes.find((n) => Array.isArray(n) && n[0] === 'section') as any[] + const ol = section.find((n: any, i: number) => i >= 2 && Array.isArray(n) && n[0] === 'ol') + const lis = ol.slice(2).filter((n: any) => Array.isArray(n) && n[0] === 'li') + expect(lis.length).toBe(2) + }) })