mirror of
https://github.com/langgenius/dify.git
synced 2026-04-17 20:09:34 +08:00
Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
30 lines
664 B
TypeScript
30 lines
664 B
TypeScript
import type { JSX } from 'react'
|
|
import type { BundledLanguage, BundledTheme } from 'shiki/bundle/web'
|
|
import { toJsxRuntime } from 'hast-util-to-jsx-runtime'
|
|
import { Fragment } from 'react'
|
|
import { jsx, jsxs } from 'react/jsx-runtime'
|
|
import { codeToHast } from 'shiki/bundle/web'
|
|
|
|
type HighlightCodeOptions = {
|
|
code: string
|
|
language: BundledLanguage
|
|
theme: BundledTheme
|
|
}
|
|
|
|
export const highlightCode = async ({
|
|
code,
|
|
language,
|
|
theme,
|
|
}: HighlightCodeOptions): Promise<JSX.Element> => {
|
|
const hast = await codeToHast(code, {
|
|
lang: language,
|
|
theme,
|
|
})
|
|
|
|
return toJsxRuntime(hast, {
|
|
Fragment,
|
|
jsx,
|
|
jsxs,
|
|
}) as JSX.Element
|
|
}
|