@readme/syntax-highlighter 13.0.0 → 13.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@readme/syntax-highlighter",
3
3
  "description": "ReadMe's React-based syntax highlighter",
4
- "version": "13.0.0",
4
+ "version": "13.1.0",
5
5
  "main": "dist/index.node.js",
6
6
  "browser": "dist/index.js",
7
7
  "license": "ISC",
@@ -27,10 +27,10 @@
27
27
  "codemirror": "^5.54.0",
28
28
  "codemirror-graphql": "1.0.2",
29
29
  "prop-types": "^15.7.2",
30
- "react-codemirror2": "^7.2.1"
30
+ "react-codemirror2": "^8.0.0"
31
31
  },
32
32
  "peerDependencies": {
33
- "@readme/variable": "15.x || 16.x",
33
+ "@readme/variable": "^16.2.0",
34
34
  "react": "16.x || 17.x || 18.x",
35
35
  "react-dom": "16.x || 17.x || 18.x"
36
36
  },
@@ -39,11 +39,11 @@
39
39
  "@babel/preset-react": "^7.24.1",
40
40
  "@readme/eslint-config": "^14.0.0",
41
41
  "@testing-library/jest-dom": "^6.4.2",
42
- "@testing-library/react": "^14.2.2",
42
+ "@testing-library/react": "^15.0.5",
43
43
  "babel-jest": "^29.7.0",
44
44
  "babel-loader": "^9.1.3",
45
45
  "core-js": "^3.36.1",
46
- "css-loader": "^6.7.3",
46
+ "css-loader": "^7.1.1",
47
47
  "eslint": "^8.57.0",
48
48
  "glob": "^10.3.12",
49
49
  "html-webpack-plugin": "^5.6.0",
@@ -52,9 +52,10 @@
52
52
  "jest-environment-jsdom": "^29.7.0",
53
53
  "node-sass": "^9.0.0",
54
54
  "prettier": "^3.2.5",
55
+ "prettier-2": "npm:prettier@^2.8.8",
55
56
  "regenerator-runtime": "^0.14.1",
56
57
  "sass-loader": "^13.3.2",
57
- "style-loader": "^3.3.4",
58
+ "style-loader": "^4.0.0",
58
59
  "terser-webpack-plugin": "^5.3.10",
59
60
  "webpack": "^5.91.0",
60
61
  "webpack-cli": "^5.1.4",
@@ -1,4 +1,4 @@
1
- import Variable, { VARIABLE_REGEXP, VariablesContext } from '@readme/variable';
1
+ import Variable, { MDX_VARIABLE_REGEXP, VARIABLE_REGEXP, VariablesContext } from '@readme/variable';
2
2
  import CodeMirror from 'codemirror';
3
3
  import 'codemirror/addon/fold/brace-fold';
4
4
  import 'codemirror/addon/fold/foldgutter';
@@ -170,19 +170,26 @@ const extractVariables = (code, opts) => {
170
170
  let offsetDelta = 0;
171
171
  const variables = [];
172
172
 
173
- const extracter = (match, capture, offset) => {
174
- const unescaped = match.replace(/^\\<</, '<<').replace(/\\>>$/, '>>');
173
+ const extracter = (match, ...rest) => {
174
+ const unescaped = opts.mdx
175
+ ? match.replace(/^\\{/, '{').replace(/\\}$/, '}')
176
+ : match.replace(/^\\<</, '<<').replace(/\\>>$/, '>>');
177
+
175
178
  if (unescaped !== match) {
176
179
  return unescaped;
177
180
  }
178
181
 
182
+ const capture = rest[opts.mdx ? 1 : 0];
183
+ const offset = rest[opts.mdx ? 3 : 1];
184
+
179
185
  variables.push({ text: capture, offset: offset - offsetDelta });
180
186
  offsetDelta += match.length;
181
187
 
182
188
  return '';
183
189
  };
184
190
 
185
- const codeWithoutVars = code.replace(new RegExp(VARIABLE_REGEXP, 'g'), extracter);
191
+ const regexp = opts.mdx ? MDX_VARIABLE_REGEXP : VARIABLE_REGEXP;
192
+ const codeWithoutVars = code.replace(new RegExp(regexp, 'g'), extracter);
186
193
 
187
194
  return [codeWithoutVars, makeReinserter(variables)];
188
195
  };
@@ -199,11 +206,16 @@ StyledSyntaxHighlighter.propTypes = {
199
206
  * @arg {{}} opts
200
207
  * @return {[Element]} Array of DOM Elements
201
208
  */
202
- const ReadmeCodeMirror = (code, lang, opts = { tokenizeVariables: false, highlightMode: false, ranges: [] }) => {
209
+ const ReadmeCodeMirror = (
210
+ code,
211
+ lang,
212
+ opts = { tokenizeVariables: false, highlightMode: false, ranges: [] },
213
+ { mdx = false } = {},
214
+ ) => {
203
215
  const mode = getMode(lang);
204
216
  const output = [];
205
217
 
206
- const [codeWithoutVars, reinsertVariables] = extractVariables(code, { scrollbarStyle: 'overlay', ...opts });
218
+ const [codeWithoutVars, reinsertVariables] = extractVariables(code, { scrollbarStyle: 'overlay', mdx, ...opts });
207
219
 
208
220
  let curStyle = null;
209
221
  let accum = '';
package/src/index.js CHANGED
@@ -18,8 +18,10 @@ const SyntaxHighlighter = (
18
18
  inline: false,
19
19
  tokenizeVariables: false,
20
20
  },
21
- editorProps = {},
21
+ props = {},
22
22
  ) => {
23
+ // eslint-disable-next-line react/prop-types
24
+ const { mdx, ...editorProps } = props;
23
25
  let theme = opts.dark ? 'material-palenight' : 'neo';
24
26
  if (opts.customTheme) {
25
27
  theme = opts.customTheme;
@@ -57,7 +59,7 @@ const SyntaxHighlighter = (
57
59
  return React.createElement(
58
60
  wrapper,
59
61
  { className: classes.join(' '), 'data-testid': 'SyntaxHighlighter' },
60
- codemirror(typeof code === 'string' ? code : '', lang, opts),
62
+ codemirror(typeof code === 'string' ? code : '', lang, opts, { mdx }),
61
63
  );
62
64
  };
63
65