|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { |
| 3 | + buildMdxFenceDelimiterEdits, |
| 4 | + type MdxFence, |
| 5 | + maskMdxFencedRegions, |
| 6 | + parseInlineMdxFenceCloser, |
| 7 | + parseInlineMdxFenceOpener, |
| 8 | + restoreMdxFencedRegions, |
| 9 | +} from './fences' |
| 10 | +import { applySourceEdits, SourceText } from './source' |
| 11 | + |
| 12 | +describe('inline fence delimiters', () => { |
| 13 | + it.each(['```', '````', '~~~', '~~~~~~'])( |
| 14 | + 'preserves %s marker length, fence info, content, and repeated-format stability', |
| 15 | + (fence) => { |
| 16 | + const source = `before \t${fence}mdx title="Example"\n <Panel />\nafter \t${fence}\t ` |
| 17 | + const expected = `before\n${fence}mdx title="Example"\n <Panel />\nafter\n${fence}` |
| 18 | + const edits = buildMdxFenceDelimiterEdits(new SourceText(source), []) |
| 19 | + expect(applySourceEdits(source, edits)).toBe(expected) |
| 20 | + expect(buildMdxFenceDelimiterEdits(new SourceText(expected), [])).toEqual( |
| 21 | + [], |
| 22 | + ) |
| 23 | + const { fences, placeholderContent } = maskMdxFencedRegions(source) |
| 24 | + expect(restoreMdxFencedRegions(placeholderContent, fences)).toBe(expected) |
| 25 | + }, |
| 26 | + ) |
| 27 | + |
| 28 | + it('uses the first inline opener and preserves its complete suffix', () => { |
| 29 | + expect(parseInlineMdxFenceOpener('before ~~ ````mdx ~~~')).toEqual({ |
| 30 | + marker: '`', |
| 31 | + length: 4, |
| 32 | + suffix: 'mdx ~~~', |
| 33 | + }) |
| 34 | + expect(parseInlineMdxFenceOpener(' \t```mdx')).toBeUndefined() |
| 35 | + expect(parseInlineMdxFenceOpener('before `` ~ around')).toBeUndefined() |
| 36 | + }) |
| 37 | + |
| 38 | + it('accepts longer closing fences on either side and rejects shorter or different markers', () => { |
| 39 | + const active: MdxFence = { marker: '`', length: 4 } |
| 40 | + expect(parseInlineMdxFenceCloser('before \t`````\t ', active)).toEqual({ |
| 41 | + marker: '`', |
| 42 | + length: 5, |
| 43 | + suffix: '', |
| 44 | + }) |
| 45 | + expect(parseInlineMdxFenceCloser('\t`````<Panel/>', active)).toEqual({ |
| 46 | + marker: '`', |
| 47 | + length: 5, |
| 48 | + suffix: '', |
| 49 | + }) |
| 50 | + for (const line of [ |
| 51 | + 'before ```', |
| 52 | + 'before ~~~~', |
| 53 | + 'before ```` x', |
| 54 | + 'before ````\u00a0', |
| 55 | + ]) { |
| 56 | + expect(parseInlineMdxFenceCloser(line, active)).toBeUndefined() |
| 57 | + } |
| 58 | + }) |
| 59 | + |
| 60 | + it('preserves CRLF and honors excluded ranges', () => { |
| 61 | + const source = 'before ```mdx\r\n<Panel /> ```\r\n' |
| 62 | + const document = new SourceText(source) |
| 63 | + const edits = buildMdxFenceDelimiterEdits(document, []) |
| 64 | + expect(applySourceEdits(source, edits)).toBe( |
| 65 | + 'before\r\n```mdx\r\n<Panel />\r\n```\r\n', |
| 66 | + ) |
| 67 | + expect( |
| 68 | + buildMdxFenceDelimiterEdits(document, [{ start: 0, end: source.length }]), |
| 69 | + ).toEqual([]) |
| 70 | + }) |
| 71 | + |
| 72 | + it('handles long nonmatching and matching whitespace runs before openers and closers', () => { |
| 73 | + const whitespace = '\t'.repeat(100_000) |
| 74 | + const source = `a${whitespace}\nbefore${whitespace}~~~mdx\na${whitespace}x\nafter${whitespace}~~~\t ` |
| 75 | + const expected = `a${whitespace}\nbefore\n~~~mdx\na${whitespace}x\nafter\n~~~` |
| 76 | + expect( |
| 77 | + applySourceEdits( |
| 78 | + source, |
| 79 | + buildMdxFenceDelimiterEdits(new SourceText(source), []), |
| 80 | + ), |
| 81 | + ).toBe(expected) |
| 82 | + }) |
| 83 | +}) |
0 commit comments