Skip to content

Commit

Permalink
fix: katex inline, closes #237
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Jan 12, 2024
1 parent 2fdf655 commit fd59a7a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
6 changes: 6 additions & 0 deletions src/components/ui/markdown/customize.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ $c = \pm\sqrt{a^2 + b^2}$
```
$c = \pm\sqrt{a^2 + b^2}$

$P(x) = a_nx^n+a_{n-1}x^{n-1} + \dots + a_1x + a_0$

```
$P(x) = a_nx^n+a_{n-1}x^{n-1} + \dots + a_1x + a_0$
```

```
$$
Expand Down
29 changes: 13 additions & 16 deletions src/components/ui/markdown/parsers/katex.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import React, { useState } from 'react'
import {
blockRegex,
parseCaptureInline,
Priority,
simpleInlineRegex,
} from 'markdown-to-jsx'
import { blockRegex, Priority, simpleInlineRegex } from 'markdown-to-jsx'
import type { MarkdownToJSX } from 'markdown-to-jsx'
import type { FC } from 'react'

Expand All @@ -17,17 +12,16 @@ export const KateXRule: MarkdownToJSX.Rule = {
match: simpleInlineRegex(
/^\$\s{0,}((?:\[.*?\]|<.*?>(?:.*?<.*?>)?|`.*?`|.)*?)\s{0,}\$/,
),
order: Priority.LOW,
parse: parseCaptureInline,
react(node, _, state?) {
try {
const str = node.content.map((item: any) => item.content).join('')

return <LateX key={state?.key}>{str}</LateX>
} catch {
return null as any
order: Priority.MED,
parse(capture) {
return {
type: 'kateX',
katex: capture[1],
}
},
react(node, output, state) {
return <LateX key={state?.key}>{node.katex}</LateX>
},
}

type LateXProps = {
Expand All @@ -52,7 +46,10 @@ const LateX: FC<LateXProps> = (props) => {
'https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.js',
).then(() => {
// @ts-ignore
const html = window.katex.renderToString(children, { displayMode, throwOnError })
const html = window.katex.renderToString(children, {
displayMode,
throwOnError,
})
setHtml(html)
})
}, [])
Expand Down

1 comment on commit fd59a7a

@vercel
Copy link

@vercel vercel bot commented on fd59a7a Jan 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

shiro – ./

innei.in
springtide.vercel.app
shiro-innei.vercel.app
shiro-git-main-innei.vercel.app

Please sign in to comment.