mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-08-12 04:50:39 +00:00
chore: Fixing Markdown renderer (#3038)
This commit is contained in:
parent
68b0071861
commit
eac1e0c7d4
1 changed files with 53 additions and 15 deletions
|
@ -1,4 +1,4 @@
|
||||||
import React, { Suspense } from "react"
|
import React, { Suspense, useEffect, useState } from "react"
|
||||||
import Markdown from "react-markdown"
|
import Markdown from "react-markdown"
|
||||||
import remarkGfm from "remark-gfm"
|
import remarkGfm from "remark-gfm"
|
||||||
|
|
||||||
|
@ -25,14 +25,27 @@ interface HighlightedPre extends React.HTMLAttributes<HTMLPreElement> {
|
||||||
}
|
}
|
||||||
|
|
||||||
const HighlightedPre = React.memo(
|
const HighlightedPre = React.memo(
|
||||||
async ({ children, language, ...props }: HighlightedPre) => {
|
({ children, language, ...props }: HighlightedPre) => {
|
||||||
|
const [tokens, setTokens] = useState<any[] | null>(null)
|
||||||
|
const [isSupported, setIsSupported] = useState(false)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let mounted = true
|
||||||
|
|
||||||
|
const loadAndHighlight = async () => {
|
||||||
|
try {
|
||||||
const { codeToTokens, bundledLanguages } = await import("shiki")
|
const { codeToTokens, bundledLanguages } = await import("shiki")
|
||||||
|
|
||||||
|
if (!mounted) return
|
||||||
|
|
||||||
if (!(language in bundledLanguages)) {
|
if (!(language in bundledLanguages)) {
|
||||||
return <pre {...props}>{children}</pre>
|
setIsSupported(false)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const { tokens } = await codeToTokens(children, {
|
setIsSupported(true)
|
||||||
|
|
||||||
|
const { tokens: highlightedTokens } = await codeToTokens(children, {
|
||||||
lang: language as keyof typeof bundledLanguages,
|
lang: language as keyof typeof bundledLanguages,
|
||||||
defaultColor: false,
|
defaultColor: false,
|
||||||
themes: {
|
themes: {
|
||||||
|
@ -41,12 +54,37 @@ const HighlightedPre = React.memo(
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (mounted) {
|
||||||
|
setTokens(highlightedTokens)
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (mounted) {
|
||||||
|
setIsSupported(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
loadAndHighlight()
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
mounted = false
|
||||||
|
}
|
||||||
|
}, [children, language])
|
||||||
|
|
||||||
|
if (!isSupported) {
|
||||||
|
return <pre {...props}>{children}</pre>
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!tokens) {
|
||||||
|
return <pre {...props}>{children}</pre>
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<pre {...props}>
|
<pre {...props}>
|
||||||
<code>
|
<code>
|
||||||
{tokens.map((line, lineIndex) => (
|
{tokens.map((line, lineIndex) => (
|
||||||
<>
|
<React.Fragment key={lineIndex}>
|
||||||
<span key={lineIndex}>
|
<span>
|
||||||
{line.map((token, tokenIndex) => {
|
{line.map((token, tokenIndex) => {
|
||||||
const style =
|
const style =
|
||||||
typeof token.htmlStyle === "string"
|
typeof token.htmlStyle === "string"
|
||||||
|
@ -65,7 +103,7 @@ const HighlightedPre = React.memo(
|
||||||
})}
|
})}
|
||||||
</span>
|
</span>
|
||||||
{lineIndex !== tokens.length - 1 && "\n"}
|
{lineIndex !== tokens.length - 1 && "\n"}
|
||||||
</>
|
</React.Fragment>
|
||||||
))}
|
))}
|
||||||
</code>
|
</code>
|
||||||
</pre>
|
</pre>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue