summaryrefslogtreecommitdiff
path: root/source/github-helpers/parse-backticks.test.ts
blob: 8c342cfd4752188d260126cfbfcc1f3e92a78519 (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
import {test, assert} from 'vitest';

import {getParsedBackticksParts} from './parse-backticks.js';

function parseBackticks(string: string): string {
	return getParsedBackticksParts(string).map(
		(part, index) => index % 2 && part.length > 0 ? `<code>${part.trim()}</code>` : part,
	).join('');
}

test('parseBackticks', () => {
	assert.equal(
		parseBackticks('multiple `code spans` between ` other ` text'),
		'multiple <code>code spans</code> between <code>other</code> text',
	);
	assert.equal(
		parseBackticks('`code` at the start'),
		'<code>code</code> at the start',
	);
	assert.equal(
		parseBackticks('code at the `end`'),
		'code at the <code>end</code>',
	);
	assert.equal(
		parseBackticks('single backtick in a code span: `` ` ``'),
		'single backtick in a code span: <code>`</code>',
	);
	assert.equal(
		parseBackticks('backtick-delimited string in a code span: `` `foo` ``'),
		'backtick-delimited string in a code span: <code>`foo`</code>',
	);
	assert.equal(
		parseBackticks('single-character code span: `a`'),
		'single-character code span: <code>a</code>',
	);
	assert.equal(
		parseBackticks(`
			triple-backtick multiline block
			\`\`\`
			foo
			bar
			\`\`\`
			in some text #3990
		`),
		`
			triple-backtick multiline block
			\`\`\`
			foo
			bar
			\`\`\`
			in some text #3990
		`,
	);
	assert.equal(
		parseBackticks(`
			empty triple-backtick block
			\`\`\`
			\`\`\`
		`),
		`
			empty triple-backtick block
			\`\`\`
			\`\`\`
		`,
	);
	assert.equal(
		parseBackticks(`
			triple-backtick code block
			\`\`\`
			foo
			bar
			\`\`\`
			in some text #3990
		`),
		`
			triple-backtick code block
			\`\`\`
			foo
			bar
			\`\`\`
			in some text #3990
		`,
	);
	assert.equal(
		parseBackticks(`
			hello\`
			\`world
		`),
		`
			hello\`
			\`world
		`,
	);
	assert.equal(
		parseBackticks(`
			hello\`\` red
			\`\`world
		`),
		`
			hello\`\` red
			\`\`world
		`,
	);
});