@@ -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+
110166describe ( 'Supported languages' , ( ) => {
111167 const languages = fixtures . map ( fixture => {
112168 return [ uppercase ( path . basename ( fixture ) ) , fixture ] ;
0 commit comments