@lobehub/ui
Version:
Lobe UI is an open-source UI component library for building AIGC web apps
151 lines (149 loc) • 8.64 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 = ["ref", "children", "className", "style", "fullFeaturedCodeBlock", "onDoubleClick", "animated", "enableLatex", "enableMermaid", "enableImageGallery", "enableCustomFootnotes", "enableGithubAlert", "enableStream", "componentProps", "rehypePluginsAhead", "allowHtml", "borderRadius", "fontSize", "headerMultiple", "marginMultiple", "variant", "reactMarkdownProps", "lineHeight", "rehypePlugins", "remarkPlugins", "remarkPluginsAhead", "components", "customRender", "showFootnotes", "citations"];
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 { cva } from 'class-variance-authority';
import { memo, useCallback, useMemo } from 'react';
import { PreviewGroup } from "../Image";
import { MarkdownRender, StreamdownRender } from "./SyntaxMarkdown";
import Typography from "./Typography";
import { MarkdownProvider } from "./components/MarkdownProvider";
import { useDelayedAnimated } from "./components/useDelayedAnimated";
import { useStyles } from "./style";
import { jsx as _jsx } from "react/jsx-runtime";
var Markdown = /*#__PURE__*/memo(function (props) {
var ref = props.ref,
_props$children = props.children,
children = _props$children === void 0 ? '' : _props$children,
className = props.className,
style = props.style,
fullFeaturedCodeBlock = props.fullFeaturedCodeBlock,
onDoubleClick = props.onDoubleClick,
animated = props.animated,
_props$enableLatex = props.enableLatex,
enableLatex = _props$enableLatex === void 0 ? true : _props$enableLatex,
_props$enableMermaid = props.enableMermaid,
enableMermaid = _props$enableMermaid === void 0 ? true : _props$enableMermaid,
enableImageGallery = props.enableImageGallery,
enableCustomFootnotes = props.enableCustomFootnotes,
enableGithubAlert = props.enableGithubAlert,
_props$enableStream = props.enableStream,
enableStream = _props$enableStream === void 0 ? true : _props$enableStream,
componentProps = props.componentProps,
rehypePluginsAhead = props.rehypePluginsAhead,
allowHtml = props.allowHtml,
borderRadius = props.borderRadius,
_props$fontSize = props.fontSize,
fontSize = _props$fontSize === void 0 ? props.variant === 'chat' ? 14 : undefined : _props$fontSize,
_props$headerMultiple = props.headerMultiple,
headerMultiple = _props$headerMultiple === void 0 ? props.variant === 'chat' ? 0.25 : undefined : _props$headerMultiple,
_props$marginMultiple = props.marginMultiple,
marginMultiple = _props$marginMultiple === void 0 ? props.variant === 'chat' ? 1 : undefined : _props$marginMultiple,
_props$variant = props.variant,
variant = _props$variant === void 0 ? 'default' : _props$variant,
reactMarkdownProps = props.reactMarkdownProps,
_props$lineHeight = props.lineHeight,
lineHeight = _props$lineHeight === void 0 ? props.variant === 'chat' ? 1.6 : undefined : _props$lineHeight,
rehypePlugins = props.rehypePlugins,
remarkPlugins = props.remarkPlugins,
remarkPluginsAhead = props.remarkPluginsAhead,
_props$components = props.components,
components = _props$components === void 0 ? {} : _props$components,
customRender = props.customRender,
_props$showFootnotes = props.showFootnotes,
showFootnotes = _props$showFootnotes === void 0 ? true : _props$showFootnotes,
citations = props.citations,
rest = _objectWithoutProperties(props, _excluded);
var _useStyles = useStyles(),
cx = _useStyles.cx,
styles = _useStyles.styles;
var delayedAnimated = useDelayedAnimated(animated);
// Style variant handling
var variants = useMemo(function () {
return cva(styles.root, {
defaultVariants: {
enableGfm: true,
enableLatex: true,
variant: 'default'
},
/* eslint-disable sort-keys-fix/sort-keys-fix */
variants: {
variant: {
default: null,
chat: styles.chat
},
enableLatex: {
true: styles.latex,
false: null
},
enableGfm: {
true: styles.gfm,
false: null
}
}
/* eslint-enable sort-keys-fix/sort-keys-fix */
});
}, [styles]);
var Render = useCallback(function (_ref) {
var enableStream = _ref.enableStream,
children = _ref.children,
reactMarkdownProps = _ref.reactMarkdownProps;
var DefaultRender = enableStream ? StreamdownRender : MarkdownRender;
var defaultDOM = /*#__PURE__*/_jsx(DefaultRender, _objectSpread(_objectSpread({}, reactMarkdownProps), {}, {
children: children
}));
return customRender ? customRender(defaultDOM, {
text: children
}) : defaultDOM;
}, [customRender]);
return /*#__PURE__*/_jsx(PreviewGroup, {
enable: enableImageGallery,
children: /*#__PURE__*/_jsx(Typography, _objectSpread(_objectSpread({
borderRadius: borderRadius,
className: cx(variants({
enableLatex: enableLatex,
variant: variant
}), className),
"data-code-type": "markdown",
fontSize: fontSize,
headerMultiple: headerMultiple,
lineHeight: lineHeight,
marginMultiple: marginMultiple,
onDoubleClick: onDoubleClick,
ref: ref,
style: style
}, rest), {}, {
children: /*#__PURE__*/_jsx(MarkdownProvider, {
allowHtml: allowHtml,
animated: delayedAnimated,
citations: citations,
componentProps: componentProps,
components: components,
enableCustomFootnotes: enableCustomFootnotes,
enableGithubAlert: enableGithubAlert,
enableLatex: enableLatex,
enableMermaid: enableMermaid,
fullFeaturedCodeBlock: fullFeaturedCodeBlock,
rehypePlugins: rehypePlugins,
rehypePluginsAhead: rehypePluginsAhead,
remarkPlugins: remarkPlugins,
remarkPluginsAhead: remarkPluginsAhead,
showFootnotes: showFootnotes,
variant: variant,
children: /*#__PURE__*/_jsx(Render, {
enableStream: enableStream && delayedAnimated,
reactMarkdownProps: reactMarkdownProps,
children: children
})
})
}))
});
});
Markdown.displayName = 'Markdown';
export default Markdown;