|
| 1 | +import { renderToStaticMarkup } from 'react-dom/server'; |
| 2 | +import { describe, expect, test } from 'vitest'; |
| 3 | +import { compileToReact } from '../src/react'; |
| 4 | + |
| 5 | +function article(source: string) { |
| 6 | + const container = document.createElement('div'); |
| 7 | + container.innerHTML = renderToStaticMarkup(compileToReact(source)); |
| 8 | + return container; |
| 9 | +} |
| 10 | + |
| 11 | +describe('atx headings', () => { |
| 12 | + test.each([ |
| 13 | + ['#', 'h1'], |
| 14 | + ['##', 'h2'], |
| 15 | + ['###', 'h3'], |
| 16 | + ['####', 'h4'], |
| 17 | + ['#####', 'h5'], |
| 18 | + ['######', 'h6'], |
| 19 | + ])('renders %s as %s', (hashes, tag) => { |
| 20 | + const container = article(`${hashes} Interaction`); |
| 21 | + const heading = container.querySelector(tag); |
| 22 | + |
| 23 | + expect(heading?.textContent).toBe('Interaction'); |
| 24 | + expect(container.textContent).not.toContain('#'); |
| 25 | + }); |
| 26 | + |
| 27 | + test('renders inline markup inside a deep heading', () => { |
| 28 | + const container = article('#### 1. `capture` **native** selection'); |
| 29 | + |
| 30 | + expect(container.querySelector('h4 code')?.textContent).toBe('capture'); |
| 31 | + expect(container.querySelector('h4 strong')?.textContent).toBe('native'); |
| 32 | + }); |
| 33 | + |
| 34 | + test('anchors h2 for the table of contents and leaves deeper levels unanchored', () => { |
| 35 | + const container = article('## Case\n\n#### Deep\n\n##### Deeper'); |
| 36 | + |
| 37 | + expect(container.querySelector('h2')?.getAttribute('id')).toBe('case'); |
| 38 | + expect(container.querySelector('h4')?.hasAttribute('id')).toBe(false); |
| 39 | + expect(container.querySelector('h5')?.hasAttribute('id')).toBe(false); |
| 40 | + }); |
| 41 | + |
| 42 | + test('leaves a seventh level as prose, the way CommonMark does', () => { |
| 43 | + const container = article('####### Too deep'); |
| 44 | + |
| 45 | + expect(container.querySelector('p')?.textContent).toBe('####### Too deep'); |
| 46 | + }); |
| 47 | + |
| 48 | + test('leaves a hash without a space as prose', () => { |
| 49 | + const container = article('####Tight'); |
| 50 | + |
| 51 | + expect(container.querySelector('p')?.textContent).toBe('####Tight'); |
| 52 | + }); |
| 53 | + |
| 54 | + test('keeps a hash line inside a fence literal', () => { |
| 55 | + const container = article('```text\n#### Not a heading\n```'); |
| 56 | + |
| 57 | + expect(container.querySelector('h4')).toBeNull(); |
| 58 | + expect(container.querySelector('pre')?.textContent).toBe('#### Not a heading'); |
| 59 | + }); |
| 60 | +}); |
0 commit comments