dify/web/app/components/base/markdown-blocks/shiki-highlight.tsx
Coding On Star 762dc5e8a6
fix(web): open embedded recommend banner plugins in the detail dialog (#41828)
Co-authored-by: fatelei <fatelei@gmail.com>
Co-authored-by: zxhlyh <jasonapring2015@outlook.com>
Co-authored-by: CodingOnStar <hanxujiang@dify.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: 姜涵煦 <hanxujiang@jianghanxudeMacBook-Pro-2.local>
Co-authored-by: L1nSn0w <l1nsn0w@qq.com>
Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com>
2026-09-07 03:19:07 +00:00

41 lines
1.2 KiB
TypeScript

import type { JSX } from 'react'
import type { 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 { bundledLanguages, getSingletonHighlighter } from 'shiki/bundle/web'
type HighlightCodeOptions = {
code: string
language: string
theme: BundledTheme
}
export const highlightCode = async ({
code,
language,
theme,
}: HighlightCodeOptions): Promise<JSX.Element> => {
const normalizedLanguage = language.trim().toLowerCase()
const lang =
normalizedLanguage === 'dotenv' || Object.hasOwn(bundledLanguages, normalizedLanguage)
? normalizedLanguage
: 'text'
// README fences may name languages outside the web bundle. Load dotenv on
// demand and keep unknown languages readable without throwing an error.
const highlighter = await getSingletonHighlighter({
langs: lang === 'dotenv' ? [(await import('shiki/langs/dotenv.mjs')).default] : [lang],
themes: [theme],
})
const hast = highlighter.codeToHast(code, {
lang,
theme,
})
return toJsxRuntime(hast, {
Fragment,
jsx,
jsxs,
}) as JSX.Element
}