micro-mdx-parser
Version:
A tiny parser to convert markdown or html into JSON
66 lines (54 loc) • 2 kB
JavaScript
const { test } = require("uvu")
const assert = require("uvu/assert")
const { formatAttributes } = require("./stringify")
const { parse, stringify } = require(".")
test('stringify() should handle simple conversions', t => {
const str1 = '<h1>Text</h1>'
assert.is(stringify(parse(str1)), str1)
const str2 = 'Text'
assert.is(stringify(parse(str2)), str2)
const str3 = '<!--Comment-->'
assert.is(stringify(parse(str3)), str3)
})
test('stringify() should work for void elements', t => {
const meta = "<meta charset='utf8'>"
assert.is(stringify(parse(meta)), "<meta charset='utf8' />") // adds closing tags
const link = "<link rel='stylesheet' href='file.css'>"
assert.is(stringify(parse(link)), "<link rel='stylesheet' href='file.css' />")
})
test('stringify() should build the class attribute properly', t => {
const elem = "<div class='foo bar baz'></div>"
assert.is(stringify(parse(elem)), elem)
})
test('stringify() should build data-* attributes properly', t => {
const elem = "<div data-one='5' data-two='five'></div>"
assert.is(stringify(parse(elem)), elem)
})
test('stringify() should build the style attribute properly', t => {
const elem = "<div style='color: #fff; font-size: 12px'></div>"
assert.is(stringify(parse(elem)), elem)
})
test('stringify() should do basic escaping if a value contains either single or double quotes', t => {
const html = '<div data-val="cake is \'good\'"></div>'
assert.is(stringify(parse(html)), html)
})
test('stringify() should preserve whitespace', t => {
const html = [
'<html> ',
' <h1> Document </h1>',
'</html> '
].join('\n')
assert.is(stringify(parse(html)), html)
})
test('formatAttributes should stringify attribute lists correctly', t => {
assert.is(formatAttributes([]), '')
assert.is(formatAttributes([{
key: 'disabled',
value: null
}]), ' disabled')
assert.is(formatAttributes([{
key: 'data-key',
value: '123'
}]), " data-key='123'")
})
test.run()