react-codemirror-runmode 2.2.1 → 2.4.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/.oxfmtrc.json +9 -0
- package/.oxlintrc.json +31 -0
- package/README.md +40 -6
- package/dist/highlight.d.ts +14 -4
- package/dist/highlight.js +8 -7
- package/dist/react-highlighter.d.ts +16 -3
- package/dist/react-highlighter.js +6 -5
- package/dist/src/highlight.d.ts +16 -0
- package/dist/src/highlight.js +76 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +2 -0
- package/dist/src/react-highlighter.d.ts +24 -0
- package/dist/src/react-highlighter.js +14 -0
- package/package.json +41 -43
- package/pnpm-workspace.yaml +43 -0
- package/src/highlight.ts +32 -33
- package/src/react-highlighter.tsx +33 -11
- package/test/fixtures/large.html +17604 -0
- package/test/index.spec.tsx +81 -5
- package/test/perf.bench.tsx +64 -0
- package/tsconfig.build.json +3 -0
- package/tsconfig.json +2 -0
- package/vite.config.ts +1 -2
- package/.prettierignore +0 -137
- package/.prettierrc.json +0 -10
- package/eslint.config.js +0 -45
|
@@ -1,21 +1,34 @@
|
|
|
1
|
-
import React, { memo, useEffect, useState } from 'react'
|
|
2
|
-
import { highlightCode } from './highlight.js'
|
|
3
|
-
import type { Highlighter as LezerHighlighter } from '@lezer/highlight'
|
|
4
1
|
import type { Language, LanguageDescription } from '@codemirror/language'
|
|
2
|
+
import type { Highlighter as LezerHighlighter } from '@lezer/highlight'
|
|
3
|
+
import React, { memo, useEffect, useState } from 'react'
|
|
4
|
+
|
|
5
|
+
import { highlightCode, type MarkdownConfig } from './highlight.js'
|
|
5
6
|
|
|
6
7
|
export type HighlighterProps = {
|
|
7
8
|
lang: string
|
|
8
9
|
children: string
|
|
9
|
-
|
|
10
|
+
/**
|
|
11
|
+
* One highlighter, or several whose emitted classes are merged per token.
|
|
12
|
+
* Pass an array to combine, e.g. a generic token theme with a
|
|
13
|
+
* markdown-specific one.
|
|
14
|
+
*/
|
|
15
|
+
theme: LezerHighlighter | readonly LezerHighlighter[]
|
|
10
16
|
fallbackLanguage?: Language
|
|
11
17
|
languages?: LanguageDescription[]
|
|
18
|
+
/**
|
|
19
|
+
* Markdown parser options, used only when `lang` is `markdown`/`md`. Enables
|
|
20
|
+
* GFM (`base: markdownLanguage`) and custom Lezer markdown extensions. Pass a
|
|
21
|
+
* stable reference (e.g. a module-level constant) to avoid re-highlighting on
|
|
22
|
+
* every render.
|
|
23
|
+
*/
|
|
24
|
+
markdownConfig?: MarkdownConfig
|
|
12
25
|
}
|
|
13
26
|
|
|
27
|
+
const WRAPPER_STYLE = { display: 'contents' } as const
|
|
28
|
+
|
|
14
29
|
export const Highlighter = memo<HighlighterProps>((props: HighlighterProps) => {
|
|
15
|
-
const { lang, children: code, theme, fallbackLanguage, languages } = props
|
|
16
|
-
const [highlightedCode, setHighlightedCode] = useState<
|
|
17
|
-
React.ReactNode[] | null
|
|
18
|
-
>(null)
|
|
30
|
+
const { lang, children: code, theme, fallbackLanguage, languages, markdownConfig } = props
|
|
31
|
+
const [highlightedCode, setHighlightedCode] = useState<React.ReactNode[] | null>(null)
|
|
19
32
|
|
|
20
33
|
useEffect(() => {
|
|
21
34
|
highlightCode(
|
|
@@ -30,10 +43,19 @@ export const Highlighter = memo<HighlighterProps>((props: HighlighterProps) => {
|
|
|
30
43
|
{text}
|
|
31
44
|
</span>
|
|
32
45
|
)
|
|
33
|
-
}
|
|
46
|
+
},
|
|
47
|
+
markdownConfig
|
|
34
48
|
).then(setHighlightedCode)
|
|
35
|
-
}, [lang, code, theme])
|
|
49
|
+
}, [lang, code, theme, markdownConfig])
|
|
36
50
|
|
|
37
|
-
return
|
|
51
|
+
return highlightedCode ? (
|
|
52
|
+
<span key="highlighted" style={WRAPPER_STYLE}>
|
|
53
|
+
{highlightedCode}
|
|
54
|
+
</span>
|
|
55
|
+
) : (
|
|
56
|
+
<span key="pending" style={WRAPPER_STYLE}>
|
|
57
|
+
{code}
|
|
58
|
+
</span>
|
|
59
|
+
)
|
|
38
60
|
})
|
|
39
61
|
Highlighter.displayName = 'Highlighter'
|