UNPKG

13.1 kBJavaScriptView Raw
1(function(global, factory) {
2 if (typeof module === "object" && typeof module.exports === "object") factory(exports, require("react"), require("prop-types"), require("katex"));
3 else if (typeof define === "function" && define.amd) define([
4 "exports",
5 "react",
6 "prop-types",
7 "katex"
8 ], factory);
9 else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.index = {}, global.react, global.propTypes, global.katex);
10})(this, function(exports, _react, _propTypes, _katex) {
11 "use strict";
12 Object.defineProperty(exports, "__esModule", {
13 value: true
14 });
15 function _export(target, all) {
16 for(var name in all)Object.defineProperty(target, name, {
17 enumerable: true,
18 get: all[name]
19 });
20 }
21 _export(exports, {
22 BlockMath: ()=>BlockMath,
23 InlineMath: ()=>InlineMath
24 });
25 _react = /*#__PURE__*/ _interopRequireWildcard(_react);
26 _propTypes = /*#__PURE__*/ _interopRequireDefault(_propTypes);
27 _katex = /*#__PURE__*/ _interopRequireDefault(_katex);
28 function _interopRequireDefault(obj) {
29 return obj && obj.__esModule ? obj : {
30 default: obj
31 };
32 }
33 function _getRequireWildcardCache(nodeInterop) {
34 if (typeof WeakMap !== "function") return null;
35 var cacheBabelInterop = new WeakMap();
36 var cacheNodeInterop = new WeakMap();
37 return (_getRequireWildcardCache = function(nodeInterop) {
38 return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
39 })(nodeInterop);
40 }
41 function _interopRequireWildcard(obj, nodeInterop) {
42 if (!nodeInterop && obj && obj.__esModule) {
43 return obj;
44 }
45 if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
46 return {
47 default: obj
48 };
49 }
50 var cache = _getRequireWildcardCache(nodeInterop);
51 if (cache && cache.has(obj)) {
52 return cache.get(obj);
53 }
54 var newObj = {};
55 var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
56 for(var key in obj){
57 if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
58 var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
59 if (desc && (desc.get || desc.set)) {
60 Object.defineProperty(newObj, key, desc);
61 } else {
62 newObj[key] = obj[key];
63 }
64 }
65 }
66 newObj.default = obj;
67 if (cache) {
68 cache.set(obj, newObj);
69 }
70 return newObj;
71 }
72 /**
73 * @typedef {import("react").ReactNode} ReactNode
74 *
75 *
76 * @callback ErrorRenderer
77 * @param {Error} error
78 * @returns {ReactNode}
79 *
80 *
81 * @typedef {object} MathComponentPropsWithMath
82 * @property {string} math
83 * @property {ReactNode=} children
84 * @property {string=} errorColor
85 * @property {ErrorRenderer=} renderError
86 *
87 *
88 * @typedef {object} MathComponentPropsWithChildren
89 * @property {string=} math
90 * @property {ReactNode} children
91 * @property {string=} errorColor
92 * @property {ErrorRenderer=} renderError
93 *
94 * @typedef {MathComponentPropsWithMath | MathComponentPropsWithChildren} MathComponentProps
95 */ const createMathComponent = (Component, { displayMode })=>{
96 /**
97 *
98 * @param {MathComponentProps} props
99 * @returns {ReactNode}
100 */ const MathComponent = ({ children , errorColor , math , renderError })=>{
101 const formula = math !== null && math !== void 0 ? math : children;
102 const { html , error } = (0, _react.useMemo)(()=>{
103 try {
104 const html = _katex.default.renderToString(formula, {
105 displayMode,
106 errorColor,
107 throwOnError: !!renderError
108 });
109 return {
110 html,
111 error: undefined
112 };
113 } catch (error) {
114 if (error instanceof _katex.default.ParseError || error instanceof TypeError) {
115 return {
116 error
117 };
118 }
119 throw error;
120 }
121 }, [
122 formula,
123 errorColor,
124 renderError
125 ]);
126 if (error) {
127 return renderError ? renderError(error) : /*#__PURE__*/ _react.default.createElement(Component, {
128 html: `${error.message}`
129 });
130 }
131 return /*#__PURE__*/ _react.default.createElement(Component, {
132 html: html
133 });
134 };
135 MathComponent.propTypes = {
136 children: _propTypes.default.string,
137 errorColor: _propTypes.default.string,
138 math: _propTypes.default.string,
139 renderError: _propTypes.default.func
140 };
141 return MathComponent;
142 };
143 const InternalPathComponentPropTypes = {
144 html: _propTypes.default.string.isRequired
145 };
146 const InternalBlockMath = ({ html })=>{
147 return /*#__PURE__*/ _react.default.createElement("div", {
148 "data-testid": "react-katex",
149 dangerouslySetInnerHTML: {
150 __html: html
151 }
152 });
153 };
154 InternalBlockMath.propTypes = InternalPathComponentPropTypes;
155 const InternalInlineMath = ({ html })=>{
156 return /*#__PURE__*/ _react.default.createElement("span", {
157 "data-testid": "react-katex",
158 dangerouslySetInnerHTML: {
159 __html: html
160 }
161 });
162 };
163 InternalInlineMath.propTypes = InternalPathComponentPropTypes;
164 const BlockMath = createMathComponent(InternalBlockMath, {
165 displayMode: true
166 });
167 const InlineMath = createMathComponent(InternalInlineMath, {
168 displayMode: false
169 });
170});
171
172//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uL3NyYy9pbmRleC5qc3giXSwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0IFJlYWN0LCB7IHVzZU1lbW8gfSBmcm9tICdyZWFjdCc7XG5pbXBvcnQgUHJvcFR5cGVzIGZyb20gJ3Byb3AtdHlwZXMnO1xuaW1wb3J0IEthVGVYIGZyb20gJ2thdGV4JztcblxuLyoqXG4gKiBAdHlwZWRlZiB7aW1wb3J0KFwicmVhY3RcIikuUmVhY3ROb2RlfSBSZWFjdE5vZGVcbiAqXG4gKlxuICogQGNhbGxiYWNrIEVycm9yUmVuZGVyZXJcbiAqIEBwYXJhbSB7RXJyb3J9IGVycm9yXG4gKiBAcmV0dXJucyB7UmVhY3ROb2RlfVxuICpcbiAqXG4gKiBAdHlwZWRlZiB7b2JqZWN0fSBNYXRoQ29tcG9uZW50UHJvcHNXaXRoTWF0aFxuICogQHByb3BlcnR5IHtzdHJpbmd9IG1hdGhcbiAqIEBwcm9wZXJ0eSB7UmVhY3ROb2RlPX0gY2hpbGRyZW5cbiAqIEBwcm9wZXJ0eSB7c3RyaW5nPX0gZXJyb3JDb2xvclxuICogQHByb3BlcnR5IHtFcnJvclJlbmRlcmVyPX0gcmVuZGVyRXJyb3JcbiAqXG4gKlxuICogQHR5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