import { test } from 'uvu' import * as assert from 'uvu/assert' import { visit } from 'unist-util-visit' import { rehype } from 'rehype' import { unified } from 'unified' import remarkParse from 'remark-parse' import remarkRehype from 'remark-rehype' import rehypeStringify from 'rehype-stringify' 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() } const processHtmlUnified = (html, options, metastring) => { return unified() .use(remarkParse) .use(remarkRehype, {}) .use(addMeta, metastring) .use(rehypePrism, options) .use(rehypeStringify) .processSync(html) .toString() } test('adds a code-highlight class to the code and pre tag', () => { const result = processHtml(dedent`
`)
const expected = dedent``
assert.is(result, expected)
})
test('add span 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
x
y
x
y
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
z = 10
x = 6
y = 7
x = 6
y = 7
x = 6
y = 7
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('with options.defaultLanguage, it adds the correct language class tag', () => {
const result = processHtml(
dedent`
x = 6
`,
{ defaultLanguage: 'py' }
)
const expected = dedent`x = 6`
assert.is(result, expected)
})
test('throws error if options.defaultLanguage is not registered with refractor', () => {
assert.throws(
() =>
processHtml(
dedent`
x = 6
`,
{ defaultLanguage: 'pyzqt' }
),
/"pyzqt" is not registered with refractor/
)
})
test('should work with multiline code / comments', () => {
const result = processHtml(
dedent`
/**
* My comment
*/
`,
{ ignoreMissing: true }
)
const expected = dedent`
/**
* My comment
*/
`
assert.is(result, expected)
})
test('adds inserted or deleted to code-line if lang=diff', async () => {
const result = processHtml(
dedent`
+ x = 6
- y = 7
z = 10
<Component/>
`
assert.is(result, expected)
})
test('diff and code highlighting should work together', () => {
const result = processHtml(
dedent`
.hello{
- background:url('./urel.png');
+ background-image:url('./urel.png');
}
`,
{ ignoreMissing: true }
)
assert.ok(result.includes(``))
assert.ok(result.includes(``))
assert.ok(result.includes(``))
assert.ok(result.includes(``))
})
test.run()