UNPKG

1.65 kBJavaScriptView Raw
1const Emoji = require('@readme/emojis').emoji;
2
3const emojis = new Emoji();
4
5const colon = ':';
6
7function tokenize(eat, value, silent) {
8 // Check if we’re at a short-code.
9 if (value.charAt(0) !== colon) return false;
10
11 const pos = value.indexOf(colon, 1);
12
13 if (pos === -1) return false;
14
15 const subvalue = value.slice(1, pos);
16
17 // Exit with true in silent
18 if (silent) return true;
19
20 const match = colon + subvalue + colon;
21
22 if (subvalue.substr(0, 3) === 'fa-') {
23 return eat(match)({
24 type: 'i',
25 data: {
26 hName: 'i',
27 hProperties: {
28 className: ['fa', subvalue],
29 },
30 },
31 });
32 }
33
34 if (emojis.is(subvalue)) {
35 return eat(match)({
36 type: 'image',
37 title: `:${subvalue}:`,
38 alt: `:${subvalue}:`,
39 url: `/img/emojis/${subvalue}.png`,
40 data: {
41 hProperties: {
42 className: 'emoji',
43 align: 'absmiddle',
44 height: '20',
45 width: '20',
46 },
47 },
48 });
49 }
50
51 return false;
52}
53
54function locate(value, fromIndex) {
55 return value.indexOf(colon, fromIndex);
56}
57
58tokenize.locator = locate;
59
60function parser() {
61 const { Parser } = this;
62 const tokenizers = Parser.prototype.inlineTokenizers;
63 const methods = Parser.prototype.inlineMethods;
64
65 tokenizers.gemoji = tokenize;
66
67 methods.splice(methods.indexOf('text'), 0, 'gemoji');
68}
69
70module.exports = parser;
71
72module.exports.sanitize = sanitizeSchema => {
73 // This is for font awesome gemoji codes
74 sanitizeSchema.attributes.i = ['className'];
75
76 // This is for `emoji` class name
77 sanitizeSchema.attributes.img.push('className');
78
79 return parser;
80};