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

Commit

Permalink
Refactor so that the variable regexp is shared across markdown and hi…
Browse files Browse the repository at this point in the history
…ghlighter
  • Loading branch information
Dom Harrington committed Aug 1, 2018
1 parent 56f2cab commit 2ad1708
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
14 changes: 9 additions & 5 deletions packages/api-explorer/src/lib/markdown/variable-parser.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
function tokenizeVariable(eat, value, silent) {
// Regex to match the following:
// - \<<apiKey\>> - escaped variables
// - <<apiKey>> - regular variables
// - <<glossary:glossary items>> - glossary
const match = /^(?:\\)?<<([-\w:\s]+)(?:\\)?>>/.exec(value);
// Modifies the regular expression to match from
// the start of the line
const match = new RegExp(`^${module.exports.VARIABLE_REGEXP}`).exec(value);

if (!match) return false;

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

return parser;
};

// Regex to match the following:
// - \<<apiKey\>> - escaped variables
// - <<apiKey>> - regular variables
// - <<glossary:glossary items>> - glossary
module.exports.VARIABLE_REGEXP = /(?:\\)?<<([-\w:\s]+)(?:\\)?>>/.source;
11 changes: 4 additions & 7 deletions packages/syntax-highlighter/codemirror.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const CodeMirror = require('codemirror');
const React = require('react');
const { VARIABLE_REGEXP } = require('@readme/api-explorer/src/lib/markdown/variable-parser');
const Variable = require('@readme/api-explorer/src/Variable');

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

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

if (!match) return value;

Expand Down
2 changes: 1 addition & 1 deletion packages/syntax-highlighter/test/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test('should concat the same style items', () => {
});


test.only('should work with modes', () => {
test('should work with modes', () => {
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>');
});

Expand Down

0 comments on commit 2ad1708

Please sign in to comment.