@@ -20,7 +20,6 @@ import rehypeSN from '@/lib/rehype-sn'
20
20
import remarkUnicode from '@/lib/remark-unicode'
21
21
import Embed from './embed'
22
22
import remarkMath from 'remark-math'
23
- import rehypeMathjax from 'rehype-mathjax'
24
23
25
24
const rehypeSNStyled = ( ) => rehypeSN ( {
26
25
stylers : [ {
@@ -35,7 +34,6 @@ const rehypeSNStyled = () => rehypeSN({
35
34
} )
36
35
37
36
const remarkPlugins = [ gfm , remarkUnicode , [ remarkMath , { singleDollarTextMath : false } ] ]
38
- const rehypePlugins = [ rehypeSNStyled , rehypeMathjax ]
39
37
40
38
export function SearchText ( { text } ) {
41
39
return (
@@ -55,6 +53,19 @@ export default memo(function Text ({ rel = UNKNOWN_LINK_REL, imgproxyUrls, child
55
53
const router = useRouter ( )
56
54
const [ show , setShow ] = useState ( false )
57
55
const containerRef = useRef ( null )
56
+ const [ mathJaxPlugin , setMathJaxPlugin ] = useState ( null )
57
+
58
+ // we only need mathjax if there's math content between $$ tags
59
+ useEffect ( ( ) => {
60
+ if ( / \$ \$ ( .| \n ) + \$ \$ / g. test ( children ) ) {
61
+ import ( 'rehype-mathjax' ) . then ( mod => {
62
+ setMathJaxPlugin ( ( ) => mod . default )
63
+ } ) . catch ( err => {
64
+ console . error ( 'error loading mathjax' , err )
65
+ setMathJaxPlugin ( null )
66
+ } )
67
+ }
68
+ } , [ children ] )
58
69
59
70
// if we are navigating to a hash, show the full text
60
71
useEffect ( ( ) => {
@@ -133,12 +144,12 @@ export default memo(function Text ({ rel = UNKNOWN_LINK_REL, imgproxyUrls, child
133
144
< ReactMarkdown
134
145
components = { components }
135
146
remarkPlugins = { remarkPlugins }
136
- rehypePlugins = { rehypePlugins }
147
+ rehypePlugins = { [ rehypeSNStyled , mathJaxPlugin ] . filter ( Boolean ) }
137
148
remarkRehypeOptions = { { clobberPrefix : `itemfn-${ itemId } -` } }
138
149
>
139
150
{ children }
140
151
</ ReactMarkdown >
141
- ) , [ components , remarkPlugins , rehypePlugins , children , itemId ] )
152
+ ) , [ components , remarkPlugins , mathJaxPlugin , children , itemId ] )
142
153
143
154
const showOverflow = useCallback ( ( ) => setShow ( true ) , [ setShow ] )
144
155
@@ -230,6 +241,7 @@ function Table ({ node, ...props }) {
230
241
function Code ( { node, inline, className, children, style, ...props } ) {
231
242
const [ ReactSyntaxHighlighter , setReactSyntaxHighlighter ] = useState ( null )
232
243
const [ syntaxTheme , setSyntaxTheme ] = useState ( null )
244
+ const language = className ?. match ( / l a n g u a g e - ( \w + ) / ) ?. [ 1 ] || 'text'
233
245
234
246
const loadHighlighter = useCallback ( ( ) =>
235
247
Promise . all ( [
@@ -239,7 +251,7 @@ function Code ({ node, inline, className, children, style, ...props }) {
239
251
)
240
252
241
253
useEffect ( ( ) => {
242
- if ( ! inline ) {
254
+ if ( ! inline && language !== 'math' ) { // MathJax should handle math
243
255
// loading the syntax highlighter and theme only when needed
244
256
loadHighlighter ( ) . then ( ( [ highlighter , theme ] ) => {
245
257
setReactSyntaxHighlighter ( ( ) => highlighter )
@@ -256,8 +268,6 @@ function Code ({ node, inline, className, children, style, ...props }) {
256
268
)
257
269
}
258
270
259
- const language = className ?. match ( / l a n g u a g e - ( \w + ) / ) ?. [ 1 ] || 'text'
260
-
261
271
return (
262
272
< ReactSyntaxHighlighter style = { syntaxTheme } language = { language } PreTag = 'div' customStyle = { { borderRadius : '0.3rem' } } { ...props } >
263
273
{ children }
0 commit comments