Skip to content

Commit

Permalink
feat: supprot block katex
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <[email protected]>
  • Loading branch information
Innei committed Dec 22, 2023
1 parent 7b07026 commit 65b4f64
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/components/ui/markdown/Markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import styles from './markdown.module.css'
import { AlertsRule } from './parsers/alert'
import { ContainerRule } from './parsers/container'
import { InsertRule } from './parsers/ins'
import { KateXRule } from './parsers/katex'
import { KateXBlockRule, KateXRule } from './parsers/katex'
import { MarkRule } from './parsers/mark'
import { MentionRule } from './parsers/mention'
import { SpoilerRule } from './parsers/spoiler'
Expand Down Expand Up @@ -256,6 +256,7 @@ export const Markdown: FC<MdProps & MarkdownToJSX.Options & PropsWithChildren> =
mark: MarkRule,
ins: InsertRule,
kateX: KateXRule,
kateXBlock: KateXBlockRule,
container: ContainerRule,
alerts: AlertsRule,

Expand Down
18 changes: 17 additions & 1 deletion src/components/ui/markdown/customize.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,20 @@ $ c = \pm\sqrt{a^2 + b^2} $
```
$c = \pm\sqrt{a^2 + b^2}$
```
$c = \pm\sqrt{a^2 + b^2}$
$c = \pm\sqrt{a^2 + b^2}$

```
$$
P\left(U,T\right)=100\left.\left(0.6\min\left(1,\frac{U-0.70}{0.90-0.70}\right)+0.4\min\left(1,\frac{T-4000}{14000-4000}\right)\right)\right.
$$
```

$$
P\left(U,T\right)=100\left.\left(0.6\min\left(1,\frac{U-0.70}{0.90-0.70}\right)+0.4\min\left(1,\frac{T-4000}{14000-4000}\right)\right)\right.
$$

-
22 changes: 22 additions & 0 deletions src/components/ui/markdown/parsers/katex.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useState } from 'react'
import {
blockRegex,
parseCaptureInline,
Priority,
simpleInlineRegex,
Expand Down Expand Up @@ -49,3 +50,24 @@ const LateX: FC<{ children: string }> = (props) => {

return <span dangerouslySetInnerHTML={{ __html: html }} />
}

export const KateXBlockRule: MarkdownToJSX.Rule = {
match: blockRegex(
new RegExp(`^\\s*\\$\\$ *(?<content>[\\s\\S]+?)\\s*\\$\\$ *(?:\n *)+\n?`),
),

order: Priority.LOW,
parse(capture) {
return {
type: 'kateXBlock',
groups: capture.groups,
}
},
react(node, _, state?) {
return (
<div key={state?.key}>
<LateX>{node.groups.content}</LateX>
</div>
)
},
}
6 changes: 3 additions & 3 deletions src/lib/cookie.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import dayjs from 'dayjs'
import Cookies from 'js-cookie'

import { constants } from '@clerk/nextjs/server'

export const TokenKey = 'mx-token'

export function getToken(): string | null {
const clerkJwt = Cookies.get(constants.Cookies.Session)
// FUCK clerk constants not export, and mark it internal and can not custom
// packages/backend/src/constants.ts
const clerkJwt = Cookies.get('__session')

const token = Cookies.get(TokenKey) || clerkJwt

Expand Down

0 comments on commit 65b4f64

Please sign in to comment.