UNPKG

1.7 kBJavaScriptView Raw
1const { VARIABLE_REGEXP } = require('@readme/variable');
2
3function tokenizeVariable(eat, value, silent) {
4 // Modifies the regular expression to match from
5 // the start of the line
6 const match = new RegExp(`^${VARIABLE_REGEXP}`).exec(value);
7
8 if (!match) return false;
9
10 if (silent) return true;
11
12 // Escaped variables should just return the text
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
36function locate(value, fromIndex) {
37 return value.indexOf('<<', fromIndex);
38}
39
40tokenizeVariable.locator = locate;
41
42function 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
52module.exports = parser;
53
54module.exports.sanitize = sanitizeSchema => {
55 // This is for our custom variable tags <<apiKey>>
56 sanitizeSchema.tagNames.push('readme-variable');
57 sanitizeSchema.attributes['readme-variable'] = ['variable'];
58
59 // This is for our glossary variable tags <<glossary:item>>
60 sanitizeSchema.tagNames.push('readme-glossary-item');
61 sanitizeSchema.attributes['readme-glossary-item'] = ['term'];
62
63 return parser;
64};