Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Tidy up code and use just one regex for escaped and non-escaped
Browse files Browse the repository at this point in the history
  • Loading branch information
Dom Harrington committed Jun 27, 2018
1 parent 9d6b95e commit d7f8f6d
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions packages/api-explorer/src/lib/markdown/variable-parser.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
function tokenizeVariable(eat, value, silent) {
const match = /^<<([-\w:\s]+)>>/.exec(value);

if (!match) {
const escapedMatch = /^\\<<([-\w:]*?)\\>>/.exec(value);
if (escapedMatch) {
return eat(escapedMatch[0])({
type: 'text',
value: escapedMatch[0].replace(/\\/g, ''),
});
}
return false;
}
// Regex to match the following:
// - \<<apiKey\>> - escaped variables
// - <<apiKey>> - regular variables
// - <<glossary:glossary items>> - glossary
const match = /^(?:\\)?<<([-\w:\s]+)(?:\\)?>>/.exec(value);

if (!match) return false

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',
Expand Down

0 comments on commit d7f8f6d

Please sign in to comment.