{"version":3,"file":"renderMaths.mjs","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":";AAGA,SAASA,EAAWC,GAAuB;AACvC,SAAOA,EACFC,QAAQ,MAAM,OAAO,EACrBA,QAAQ,MAAM,MAAM,EACpBA,QAAQ,MAAM,MAAM,EACpBA,QAAQ,MAAM,QAAQ,EACtBA,QAAQ,MAAM,OAAO;AAC9B;AAQO,SAASC,EAAWC,GAA0D;AACjF,QAAM;AAAA,IACFC,UAAAA;AAAAA,IACAC,aAAAA,IAAc;AAAA,IACdC,cAAAA,IAAe,CAAA;AAAA,EAAC,IAChBH,GACEI,IAAoBC,EAAiBJ,CAAQ,GAE7CK,IAAmB;AAAA,IACrBC,UAAU;AAAA,IACVC,SAAS;AAAA,EAAA;AAGb,MAAI;AAQA,WAAO;AAAA,MAAEC,MAPIC,EAAMC,eAAeP,GAAmB;AAAA,QACjDQ,cAAc;AAAA,QACdC,YAAY;AAAA,QACZX,aAAAA;AAAAA,QACAY,QAAQ;AAAA,QACR,GAAGX;AAAAA,MAAAA,CACN;AAAA,MACcG,OAAAA;AAAAA,IAAAA;AAAAA,EACnB,SAASS,GAAK;AACV,UAAMP,IAAUO,aAAeC,QAAQD,EAAIP,UAAU;AAErD,WAAIR,EAAQiB,WAAWF,aAAeC,SAClChB,EAAQiB,QAAQF,CAAG,GAKhB;AAAA,MACHN,MAHS,0GAA0GP,IAAc,UAAU,cAAc,aAAaN,EAAWY,CAAO,CAAC;AAAA,MAIzLF,OAAO;AAAA,QACHC,UAAU;AAAA,QACVC,SAAAA;AAAAA,MAAAA;AAAAA,IACJ;AAAA,EAER;AACJ;AAQO,SAASU,EAAgBjB,GAA2B;AACvD,MAAI;AACAS,WAAAA,EAAMC,eAAeN,EAAiBJ,CAAQ,GAAG;AAAA,MAAEW,cAAc;AAAA,IAAA,CAAM,GAChE;AAAA,EACX,QAAgB;AACZ,WAAO;AAAA,EACX;AACJ;AAQO,SAASP,EAAiBJ,GAA0B;AACvD,SAAOA,EACFH,QAAQ,gCAAgC,EAAE,EAC1CA,QAAQ,mBAAmB,EAAE;AACtC;"}