summaryrefslogtreecommitdiff
path: root/packages/markdown/remark/test/expressions.test.js
blob: 0361e1df88483ed040b662151f9784ef01ba3364 (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
import { renderMarkdown } from '../dist/index.js';
import chai, { expect } from 'chai';

describe('expressions', () => {
	it('should be able to serialize bare expression', async () => {
		const { code } = await renderMarkdown(`{a}`, {});

		chai.expect(code).to.equal(`{a}`);
	});

	it('should be able to serialize expression inside component', async () => {
		const { code } = await renderMarkdown(`<Component>{a}</Component>`, {});

		chai.expect(code).to.equal(`<Component>{a}</Component>`);
	});

	it('should be able to serialize expression inside markdown', async () => {
		const { code } = await renderMarkdown(`# {frontmatter.title}`, {});

		chai
			.expect(code)
			.to.equal(`<h1 id={$$slug(\`\${frontmatter.title}\`)}>{frontmatter.title}</h1>`);
	});

	it('should be able to serialize complex expression inside markdown', async () => {
		const { code } = await renderMarkdown(`# Hello {frontmatter.name}`, {});

		chai
			.expect(code)
			.to.equal(`<h1 id={$$slug(\`Hello \${frontmatter.name}\`)}>Hello {frontmatter.name}</h1>`);
	});

	it('should be able to serialize complex expression with markup inside markdown', async () => {
		const { code } = await renderMarkdown(`# Hello <span>{frontmatter.name}</span>`, {});

		chai
			.expect(code)
			.to.equal(
				`<h1 id={$$slug(\`Hello \${frontmatter.name}\`)}>Hello <span>{frontmatter.name}</span></h1>`
			);
	});

	it('should be able to avoid evaluating JSX-like expressions in an inline code & generate a slug for id', async () => {
		const { code } = await renderMarkdown(`# \`{frontmatter.title}\``, {});

		chai
			.expect(code)
			.to.equal('<h1 id="frontmattertitle"><code is:raw>{frontmatter.title}</code></h1>');
	});

	it('should be able to avoid evaluating JSX-like expressions in inline codes', async () => {
		const { code } = await renderMarkdown(`# \`{ foo }\` is a shorthand for \`{ foo: foo }\``, {});

		chai
			.expect(code)
			.to.equal(
				'<h1 id="-foo--is-a-shorthand-for--foo-foo-"><code is:raw>{ foo }</code> is a shorthand for <code is:raw>{ foo: foo }</code></h1>'
			);
	});

	it('should be able to avoid evaluating JSX-like expressions & escape HTML tag characters in inline codes', async () => {
		const { code } = await renderMarkdown(
			`###### \`{}\` is equivalent to \`Record<never, never>\` <small>(at TypeScript v{frontmatter.version})</small>`,
			{}
		);

		chai
			.expect(code)
			.to.equal(
				`<h6 id={$$slug(\`{} is equivalent to Record&lt;never, never&gt; (at TypeScript v\${frontmatter.version})\`)}><code is:raw>{}</code> is equivalent to <code is:raw>Record&lt;never, never&gt;</code> <small>(at TypeScript v{frontmatter.version})</small></h6>`
			);
	});

	it('should be able to encode ampersand characters in code blocks', async () => {
		const { code } = await renderMarkdown(
			'The ampersand in `&nbsp;` must be encoded in code blocks.',
			{}
		);

		chai
			.expect(code)
			.to.equal(
				'<p>The ampersand in <code is:raw>&amp;nbsp;</code> must be encoded in code blocks.</p>'
			)
	});

	it('should be able to encode ampersand characters in fenced code blocks', async () => {
		const { code } = await renderMarkdown(`
		\`\`\`md
			The ampersand in \`&nbsp;\` must be encoded in code blocks.
		\`\`\`
		`);

		chai
			.expect(code)
			.to.match(
				/^<pre is:raw.*<code>.*The ampersand in `&amp;nbsp;`/
			);
	})

	it('should be able to serialize function expression', async () => {
		const { code } = await renderMarkdown(
			`{frontmatter.list.map(item => <p id={item}>{item}</p>)}`,
			{}
		);

		chai.expect(code).to.equal(`{frontmatter.list.map(item => <p id={item}>{item}</p>)}`);
	});

	it('should unwrap HTML comments in inline code blocks', async () => {
		const { code } = await renderMarkdown(`\`{/*<!-- HTML comment -->*/}\``);

		chai.expect(code).to.equal('<p><code is:raw>&lt;!-- HTML comment --&gt;</code></p>');
	});

	it('should unwrap HTML comments in code fences', async () => {
		const { code } = await renderMarkdown(
			`
			  \`\`\`
				<!-- HTML comment -->
				\`\`\`
			`
		);

		chai.expect(code).to.match(/(?<!{\/\*)&lt;!-- HTML comment --&gt;(?!\*\/})/);
	});
});