1 | const { VARIABLE_REGEXP } = require('@readme/variable');
|
2 |
|
3 | function tokenizeVariable(eat, value, silent) {
|
4 |
|
5 |
|
6 | const match = new RegExp(`^${VARIABLE_REGEXP}`).exec(value);
|
7 |
|
8 | if (!match) return false;
|
9 |
|
10 | if (silent) return true;
|
11 |
|
12 |
|
13 | if (match[0].startsWith('\\')) {
|
14 | return eat(match[0])({
|
15 | type: 'text',
|
16 | value: match[0].replace(/\\/g, ''),
|
17 | });
|
18 | }
|
19 |
|
20 | if (match[1].startsWith('glossary:')) {
|
21 | return eat(match[0])({
|
22 | type: 'readme-glossary-item',
|
23 | data: {
|
24 | hName: 'readme-glossary-item',
|
25 | hProperties: { term: match[1].replace('glossary:', '') },
|
26 | },
|
27 | });
|
28 | }
|
29 |
|
30 | return eat(match[0])({
|
31 | type: 'readme-variable',
|
32 | data: { hName: 'readme-variable', hProperties: { variable: match[1] } },
|
33 | });
|
34 | }
|
35 |
|
36 | function locate(value, fromIndex) {
|
37 | return value.indexOf('<<', fromIndex);
|
38 | }
|
39 |
|
40 | tokenizeVariable.locator = locate;
|
41 |
|
42 | function parser() {
|
43 | const { Parser } = this;
|
44 | const tokenizers = Parser.prototype.inlineTokenizers;
|
45 | const methods = Parser.prototype.inlineMethods;
|
46 |
|
47 | tokenizers.variable = tokenizeVariable;
|
48 |
|
49 | methods.splice(methods.indexOf('text'), 0, 'variable');
|
50 | }
|
51 |
|
52 | module.exports = parser;
|
53 |
|
54 | module.exports.sanitize = sanitizeSchema => {
|
55 |
|
56 | sanitizeSchema.tagNames.push('readme-variable');
|
57 | sanitizeSchema.attributes['readme-variable'] = ['variable'];
|
58 |
|
59 |
|
60 | sanitizeSchema.tagNames.push('readme-glossary-item');
|
61 | sanitizeSchema.attributes['readme-glossary-item'] = ['term'];
|
62 |
|
63 | return parser;
|
64 | };
|