react-codemirror-runmode 2.0.1 → 2.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/README.md +5 -1
- package/dist/highlight.d.ts +3 -3
- package/dist/highlight.js +6 -6
- package/dist/react-highlighter.d.ts +3 -0
- package/dist/react-highlighter.js +4 -3
- package/eslint.config.js +45 -0
- package/package.json +18 -18
- package/src/highlight.ts +8 -5
- package/src/react-highlighter.tsx +8 -2
- package/test/index.spec.tsx +2 -0
- package/tsconfig.json +1 -1
- package/.eslintignore +0 -1
- package/.eslintrc.yml +0 -19
package/README.md
CHANGED
|
@@ -39,14 +39,18 @@ Props:
|
|
|
39
39
|
- `lang`: `string` - The name of the language
|
|
40
40
|
- `theme`: [`Highlighter`](https://lezer.codemirror.net/docs/ref/#highlight.Highlighter) - The highlight style
|
|
41
41
|
- `children`: `string` - The code to highlight
|
|
42
|
+
- `fallbackLanguage`: `Language` - Optional fallback language to use if the specified language isn't found
|
|
43
|
+
- `languages`: `LanguageDescription[]` - Optional custom list of language descriptions
|
|
42
44
|
|
|
43
|
-
### `highlightCode<
|
|
45
|
+
### `highlightCode<o>(languageName, input, highlightStyle, fallbackLanguage?, languages?, callback): Promise<Output[]>`
|
|
44
46
|
|
|
45
47
|
Parameters:
|
|
46
48
|
|
|
47
49
|
- `languageName`: `string` - The name of the language
|
|
48
50
|
- `input`: `string` - The code to highlight
|
|
49
51
|
- `highlighter`: [`Highlighter`](https://lezer.codemirror.net/docs/ref/#highlight.Highlighter) - The highlight style
|
|
52
|
+
- `fallbackLanguage`: `Language` - Optional fallback language to use if the specified language isn't found
|
|
53
|
+
- `languages`: `LanguageDescription[]` - Optional custom list of language descriptions
|
|
50
54
|
- `callback`: `(text: string, style: string | null, from: number, to: number) => Output)` - A callback function that converts the parsed tokens
|
|
51
55
|
|
|
52
56
|
### `getCodeParser(languageName, defaultLanguage?): Promise<Parser | null>`
|
package/dist/highlight.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Parser } from '@lezer/common';
|
|
2
|
-
import { Language } from '@codemirror/language';
|
|
2
|
+
import { Language, LanguageDescription } from '@codemirror/language';
|
|
3
3
|
import { Highlighter } from '@lezer/highlight';
|
|
4
|
-
export declare function getCodeParser(languageName: string,
|
|
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[]>;
|
|
4
|
+
export declare function getCodeParser(languageName: string, fallbackLanguage?: Language, languages?: LanguageDescription[]): Promise<Parser | null>;
|
|
5
|
+
export declare function highlightCode<Output>(languageName: string, input: string, highlighter: Highlighter, fallbackLanguage: Language | undefined, languages: LanguageDescription[] | undefined, callback: (text: string, style: string | null, from: number, to: number) => Output): Promise<Output[]>;
|
package/dist/highlight.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { LanguageDescription } from '@codemirror/language';
|
|
2
2
|
import { highlightTree } from '@lezer/highlight';
|
|
3
|
-
import { languages } from '@codemirror/language-data';
|
|
4
|
-
export async function getCodeParser(languageName,
|
|
5
|
-
if (languageName
|
|
3
|
+
import { languages as builtinLanguages } from '@codemirror/language-data';
|
|
4
|
+
export async function getCodeParser(languageName, fallbackLanguage, languages = builtinLanguages) {
|
|
5
|
+
if (languageName) {
|
|
6
6
|
const found = LanguageDescription.matchLanguageName(languages, languageName, true);
|
|
7
7
|
if (found instanceof LanguageDescription) {
|
|
8
8
|
if (!found.support)
|
|
@@ -12,10 +12,10 @@ export async function getCodeParser(languageName, defaultLanguage) {
|
|
|
12
12
|
else if (found)
|
|
13
13
|
return found.parser;
|
|
14
14
|
}
|
|
15
|
-
return
|
|
15
|
+
return fallbackLanguage ? fallbackLanguage.parser : null;
|
|
16
16
|
}
|
|
17
|
-
export async function highlightCode(languageName, input, highlighter, callback) {
|
|
18
|
-
const parser = await getCodeParser(languageName);
|
|
17
|
+
export async function highlightCode(languageName, input, highlighter, fallbackLanguage, languages, callback) {
|
|
18
|
+
const parser = await getCodeParser(languageName, fallbackLanguage, languages);
|
|
19
19
|
if (parser) {
|
|
20
20
|
const tree = parser.parse(input);
|
|
21
21
|
const output = [];
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import type { Highlighter as LezerHighlighter } from '@lezer/highlight';
|
|
3
|
+
import type { Language, LanguageDescription } from '@codemirror/language';
|
|
3
4
|
export type HighlighterProps = {
|
|
4
5
|
lang: string;
|
|
5
6
|
children: string;
|
|
6
7
|
theme: LezerHighlighter;
|
|
8
|
+
fallbackLanguage?: Language;
|
|
9
|
+
languages?: LanguageDescription[];
|
|
7
10
|
};
|
|
8
11
|
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, theme } = props;
|
|
5
|
+
const { lang, children: code, theme, fallbackLanguage, languages } = props;
|
|
6
6
|
const [highlightedCode, setHighlightedCode] = useState(null);
|
|
7
7
|
useEffect(() => {
|
|
8
|
-
highlightCode(lang, code, theme, (text, style, from) => {
|
|
8
|
+
highlightCode(lang, code, theme, fallbackLanguage, languages, (text, style, from) => {
|
|
9
9
|
return (_jsx("span", { className: style || '', children: text }, from));
|
|
10
10
|
}).then(setHighlightedCode);
|
|
11
11
|
}, [lang, code, theme]);
|
|
12
12
|
return _jsx(_Fragment, { children: highlightedCode || code });
|
|
13
13
|
});
|
|
14
|
+
Highlighter.displayName = 'Highlighter';
|
package/eslint.config.js
ADDED
|
@@ -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
|
|
3
|
+
"version": "2.1.0",
|
|
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.
|
|
34
|
+
"@codemirror/language": "^6.11.0",
|
|
35
35
|
"@codemirror/language-data": "^6.5.1",
|
|
36
|
-
"@lezer/common": "^1.2.
|
|
37
|
-
"@lezer/highlight": "^1.2.
|
|
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": "^
|
|
45
|
-
"@types/node": "^
|
|
46
|
-
"@types/react": "^
|
|
47
|
-
"@
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"eslint": "^
|
|
51
|
-
"
|
|
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.
|
|
55
|
-
"react": "^
|
|
56
|
-
"typescript": "^5.
|
|
57
|
-
"
|
|
58
|
-
"
|
|
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,13 +1,14 @@
|
|
|
1
1
|
import { Parser } from '@lezer/common'
|
|
2
2
|
import { Language, LanguageDescription } from '@codemirror/language'
|
|
3
3
|
import { Highlighter, highlightTree } from '@lezer/highlight'
|
|
4
|
-
import { languages } from '@codemirror/language-data'
|
|
4
|
+
import { languages as builtinLanguages } from '@codemirror/language-data'
|
|
5
5
|
|
|
6
6
|
export async function getCodeParser(
|
|
7
7
|
languageName: string,
|
|
8
|
-
|
|
8
|
+
fallbackLanguage?: Language,
|
|
9
|
+
languages: LanguageDescription[] = builtinLanguages
|
|
9
10
|
): Promise<Parser | null> {
|
|
10
|
-
if (languageName
|
|
11
|
+
if (languageName) {
|
|
11
12
|
const found = LanguageDescription.matchLanguageName(
|
|
12
13
|
languages,
|
|
13
14
|
languageName,
|
|
@@ -18,13 +19,15 @@ export async function getCodeParser(
|
|
|
18
19
|
return found.support ? found.support.language.parser : null
|
|
19
20
|
} else if (found) return (found as any).parser
|
|
20
21
|
}
|
|
21
|
-
return
|
|
22
|
+
return fallbackLanguage ? fallbackLanguage.parser : null
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
export async function highlightCode<Output>(
|
|
25
26
|
languageName: string,
|
|
26
27
|
input: string,
|
|
27
28
|
highlighter: Highlighter,
|
|
29
|
+
fallbackLanguage: Language | undefined,
|
|
30
|
+
languages: LanguageDescription[] | undefined,
|
|
28
31
|
callback: (
|
|
29
32
|
text: string,
|
|
30
33
|
style: string | null,
|
|
@@ -32,7 +35,7 @@ export async function highlightCode<Output>(
|
|
|
32
35
|
to: number
|
|
33
36
|
) => Output
|
|
34
37
|
): Promise<Output[]> {
|
|
35
|
-
const parser = await getCodeParser(languageName)
|
|
38
|
+
const parser = await getCodeParser(languageName, fallbackLanguage, languages)
|
|
36
39
|
if (parser) {
|
|
37
40
|
const tree = parser.parse(input)
|
|
38
41
|
const output: Array<Output> = []
|
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
import React, { memo, useEffect, useState } from 'react'
|
|
2
|
-
import { highlightCode } from './highlight'
|
|
2
|
+
import { highlightCode } from './highlight.js'
|
|
3
3
|
import type { Highlighter as LezerHighlighter } from '@lezer/highlight'
|
|
4
|
+
import type { Language, LanguageDescription } from '@codemirror/language'
|
|
4
5
|
|
|
5
6
|
export type HighlighterProps = {
|
|
6
7
|
lang: string
|
|
7
8
|
children: string
|
|
8
9
|
theme: LezerHighlighter
|
|
10
|
+
fallbackLanguage?: Language
|
|
11
|
+
languages?: LanguageDescription[]
|
|
9
12
|
}
|
|
10
13
|
|
|
11
14
|
export const Highlighter = memo<HighlighterProps>((props: HighlighterProps) => {
|
|
12
|
-
const { lang, children: code, theme } = props
|
|
15
|
+
const { lang, children: code, theme, fallbackLanguage, languages } = props
|
|
13
16
|
const [highlightedCode, setHighlightedCode] = useState<
|
|
14
17
|
React.ReactNode[] | null
|
|
15
18
|
>(null)
|
|
@@ -19,6 +22,8 @@ export const Highlighter = memo<HighlighterProps>((props: HighlighterProps) => {
|
|
|
19
22
|
lang,
|
|
20
23
|
code,
|
|
21
24
|
theme,
|
|
25
|
+
fallbackLanguage,
|
|
26
|
+
languages,
|
|
22
27
|
(text: string, style: string | null, from: number) => {
|
|
23
28
|
return (
|
|
24
29
|
<span key={from} className={style || ''}>
|
|
@@ -31,3 +36,4 @@ export const Highlighter = memo<HighlighterProps>((props: HighlighterProps) => {
|
|
|
31
36
|
|
|
32
37
|
return <>{highlightedCode || code}</>
|
|
33
38
|
})
|
|
39
|
+
Highlighter.displayName = 'Highlighter'
|
package/test/index.spec.tsx
CHANGED
package/tsconfig.json
CHANGED
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
|