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.
@@ -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
- theme: LezerHighlighter
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 <>{highlightedCode || code}</>
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'