From f8e8de70c8cb1608c96f376aa8616e4a2ac46dcc Mon Sep 17 00:00:00 2001 From: Kelly Joseph Price Date: Thu, 20 Jul 2023 11:34:20 -0700 Subject: [PATCH] fix: dont variable-ize escaped sequences (#458) * fix: dont variable-ize escaped sequences * chore: oops --- __tests__/codeMirror.test.js | 6 ++++++ src/codeMirror/index.jsx | 9 +++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/__tests__/codeMirror.test.js b/__tests__/codeMirror.test.js index 0803d37..1d4a19b 100644 --- a/__tests__/codeMirror.test.js +++ b/__tests__/codeMirror.test.js @@ -97,6 +97,12 @@ describe('variable substitution', () => { 'APIKEY NAME' ); }); + + it('should NOT tokenize escaped variables', () => { + expect(mount(syntaxHighlighter('\\<>', 'json', { tokenizeVariables: true })).text()).toBe('<>'); + expect(mount(syntaxHighlighter('<>', 'json', { tokenizeVariables: true })).text()).toBe('<>'); + expect(mount(syntaxHighlighter('\\<>', 'json', { tokenizeVariables: true })).text()).toBe('<>'); + }); }); describe('Supported languages', () => { diff --git a/src/codeMirror/index.jsx b/src/codeMirror/index.jsx index 513589c..d76fe1d 100644 --- a/src/codeMirror/index.jsx +++ b/src/codeMirror/index.jsx @@ -169,9 +169,14 @@ const extractVariables = (code, opts) => { let offsetDelta = 0; const variables = []; - const extracter = ({ length }, capture, offset) => { + const extracter = (match, capture, offset) => { + const unescaped = match.replace(/^\\<>$/, '>>'); + if (unescaped !== match) { + return unescaped; + } + variables.push({ text: capture, offset: offset - offsetDelta }); - offsetDelta += length; + offsetDelta += match.length; return ''; };