import { Parser } from '../../lib/marked.esm.js'; import { htmlIsEqual, firstDiff } from '@markedjs/testutils'; import assert from 'node:assert'; import { describe, it } from 'node:test'; async function expectHtml({ tokens, options, html, inline }) { const parser = new Parser(options); const actual = parser[inline ? 'parseInline' : 'parse'](tokens); const testDiff = await firstDiff(actual, html); assert.ok(await htmlIsEqual(html, actual), `Expected: ${testDiff.expected}\n Actual: ${testDiff.actual}`); } describe('Parser', () => { describe('block', () => { it('space between paragraphs', async() => { await expectHtml({ tokens: [ { type: 'paragraph', text: 'paragraph 1', tokens: [{ type: 'text', text: 'paragraph 1' }] }, { type: 'space' }, { type: 'paragraph', text: 'paragraph 2', tokens: [{ type: 'text', text: 'paragraph 2' }] } ], html: '

paragraph 1

paragraph 2

' }); }); it('hr', async() => { await expectHtml({ tokens: [ { type: 'hr' } ], html: '
' }); }); it('heading', async() => { await expectHtml({ tokens: [ { type: 'heading', depth: 1, text: 'heading', tokens: [{ type: 'text', text: 'heading' }] } ], html: '

heading

' }); }); it('code', async() => { await expectHtml({ tokens: [ { type: 'code', text: 'code' } ], html: '
code
' }); }); it('table', async() => { await expectHtml({ tokens: [ { type: 'table', align: ['left', 'right'], header: [ { text: 'a', tokens: [{ type: 'text', raw: 'a', text: 'a' }], header: true, align: 'left' }, { text: 'b', tokens: [{ type: 'text', raw: 'b', text: 'b' }], header: true, align: 'right' } ], rows: [ [ { text: '1', tokens: [{ type: 'text', raw: '1', text: '1' }], header: false, align: 'left' }, { text: '2', tokens: [{ type: 'text', raw: '2', text: '2' }], header: false, align: 'right' } ] ] } ], html: `
a b
1 2
` }); }); it('blockquote', async() => { await expectHtml({ tokens: [ { type: 'blockquote', tokens: [ { type: 'paragraph', text: 'blockquote', tokens: [{ type: 'text', text: 'blockquote' }] } ] } ], html: '

blockquote

' }); }); describe('list', () => { it('unordered', async() => { await expectHtml({ tokens: [ { type: 'list', ordered: false, start: '', loose: false, items: [ { task: false, checked: undefined, tokens: [ { type: 'text', text: 'item 1', tokens: [{ type: 'text', text: 'item 1' }] } ] }, { task: false, checked: undefined, tokens: [ { type: 'text', text: 'item 2', tokens: [{ type: 'text', text: 'item 2' }] } ] } ] } ], html: ` ` }); }); it('ordered', async() => { await expectHtml({ tokens: [ { type: 'list', ordered: true, start: 2, loose: false, items: [ { task: false, checked: undefined, tokens: [ { type: 'text', text: 'item 1', tokens: [{ type: 'text', text: 'item 1' }] } ] }, { task: false, checked: undefined, tokens: [ { type: 'text', text: 'item 2', tokens: [{ type: 'text', text: 'item 2' }] } ] } ] } ], html: `
  1. item 1
  2. item 2
` }); }); it('tasks', async() => { await expectHtml({ tokens: [ { type: 'list', ordered: false, start: '', loose: false, items: [ { task: true, checked: false, tokens: [ { type: 'text', text: 'item 1', tokens: [{ type: 'text', text: 'item 1' }] } ] }, { task: true, checked: true, tokens: [ { type: 'text', text: 'item 2', tokens: [{ type: 'text', text: 'item 2' }] } ] } ] } ], html: ` ` }); }); it('loose', async() => { await expectHtml({ tokens: [ { type: 'list', ordered: false, start: '', loose: true, items: [ { task: false, checked: undefined, loose: true, tokens: [ { type: 'text', text: 'item 1', tokens: [{ type: 'text', text: 'item 1' }] } ] }, { task: false, checked: undefined, loose: true, tokens: [ { type: 'text', text: 'item 2', tokens: [{ type: 'text', text: 'item 2' }] } ] } ] } ], html: ` ` }); }); }); it('html', async() => { await expectHtml({ tokens: [ { type: 'html', text: '
html
' } ], html: '
html
' }); }); it('paragraph', async() => { await expectHtml({ tokens: [ { type: 'paragraph', text: 'paragraph 1', tokens: [{ type: 'text', text: 'paragraph 1' }] } ], html: '

paragraph 1

' }); }); it('text', async() => { await expectHtml({ tokens: [ { type: 'text', text: 'text 1' }, { type: 'text', text: 'text 2' } ], html: '

text 1\ntext 2

' }); }); }); describe('inline', () => { it('escape', async() => { await expectHtml({ inline: true, tokens: [{ type: 'escape', text: '>' }], html: '>' }); }); it('html', async() => { await expectHtml({ inline: true, tokens: [ { type: 'html', text: '
' }, { type: 'text', text: 'html' }, { type: 'html', text: '
' } ], html: '
html
' }); }); it('link', async() => { await expectHtml({ inline: true, tokens: [ { type: 'link', text: 'link', href: 'https://example.com', title: 'title', tokens: [{ type: 'text', text: 'link' }] } ], html: 'link' }); }); it('image', async() => { await expectHtml({ inline: true, tokens: [ { type: 'image', text: 'image', href: 'image.png', title: 'title' } ], html: 'image' }); }); it('strong', async() => { await expectHtml({ inline: true, tokens: [ { type: 'strong', text: 'strong', tokens: [{ type: 'text', text: 'strong' }] } ], html: 'strong' }); }); it('em', async() => { await expectHtml({ inline: true, tokens: [ { type: 'em', text: 'em', tokens: [{ type: 'text', text: 'em' }] } ], html: 'em' }); }); it('codespan', async() => { await expectHtml({ inline: true, tokens: [ { type: 'codespan', text: 'code' } ], html: 'code' }); }); it('br', async() => { await expectHtml({ inline: true, tokens: [ { type: 'br' } ], html: '
' }); }); it('del', async() => { await expectHtml({ inline: true, tokens: [ { type: 'del', text: 'del', tokens: [{ type: 'text', text: 'del' }] } ], html: 'del' }); }); it('text', async() => { await expectHtml({ inline: true, tokens: [ { type: 'text', text: 'text 1' }, { type: 'text', text: 'text 2' } ], html: 'text 1text 2' }); }); }); });