Skip to content

Commit

Permalink
feat: mdx variable regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyjosephprice committed Jun 10, 2024
1 parent ba19345 commit 3a1ba07
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
44 changes: 43 additions & 1 deletion __tests__/index.test.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { fireEvent, render } = require('@testing-library/react');
const React = require('react');

const { Variable, VARIABLE_REGEXP } = require('../index');
const { Variable, VARIABLE_REGEXP, MDX_VARIABLE_REGEXP } = require('../index');

describe('single variable', () => {
const props = {
Expand Down Expand Up @@ -238,3 +238,45 @@ describe('VARIABLE_REGEXP', () => {
expect('<<what the\r\nfuck>>').not.toMatch(new RegExp(VARIABLE_REGEXP));
});
});

describe('MDX_VARIABLE_REGEXP', () => {
it('should **not** match against periods', () => {
expect('{user.api.key}').not.toMatch(new RegExp(MDX_VARIABLE_REGEXP));
});

it('should **not** match against hyphens', () => {
expect('{user.api-key}').not.toMatch(new RegExp(MDX_VARIABLE_REGEXP));
});

it('should **not** match against spaces', () => {
expect('{user.api key}').not.toMatch(new RegExp(MDX_VARIABLE_REGEXP));
});

it('should **not match against colons', () => {
expect('{user.glossary:term}').not.toMatch(new RegExp(MDX_VARIABLE_REGEXP));
});

it('should match against underscores', () => {
expect('{user.api_key}').toMatch(new RegExp(MDX_VARIABLE_REGEXP));
});

it('should be case insensitive', () => {
expect('{user.apiKeY}').toMatch(new RegExp(MDX_VARIABLE_REGEXP));
});

it('should match non-leading numeric characters', () => {
expect('{user.P2P}').toMatch(new RegExp(MDX_VARIABLE_REGEXP));
expect('{user.123}').not.toMatch(new RegExp(MDX_VARIABLE_REGEXP));
expect('{user.A123}').toMatch(new RegExp(MDX_VARIABLE_REGEXP));
});

it('should match non-english characters', () => {
expect('{user.片仮名}').toMatch(new RegExp(MDX_VARIABLE_REGEXP));
});

it('should NOT match against newlines', () => {
expect('{user.what_the\nfuck}').not.toMatch(new RegExp(MDX_VARIABLE_REGEXP));
expect('{user.what_the\rfuck}').not.toMatch(new RegExp(MDX_VARIABLE_REGEXP));
expect('{user.what_the\r\nfuck}').not.toMatch(new RegExp(MDX_VARIABLE_REGEXP));
});
});
3 changes: 3 additions & 0 deletions index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,8 @@ module.exports.Variable = Variable;
// - <<apiKey>> - regular variables
// - <<glossary:glossary items>> - glossary
module.exports.VARIABLE_REGEXP = /(?:\\)?<<((?:(?![\r\n])[-_\p{L}:.\s\d])+)(?:\\)?>>/iu.source;
// copied from here: https://stackoverflow.com/a/6926184
module.exports.MDX_VARIABLE_REGEXP =
/(\\)?\{user.[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\u200C\u200D]*(\\)?\}/iu.source;
module.exports.VariablesContext = VariablesContext;
module.exports.SelectedAppContext = SelectedAppContext;

0 comments on commit 3a1ba07

Please sign in to comment.