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: '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 |
' }); }); 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: `blockquote
item 1
item 2
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: 'code
'
});
});
it('br', async() => {
await expectHtml({
inline: true,
tokens: [
{
type: 'br'
}
],
html: '