aboutsummaryrefslogtreecommitdiff
path: root/packages/integrations/markdoc/test/syntax-highlighting.test.js
blob: ef1845eb942e28dc5119aa16f5eeda245298844f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { parseHTML } from 'linkedom';
import { expect } from 'chai';
import Markdoc from '@markdoc/markdoc';
import { shiki } from '../dist/config.js';
import { setupConfig } from '../dist/runtime.js';
import { isHTMLString } from 'astro/runtime/server/index.js';

const entry = `
\`\`\`ts
const highlighting = true;
\`\`\`

\`\`\`css
.highlighting {
	color: red;
}
\`\`\`
`;

describe('Markdoc - syntax highlighting', () => {
	it('transforms with defaults', async () => {
		const ast = Markdoc.parse(entry);
		const content = Markdoc.transform(ast, await getConfigExtendingShiki());

		expect(content.children).to.have.lengthOf(2);
		for (const codeBlock of content.children) {
			expect(isHTMLString(codeBlock)).to.be.true;

			const pre = parsePreTag(codeBlock);
			expect(pre.classList).to.include('astro-code');
			expect(pre.classList).to.include('github-dark');
		}
	});
	it('transforms with `theme` property', async () => {
		const ast = Markdoc.parse(entry);
		const content = Markdoc.transform(
			ast,
			await getConfigExtendingShiki({
				theme: 'dracula',
			})
		);
		expect(content.children).to.have.lengthOf(2);
		for (const codeBlock of content.children) {
			expect(isHTMLString(codeBlock)).to.be.true;

			const pre = parsePreTag(codeBlock);
			expect(pre.classList).to.include('astro-code');
			expect(pre.classList).to.include('dracula');
		}
	});
	it('transforms with `wrap` property', async () => {
		const ast = Markdoc.parse(entry);
		const content = Markdoc.transform(
			ast,
			await getConfigExtendingShiki({
				wrap: true,
			})
		);
		expect(content.children).to.have.lengthOf(2);
		for (const codeBlock of content.children) {
			expect(isHTMLString(codeBlock)).to.be.true;

			const pre = parsePreTag(codeBlock);
			expect(pre.getAttribute('style')).to.include('white-space: pre-wrap');
			expect(pre.getAttribute('style')).to.include('word-wrap: break-word');
		}
	});
});

/**
 * @param {import('astro').ShikiConfig} config
 * @returns {import('../src/config.js').AstroMarkdocConfig}
 */
async function getConfigExtendingShiki(config) {
	return setupConfig({
		extends: [await shiki(config)],
	});
}

/**
 * @param {string} html
 * @returns {HTMLPreElement}
 */
function parsePreTag(html) {
	const { document } = parseHTML(html);
	const pre = document.querySelector('pre');
	expect(pre).to.exist;
	return pre;
}