@lobehub/ui
Version:
Lobe UI is an open-source UI component library for building AIGC web apps
49 lines (47 loc) • 1.52 kB
JavaScript
import { FALLBACK_LANG } from "../../Highlighter/const.mjs";
import Pre_default, { PreMermaid, PreSingleLine } from "../../mdx/mdxComponents/Pre.mjs";
import { memo } from "react";
import { jsx } from "react/jsx-runtime";
//#region src/Markdown/components/CodeBlock.tsx
const countLines = (str) => {
const matches = str.match(/\n/g);
return matches ? matches.length : 1;
};
const useCode = (raw) => {
if (!raw) return;
const { children = "", className } = raw?.props || { children: "" };
if (!children) return;
const content = Array.isArray(children) ? children[0] : children;
const lang = className?.replace("language-", "") || FALLBACK_LANG;
return {
content,
isSingleLine: countLines(content) <= 1 && content.length <= 32,
lang
};
};
const CodeBlock = memo(({ fullFeatured, enableMermaid, highlight, mermaid, children, animated, ...rest }) => {
const code = useCode(children);
if (!code) return;
if (enableMermaid && code.lang === "mermaid") return /* @__PURE__ */ jsx(PreMermaid, {
animated,
fullFeatured,
...mermaid,
...rest,
children: code.content
});
if (!highlight && code.isSingleLine) return /* @__PURE__ */ jsx(PreSingleLine, {
language: code.lang,
children: code.content
});
return /* @__PURE__ */ jsx(Pre_default, {
animated,
fullFeatured,
language: code.lang,
...highlight,
...rest,
children: code.content
});
}, (prevProps, nextProps) => prevProps.children === nextProps.children);
//#endregion
export { CodeBlock };
//# sourceMappingURL=CodeBlock.mjs.map