{"version":3,"file":"renderMaths.cjs","sources":["../../src/core/renderMaths.ts"],"sourcesContent":["import katex from 'katex';\nimport type { MathOptions, MathError } from './types';\n\nfunction escapeHtml(value: string): string {\n    return value\n        .replace(/&/g, '&amp;')\n        .replace(/</g, '&lt;')\n        .replace(/>/g, '&gt;')\n        .replace(/\"/g, '&quot;')\n        .replace(/'/g, '&#39;');\n}\n\n/**\n * Renders a LaTeX math equation to HTML string using KaTeX\n * \n * @param options - The rendering options\n * @returns The rendered HTML string\n */\nexport function renderMath(options: MathOptions): { html: string; error: MathError } {\n    const {\n        equation,\n        displayMode = false,\n        katexOptions = {}\n    } = options;\n    const sanitizedEquation = sanitizeEquation(equation);\n\n    const error: MathError = {\n        hasError: false,\n        message: '',\n    };\n\n    try {\n        const html = katex.renderToString(sanitizedEquation, {\n            throwOnError: false,\n            errorColor: '#f44336',\n            displayMode,\n            output: 'html',\n            ...katexOptions,\n        });\n        return { html, error };\n    } catch (err) {\n        const message = err instanceof Error ? err.message : 'Unknown error rendering equation';\n        // Call the error callback if provided\n        if (options.onError && err instanceof Error) {\n            options.onError(err);\n        }\n        // Create an error message as HTML\n        const html = `<span style=\"color: #f44336; border: 1px solid #f44336; padding: 2px 4px; border-radius: 4px; display: ${displayMode ? 'block' : 'inline-block'};\">Error: ${escapeHtml(message)}</span>`;\n\n        return {\n            html,\n            error: {\n                hasError: true,\n                message,\n            }\n        };\n    }\n}\n\n/**\n * Checks if a string is a valid LaTeX equation\n * \n * @param equation - The equation to validate\n * @returns Whether the equation is valid\n */\nexport function isValidEquation(equation: string): boolean {\n    try {\n        katex.renderToString(sanitizeEquation(equation), { throwOnError: true });\n        return true;\n    } catch (error) {\n        return false;\n    }\n}\n\n/**\n * Sanitizes an equation input to prevent code injection\n * \n * @param equation - The input equation\n * @returns The sanitized equation\n */\nexport function sanitizeEquation(equation: string): string {\n    return equation\n        .replace(/<script[^>]*>.*?<\\/script>/gi, '')\n        .replace(/<\\/?[^>]+(>|$)/g, '');\n}\n"],"names":["escapeHtml","value","replace","renderMath","options","equation","displayMode","katexOptions","sanitizedEquation","sanitizeEquation","error","hasError","message","html","katex","renderToString","throwOnError","errorColor","output","err","Error","onError","isValidEquation"],"mappings":"yGAGA,SAASA,EAAWC,EAAuB,CACvC,OAAOA,EACFC,QAAQ,KAAM,OAAO,EACrBA,QAAQ,KAAM,MAAM,EACpBA,QAAQ,KAAM,MAAM,EACpBA,QAAQ,KAAM,QAAQ,EACtBA,QAAQ,KAAM,OAAO,CAC9B,CAQO,SAASC,EAAWC,EAA0D,CACjF,KAAM,CACFC,SAAAA,EACAC,YAAAA,EAAc,GACdC,aAAAA,EAAe,CAAA,CAAC,EAChBH,EACEI,EAAoBC,EAAiBJ,CAAQ,EAE7CK,EAAmB,CACrBC,SAAU,GACVC,QAAS,EAAA,EAGb,GAAI,CAQA,MAAO,CAAEC,KAPIC,EAAMC,eAAeP,EAAmB,CACjDQ,aAAc,GACdC,WAAY,UACZX,YAAAA,EACAY,OAAQ,OACR,GAAGX,CAAAA,CACN,EACcG,MAAAA,CAAAA,CACnB,OAASS,EAAK,CACV,MAAMP,EAAUO,aAAeC,MAAQD,EAAIP,QAAU,mCAErD,OAAIR,EAAQiB,SAAWF,aAAeC,OAClChB,EAAQiB,QAAQF,CAAG,EAKhB,CACHN,KAHS,0GAA0GP,EAAc,QAAU,cAAc,aAAaN,EAAWY,CAAO,CAAC,UAIzLF,MAAO,CACHC,SAAU,GACVC,QAAAA,CAAAA,CACJ,CAER,CACJ,CAQO,SAASU,EAAgBjB,EAA2B,CACvD,GAAI,CACAS,OAAAA,EAAMC,eAAeN,EAAiBJ,CAAQ,EAAG,CAAEW,aAAc,EAAA,CAAM,EAChE,EACX,MAAgB,CACZ,MAAO,EACX,CACJ,CAQO,SAASP,EAAiBJ,EAA0B,CACvD,OAAOA,EACFH,QAAQ,+BAAgC,EAAE,EAC1CA,QAAQ,kBAAmB,EAAE,CACtC"}