Skip to content

Commit fd59a7a

Browse files
committed
fix: katex inline, closes #237
Signed-off-by: Innei <[email protected]>
1 parent 2fdf655 commit fd59a7a

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

src/components/ui/markdown/customize.md

+6
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,12 @@ $c = \pm\sqrt{a^2 + b^2}$
241241
```
242242
$c = \pm\sqrt{a^2 + b^2}$
243243

244+
$P(x) = a_nx^n+a_{n-1}x^{n-1} + \dots + a_1x + a_0$
245+
246+
```
247+
$P(x) = a_nx^n+a_{n-1}x^{n-1} + \dots + a_1x + a_0$
248+
```
249+
244250
```
245251
$$
246252

src/components/ui/markdown/parsers/katex.tsx

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import React, { useState } from 'react'
2-
import {
3-
blockRegex,
4-
parseCaptureInline,
5-
Priority,
6-
simpleInlineRegex,
7-
} from 'markdown-to-jsx'
2+
import { blockRegex, Priority, simpleInlineRegex } from 'markdown-to-jsx'
83
import type { MarkdownToJSX } from 'markdown-to-jsx'
94
import type { FC } from 'react'
105

@@ -17,17 +12,16 @@ export const KateXRule: MarkdownToJSX.Rule = {
1712
match: simpleInlineRegex(
1813
/^\$\s{0,}((?:\[.*?\]|<.*?>(?:.*?<.*?>)?|`.*?`|.)*?)\s{0,}\$/,
1914
),
20-
order: Priority.LOW,
21-
parse: parseCaptureInline,
22-
react(node, _, state?) {
23-
try {
24-
const str = node.content.map((item: any) => item.content).join('')
25-
26-
return <LateX key={state?.key}>{str}</LateX>
27-
} catch {
28-
return null as any
15+
order: Priority.MED,
16+
parse(capture) {
17+
return {
18+
type: 'kateX',
19+
katex: capture[1],
2920
}
3021
},
22+
react(node, output, state) {
23+
return <LateX key={state?.key}>{node.katex}</LateX>
24+
},
3125
}
3226

3327
type LateXProps = {
@@ -52,7 +46,10 @@ const LateX: FC<LateXProps> = (props) => {
5246
'https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js',
5347
).then(() => {
5448
// @ts-ignore
55-
const html = window.katex.renderToString(children, { displayMode, throwOnError })
49+
const html = window.katex.renderToString(children, {
50+
displayMode,
51+
throwOnError,
52+
})
5653
setHtml(html)
5754
})
5855
}, [])

0 commit comments

Comments
 (0)