-
Notifications
You must be signed in to change notification settings - Fork 18
/
test.ts
244 lines (213 loc) · 11.3 KB
/
test.ts
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import assert from 'assert';
import { remark } from 'remark';
import { unified } from 'unified';
import remarkParse from 'remark-parse';
import gfm from 'remark-gfm';
import github from 'remark-github';
import remarkRehype from 'remark-rehype';
import headings from 'rehype-autolink-headings';
import slug from 'rehype-slug';
import emoji from './index.js';
import rehypeStringify from 'rehype-stringify';
import rehypeSanitize, { defaultSchema } from 'rehype-sanitize';
const schema = structuredClone(defaultSchema);
assert.ok(schema.attributes);
if ('span' in schema.attributes) {
schema.attributes['span'].push('role', 'ariaLabel');
} else {
schema.attributes['span'] = ['role', 'ariaLabel'];
}
const compiler = remark().use(emoji);
const padded = remark().use(emoji, { padSpaceAfter: true });
const emoticon = remark().use(emoji, { emoticon: true });
const padAndEmoticon = remark().use(emoji, { padSpaceAfter: true, emoticon: true });
const ariaHtml = unified()
.use(remarkParse)
.use(emoji, { emoticon: true, accessible: true })
.use(remarkRehype)
.use(rehypeSanitize, schema)
.use(rehypeStringify);
const githubFlavor = remark().use(gfm).use(github).use(emoji);
const toRehype = unified().use(remarkParse).use(emoji).use(remarkRehype).use(slug).use(headings).use(rehypeStringify);
describe('remark-emoji', function () {
describe('minimal compiler', function () {
it('replaces emojis in text', async function () {
const tests: Record<string, string> = {
'This is :dog:': 'This is 🐶\n',
':dog: is not :cat:': '🐶 is not 🐱\n',
'Please vote with :+1: or :-1:': 'Please vote with 👍 or 👎\n',
':triumph:': '😤\n',
':stuck_out_tongue_winking_eye:': '😜\n',
};
for (const [input, expected] of Object.entries(tests)) {
const result = await compiler.process(input);
assert.equal(String(result), expected, `input: ${JSON.stringify(input)}`);
}
});
it('does not replace emoji-like but not-a-emoji stuffs', async function () {
const tests: Record<string, string> = {
'This text does not include emoji.': 'This text does not include emoji.\n',
':++: or :foo: or :cat': ':++: or :foo: or :cat\n',
'::': '::\n',
};
for (const [input, expected] of Object.entries(tests)) {
const result = await compiler.process(input);
assert.equal(String(result), expected, `input: ${JSON.stringify(input)}`);
}
});
it('replaces in link text', async function () {
const tests: Record<string, string> = {
'In inline code, `:dog: and :-) is not replaced`': 'In inline code, `:dog: and :-) is not replaced`\n',
'In code, \n```\n:dog: and :-) is not replaced\n```':
'In code,\n\n```\n:dog: and :-) is not replaced\n```\n',
'[here :dog: and :cat: and :-) pictures!](https://example.com)':
'[here 🐶 and 🐱 and :-) pictures!](https://example.com)\n',
};
for (const [input, expected] of Object.entries(tests)) {
const result = await compiler.process(input);
assert.equal(String(result), expected, `input: ${JSON.stringify(input)}`);
}
});
it('handles an emoji including 2 underscores', async function () {
const result = await compiler.process(':heavy_check_mark:');
assert.equal(String(result), '✔️\n');
});
it('adds an white space after emoji when padSpaceAfter is set to true', async function () {
const tests: Record<string, string> = {
':dog: is dog': '🐶 is dog\n',
'dog is :dog:': 'dog is 🐶 \n',
':dog: is not :cat:': '🐶 is not 🐱 \n',
':triumph:': '😤 \n',
':-)': ':-)\n',
'Smile :-), not >:(!': 'Smile :-), not >:(!\n',
};
for (const [input, expected] of Object.entries(tests)) {
const result = await padded.process(input);
assert.equal(String(result), expected, `input: ${JSON.stringify(input)}`);
}
});
it('handles emoji that use dashes to separate words instead of underscores', async function () {
const result = await compiler.process('T-Rex emoji is :t-rex:');
assert.equal(String(result), 'T-Rex emoji is 🦖\n');
});
});
describe('transforms markdown AST to rehype with other remark and rehype plugins', function () {
it('replaces emois in transformed HTML text', async function () {
// `id="hello-world"` is inserted by rehype-slug
// `<a aria-hidden="true" tabindex="-1" href="#hello-world">...` is inserted by rehype-autolink-headings
const input = '# Hello world\nWoo :dog: is not :cat:.';
const expected =
'<h1 id="hello-world"><a aria-hidden="true" tabindex="-1" href="#hello-world"><span class="icon icon-link"></span></a>Hello world</h1>\n<p>Woo 🐶 is not 🐱.</p>';
const result = await toRehype.process(input);
assert.equal(String(result), expected, `input: ${JSON.stringify(input)}`);
});
});
describe('transforms markdown AST with other remark plugins', function () {
it('replaces emois in text with GitHub-Flavored Markdown extensions', async function () {
// `- [x]` task list is handled by remark-gfm
// `@rhysd` auto link is handled by remark-github
const input = '- [x] @rhysd is a :dog:.';
const expected = '* [x] [**@rhysd**](https://github.com/rhysd) is a 🐶.\n';
const result = await githubFlavor.process(input);
assert.equal(String(result), expected, `input: ${JSON.stringify(input)}`);
});
});
describe('emoticon support', function () {
it('replaces emojis in text', async function () {
const tests: Record<string, string> = {
'This is :dog:': 'This is 🐶\n',
':dog: is not :cat:': '🐶 is not 🐱\n',
'Please vote with :+1: or :-1:': 'Please vote with 👍 or 👎\n',
':triumph:': '😤\n',
};
for (const [input, expected] of Object.entries(tests)) {
const result = await emoticon.process(input);
assert.equal(String(result), expected, `input: ${JSON.stringify(input)}`);
}
});
it('does not replace emoji-like but not-a-emoji stuffs', async function () {
const tests: Record<string, string> = {
'This text does not include emoji.': 'This text does not include emoji.\n',
':++: or :foo: or :cat': ':++: or :foo: or :cat\n',
'::': '::\n',
};
for (const [input, expected] of Object.entries(tests)) {
const result = await emoticon.process(input);
assert.equal(String(result), expected, `input: ${JSON.stringify(input)}`);
}
});
it('replaces in link text', async function () {
const tests: Record<string, string> = {
'In inline code, `:dog: and :-) is not replaced`': 'In inline code, `:dog: and :-) is not replaced`\n',
'In code, \n```\n:dog: and :-) is not replaced\n```':
'In code,\n\n```\n:dog: and :-) is not replaced\n```\n',
'[here :dog: and :cat: and :-) pictures!](https://example.com)':
'[here 🐶 and 🐱 and 😃 pictures!](https://example.com)\n',
};
for (const [input, expected] of Object.entries(tests)) {
const result = await emoticon.process(input);
assert.equal(String(result), expected, `input: ${JSON.stringify(input)}`);
}
});
it('handles an emoji including 2 underscores', async function () {
const result = await emoticon.process(':heavy_check_mark:');
assert.equal(String(result), '✔️\n');
});
it('adds an white space after emoji when padSpaceAfter is set to true', async function () {
const tests: Record<string, string> = {
':dog: is dog': '🐶 is dog\n',
'dog is :dog:': 'dog is 🐶 \n',
':dog: is not :cat:': '🐶 is not 🐱 \n',
':triumph:': '😤 \n',
':-)': '😃 \n',
'Smile :-), not >:(!': 'Smile 😃 , not 😠 !\n',
};
for (const [input, expected] of Object.entries(tests)) {
const result = await padAndEmoticon.process(input);
assert.equal(String(result), expected, `input: ${JSON.stringify(input)}`);
}
});
it('handles emoji that use dashes to separate words instead of underscores', async function () {
const result = await emoticon.process('T-Rex emoji is :t-rex:');
assert.equal(String(result), 'T-Rex emoji is 🦖\n');
});
it('handles emoji shortcodes (emoticon)', async function () {
const tests: Record<string, string> = {
':p': '😛\n',
':-)': '😃\n',
'With-in some text :-p, also with some :o spaces :-)!':
'With-in some text 😛, also with some 😮 spaces 😃!\n',
'Four char code ]:-)': 'Four char code 😈\n',
'No problem with :dog: - :d': 'No problem with 🐶 - 😛\n',
'With double quotes :"D': 'With double quotes 😊\n',
};
for (const [input, expected] of Object.entries(tests)) {
const result = await emoticon.process(input);
assert.equal(String(result), expected, `input: ${JSON.stringify(input)}`);
}
});
it('should not break ::: (#23)', async function () {
const input = '::: danger Title\n\nbla bla bla\n\n:::\n';
const result = await emoticon.process(input);
assert.equal(String(result), input);
});
});
describe('accessibility support', function () {
it('wraps emoji with span', async function () {
const tests: Record<string, string> = {
':dog:': '<p><span role="img" aria-label="dog emoji">🐶</span></p>',
':dog: :cat:':
'<p><span role="img" aria-label="dog emoji">🐶</span> <span role="img" aria-label="cat emoji">🐱</span></p>',
':-)': '<p><span role="img" aria-label="smiley emoticon">😃</span></p>',
':+1:': '<p><span role="img" aria-label="+1 emoji">👍</span></p>',
':-1:': '<p><span role="img" aria-label="-1 emoji">👎</span></p>',
':stuck_out_tongue_winking_eye:':
'<p><span role="img" aria-label="stuck out tongue winking eye emoji">😜</span></p>',
};
for (const [input, expected] of Object.entries(tests)) {
const result = await ariaHtml.process(input);
assert.equal(String(result), expected, `input: ${JSON.stringify(input)}`);
}
});
});
});