react-codemirror-runmode 2.0.0 → 2.0.2

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/README.md CHANGED
@@ -20,7 +20,7 @@ import ReactDOM from 'react-dom/client'
20
20
  const code = 'const x = 123'
21
21
 
22
22
  ReactDOM.createRoot(document.getElementById('app')!).render(
23
- <Highlighter lang="js" highlightStyle={oneDarkHighlightStyle}>
23
+ <Highlighter lang="js" theme={oneDarkHighlightStyle}>
24
24
  {code}
25
25
  </Highlighter>
26
26
  )
@@ -37,7 +37,7 @@ You can apply custom themes using CodeMirror's theme system. This component uses
37
37
  Props:
38
38
 
39
39
  - `lang`: `string` - The name of the language
40
- - `highlightStyle`: [`HighlightStyle`](https://codemirror.net/docs/ref#language.HighlightStyle) - The highlight style
40
+ - `theme`: [`Highlighter`](https://lezer.codemirror.net/docs/ref/#highlight.Highlighter) - The highlight style
41
41
  - `children`: `string` - The code to highlight
42
42
 
43
43
  ### `highlightCode<Output>(languageName, input, highlightStyle, callback): Promise<Output[]>`
@@ -46,7 +46,7 @@ Parameters:
46
46
 
47
47
  - `languageName`: `string` - The name of the language
48
48
  - `input`: `string` - The code to highlight
49
- - `highlightStyle`: [`HighlightStyle`](https://codemirror.net/docs/ref#language.HighlightStyle) - The highlight style
49
+ - `highlighter`: [`Highlighter`](https://lezer.codemirror.net/docs/ref/#highlight.Highlighter) - The highlight style
50
50
  - `callback`: `(text: string, style: string | null, from: number, to: number) => Output)` - A callback function that converts the parsed tokens
51
51
 
52
52
  ### `getCodeParser(languageName, defaultLanguage?): Promise<Parser | null>`
@@ -1,4 +1,5 @@
1
1
  import { Parser } from '@lezer/common';
2
- import { HighlightStyle, Language } from '@codemirror/language';
2
+ import { Language } from '@codemirror/language';
3
+ import { Highlighter } from '@lezer/highlight';
3
4
  export declare function getCodeParser(languageName: string, defaultLanguage?: Language): Promise<Parser | null>;
4
- export declare function highlightCode<Output>(languageName: string, input: string, highlightStyle: HighlightStyle, callback: (text: string, style: string | null, from: number, to: number) => Output): Promise<Output[]>;
5
+ export declare function highlightCode<Output>(languageName: string, input: string, highlighter: Highlighter, callback: (text: string, style: string | null, from: number, to: number) => Output): Promise<Output[]>;
package/dist/highlight.js CHANGED
@@ -14,13 +14,13 @@ export async function getCodeParser(languageName, defaultLanguage) {
14
14
  }
15
15
  return defaultLanguage ? defaultLanguage.parser : null;
16
16
  }
17
- export async function highlightCode(languageName, input, highlightStyle, callback) {
17
+ export async function highlightCode(languageName, input, highlighter, callback) {
18
18
  const parser = await getCodeParser(languageName);
19
19
  if (parser) {
20
20
  const tree = parser.parse(input);
21
21
  const output = [];
22
22
  let pos = 0;
23
- highlightTree(tree, highlightStyle, (from, to, classes) => {
23
+ highlightTree(tree, highlighter, (from, to, classes) => {
24
24
  if (from > pos)
25
25
  output.push(callback(input.slice(pos, from), null, pos, from));
26
26
  output.push(callback(input.slice(from, to), classes, from, to));
@@ -1,8 +1,8 @@
1
1
  import React from 'react';
2
- import type { HighlightStyle } from '@codemirror/language';
2
+ import type { Highlighter as LezerHighlighter } from '@lezer/highlight';
3
3
  export type HighlighterProps = {
4
4
  lang: string;
5
5
  children: string;
6
- highlightStyle: HighlightStyle;
6
+ theme: LezerHighlighter;
7
7
  };
8
8
  export declare const Highlighter: React.NamedExoticComponent<HighlighterProps>;
@@ -1,13 +1,14 @@
1
1
  import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
2
2
  import { memo, useEffect, useState } from 'react';
3
- import { highlightCode } from './highlight';
3
+ import { highlightCode } from './highlight.js';
4
4
  export const Highlighter = memo((props) => {
5
- const { lang, children: code, highlightStyle } = props;
5
+ const { lang, children: code, theme } = props;
6
6
  const [highlightedCode, setHighlightedCode] = useState(null);
7
7
  useEffect(() => {
8
- highlightCode(lang, code, highlightStyle, (text, style, from) => {
8
+ highlightCode(lang, code, theme, (text, style, from) => {
9
9
  return (_jsx("span", { className: style || '', children: text }, from));
10
10
  }).then(setHighlightedCode);
11
- }, [lang, code, highlightStyle]);
11
+ }, [lang, code, theme]);
12
12
  return _jsx(_Fragment, { children: highlightedCode || code });
13
13
  });
14
+ Highlighter.displayName = 'Highlighter';
@@ -0,0 +1,45 @@
1
+ import js from '@eslint/js'
2
+ import eslintTs from 'typescript-eslint'
3
+ import eslintReact from 'eslint-plugin-react'
4
+
5
+ export default eslintTs.config(
6
+ { ignores: ['dist'] },
7
+ js.configs.recommended,
8
+ eslintTs.configs.recommended,
9
+ eslintReact.configs.flat.recommended,
10
+ eslintReact.configs.flat['jsx-runtime'],
11
+ {
12
+ plugins: {
13
+ '@typescript-eslint': eslintTs.plugin
14
+ },
15
+ rules: {
16
+ // TypeScript
17
+ '@typescript-eslint/no-explicit-any': 'off',
18
+ '@typescript-eslint/no-unused-vars': [
19
+ 'warn',
20
+ {
21
+ argsIgnorePattern: '^_',
22
+ varsIgnorePattern: '^_',
23
+ caughtErrorsIgnorePattern: '^_'
24
+ }
25
+ ],
26
+ '@typescript-eslint/ban-ts-comment': 'off',
27
+ '@typescript-eslint/no-this-alias': 'off',
28
+ '@typescript-eslint/no-var-requires': 'off',
29
+ '@typescript-eslint/no-unused-expressions': 'off',
30
+
31
+ // JavaScript and React rules
32
+ 'no-useless-escape': 0,
33
+ 'prefer-const': 2,
34
+ 'no-unused-vars': 0
35
+ }
36
+ },
37
+
38
+ {
39
+ settings: {
40
+ react: {
41
+ version: '19'
42
+ }
43
+ }
44
+ }
45
+ )
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-codemirror-runmode",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "Syntax highlighting for react, utilizing CodeMirror's parser",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -31,30 +31,30 @@
31
31
  "url": "https://github.com/craftzdog/react-codemirror-runmode.git"
32
32
  },
33
33
  "dependencies": {
34
- "@codemirror/language": "^6.10.1",
34
+ "@codemirror/language": "^6.11.0",
35
35
  "@codemirror/language-data": "^6.5.1",
36
- "@lezer/common": "^1.2.1",
37
- "@lezer/highlight": "^1.2.0"
36
+ "@lezer/common": "^1.2.3",
37
+ "@lezer/highlight": "^1.2.1"
38
38
  },
39
39
  "peerDependencies": {
40
40
  "react": ">= 18"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@codemirror/theme-one-dark": "^6.1.2",
44
- "@testing-library/react": "^15.0.7",
45
- "@types/node": "^20.12.11",
46
- "@types/react": "^18.3.1",
47
- "@typescript-eslint/eslint-plugin": "^7.8.0",
48
- "@typescript-eslint/parser": "^7.8.0",
49
- "@vitejs/plugin-react": "^4.2.1",
50
- "eslint": "^8.57.0",
51
- "eslint-config-prettier": "^9.1.0",
52
- "jsdom": "^24.0.0",
44
+ "@testing-library/react": "^16.2.0",
45
+ "@types/node": "^22.13.10",
46
+ "@types/react": "^19.0.12",
47
+ "@vitejs/plugin-react": "^4.3.4",
48
+ "eslint": "^9.22.0",
49
+ "eslint-config-prettier": "^10.1.1",
50
+ "eslint-plugin-react": "^7.37.4",
51
+ "jsdom": "^26.0.0",
53
52
  "npm-run-all": "^4.1.5",
54
- "prettier": "^3.2.5",
55
- "react": "^18.3.1",
56
- "typescript": "^5.4.5",
57
- "vite": "^5.2.11",
58
- "vitest": "^1.6.0"
53
+ "prettier": "^3.5.3",
54
+ "react": "^19.0.0",
55
+ "typescript": "^5.8.2",
56
+ "typescript-eslint": "^8.27.0",
57
+ "vite": "^6.2.2",
58
+ "vitest": "^3.0.9"
59
59
  }
60
60
  }
package/src/highlight.ts CHANGED
@@ -1,10 +1,6 @@
1
1
  import { Parser } from '@lezer/common'
2
- import {
3
- HighlightStyle,
4
- Language,
5
- LanguageDescription
6
- } from '@codemirror/language'
7
- import { highlightTree } from '@lezer/highlight'
2
+ import { Language, LanguageDescription } from '@codemirror/language'
3
+ import { Highlighter, highlightTree } from '@lezer/highlight'
8
4
  import { languages } from '@codemirror/language-data'
9
5
 
10
6
  export async function getCodeParser(
@@ -28,7 +24,7 @@ export async function getCodeParser(
28
24
  export async function highlightCode<Output>(
29
25
  languageName: string,
30
26
  input: string,
31
- highlightStyle: HighlightStyle,
27
+ highlighter: Highlighter,
32
28
  callback: (
33
29
  text: string,
34
30
  style: string | null,
@@ -41,7 +37,7 @@ export async function highlightCode<Output>(
41
37
  const tree = parser.parse(input)
42
38
  const output: Array<Output> = []
43
39
  let pos = 0
44
- highlightTree(tree, highlightStyle, (from, to, classes) => {
40
+ highlightTree(tree, highlighter, (from, to, classes) => {
45
41
  if (from > pos)
46
42
  output.push(callback(input.slice(pos, from), null, pos, from))
47
43
  output.push(callback(input.slice(from, to), classes, from, to))
@@ -1,15 +1,15 @@
1
1
  import React, { memo, useEffect, useState } from 'react'
2
- import { highlightCode } from './highlight'
3
- import type { HighlightStyle } from '@codemirror/language'
2
+ import { highlightCode } from './highlight.js'
3
+ import type { Highlighter as LezerHighlighter } from '@lezer/highlight'
4
4
 
5
5
  export type HighlighterProps = {
6
6
  lang: string
7
7
  children: string
8
- highlightStyle: HighlightStyle
8
+ theme: LezerHighlighter
9
9
  }
10
10
 
11
11
  export const Highlighter = memo<HighlighterProps>((props: HighlighterProps) => {
12
- const { lang, children: code, highlightStyle } = props
12
+ const { lang, children: code, theme } = props
13
13
  const [highlightedCode, setHighlightedCode] = useState<
14
14
  React.ReactNode[] | null
15
15
  >(null)
@@ -18,7 +18,7 @@ export const Highlighter = memo<HighlighterProps>((props: HighlighterProps) => {
18
18
  highlightCode(
19
19
  lang,
20
20
  code,
21
- highlightStyle,
21
+ theme,
22
22
  (text: string, style: string | null, from: number) => {
23
23
  return (
24
24
  <span key={from} className={style || ''}>
@@ -27,7 +27,8 @@ export const Highlighter = memo<HighlighterProps>((props: HighlighterProps) => {
27
27
  )
28
28
  }
29
29
  ).then(setHighlightedCode)
30
- }, [lang, code, highlightStyle])
30
+ }, [lang, code, theme])
31
31
 
32
32
  return <>{highlightedCode || code}</>
33
33
  })
34
+ Highlighter.displayName = 'Highlighter'
@@ -70,7 +70,7 @@ describe('React Highlighter', () => {
70
70
  const code = 'const x = 123'
71
71
  render(
72
72
  <code className="mde-preview">
73
- <Highlighter lang="js" highlightStyle={oneDarkHighlightStyle}>
73
+ <Highlighter lang="js" theme={oneDarkHighlightStyle}>
74
74
  {code}
75
75
  </Highlighter>
76
76
  </code>
package/tsconfig.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "compilerOptions": {
4
4
  "target": "esnext",
5
5
  "module": "esnext",
6
- "lib": ["es2022", "dom"],
6
+ "lib": ["es2024", "dom"],
7
7
  "jsx": "react-jsx",
8
8
  "esModuleInterop": true,
9
9
  "strict": true,
package/.eslintignore DELETED
@@ -1 +0,0 @@
1
- dist
package/.eslintrc.yml DELETED
@@ -1,19 +0,0 @@
1
- root: true
2
- extends:
3
- - plugin:@typescript-eslint/eslint-recommended
4
- - plugin:@typescript-eslint/recommended
5
- - prettier
6
- parser: '@typescript-eslint/parser'
7
- parserOptions:
8
- ecmaVersion: 2022
9
- sourceType: 'module'
10
- rules:
11
- no-useless-escape: 0
12
- '@typescript-eslint/no-explicit-any': 0
13
- '@typescript-eslint/no-unused-vars':
14
- - 2
15
- - argsIgnorePattern: ^_
16
- varsIgnorePattern: ^_
17
- '@typescript-eslint/ban-ts-comment': 0
18
- '@typescript-eslint/no-this-alias': 0
19
- '@typescript-eslint/no-var-requires': 0