Skip to content

Commit bc3c008

Browse files
Soxasorahuumn
andauthored
Dynamically import MathJax (#1910)
* Dynamically import MathJax * Only load if there's math content; cleanup * avoid loading RSH on Math, we have MathJax for that; cleanup * support multiline mathjax --------- Co-authored-by: k00b <k00b@stacker.news>
1 parent c571ba0 commit bc3c008

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

components/text.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import rehypeSN from '@/lib/rehype-sn'
2020
import remarkUnicode from '@/lib/remark-unicode'
2121
import Embed from './embed'
2222
import remarkMath from 'remark-math'
23-
import rehypeMathjax from 'rehype-mathjax'
2423

2524
const rehypeSNStyled = () => rehypeSN({
2625
stylers: [{
@@ -35,7 +34,6 @@ const rehypeSNStyled = () => rehypeSN({
3534
})
3635

3736
const remarkPlugins = [gfm, remarkUnicode, [remarkMath, { singleDollarTextMath: false }]]
38-
const rehypePlugins = [rehypeSNStyled, rehypeMathjax]
3937

4038
export function SearchText ({ text }) {
4139
return (
@@ -55,6 +53,19 @@ export default memo(function Text ({ rel = UNKNOWN_LINK_REL, imgproxyUrls, child
5553
const router = useRouter()
5654
const [show, setShow] = useState(false)
5755
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])
5869

5970
// if we are navigating to a hash, show the full text
6071
useEffect(() => {
@@ -133,12 +144,12 @@ export default memo(function Text ({ rel = UNKNOWN_LINK_REL, imgproxyUrls, child
133144
<ReactMarkdown
134145
components={components}
135146
remarkPlugins={remarkPlugins}
136-
rehypePlugins={rehypePlugins}
147+
rehypePlugins={[rehypeSNStyled, mathJaxPlugin].filter(Boolean)}
137148
remarkRehypeOptions={{ clobberPrefix: `itemfn-${itemId}-` }}
138149
>
139150
{children}
140151
</ReactMarkdown>
141-
), [components, remarkPlugins, rehypePlugins, children, itemId])
152+
), [components, remarkPlugins, mathJaxPlugin, children, itemId])
142153

143154
const showOverflow = useCallback(() => setShow(true), [setShow])
144155

@@ -230,6 +241,7 @@ function Table ({ node, ...props }) {
230241
function Code ({ node, inline, className, children, style, ...props }) {
231242
const [ReactSyntaxHighlighter, setReactSyntaxHighlighter] = useState(null)
232243
const [syntaxTheme, setSyntaxTheme] = useState(null)
244+
const language = className?.match(/language-(\w+)/)?.[1] || 'text'
233245

234246
const loadHighlighter = useCallback(() =>
235247
Promise.all([
@@ -239,7 +251,7 @@ function Code ({ node, inline, className, children, style, ...props }) {
239251
)
240252

241253
useEffect(() => {
242-
if (!inline) {
254+
if (!inline && language !== 'math') { // MathJax should handle math
243255
// loading the syntax highlighter and theme only when needed
244256
loadHighlighter().then(([highlighter, theme]) => {
245257
setReactSyntaxHighlighter(() => highlighter)
@@ -256,8 +268,6 @@ function Code ({ node, inline, className, children, style, ...props }) {
256268
)
257269
}
258270

259-
const language = className?.match(/language-(\w+)/)?.[1] || 'text'
260-
261271
return (
262272
<ReactSyntaxHighlighter style={syntaxTheme} language={language} PreTag='div' customStyle={{ borderRadius: '0.3rem' }} {...props}>
263273
{children}

0 commit comments

Comments
 (0)