summaryrefslogtreecommitdiff
path: root/packages/integrations/markdoc/test/syntax-highlighting.test.js
blob: 6ea841ae12496d0343344de84e0022d52fe2df80 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import Markdoc from '@markdoc/markdoc';
import { isHTMLString } from 'astro/runtime/server/index.js';
import { parseHTML } from 'linkedom';
import prism from '../dist/extensions/prism.js';
import shiki from '../dist/extensions/shiki.js';
import { setupConfig } from '../dist/runtime.js';

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

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

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

			assert.equal(content.children.length, 2);
			for (const codeBlock of content.children) {
				assert.equal(isHTMLString(codeBlock), true);

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

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

				const pre = parsePreTag(codeBlock);
				assert.equal(pre.getAttribute('style').includes('white-space: pre-wrap'), true);
				assert.equal(pre.getAttribute('style').includes('word-wrap: break-word'), true);
			}
		});
		it('transform within if tags', async () => {
			const ast = Markdoc.parse(`
{% if equals("true", "true") %}
Inside truthy

\`\`\`js
const hello = "yes";
\`\`\`

{% /if %}`);
			const content = await Markdoc.transform(ast, await getConfigExtendingShiki());
			assert.equal(content.children.length, 1);
			assert.equal(content.children[0].length, 2);
			const pTag = content.children[0][0];
			assert.equal(pTag.name, 'p');
			const codeBlock = content.children[0][1];
			assert.equal(isHTMLString(codeBlock), true);
			const pre = parsePreTag(codeBlock);
			assert.equal(pre.classList.contains('astro-code'), true);
			assert.equal(pre.classList.contains('github-dark'), true);
		});
	});

	describe('prism', () => {
		it('transforms', async () => {
			const ast = Markdoc.parse(entry);
			const config = await setupConfig({
				extends: [prism()],
			});
			const content = await Markdoc.transform(ast, config);

			assert.equal(content.children.length, 2);
			const [tsBlock, cssBlock] = content.children;

			assert.equal(isHTMLString(tsBlock), true);
			assert.equal(isHTMLString(cssBlock), true);

			const preTs = parsePreTag(tsBlock);
			assert.equal(preTs.classList.contains('language-ts'), true);

			const preCss = parsePreTag(cssBlock);
			assert.equal(preCss.classList.contains('language-css'), true);
		});
	});
});

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

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