@lobehub/ui
Version:
Lobe UI is an open-source UI component library for building AIGC web apps
158 lines (157 loc) • 12.1 kB
JavaScript
'use client';
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
var _excluded = ["children", "className", "style", "fullFeaturedCodeBlock", "onDoubleClick", "enableLatex", "enableMermaid", "enableImageGallery", "enableCustomFootnotes", "componentProps", "allowHtml", "fontSize", "headerMultiple", "marginMultiple", "showFootnotes", "variant", "lineHeight", "rehypePlugins", "remarkPlugins", "remarkPluginsAhead", "components", "customRender", "citations"];
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import { memo, useMemo } from 'react';
import ReactMarkdown from 'react-markdown';
import rehypeKatex from 'rehype-katex';
import rehypeRaw from 'rehype-raw';
import remarkBreaks from 'remark-breaks';
import remarkGfm from 'remark-gfm';
import remarkMath from 'remark-math';
import ImageGallery from "../Image/ImageGallery";
import Image from "../mdx/mdxComponents/Image";
import Link from "../mdx/mdxComponents/Link";
import Section from "../mdx/mdxComponents/Section";
import Video from "../mdx/mdxComponents/Video";
import { CodeFullFeatured, CodeLite } from "./CodeBlock";
import { useStyles as useMarkdownStyles } from "./markdown.style";
import { rehypeFootnoteLinks, remarkCustomFootnotes } from "./plugins/footnote";
import { rehypeKatexDir } from "./plugins/katexDir";
import { useStyles } from "./style";
import { escapeBrackets, escapeMhchem, fixMarkdownBold, transformCitations } from "./utils";
import { jsx as _jsx } from "react/jsx-runtime";
var Markdown = /*#__PURE__*/memo(function (_ref) {
var children = _ref.children,
className = _ref.className,
style = _ref.style,
fullFeaturedCodeBlock = _ref.fullFeaturedCodeBlock,
onDoubleClick = _ref.onDoubleClick,
_ref$enableLatex = _ref.enableLatex,
enableLatex = _ref$enableLatex === void 0 ? true : _ref$enableLatex,
_ref$enableMermaid = _ref.enableMermaid,
enableMermaid = _ref$enableMermaid === void 0 ? true : _ref$enableMermaid,
_ref$enableImageGalle = _ref.enableImageGallery,
enableImageGallery = _ref$enableImageGalle === void 0 ? true : _ref$enableImageGalle,
enableCustomFootnotes = _ref.enableCustomFootnotes,
componentProps = _ref.componentProps,
allowHtml = _ref.allowHtml,
fontSize = _ref.fontSize,
headerMultiple = _ref.headerMultiple,
marginMultiple = _ref.marginMultiple,
showFootnotes = _ref.showFootnotes,
_ref$variant = _ref.variant,
variant = _ref$variant === void 0 ? 'normal' : _ref$variant,
lineHeight = _ref.lineHeight,
rehypePlugins = _ref.rehypePlugins,
remarkPlugins = _ref.remarkPlugins,
remarkPluginsAhead = _ref.remarkPluginsAhead,
_ref$components = _ref.components,
components = _ref$components === void 0 ? {} : _ref$components,
customRender = _ref.customRender,
citations = _ref.citations,
rest = _objectWithoutProperties(_ref, _excluded);
var _useStyles = useStyles({
fontSize: fontSize,
headerMultiple: headerMultiple,
lineHeight: lineHeight,
marginMultiple: marginMultiple
}),
cx = _useStyles.cx,
styles = _useStyles.styles;
var _useMarkdownStyles = useMarkdownStyles({
fontSize: fontSize,
headerMultiple: headerMultiple,
marginMultiple: marginMultiple
}),
mdStyles = _useMarkdownStyles.styles;
var isChatMode = variant === 'chat';
var escapedContent = useMemo(function () {
if (!enableLatex) return fixMarkdownBold(children);
var defaultValue = fixMarkdownBold(escapeMhchem(escapeBrackets(children)));
if (enableCustomFootnotes) return transformCitations(defaultValue, citations === null || citations === void 0 ? void 0 : citations.length);
return defaultValue;
}, [children, enableLatex, enableCustomFootnotes]);
var memoComponents = useMemo(function () {
return _objectSpread({
a: function a(props) {
return /*#__PURE__*/_jsx(Link, _objectSpread(_objectSpread({
citations: citations
}, props), componentProps === null || componentProps === void 0 ? void 0 : componentProps.a));
},
img: enableImageGallery ? function (props) {
var _componentProps$img, _componentProps$img2;
return /*#__PURE__*/_jsx(Image, _objectSpread(_objectSpread(_objectSpread({}, props), componentProps === null || componentProps === void 0 ? void 0 : componentProps.img), {}, {
style: isChatMode ? _objectSpread({
height: 'auto',
maxWidth: 640
}, componentProps === null || componentProps === void 0 || (_componentProps$img = componentProps.img) === null || _componentProps$img === void 0 ? void 0 : _componentProps$img.style) : componentProps === null || componentProps === void 0 || (_componentProps$img2 = componentProps.img) === null || _componentProps$img2 === void 0 ? void 0 : _componentProps$img2.style
}));
} : undefined,
pre: function pre(props) {
return fullFeaturedCodeBlock ? /*#__PURE__*/_jsx(CodeFullFeatured, _objectSpread(_objectSpread({
enableMermaid: enableMermaid,
highlight: componentProps === null || componentProps === void 0 ? void 0 : componentProps.highlight,
mermaid: componentProps === null || componentProps === void 0 ? void 0 : componentProps.mermaid
}, props), componentProps === null || componentProps === void 0 ? void 0 : componentProps.pre)) : /*#__PURE__*/_jsx(CodeLite, _objectSpread(_objectSpread({
enableMermaid: enableMermaid,
highlight: componentProps === null || componentProps === void 0 ? void 0 : componentProps.highlight,
mermaid: componentProps === null || componentProps === void 0 ? void 0 : componentProps.mermaid
}, props), componentProps === null || componentProps === void 0 ? void 0 : componentProps.pre));
},
section: function section(props) {
return /*#__PURE__*/_jsx(Section, _objectSpread({
showCitations: showFootnotes
}, props));
},
video: function video(props) {
return /*#__PURE__*/_jsx(Video, _objectSpread(_objectSpread({}, props), componentProps === null || componentProps === void 0 ? void 0 : componentProps.video));
}
}, components);
}, [].concat(_toConsumableArray(Object.values(components || {})), _toConsumableArray(Object.values(componentProps || {})), [enableImageGallery, enableMermaid, fullFeaturedCodeBlock], _toConsumableArray(citations || []), [showFootnotes]));
var innerRehypePlugins = Array.isArray(rehypePlugins) ? rehypePlugins : [rehypePlugins];
var memoRehypePlugins = useMemo(function () {
return [allowHtml && rehypeRaw, enableLatex && rehypeKatex, enableLatex && rehypeKatexDir, enableCustomFootnotes && rehypeFootnoteLinks].concat(_toConsumableArray(innerRehypePlugins)).filter(Boolean);
}, [allowHtml, enableLatex, enableCustomFootnotes].concat(_toConsumableArray(innerRehypePlugins)));
var innerRemarkPlugins = Array.isArray(remarkPlugins) ? remarkPlugins : [remarkPlugins];
var innerRemarkPluginsAhead = Array.isArray(remarkPluginsAhead) ? remarkPluginsAhead : [remarkPluginsAhead];
var memoRemarkPlugins = useMemo(function () {
return [].concat(_toConsumableArray(innerRemarkPluginsAhead), [[remarkGfm, {
singleTilde: false
}], enableCustomFootnotes && remarkCustomFootnotes, enableLatex && remarkMath, isChatMode && remarkBreaks], _toConsumableArray(innerRemarkPlugins)).filter(Boolean);
}, [isChatMode, enableCustomFootnotes, enableLatex].concat(_toConsumableArray(innerRemarkPluginsAhead), _toConsumableArray(innerRemarkPlugins)));
var defaultDOM = /*#__PURE__*/_jsx(ImageGallery, {
enable: enableImageGallery,
children: /*#__PURE__*/_jsx(ReactMarkdown, _objectSpread(_objectSpread({
className: cx(mdStyles.__root, mdStyles.a, mdStyles.blockquote, mdStyles.code, mdStyles.details, mdStyles.header, mdStyles.hr, mdStyles.img, mdStyles.kbd, mdStyles.list, mdStyles.p, mdStyles.pre, mdStyles.strong, mdStyles.table, mdStyles.video, mdStyles.svg, enableLatex && styles.latex, isChatMode && styles.chat),
components: memoComponents,
rehypePlugins: memoRehypePlugins,
remarkPlugins: memoRemarkPlugins
}, rest), {}, {
children: escapedContent
}))
});
var markdownContent = customRender ? customRender(defaultDOM, {
text: escapedContent
}) : defaultDOM;
return /*#__PURE__*/_jsx("div", {
className: cx(styles.root, className),
"data-code-type": "markdown",
onDoubleClick: onDoubleClick,
style: style,
children: markdownContent
});
});
export default Markdown;