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

Commit 2ad1708

Browse files
author
Dom Harrington
committed
Refactor so that the variable regexp is shared across markdown and highlighter
1 parent 56f2cab commit 2ad1708

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

packages/api-explorer/src/lib/markdown/variable-parser.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
function tokenizeVariable(eat, value, silent) {
2-
// Regex to match the following:
3-
// - \<<apiKey\>> - escaped variables
4-
// - <<apiKey>> - regular variables
5-
// - <<glossary:glossary items>> - glossary
6-
const match = /^(?:\\)?<<([-\w:\s]+)(?:\\)?>>/.exec(value);
2+
// Modifies the regular expression to match from
3+
// the start of the line
4+
const match = new RegExp(`^${module.exports.VARIABLE_REGEXP}`).exec(value);
75

86
if (!match) return false;
97

@@ -62,3 +60,9 @@ module.exports.sanitize = sanitizeSchema => {
6260

6361
return parser;
6462
};
63+
64+
// Regex to match the following:
65+
// - \<<apiKey\>> - escaped variables
66+
// - <<apiKey>> - regular variables
67+
// - <<glossary:glossary items>> - glossary
68+
module.exports.VARIABLE_REGEXP = /(?:\\)?<<([-\w:\s]+)(?:\\)?>>/.source;

packages/syntax-highlighter/codemirror.jsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const CodeMirror = require('codemirror');
22
const React = require('react');
3+
const { VARIABLE_REGEXP } = require('@readme/api-explorer/src/lib/markdown/variable-parser');
34
const Variable = require('@readme/api-explorer/src/Variable');
45

56
require('codemirror/addon/runmode/runmode');
@@ -65,13 +66,9 @@ module.exports = (code, lang, opts = { tokenizeVariables: false }) => {
6566
const mode = getMode(lang);
6667

6768
function tokenizeVariable(value) {
68-
// Regex to match the following:
69-
// - \<<apiKey\>> - escaped variables
70-
// - <<apiKey>> - regular variables
71-
// - '<<apiKey>>' - quoted regular variables (code samples)
72-
// - <<glossary:glossary items>> - glossary
73-
// Also matches any characters that may be enclosing the variable like: ' "
74-
const match = /(.*)(?:\\)?<<([-\w:\s]+)(?:\\)?>>(.*)/.exec(value);
69+
// Modifies the regular expression to match anything
70+
// before or after like quote characters: ' "
71+
const match = new RegExp(`(.*)${VARIABLE_REGEXP}(.*)`).exec(value);
7572

7673
if (!match) return value;
7774

packages/syntax-highlighter/test/index.test.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ test('should concat the same style items', () => {
2323
});
2424

2525

26-
test.only('should work with modes', () => {
26+
test('should work with modes', () => {
2727
expect(shallow(syntaxHighlighter('{ "a": 1 }', 'json')).html()).toBe('<span class="cm-s-neo">{ <span class="cm-property">&quot;a&quot;</span>: <span class="cm-number">1</span> }</span>');
2828
});
2929

0 commit comments

Comments
 (0)