-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathvariable-parser.js
More file actions
66 lines (52 loc) · 1.65 KB
/
variable-parser.js
File metadata and controls
66 lines (52 loc) · 1.65 KB
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
const { VARIABLE_REGEXP } = require('@readme/variable');
const { insertInlineTokenizerBefore } = require('./utils');
function tokenizeVariable(eat, value, silent) {
// Modifies the regular expression to match from
// the start of the line
const match = new RegExp(`^${VARIABLE_REGEXP}`).exec(value);
if (!match) return false;
/* istanbul ignore if */
if (silent) return true;
// Escaped variables should just return the text
if (match[0].startsWith('\\')) {
return eat(match[0])({
type: 'text',
value: match[0].replace(/\\/g, ''),
});
}
if (match[1].startsWith('glossary:')) {
return eat(match[0])({
type: 'readme-glossary-item',
data: {
hName: 'readme-glossary-item',
hProperties: { term: match[1].replace('glossary:', '') },
},
});
}
return eat(match[0])({
type: 'readme-variable',
text: match[1],
data: { hName: 'readme-variable', hProperties: { variable: match[1] } },
});
}
function locate(value, fromIndex) {
return value.indexOf('<<', fromIndex);
}
tokenizeVariable.locator = locate;
function parser() {
insertInlineTokenizerBefore.call(this, {
name: 'variable',
before: 'text',
tokenizer: tokenizeVariable,
});
}
module.exports = parser;
module.exports.sanitize = sanitizeSchema => {
// This is for our custom variable tags <<apiKey>>
sanitizeSchema.tagNames.push('readme-variable');
sanitizeSchema.attributes['readme-variable'] = ['variable'];
// This is for our glossary variable tags <<glossary:item>>
sanitizeSchema.tagNames.push('readme-glossary-item');
sanitizeSchema.attributes['readme-glossary-item'] = ['term'];
return parser;
};