Skip to content

Commit 4e236ff

Browse files
feat: mdx vars (#531)
* feat: mdx variables * cleanup * chore: silly eslint * chore(deps): update variable * replace comments
1 parent 99923aa commit 4e236ff

File tree

6 files changed

+832
-946
lines changed

6 files changed

+832
-946
lines changed

__tests__/codeMirror.test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,62 @@ describe('variable substitution', () => {
107107
});
108108
});
109109

110+
describe('variable substitution { mdx: true }', () => {
111+
it('should tokenize variables (double quotes)', () => {
112+
render(syntaxHighlighter('"{user.apiKey}"', 'json', { tokenizeVariables: true }, { mdx: true }));
113+
expect(screen.getByText('APIKEY')).toBeVisible();
114+
});
115+
116+
it('should tokenize variables (single quotes)', () => {
117+
render(syntaxHighlighter("'{user.apiKey}'", 'json', { tokenizeVariables: true }, { mdx: true }));
118+
expect(screen.getByText('APIKEY')).toBeVisible();
119+
});
120+
121+
it('should keep enclosing characters around the variable', () => {
122+
render(syntaxHighlighter("'{user.apiKey}'", 'json', { tokenizeVariables: true }, { mdx: true }));
123+
expect(screen.getByTestId('SyntaxHighlighter')).toHaveTextContent("'APIKEY'");
124+
});
125+
126+
it('should tokenize variables outside of quotes', () => {
127+
render(syntaxHighlighter('{user.apiKey}', 'json', { tokenizeVariables: true }, { mdx: true }));
128+
expect(screen.getByText('APIKEY')).toBeVisible();
129+
});
130+
131+
it('should tokenize variables outside of quotes over multiple lines', () => {
132+
const codeBlock = `
133+
const foo = {user.apiKey};
134+
const bar = {user.name};
135+
136+
fetch({ foo, bar, baz: {user.token} });
137+
`;
138+
139+
render(syntaxHighlighter(codeBlock, 'json', { tokenizeVariables: true }, { mdx: true }));
140+
expect(screen.getByTestId('SyntaxHighlighter').textContent).toMatchInlineSnapshot(`
141+
"
142+
const foo = APIKEY;
143+
const bar = NAME;
144+
145+
fetch({ foo, bar, baz: TOKEN });
146+
"
147+
`);
148+
});
149+
150+
it('should tokenize multiple variables per line', () => {
151+
render(syntaxHighlighter('{user.apiKey} {user.name}', 'json', { tokenizeVariables: true }, { mdx: true }));
152+
expect(screen.getByTestId('SyntaxHighlighter')).toHaveTextContent('APIKEY NAME');
153+
});
154+
155+
it('should not tokenize bracket style', () => {
156+
render(syntaxHighlighter('<<wat>>', 'json', { tokenizeVariables: true }, { mdx: true }));
157+
expect(screen.getByTestId('SyntaxHighlighter')).toHaveTextContent('<<wat>>');
158+
});
159+
160+
it.each(['\\{user.wat}', '{user.wat\\}', '\\{user.wat\\}'])('should NOT tokenize escaped variables %s', code => {
161+
render(syntaxHighlighter(code, 'json', { tokenizeVariables: true }, { mdx: true }));
162+
expect(screen.getByTestId('SyntaxHighlighter')).toHaveTextContent('{user.wat}');
163+
});
164+
});
165+
110166
describe('Supported languages', () => {
111167
const languages = fixtures.map(fixture => {
112168
return [uppercase(path.basename(fixture)), fixture];

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module.exports = {
66
moduleNameMapper: {
77
'^.+\\.(css|less|scss)$': 'identity-obj-proxy',
88
},
9+
prettierPath: require.resolve('prettier-2'),
910
setupFilesAfterEnv: [path.join(__dirname, '__tests__/jest.setup.js')],
1011
testEnvironment: 'jsdom',
1112
testPathIgnorePatterns: ['<rootDir>/__tests__/__fixtures__/', '<rootDir>/__tests__/jest.setup.js'],

0 commit comments

Comments
 (0)