react-codemirror-runmode 2.0.0 → 2.0.1
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 +3 -3
- package/dist/highlight.d.ts +3 -2
- package/dist/highlight.js +2 -2
- package/dist/react-highlighter.d.ts +2 -2
- package/dist/react-highlighter.js +3 -3
- package/package.json +1 -1
- package/src/highlight.ts +4 -8
- package/src/react-highlighter.tsx +5 -5
- package/test/index.spec.tsx +1 -1
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"
|
|
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
|
-
- `
|
|
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
|
-
- `
|
|
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>`
|
package/dist/highlight.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Parser } from '@lezer/common';
|
|
2
|
-
import {
|
|
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,
|
|
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,
|
|
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,
|
|
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 {
|
|
2
|
+
import type { Highlighter as LezerHighlighter } from '@lezer/highlight';
|
|
3
3
|
export type HighlighterProps = {
|
|
4
4
|
lang: string;
|
|
5
5
|
children: string;
|
|
6
|
-
|
|
6
|
+
theme: LezerHighlighter;
|
|
7
7
|
};
|
|
8
8
|
export declare const Highlighter: React.NamedExoticComponent<HighlighterProps>;
|
|
@@ -2,12 +2,12 @@ import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
|
|
|
2
2
|
import { memo, useEffect, useState } from 'react';
|
|
3
3
|
import { highlightCode } from './highlight';
|
|
4
4
|
export const Highlighter = memo((props) => {
|
|
5
|
-
const { lang, children: code,
|
|
5
|
+
const { lang, children: code, theme } = props;
|
|
6
6
|
const [highlightedCode, setHighlightedCode] = useState(null);
|
|
7
7
|
useEffect(() => {
|
|
8
|
-
highlightCode(lang, code,
|
|
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,
|
|
11
|
+
}, [lang, code, theme]);
|
|
12
12
|
return _jsx(_Fragment, { children: highlightedCode || code });
|
|
13
13
|
});
|
package/package.json
CHANGED
package/src/highlight.ts
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import { Parser } from '@lezer/common'
|
|
2
|
-
import {
|
|
3
|
-
|
|
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
|
-
|
|
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,
|
|
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
2
|
import { highlightCode } from './highlight'
|
|
3
|
-
import type {
|
|
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
|
-
|
|
8
|
+
theme: LezerHighlighter
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
export const Highlighter = memo<HighlighterProps>((props: HighlighterProps) => {
|
|
12
|
-
const { lang, children: code,
|
|
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
|
-
|
|
21
|
+
theme,
|
|
22
22
|
(text: string, style: string | null, from: number) => {
|
|
23
23
|
return (
|
|
24
24
|
<span key={from} className={style || ''}>
|
|
@@ -27,7 +27,7 @@ export const Highlighter = memo<HighlighterProps>((props: HighlighterProps) => {
|
|
|
27
27
|
)
|
|
28
28
|
}
|
|
29
29
|
).then(setHighlightedCode)
|
|
30
|
-
}, [lang, code,
|
|
30
|
+
}, [lang, code, theme])
|
|
31
31
|
|
|
32
32
|
return <>{highlightedCode || code}</>
|
|
33
33
|
})
|
package/test/index.spec.tsx
CHANGED
|
@@ -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"
|
|
73
|
+
<Highlighter lang="js" theme={oneDarkHighlightStyle}>
|
|
74
74
|
{code}
|
|
75
75
|
</Highlighter>
|
|
76
76
|
</code>
|