1 | const Emoji = require('@readme/emojis').emoji;
|
2 |
|
3 | const emojis = new Emoji();
|
4 |
|
5 | const colon = ':';
|
6 |
|
7 | function tokenize(eat, value, silent) {
|
8 |
|
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 |
|
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 |
|
54 | function locate(value, fromIndex) {
|
55 | return value.indexOf(colon, fromIndex);
|
56 | }
|
57 |
|
58 | tokenize.locator = locate;
|
59 |
|
60 | function 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 |
|
70 | module.exports = parser;
|
71 |
|
72 | module.exports.sanitize = sanitizeSchema => {
|
73 |
|
74 | sanitizeSchema.attributes.i = ['className'];
|
75 |
|
76 |
|
77 | sanitizeSchema.attributes.img.push('className');
|
78 |
|
79 | return parser;
|
80 | };
|