UNPKG

react-scitext

Version:

React component for rendering scientific text with Markdown, LaTeX, and SMILES support

1,490 lines (1,487 loc) 48.5 kB
import Ke, { isValidElement as wr, useRef as Sr, useState as ze, useEffect as Pr } from "react"; import Ge from "react-markdown"; import Qe from "remark-gfm"; import * as Ze from "smiles-drawer"; import { BlockMath as Ye, InlineMath as z } from "react-katex"; var me = { exports: {} }, Q = {}; /** * @license React * react-jsx-runtime.production.min.js * * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var Be; function kr() { if (Be) return Q; Be = 1; var r = Ke, t = Symbol.for("react.element"), n = Symbol.for("react.fragment"), c = Object.prototype.hasOwnProperty, f = r.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner, p = { key: !0, ref: !0, __self: !0, __source: !0 }; function m(o, h, s) { var i, d = {}, $ = null, R = null; s !== void 0 && ($ = "" + s), h.key !== void 0 && ($ = "" + h.key), h.ref !== void 0 && (R = h.ref); for (i in h) c.call(h, i) && !p.hasOwnProperty(i) && (d[i] = h[i]); if (o && o.defaultProps) for (i in h = o.defaultProps, h) d[i] === void 0 && (d[i] = h[i]); return { $$typeof: t, type: o, key: $, ref: R, props: d, _owner: f.current }; } return Q.Fragment = n, Q.jsx = m, Q.jsxs = m, Q; } var ee = {}; /** * @license React * react-jsx-runtime.development.js * * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var He; function Tr() { return He || (He = 1, process.env.NODE_ENV !== "production" && function() { var r = Ke, t = Symbol.for("react.element"), n = Symbol.for("react.portal"), c = Symbol.for("react.fragment"), f = Symbol.for("react.strict_mode"), p = Symbol.for("react.profiler"), m = Symbol.for("react.provider"), o = Symbol.for("react.context"), h = Symbol.for("react.forward_ref"), s = Symbol.for("react.suspense"), i = Symbol.for("react.suspense_list"), d = Symbol.for("react.memo"), $ = Symbol.for("react.lazy"), R = Symbol.for("react.offscreen"), O = Symbol.iterator, N = "@@iterator"; function M(e) { if (e === null || typeof e != "object") return null; var a = O && e[O] || e[N]; return typeof a == "function" ? a : null; } var A = r.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED; function _(e) { { for (var a = arguments.length, u = new Array(a > 1 ? a - 1 : 0), v = 1; v < a; v++) u[v - 1] = arguments[v]; T("error", e, u); } } function T(e, a, u) { { var v = A.ReactDebugCurrentFrame, b = v.getStackAddendum(); b !== "" && (a += "%s", u = u.concat([b])); var y = u.map(function(x) { return String(x); }); y.unshift("Warning: " + a), Function.prototype.apply.call(console[e], console, y); } } var ae = !1, ie = !1, X = !1, C = !1, D = !1, w; w = Symbol.for("react.module.reference"); function L(e) { return !!(typeof e == "string" || typeof e == "function" || e === c || e === p || D || e === f || e === s || e === i || C || e === R || ae || ie || X || typeof e == "object" && e !== null && (e.$$typeof === $ || e.$$typeof === d || e.$$typeof === m || e.$$typeof === o || e.$$typeof === h || // This needs to include all possible module reference object // types supported by any Flight configuration anywhere since // we don't know which Flight build this will end up being used // with. e.$$typeof === w || e.getModuleId !== void 0)); } function J(e, a, u) { var v = e.displayName; if (v) return v; var b = a.displayName || a.name || ""; return b !== "" ? u + "(" + b + ")" : u; } function W(e) { return e.displayName || "Context"; } function I(e) { if (e == null) return null; if (typeof e.tag == "number" && _("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."), typeof e == "function") return e.displayName || e.name || null; if (typeof e == "string") return e; switch (e) { case c: return "Fragment"; case n: return "Portal"; case p: return "Profiler"; case f: return "StrictMode"; case s: return "Suspense"; case i: return "SuspenseList"; } if (typeof e == "object") switch (e.$$typeof) { case o: var a = e; return W(a) + ".Consumer"; case m: var u = e; return W(u._context) + ".Provider"; case h: return J(e, e.render, "ForwardRef"); case d: var v = e.displayName || null; return v !== null ? v : I(e.type) || "Memo"; case $: { var b = e, y = b._payload, x = b._init; try { return I(x(y)); } catch { return null; } } } return null; } var V = Object.assign, K = 0, be, ye, $e, Ee, je, _e, Ce; function Re() { } Re.__reactDisabledLog = !0; function er() { { if (K === 0) { be = console.log, ye = console.info, $e = console.warn, Ee = console.error, je = console.group, _e = console.groupCollapsed, Ce = console.groupEnd; var e = { configurable: !0, enumerable: !0, value: Re, writable: !0 }; Object.defineProperties(console, { info: e, log: e, warn: e, error: e, group: e, groupCollapsed: e, groupEnd: e }); } K++; } } function rr() { { if (K--, K === 0) { var e = { configurable: !0, enumerable: !0, writable: !0 }; Object.defineProperties(console, { log: V({}, e, { value: be }), info: V({}, e, { value: ye }), warn: V({}, e, { value: $e }), error: V({}, e, { value: Ee }), group: V({}, e, { value: je }), groupCollapsed: V({}, e, { value: _e }), groupEnd: V({}, e, { value: Ce }) }); } K < 0 && _("disabledDepth fell below zero. This is a bug in React. Please file an issue."); } } var le = A.ReactCurrentDispatcher, oe; function re(e, a, u) { { if (oe === void 0) try { throw Error(); } catch (b) { var v = b.stack.trim().match(/\n( *(at )?)/); oe = v && v[1] || ""; } return ` ` + oe + e; } } var ce = !1, te; { var tr = typeof WeakMap == "function" ? WeakMap : Map; te = new tr(); } function we(e, a) { if (!e || ce) return ""; { var u = te.get(e); if (u !== void 0) return u; } var v; ce = !0; var b = Error.prepareStackTrace; Error.prepareStackTrace = void 0; var y; y = le.current, le.current = null, er(); try { if (a) { var x = function() { throw Error(); }; if (Object.defineProperty(x.prototype, "props", { set: function() { throw Error(); } }), typeof Reflect == "object" && Reflect.construct) { try { Reflect.construct(x, []); } catch (P) { v = P; } Reflect.construct(e, [], x); } else { try { x.call(); } catch (P) { v = P; } e.call(x.prototype); } } else { try { throw Error(); } catch (P) { v = P; } e(); } } catch (P) { if (P && v && typeof P.stack == "string") { for (var g = P.stack.split(` `), S = v.stack.split(` `), E = g.length - 1, j = S.length - 1; E >= 1 && j >= 0 && g[E] !== S[j]; ) j--; for (; E >= 1 && j >= 0; E--, j--) if (g[E] !== S[j]) { if (E !== 1 || j !== 1) do if (E--, j--, j < 0 || g[E] !== S[j]) { var k = ` ` + g[E].replace(" at new ", " at "); return e.displayName && k.includes("<anonymous>") && (k = k.replace("<anonymous>", e.displayName)), typeof e == "function" && te.set(e, k), k; } while (E >= 1 && j >= 0); break; } } } finally { ce = !1, le.current = y, rr(), Error.prepareStackTrace = b; } var H = e ? e.displayName || e.name : "", F = H ? re(H) : ""; return typeof e == "function" && te.set(e, F), F; } function nr(e, a, u) { return we(e, !1); } function sr(e) { var a = e.prototype; return !!(a && a.isReactComponent); } function ne(e, a, u) { if (e == null) return ""; if (typeof e == "function") return we(e, sr(e)); if (typeof e == "string") return re(e); switch (e) { case s: return re("Suspense"); case i: return re("SuspenseList"); } if (typeof e == "object") switch (e.$$typeof) { case h: return nr(e.render); case d: return ne(e.type, a, u); case $: { var v = e, b = v._payload, y = v._init; try { return ne(y(b), a, u); } catch { } } } return ""; } var G = Object.prototype.hasOwnProperty, Se = {}, Pe = A.ReactDebugCurrentFrame; function se(e) { if (e) { var a = e._owner, u = ne(e.type, e._source, a ? a.type : null); Pe.setExtraStackFrame(u); } else Pe.setExtraStackFrame(null); } function ar(e, a, u, v, b) { { var y = Function.call.bind(G); for (var x in e) if (y(e, x)) { var g = void 0; try { if (typeof e[x] != "function") { var S = Error((v || "React class") + ": " + u + " type `" + x + "` is invalid; it must be a function, usually from the `prop-types` package, but received `" + typeof e[x] + "`.This often happens because of typos such as `PropTypes.function` instead of `PropTypes.func`."); throw S.name = "Invariant Violation", S; } g = e[x](a, x, v, u, null, "SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED"); } catch (E) { g = E; } g && !(g instanceof Error) && (se(b), _("%s: type specification of %s `%s` is invalid; the type checker function must return `null` or an `Error` but returned a %s. You may have forgotten to pass an argument to the type checker creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and shape all require an argument).", v || "React class", u, x, typeof g), se(null)), g instanceof Error && !(g.message in Se) && (Se[g.message] = !0, se(b), _("Failed %s type: %s", u, g.message), se(null)); } } } var ir = Array.isArray; function ue(e) { return ir(e); } function lr(e) { { var a = typeof Symbol == "function" && Symbol.toStringTag, u = a && e[Symbol.toStringTag] || e.constructor.name || "Object"; return u; } } function or(e) { try { return ke(e), !1; } catch { return !0; } } function ke(e) { return "" + e; } function Te(e) { if (or(e)) return _("The provided key is an unsupported type %s. This value must be coerced to a string before before using it here.", lr(e)), ke(e); } var Me = A.ReactCurrentOwner, cr = { key: !0, ref: !0, __self: !0, __source: !0 }, Ae, Oe; function ur(e) { if (G.call(e, "ref")) { var a = Object.getOwnPropertyDescriptor(e, "ref").get; if (a && a.isReactWarning) return !1; } return e.ref !== void 0; } function fr(e) { if (G.call(e, "key")) { var a = Object.getOwnPropertyDescriptor(e, "key").get; if (a && a.isReactWarning) return !1; } return e.key !== void 0; } function dr(e, a) { typeof e.ref == "string" && Me.current; } function hr(e, a) { { var u = function() { Ae || (Ae = !0, _("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)", a)); }; u.isReactWarning = !0, Object.defineProperty(e, "key", { get: u, configurable: !0 }); } } function pr(e, a) { { var u = function() { Oe || (Oe = !0, _("%s: `ref` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)", a)); }; u.isReactWarning = !0, Object.defineProperty(e, "ref", { get: u, configurable: !0 }); } } var mr = function(e, a, u, v, b, y, x) { var g = { // This tag allows us to uniquely identify this as a React Element $$typeof: t, // Built-in properties that belong on the element type: e, key: a, ref: u, props: x, // Record the component responsible for creating this element. _owner: y }; return g._store = {}, Object.defineProperty(g._store, "validated", { configurable: !1, enumerable: !1, writable: !0, value: !1 }), Object.defineProperty(g, "_self", { configurable: !1, enumerable: !1, writable: !1, value: v }), Object.defineProperty(g, "_source", { configurable: !1, enumerable: !1, writable: !1, value: b }), Object.freeze && (Object.freeze(g.props), Object.freeze(g)), g; }; function vr(e, a, u, v, b) { { var y, x = {}, g = null, S = null; u !== void 0 && (Te(u), g = "" + u), fr(a) && (Te(a.key), g = "" + a.key), ur(a) && (S = a.ref, dr(a, b)); for (y in a) G.call(a, y) && !cr.hasOwnProperty(y) && (x[y] = a[y]); if (e && e.defaultProps) { var E = e.defaultProps; for (y in E) x[y] === void 0 && (x[y] = E[y]); } if (g || S) { var j = typeof e == "function" ? e.displayName || e.name || "Unknown" : e; g && hr(x, j), S && pr(x, j); } return mr(e, g, S, b, v, Me.current, x); } } var fe = A.ReactCurrentOwner, Ne = A.ReactDebugCurrentFrame; function B(e) { if (e) { var a = e._owner, u = ne(e.type, e._source, a ? a.type : null); Ne.setExtraStackFrame(u); } else Ne.setExtraStackFrame(null); } var de; de = !1; function he(e) { return typeof e == "object" && e !== null && e.$$typeof === t; } function Le() { { if (fe.current) { var e = I(fe.current.type); if (e) return ` Check the render method of \`` + e + "`."; } return ""; } } function gr(e) { return ""; } var We = {}; function xr(e) { { var a = Le(); if (!a) { var u = typeof e == "string" ? e : e.displayName || e.name; u && (a = ` Check the top-level render call using <` + u + ">."); } return a; } } function Ie(e, a) { { if (!e._store || e._store.validated || e.key != null) return; e._store.validated = !0; var u = xr(a); if (We[u]) return; We[u] = !0; var v = ""; e && e._owner && e._owner !== fe.current && (v = " It was passed a child from " + I(e._owner.type) + "."), B(e), _('Each child in a list should have a unique "key" prop.%s%s See https://reactjs.org/link/warning-keys for more information.', u, v), B(null); } } function De(e, a) { { if (typeof e != "object") return; if (ue(e)) for (var u = 0; u < e.length; u++) { var v = e[u]; he(v) && Ie(v, a); } else if (he(e)) e._store && (e._store.validated = !0); else if (e) { var b = M(e); if (typeof b == "function" && b !== e.entries) for (var y = b.call(e), x; !(x = y.next()).done; ) he(x.value) && Ie(x.value, a); } } } function br(e) { { var a = e.type; if (a == null || typeof a == "string") return; var u; if (typeof a == "function") u = a.propTypes; else if (typeof a == "object" && (a.$$typeof === h || // Note: Memo only checks outer props here. // Inner props are checked in the reconciler. a.$$typeof === d)) u = a.propTypes; else return; if (u) { var v = I(a); ar(u, e.props, "prop", v, e); } else if (a.PropTypes !== void 0 && !de) { de = !0; var b = I(a); _("Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?", b || "Unknown"); } typeof a.getDefaultProps == "function" && !a.getDefaultProps.isReactClassApproved && _("getDefaultProps is only used on classic React.createClass definitions. Use a static property named `defaultProps` instead."); } } function yr(e) { { for (var a = Object.keys(e.props), u = 0; u < a.length; u++) { var v = a[u]; if (v !== "children" && v !== "key") { B(e), _("Invalid prop `%s` supplied to `React.Fragment`. React.Fragment can only have `key` and `children` props.", v), B(null); break; } } e.ref !== null && (B(e), _("Invalid attribute `ref` supplied to `React.Fragment`."), B(null)); } } var Ve = {}; function Fe(e, a, u, v, b, y) { { var x = L(e); if (!x) { var g = ""; (e === void 0 || typeof e == "object" && e !== null && Object.keys(e).length === 0) && (g += " You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports."); var S = gr(); S ? g += S : g += Le(); var E; e === null ? E = "null" : ue(e) ? E = "array" : e !== void 0 && e.$$typeof === t ? (E = "<" + (I(e.type) || "Unknown") + " />", g = " Did you accidentally export a JSX literal instead of a component?") : E = typeof e, _("React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: %s.%s", E, g); } var j = vr(e, a, u, b, y); if (j == null) return j; if (x) { var k = a.children; if (k !== void 0) if (v) if (ue(k)) { for (var H = 0; H < k.length; H++) De(k[H], e); Object.freeze && Object.freeze(k); } else _("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead."); else De(k, e); } if (G.call(a, "key")) { var F = I(e), P = Object.keys(a).filter(function(Rr) { return Rr !== "key"; }), pe = P.length > 0 ? "{key: someKey, " + P.join(": ..., ") + ": ...}" : "{key: someKey}"; if (!Ve[F + pe]) { var Cr = P.length > 0 ? "{" + P.join(": ..., ") + ": ...}" : "{}"; _(`A props object containing a "key" prop is being spread into JSX: let props = %s; <%s {...props} /> React keys must be passed directly to JSX without using spread: let props = %s; <%s key={someKey} {...props} />`, pe, F, Cr, F), Ve[F + pe] = !0; } } return e === c ? yr(j) : br(j), j; } } function $r(e, a, u) { return Fe(e, a, u, !0); } function Er(e, a, u) { return Fe(e, a, u, !1); } var jr = Er, _r = $r; ee.Fragment = c, ee.jsx = jr, ee.jsxs = _r; }()), ee; } process.env.NODE_ENV === "production" ? me.exports = kr() : me.exports = Tr(); var l = me.exports; function ge(r) { const t = [], n = /\\(begin|end)\{([^}]+)\}/g; let c; const f = []; for (; (c = n.exec(r)) !== null; ) f.push({ type: c[1], // 'begin' or 'end' env: c[2], index: c.index, length: c[0].length }); const p = []; for (const m of f) if (m.type === "begin") p.push(m); else if (m.type === "end" && p.length > 0 && p[p.length - 1].env === m.env) { const o = p.pop(); if (p.length === 0) { const h = o.index, s = m.index + m.length; t.push({ start: h, end: s, content: r.substring(h, s) }); } } return t.sort((m, o) => m.start - o.start); } function Mr(r) { return r.split(/(\\\(.+?\\\)|\$\$.*?\$\$|\$.*?\$|\\\[.*?\\\])/gs); } function Ue(r) { return r.match(/^\\\((.+?)\\\)$/s); } function Z(r) { const t = r.match(/^\\\(\s*([a-zA-Z_0-9]{1,3})\s*\\\)$/s); if (!t) return null; const n = t[1]; return n.includes("\\") || n.includes("{") || n.includes("}") || n.includes("^") || n.includes("_") || n.includes("frac") ? null : { match: t, content: n }; } function Y(r) { const t = r.match(/^\$([a-zA-Z][a-zA-Z0-9_]{0,2})\$$/s); if (!t) return null; const n = t[1]; return n.includes("\\") || n.includes("{") || n.includes("}") || n.includes("^") || n.includes("_") || n.includes("frac") || n.includes("+") || n.includes("-") || n.includes("*") || n.includes("/") || n.includes("=") || n.includes("(") || n.includes(")") || n.length > 3 ? null : { match: t, content: n }; } function qe(r) { return r.match(/^\\\[(.+?)\\\]$/s) || r.match(/^\$\$(.*?)\$\$$/s); } function q(r) { const t = r.match(/^\$(.+?)\$$/s); if (!t) return null; const n = t[1]; return [ /[+\-*/=<>≤≥≠±∞∑∏∫∪∩∈∉⊆⊇∅∀∃∴∝]/, // Mathematical operators and symbols /\\(frac|sqrt|int|sum|prod|lim|sin|cos|tan|log|ln|exp|times|cup|cap|subset|supset|in|notin|forall|exists|therefore|propto)/, // LaTeX functions /\([^)]*[+\-*/=,][^)]*\)/, // Expressions in parentheses with operators or commas /\{[^}]*[,=][^}]*\}/, // Sets with commas or equals (like {a,b} or {x=1}) /\{[^}]*\([^)]*,[^)]*\)[^}]*\}/, // Sets containing coordinate pairs like {(a,c), (b,d)} /[_^]/, // Subscripts or superscripts /[A-Z][a-z]?_?\d+/, // Chemical formulas like C_6H_6, H2O, etc. /\\[a-zA-Z]+/, // LaTeX commands like \alpha, \beta, etc. /\d+\s*[a-zA-Z]/, // Numbers with variables like 2x, 3y /[a-zA-Z]\s*[=]\s*[^,}]/ // Variable assignments like f=, x= ].some((f) => f.test(n)) ? t : null; } function Ar(r) { return r.match(/^\\begin\{([^}]+)\}(.*?)\\end\{\1\}$/s); } function Or(r) { return ![ /\\input\{/, /\\include\{/, /\\write/, /\\read/, /\\openin/, /\\openout/, /\\immediate/, /\\special/, /\\pdfliteral/ ].some((n) => n.test(r)); } function Kr(r) { return r.replace(/\\input\{[^}]*\}/g, "").replace(/\\include\{[^}]*\}/g, "").replace(/\\write[^{]*\{[^}]*\}/g, "").replace(/\\read[^{]*\{[^}]*\}/g, ""); } function U(r) { return r.split( /(\$[a-zA-Z][a-zA-Z0-9_]{0,2}\$|\\\([a-zA-Z][a-zA-Z0-9]{0,2}\\\))/g ).map((n, c) => { const f = Y(n); if (f) return /* @__PURE__ */ l.jsx( "span", { className: "font-bold", role: "text", "aria-label": `Variable ${f.content}`, children: f.content }, c ); const p = Z(n); return p ? /* @__PURE__ */ l.jsx( "span", { className: "font-bold italic", role: "math", "aria-label": `Mathematical variable ${p.content}`, children: p.content }, c ) : /* @__PURE__ */ l.jsx("span", { children: n }, c); }).filter((n) => !(n.props.children === "")); } function Nr(r, t) { const n = `h${t}`, c = { 1: "text-3xl font-bold mb-4 mt-6", 2: "text-2xl font-semibold mb-3 mt-5", 3: "text-xl font-medium mb-2 mt-4", 4: "text-lg font-medium mb-2 mt-3", 5: "text-base font-medium mb-1 mt-2", 6: "text-sm font-medium mb-1 mt-2" }, f = r.toLowerCase().replace(/[^a-z0-9\s]/g, "").replace(/\s+/g, "-").substring(0, 50), p = Lr(r); return /* @__PURE__ */ l.jsx( n, { id: f, className: c[t], tabIndex: -1, children: p } ); } function Lr(r) { const t = [], n = /(\\\[.*?\\\]|\$\$.*?\$\$|\\\(.*?\\\)|\$[^$\s][^$]*[^$\s]\$|\$[^$\s]+\$)/gs, c = []; let f; for (; (f = n.exec(r)) !== null; ) f[0].startsWith("$") && !f[0].startsWith("$$") && Y(f[0]) || f[0].startsWith("\\(") && f[0].endsWith("\\)") && Z(f[0]) || f[0].startsWith("$") && !f[0].startsWith("$$") && !q(f[0]) || c.push({ start: f.index, end: f.index + f[0].length, content: f[0] }); let p = 0; if (c.forEach((m, o) => { if (m.start > p) { const h = r.slice(p, m.start); if (h) { const s = U(h); t.push(...s); } } t.push( /* @__PURE__ */ l.jsx("span", { className: "math-content", children: m.content }, `math-${o}`) ), p = m.end; }), p < r.length) { const m = r.slice(p); if (m) { const o = U(m); t.push(...o); } } return c.length === 0 ? U(r) : t.filter((m) => { if (wr(m)) { const o = m.props; if (typeof o.children == "string") return o.children.trim() !== ""; } return !0; }); } function Wr(r) { return !r || typeof r != "string" || r.length > 1e5 ? !1 : ![ /javascript:/gi, /data:text\/html/gi, /vbscript:/gi, /<script[^>]*>/gi, /<iframe[^>]*>/gi, /<object[^>]*>/gi, /<embed[^>]*>/gi, /<link[^>]*>/gi, /<meta[^>]*>/gi ].some((n) => n.test(r)); } function Gr(r) { return r.replace(/<script[^>]*>.*?<\/script>/gis, "").replace(/<iframe[^>]*>.*?<\/iframe>/gis, "").replace(/javascript:/gi, "").replace(/data:text\/html/gi, "").replace(/vbscript:/gi, ""); } function Qr(r) { return r.split(/(<smiles>.*?<\/smiles>)/gs); } function Ir(r) { return r.match(/<smiles>(.*?)<\/smiles>/s); } function Dr(r) { return !r || typeof r != "string" || r.length > 1e3 ? !1 : /^[a-zA-Z0-9\[\]()=#@+\-\\\/:.%]+$/.test(r); } function Vr(r) { return r.replace(/<[^>]*>/g, "").replace(/javascript:/gi, "").replace(/data:/gi, "").trim(); } function et(r) { const t = Ir(r); return t ? t[1] : null; } const xe = ({ code: r, errorCallback: t }) => { const n = Sr(null), [c, f] = ze(!1), [p, m] = ze(""); return Pr(() => { if (!r || !n.current) return; const o = Vr(r); if (!Dr(o)) { const h = new Error(`Invalid SMILES code: ${r}`); f(!0), m(`Error rendering chemical structure: ${r}`), t(h); return; } f(!1), m(""); try { const h = new Ze.SvgDrawer({ width: 300, height: 200 }); Ze.parse( o, (s) => { if (n.current) { n.current.innerHTML = ""; const i = document.createElementNS( "http://www.w3.org/2000/svg", "svg" ); i.setAttribute("width", "300"), i.setAttribute("height", "200"), i.setAttribute("data-smiles", r), i.style.maxWidth = "100%", i.style.height = "auto", n.current.appendChild(i), h.draw(s, i, "light"); } }, (s) => { f(!0), m(`Error rendering chemical structure: ${r}`), t && t(s); } ); } catch (h) { f(!0), m(`Error rendering chemical structure: ${r}`), t && t(h); } }, [r, t]), c ? /* @__PURE__ */ l.jsx( "div", { className: "smiles-error bg-red-50 border border-red-200 rounded-md p-3 my-2", role: "alert", "aria-live": "polite", "aria-label": "Chemical structure rendering error", children: /* @__PURE__ */ l.jsx("p", { className: "text-sm text-red-800", children: p }) } ) : /* @__PURE__ */ l.jsxs( "div", { className: "smiles-container block", role: "img", "aria-label": `Chemical structure: ${r}`, "aria-describedby": `smiles-description-${r.replace( /[^a-zA-Z0-9]/g, "" )}`, children: [ /* @__PURE__ */ l.jsx("div", { ref: n, style: { maxWidth: "100%", height: "auto" } }), /* @__PURE__ */ l.jsxs( "span", { id: `smiles-description-${r.replace(/[^a-zA-Z0-9]/g, "")}`, style: { position: "absolute", width: "1px", height: "1px", padding: 0, margin: "-1px", overflow: "hidden", clip: "rect(0, 0, 0, 0)", whiteSpace: "nowrap", border: 0 }, children: [ "SMILES notation: ", r, ". This represents a chemical structure rendered as a 2D diagram." ] } ) ] } ); }; class Fr { constructor(t) { this.text = t; } // Find all content matches (SMILES, math, environments, headings) parseContent() { const t = []; return this.findSmilesMatches(t), this.findHeadingMatches(t), this.findMathMatches(t), this.findEnvironmentMatches(t), this.filterNestedMatches(t); } findSmilesMatches(t) { const n = /<smiles>.*?<\/smiles>/gs; let c; for (; (c = n.exec(this.text)) !== null; ) t.push({ start: c.index, end: c.index + c[0].length, content: c[0], type: "smiles" }); } findHeadingMatches(t) { const n = /^(#{1,6})\s+(.+)$/gm; let c; for (; (c = n.exec(this.text)) !== null; ) t.push({ start: c.index, end: c.index + c[0].length, content: c[0], type: "heading" }); } findMathMatches(t) { const n = /(\$\$.*?\$\$|\\\[.*?\\\])/gs; let c; for (; (c = n.exec(this.text)) !== null; ) t.push({ start: c.index, end: c.index + c[0].length, content: c[0], type: "math" }); const f = /(\\\(.*?\\\)|\$[^$\s][^$]*[^$\s]\$|\$[^$\s]+\$)/gs; let p; for (; (p = f.exec(this.text)) !== null; ) t.some( (o) => p.index < o.end && p.index + p[0].length > o.start ) || this.isInListContext(p.index) || this.shouldSkipMathFragment(p[0]) || t.push({ start: p.index, end: p.index + p[0].length, content: p[0], type: "math" }); } findEnvironmentMatches(t) { ge(this.text).forEach((c) => { t.push({ ...c, type: "env" }); }); } isInListContext(t) { const n = this.text.lastIndexOf(` `, t - 1), c = n === -1 ? 0 : n + 1, f = this.text.slice(c, t); return /^\s*[-*+]\s*$/.test(f); } shouldSkipMathFragment(t) { return !!(t.startsWith("$") && !t.startsWith("$$") && Y(t) || t.startsWith("\\(") && t.endsWith("\\)") && Z(t) || t.startsWith("$") && !t.startsWith("$$") && !q(t)); } filterNestedMatches(t) { return t.sort((n, c) => n.start - c.start), t.filter((n, c, f) => !f.some((m, o) => c === o ? !1 : n.start >= m.start && n.end <= m.end)); } } function zr(r, t) { const n = r.replace(/^\\begin\{[^}]+\}/s, "").replace(/\\end\{[^}]+\}$/s, "").trim(), c = ge(n), f = {}; let p = "", m = 0; c.forEach((h, s) => { const i = `__LATEX_ENV_PLACEHOLDER_${s}__`; f[i] = h.content, p += n.substring(m, h.start), p += i, m = h.end; }), p += n.substring(m); let o = p.split(/\\item/).filter((h) => h.trim()); return o.length === 0 && p.trim() && (o = [p.trim()]), o.map((h, s) => { const i = h.trim(), d = ($) => { const R = /(__LATEX_ENV_PLACEHOLDER_\d+__)/g; return $.split(R).filter(Boolean).map((N, M) => { if (f[N]) return /* @__PURE__ */ l.jsx("span", { children: f[N] }, M); const A = /* @__PURE__ */ new Map(); let _ = 0, T = N; const ae = /<smiles>(.*?)<\/smiles>/g; T = T.replace( ae, (C, D) => { const w = `SMILES_PLACEHOLDER_${_++}`; return A.set( w, /* @__PURE__ */ l.jsx( xe, { code: D, errorCallback: (L) => console.error(L) }, w ) ), w; } ); const ie = /(\\\(.*?\\\)|\$[^$\s][^$]*[^$\s]\$|\$[^$\s]+\$)/gs; T = T.replace(ie, (C) => { if (Y(C) || C.startsWith("\\(") && C.endsWith("\\)") && Z(C) || C.startsWith("$") && !C.startsWith("$$") && !q(C)) return C; const w = `MATH_PLACEHOLDER_${_++}`; return A.set( w, /* @__PURE__ */ l.jsx( z, { math: C.startsWith("\\(") ? C.slice(2, -2) : C.slice(1, -1) }, w ) ), w; }), T = T.replace( /\$([a-zA-Z][a-zA-Z0-9_]{0,2})\$/g, "**$1**" ), T = T.replace( /\\\(([a-zA-Z][a-zA-Z0-9]{0,2})\\\)/g, "***$1***" ); let X = [T]; return A.forEach((C, D) => { X = X.flatMap((w) => { if (typeof w == "string" && w.includes(D)) { const L = w.split(D), J = []; for (let W = 0; W < L.length; W++) W > 0 && J.push(C), L[W] && J.push(L[W]); return J; } return [w]; }); }), /* @__PURE__ */ l.jsx("span", { children: X }, M); }); }; if (t === "description") { const $ = i.match(/^\[([^\]]+)\]\s*(.*)$/s); if ($) { const [, R, O] = $; return /* @__PURE__ */ l.jsxs("div", { className: "mb-2", children: [ /* @__PURE__ */ l.jsx("dt", { className: "font-semibold", children: R }), /* @__PURE__ */ l.jsx("dd", { className: "ml-4", children: d(O) }) ] }, s); } else return /* @__PURE__ */ l.jsx("div", { className: "mb-2", children: /* @__PURE__ */ l.jsx("dd", { className: "ml-0", children: d(i) }) }, s); } return /* @__PURE__ */ l.jsx("li", { className: "mb-1", children: d(i) }, s); }).filter(Boolean); } function Zr(r, t) { const n = zr(t, r); if (n.length === 0) return /* @__PURE__ */ l.jsxs("div", { className: "bg-yellow-50 dark:bg-yellow-900/20 border border-yellow-200 dark:border-yellow-800 p-3 rounded-md my-4", children: [ /* @__PURE__ */ l.jsxs("p", { className: "text-sm text-yellow-800 dark:text-yellow-200 mb-2", children: [ "Malformed LaTeX ", r, " environment:" ] }), /* @__PURE__ */ l.jsx("pre", { className: "whitespace-pre-wrap text-sm text-gray-700 dark:text-gray-300", children: t }) ] }); switch (r) { case "enumerate": return /* @__PURE__ */ l.jsx("ol", { className: "list-decimal list-outside space-y-1 my-4 pl-5", children: n }); case "itemize": return /* @__PURE__ */ l.jsx("ul", { className: "list-disc list-outside space-y-1 my-4 pl-5", children: n }); case "description": return /* @__PURE__ */ l.jsx("dl", { className: "space-y-2 my-4 pl-5", children: n }); default: return /* @__PURE__ */ l.jsxs("div", { className: "bg-gray-50 dark:bg-gray-800 border border-gray-200 dark:border-gray-700 p-4 rounded-md my-4", children: [ /* @__PURE__ */ l.jsxs("p", { className: "text-sm text-gray-600 dark:text-gray-400 mb-2", children: [ "LaTeX ", r, " environment:" ] }), /* @__PURE__ */ l.jsx("div", { className: "pl-4", children: n.length > 0 ? /* @__PURE__ */ l.jsx("ul", { className: "list-none space-y-1", children: n }) : /* @__PURE__ */ l.jsx("pre", { className: "whitespace-pre-wrap text-sm", children: t }) }) ] }); } } function Xe(r, t = !1) { const n = Ar(r); if (n) { const [, o] = n; return Zr(o, r); } const c = qe(r), f = Ue(r), p = q(r); if (c && !t) { const o = c[1] || c[2]; return /* @__PURE__ */ l.jsx("div", { className: "my-2", children: /* @__PURE__ */ l.jsx(Ye, { math: o }) }); } else { if (f) return /* @__PURE__ */ l.jsx(z, { math: f[1] }); if (p) return /* @__PURE__ */ l.jsx(z, { math: p[1] }); { const o = Y(r); if (o) return /* @__PURE__ */ l.jsx("span", { className: "font-bold", children: o.content }); const h = Z(r); if (h) return /* @__PURE__ */ l.jsx("span", { className: "font-bold italic", children: h.content }); } } const m = Mr(r); return /* @__PURE__ */ l.jsx(l.Fragment, { children: m.map((o, h) => { const s = qe(o); if (s && !t) { const i = s[1] || s[2]; return /* @__PURE__ */ l.jsx("div", { className: "my-2", children: /* @__PURE__ */ l.jsx(Ye, { math: i }) }, h); } else if (s && t) { const i = s[1] || s[2]; return /* @__PURE__ */ l.jsx(z, { math: i }, h); } else { const i = Y(o); if (i) return /* @__PURE__ */ l.jsx("span", { className: "font-bold", children: i.content }, h); { const d = Z(o); if (d) return /* @__PURE__ */ l.jsx("span", { className: "font-bold italic", children: d.content }, h); { const $ = Ue(o); if ($) return /* @__PURE__ */ l.jsx(z, { math: $[1] }, h); { const R = q(o); return R ? /* @__PURE__ */ l.jsx(z, { math: R[1] }, h) : /* @__PURE__ */ l.jsx("span", { className: "whitespace-pre-line", children: o }, h); } } } } }) }); } function ve(r, t) { const n = /* @__PURE__ */ new Map(); let c = 0, f = r; const p = /<smiles>(.*?)<\/smiles>/g; f = f.replace( p, (s, i) => { const d = `SMILES_PLACEHOLDER_${c++}`; return n.set( d, /* @__PURE__ */ l.jsx( xe, { code: i, errorCallback: ($) => console.error($) }, d ) ), d; } ); const m = /(\\\(.*?\\\)|\$[^$\s][^$]*[^$\s]\$|\$[^$\s]+\$)/gs; f = f.replace(m, (s) => { if (Y(s) || s.startsWith("\\(") && s.endsWith("\\)") && Z(s) || s.startsWith("$") && !s.startsWith("$$") && !q(s)) return s; const d = `MATH_PLACEHOLDER_${c++}`; return n.set( d, /* @__PURE__ */ l.jsx( z, { math: s.startsWith("\\(") ? s.slice(2, -2) : s.slice(1, -1) }, d ) ), d; }), f = f.replace( /\$([a-zA-Z][a-zA-Z0-9_]{0,2})\$/g, "**$1**" ), f = f.replace( /\\\(([a-zA-Z][a-zA-Z0-9]{0,2})\\\)/g, "***$1***" ); const o = () => (s) => { if (typeof s == "string") { let i = [s]; return n.forEach((d, $) => { i = i.flatMap((R) => { if (typeof R == "string" && R.includes($)) { const O = R.split($), N = []; for (let M = 0; M < O.length; M++) M > 0 && N.push(d), O[M] && N.push(O[M]); return N; } return [R]; }); }), i; } return Array.isArray(s) ? s.map(o()) : s; }, h = { // Paragraph elements p: ({ children: s, ...i }) => { const d = o(); return /* @__PURE__ */ l.jsx("p", { ...i, children: d(s) }); }, // Heading elements h1: ({ children: s, ...i }) => { const d = o(); return /* @__PURE__ */ l.jsx("h1", { className: "text-3xl font-bold mb-4 mt-6", ...i, children: d(s) }); }, h2: ({ children: s, ...i }) => { const d = o(); return /* @__PURE__ */ l.jsx("h2", { className: "text-2xl font-semibold mb-3 mt-5", ...i, children: d(s) }); }, h3: ({ children: s, ...i }) => { const d = o(); return /* @__PURE__ */ l.jsx("h3", { className: "text-xl font-medium mb-2 mt-4", ...i, children: d(s) }); }, h4: ({ children: s, ...i }) => { const d = o(); return /* @__PURE__ */ l.jsx("h4", { className: "text-lg font-medium mb-2 mt-3", ...i, children: d(s) }); }, h5: ({ children: s, ...i }) => { const d = o(); return /* @__PURE__ */ l.jsx("h5", { className: "text-base font-medium mb-1 mt-2", ...i, children: d(s) }); }, h6: ({ children: s, ...i }) => { const d = o(); return /* @__PURE__ */ l.jsx("h6", { className: "text-sm font-medium mb-1 mt-2", ...i, children: d(s) }); }, // Text formatting elements strong: ({ children: s, ...i }) => { const d = o(); return /* @__PURE__ */ l.jsx("strong", { ...i, children: d(s) }); }, em: ({ children: s, ...i }) => { const d = o(); return /* @__PURE__ */ l.jsx("em", { ...i, children: d(s) }); }, code: ({ children: s, ...i }) => { const d = o(); return /* @__PURE__ */ l.jsx("code", { ...i, children: d(s) }); }, // List elements ul: ({ children: s, ...i }) => { const d = o(); return /* @__PURE__ */ l.jsx("ul", { className: "list-disc list-outside space-y-1 my-4 pl-5", ...i, children: d(s) }); }, ol: ({ children: s, ...i }) => { const d = o(); return /* @__PURE__ */ l.jsx( "ol", { className: "list-decimal list-outside space-y-1 my-4 pl-5", ...i, children: d(s) } ); }, li: ({ children: s, ...i }) => { const d = o(); return /* @__PURE__ */ l.jsx("li", { className: "mb-1", ...i, children: d(s) }); }, // Link elements a: ({ children: s, ...i }) => { const d = o(); return /* @__PURE__ */ l.jsx("a", { ...i, children: d(s) }); }, // Block quote elements blockquote: ({ children: s, ...i }) => { const d = o(); return /* @__PURE__ */ l.jsx("blockquote", { ...i, children: d(s) }); }, // Span elements (for inline content) span: ({ children: s, ...i }) => { const d = o(); return /* @__PURE__ */ l.jsx("span", { ...i, children: d(s) }); }, // Div elements div: ({ children: s, ...i }) => { const d = o(); return /* @__PURE__ */ l.jsx("div", { ...i, children: d(s) }); } }; return /* @__PURE__ */ l.jsx( Ge, { remarkPlugins: [Qe], components: h, children: f }, `text-${t}` ); } const rt = ({ content: r, renderAsMarkdown: t = !0, inline: n = !1 }) => !r || typeof r != "string" ? null : Wr(r) ? ((f) => ge(f).length > 0 || // Has LaTeX environments f.includes("\\[") || // Has bracket block math f.includes("$$"))(r) || !t ? Yr(r, { renderAsMarkdown: t, inline: n }) : Hr(r, { inline: n }) : /* @__PURE__ */ l.jsx( "div", { className: "error-message bg-red-50 border border-red-200 rounded-md p-3", role: "alert", "aria-live": "polite", children: /* @__PURE__ */ l.jsx("p", { className: "text-red-800", children: "Invalid or potentially unsafe content" }) } ); function Yr(r, t) { const { renderAsMarkdown: n, inline: c } = t, m = new Fr(r).parseContent().filter((s) => s.type === "math" ? s.content.match(/^(\$\$|\\\[)/) : s.type === "smiles" || s.type === "heading" || s.type === "env"), o = []; let h = 0; if (m.forEach((s, i) => { if (s.start > h) { const d = r.slice(h, s.start); if (d.trim()) if (n) { const $ = ve( d, i ); o.push($); } else { const $ = U(d); o.push( /* @__PURE__ */ l.jsx("span", { className: "whitespace-pre-line", children: $ }, `text-${i}`) ); } } o.push(Br(s, i)), h = s.end; }), h < r.length) { const s = r.slice(h); if (s.trim()) if (n) { const i = ve( s, h ); o.push(i); } else { const i = U(s); o.push( /* @__PURE__ */ l.jsx("span", { className: "whitespace-pre-line", children: i }, "text-final") ); } } return c ? /* @__PURE__ */ l.jsx( "span", { className: "prose-inline", role: "presentation", "aria-label": "Scientific content with mixed formatting", children: o.length > 0 ? o : Je(r, n) } ) : /* @__PURE__ */ l.jsx( "div", { className: "prose dark:prose-invert max-w-none", role: "article", "aria-label": "Scientific document content", children: o.length > 0 ? o : Je(r, n) } ); } function Br(r, t) { var n; switch (r.type) { case "smiles": const c = (n = r.content.match(/<smiles>(.*?)<\/smiles>/)) == null ? void 0 : n[1]; if (c) return /* @__PURE__ */ l.jsx( xe, { code: c, errorCallback: (m) => console.error(m) }, `smiles-${t}` ); break; case "heading": const f = r.content.match(/^(#{1,6})\s+(.+)$/); if (f) { const m = f[1].length, o = f[2]; return /* @__PURE__ */ l.jsx("div", { children: Nr(o, m) }, `heading-${t}`); } break; case "math": case "env": return Or(r.content) ? r.content.match(/^(\$\$|\\\[)/) ? /* @__PURE__ */ l.jsx( "div", { role: "math", "aria-label": `Mathematical expression: ${r.content.replace(/[\$\\\[\]]/g, "").trim()}`, children: Xe(r.content, !1) }, `latex-${t}` ) : /* @__PURE__ */ l.jsx( "span", { role: "math", "aria-label": `Inline math: ${r.content.replace(/[\$\\\(\)]/g, "").trim()}`, children: Xe(r.content, !0) }, `latex-${t}` ) : /* @__PURE__ */ l.jsx( "div", { className: "bg-red-50 border border-red-200 rounded-md p-3", role: "alert", "aria-live": "polite", "aria-label": "LaTeX rendering error", children: /* @__PURE__ */ l.jsx("p", { className: "text-red-800", children: "Invalid LaTeX content" }) }, `error-${t}` ); } return /* @__PURE__ */ l.jsx("span", { children: r.content }, `fallback-${t}`); } function Hr(r, t) { const { inline: n } = t, c = ve(r, 0); return n ? /* @__PURE__ */ l.jsx( "span", { className: "prose-inline", role: "presentation", "aria-label": "Scientific content with mixed formatting", children: c } ) : /* @__PURE__ */ l.jsx( "div", { className: "prose dark:prose-invert max-w-none", role: "article", "aria-label": "Scientific document content", children: c } ); } function Je(r, t) { return t ? /* @__PURE__ */ l.jsx(Ge, { remarkPlugins: [Qe], children: r.replace(/\$([a-zA-Z][a-zA-Z0-9_]{0,2})\$/g, "**$1**").replace(/\\\(\s*([a-zA-Z][a-zA-Z0-9]{0,2})\s*\\\)/g, "***$1***") }) : /* @__PURE__ */ l.jsx("span", { className: "whitespace-pre-line", children: U(r) }); } export { rt as RichText, xe as Smiles, et as extractSmilesCode, ge as findTopLevelEnvs, qe as isBlockLatexFragment, Ue as isInlineLatexFragment, Ar as isLatexEnvironment, q as isSelectiveInlineLatex, Y as isSimpleVariable, Z as isSmallVariableLatex, Ir as isSmilesCode, Lr as processMixedContent, U as processTextWithSmallVariables, Nr as renderHeading, Kr as sanitizeLatexContent, Gr as sanitizeMarkdownContent, Vr as sanitizeSmilesCode, Mr as splitLatex, Qr as splitSmiles, Or as validateLatexInput, Wr as validateMarkdownContent, Dr as validateSmilesCode };