import { test } from 'uvu' import * as assert from 'uvu/assert' import { visit } from 'unist-util-visit' import rehype from 'rehype' import dedent from 'dedent' import rehypePrism from './index.js' /** * Mock meta in code block */ const addMeta = (metastring) => { if (!metastring) return return (tree) => { visit(tree, 'element', (node, index, parent) => { if (node.tagName === 'code') { node.data = { meta: metastring } } }) } } const processHtml = (html, options, metastring) => { return rehype() .data('settings', { fragment: true }) .use(addMeta, metastring) .use(rehypePrism, options) .processSync(html) .toString() } test('copies the language- class to pre tag', () => { const result = processHtml(dedent`
`)
const expected = dedent``
assert.is(result, expected)
})
test('add div with class code line for each line', () => {
const result = processHtml(dedent`
x = 6
`)
const expected = dedent`x = 6`
assert.is(result, expected)
})
test('finds code and highlights', () => {
const result = processHtml(dedent`
foo
x = 6
foo
x = 6
foo
x = 6
foo
x = 6
foo
x = 6
y = 7
x = 6
y = 7
x = 6
y = 7
z = 10
x = 6
y = 7
z = 10
x = 6
y = 7
x = 6
y = 7
x = 6
y = 7
z = 10
x = 6
`),
/Unknown language/
)
})
test('with options.ignoreMissing, does nothing to code block with fake language- class', () => {
const result = processHtml(
dedent`
x = 6
`,
{ ignoreMissing: true }
)
const expected = dedent`x = 6`
assert.is(result, expected)
})
test.run()