@lobehub/ui
Version:
Lobe UI is an open-source UI component library for building AIGC web apps
152 lines (149 loc) • 9.43 kB
JavaScript
'use client';
var _excluded = ["node"],
_excluded2 = ["node"],
_excluded3 = ["node"],
_excluded4 = ["node"],
_excluded5 = ["node"],
_excluded6 = ["node"];
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); }
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 { useCallback, useMemo } from 'react';
import Hotkey from "../../Hotkey";
import { CodeBlock } from "../../Markdown/components/CodeBlock";
import { useMarkdownContext } from "../../Markdown/components/MarkdownProvider";
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 { jsx as _jsx } from "react/jsx-runtime";
export var useMarkdownComponents = function useMarkdownComponents() {
var _componentProps$highl2, _componentProps$merma2;
var _useMarkdownContext = useMarkdownContext(),
components = _useMarkdownContext.components,
animated = _useMarkdownContext.animated,
citations = _useMarkdownContext.citations,
componentProps = _useMarkdownContext.componentProps,
enableMermaid = _useMarkdownContext.enableMermaid,
fullFeaturedCodeBlock = _useMarkdownContext.fullFeaturedCodeBlock,
showFootnotes = _useMarkdownContext.showFootnotes;
var memoA = useCallback(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function (_ref) {
var node = _ref.node,
props = _objectWithoutProperties(_ref, _excluded);
return /*#__PURE__*/_jsx(Link, _objectSpread(_objectSpread({
citations: citations
}, props), componentProps === null || componentProps === void 0 ? void 0 : componentProps.a));
}, [citations, componentProps === null || componentProps === void 0 ? void 0 : componentProps.a]);
var memoImg = useCallback(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function (_ref2) {
var node = _ref2.node,
props = _objectWithoutProperties(_ref2, _excluded2);
return /*#__PURE__*/_jsx(Image, _objectSpread(_objectSpread({}, props), componentProps === null || componentProps === void 0 ? void 0 : componentProps.img));
}, [componentProps === null || componentProps === void 0 ? void 0 : componentProps.img]);
var memoVideo = useCallback(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function (_ref3) {
var node = _ref3.node,
props = _objectWithoutProperties(_ref3, _excluded3);
return /*#__PURE__*/_jsx(Video, _objectSpread(_objectSpread({}, props), componentProps === null || componentProps === void 0 ? void 0 : componentProps.video));
}, [componentProps === null || componentProps === void 0 ? void 0 : componentProps.video]);
var memoSection = useCallback(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function (_ref4) {
var node = _ref4.node,
props = _objectWithoutProperties(_ref4, _excluded4);
return /*#__PURE__*/_jsx(Section, _objectSpread({
showFootnotes: showFootnotes
}, props));
}, [showFootnotes]);
var memoKbd = useCallback(function (_ref5) {
var children = _ref5.children;
return /*#__PURE__*/_jsx(Hotkey, {
keys: children,
style: {
display: 'inline-flex'
}
});
}, []);
var memoBr = useCallback(function () {
return /*#__PURE__*/_jsx("br", {});
}, []);
var memeP = useCallback(function (_ref6) {
var _children$props;
var style = _ref6.style,
children = _ref6.children,
className = _ref6.className;
var skipWrapperTags = ['img', 'video'];
if (_typeof(children) === 'object' && skipWrapperTags.includes(children === null || children === void 0 || (_children$props = children.props) === null || _children$props === void 0 || (_children$props = _children$props.node) === null || _children$props === void 0 ? void 0 : _children$props.tagName)) {
return children;
}
return /*#__PURE__*/_jsx("p", {
className: className,
style: style,
children: children
});
}, []);
// Stable references for theme objects to prevent unnecessary re-renders
var highlightTheme = useMemo(function () {
var _componentProps$highl;
return componentProps === null || componentProps === void 0 || (_componentProps$highl = componentProps.highlight) === null || _componentProps$highl === void 0 ? void 0 : _componentProps$highl.theme;
}, [JSON.stringify(componentProps === null || componentProps === void 0 || (_componentProps$highl2 = componentProps.highlight) === null || _componentProps$highl2 === void 0 ? void 0 : _componentProps$highl2.theme)]);
var mermaidTheme = useMemo(function () {
var _componentProps$merma;
return componentProps === null || componentProps === void 0 || (_componentProps$merma = componentProps.mermaid) === null || _componentProps$merma === void 0 ? void 0 : _componentProps$merma.theme;
}, [JSON.stringify(componentProps === null || componentProps === void 0 || (_componentProps$merma2 = componentProps.mermaid) === null || _componentProps$merma2 === void 0 ? void 0 : _componentProps$merma2.theme)]);
// Create stable component props reference
var stableComponentProps = useMemo(function () {
if (!componentProps) return;
return {
highlight: componentProps.highlight ? _objectSpread(_objectSpread({}, componentProps.highlight), {}, {
theme: highlightTheme
}) : undefined,
mermaid: componentProps.mermaid ? _objectSpread(_objectSpread({}, componentProps.mermaid), {}, {
theme: mermaidTheme
}) : undefined
};
}, [highlightTheme, mermaidTheme]);
var memoPre = useCallback(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function (_ref7) {
var node = _ref7.node,
props = _objectWithoutProperties(_ref7, _excluded5);
return /*#__PURE__*/_jsx(CodeBlock, _objectSpread(_objectSpread(_objectSpread({
animated: animated,
enableMermaid: enableMermaid,
fullFeatured: fullFeaturedCodeBlock
}, stableComponentProps), componentProps === null || componentProps === void 0 ? void 0 : componentProps.pre), props));
}, [animated, enableMermaid, fullFeaturedCodeBlock, stableComponentProps, componentProps === null || componentProps === void 0 ? void 0 : componentProps.pre]);
var memoColorPreview = useCallback(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function (_ref8) {
var node = _ref8.node,
props = _objectWithoutProperties(_ref8, _excluded6);
return /*#__PURE__*/_jsx("code", _objectSpread({}, props));
}, []);
var memoComponents = useMemo(function () {
return {
a: memoA,
br: memoBr,
colorPreview: memoColorPreview,
img: memoImg,
kbd: memoKbd,
p: memeP,
pre: memoPre,
section: memoSection,
video: memoVideo
};
}, [memoA, memoBr, memoImg, memoVideo, memoPre, memoSection, memeP, memoColorPreview, memoKbd]);
return useMemo(function () {
return _objectSpread(_objectSpread({}, memoComponents), components);
}, [memoComponents, components]);
};