{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import katexLib, { type KatexOptions as BaseKatexOptions } from \"katex\";\nimport { defineMdastPlugin, type MdastNode, type MdastPluginDefinition } from \"satteri\";\n\nexport type KatexOptions = Omit<BaseKatexOptions, \"displayMode\" | \"throwOnError\">;\n\ntype MathLikeNode = Extract<MdastNode, { type: \"math\" | \"inlineMath\"; value: string }>;\n\ninterface DiagnosticContext {\n  report(input: {\n    message: string;\n    node?: Readonly<MdastNode>;\n    severity?: \"error\" | \"warning\" | \"info\";\n  }): void;\n}\n\nconst emptyOptions: Readonly<KatexOptions> = {};\n\nfunction escapeHtml(value: string) {\n  return value\n    .replaceAll(\"&\", \"&amp;\")\n    .replaceAll(\"<\", \"&lt;\")\n    .replaceAll(\">\", \"&gt;\")\n    .replaceAll('\"', \"&quot;\")\n    .replaceAll(\"'\", \"&#39;\");\n}\n\nfunction renderKatexError(value: string, error: unknown, options: Readonly<KatexOptions>) {\n  const title = escapeHtml(String(error));\n  const color = escapeHtml(options.errorColor ?? \"#cc0000\");\n  const content = escapeHtml(value);\n\n  return `<span class=\"katex-error\" style=\"color:${color}\" title=\"${title}\">${content}</span>`;\n}\n\nfunction renderMath(\n  node: Readonly<MathLikeNode>,\n  displayMode: boolean,\n  options: Readonly<KatexOptions>,\n  ctx: DiagnosticContext,\n) {\n  const value = node.value;\n\n  try {\n    return katexLib.renderToString(value, {\n      ...options,\n      displayMode,\n      throwOnError: true,\n    });\n  } catch (error) {\n    const cause = error instanceof Error ? error : new Error(String(error));\n\n    ctx.report({\n      message: `Could not render math with KaTeX: ${cause.message}`,\n      node,\n      severity: \"error\",\n    });\n\n    try {\n      return katexLib.renderToString(value, {\n        ...options,\n        displayMode,\n        strict: \"ignore\",\n        throwOnError: false,\n      });\n    } catch {\n      return renderKatexError(value, error, options);\n    }\n  }\n}\n\nexport function katex(options?: Readonly<KatexOptions>): MdastPluginDefinition {\n  const settings = options ?? emptyOptions;\n\n  return defineMdastPlugin({\n    name: \"katex\",\n    math(node, ctx) {\n      return { rawHtml: renderMath(node, true, settings, ctx) };\n    },\n    inlineMath(node, ctx) {\n      return { type: \"html\", value: renderMath(node, false, settings, ctx) };\n    },\n  });\n}\n"],"mappings":";;;AAeA,MAAM,eAAuC,CAAC;AAE9C,SAAS,WAAW,OAAe;CACjC,OAAO,MACJ,WAAW,KAAK,OAAO,CAAC,CACxB,WAAW,KAAK,MAAM,CAAC,CACvB,WAAW,KAAK,MAAM,CAAC,CACvB,WAAW,MAAK,QAAQ,CAAC,CACzB,WAAW,KAAK,OAAO;AAC5B;AAEA,SAAS,iBAAiB,OAAe,OAAgB,SAAiC;CACxF,MAAM,QAAQ,WAAW,OAAO,KAAK,CAAC;CAItC,OAAO,0CAHO,WAAW,QAAQ,cAAc,SAGM,EAAE,WAAW,MAAM,IAFxD,WAAW,KAEuD,EAAE;AACtF;AAEA,SAAS,WACP,MACA,aACA,SACA,KACA;CACA,MAAM,QAAQ,KAAK;CAEnB,IAAI;EACF,OAAO,SAAS,eAAe,OAAO;GACpC,GAAG;GACH;GACA,cAAc;EAChB,CAAC;CACH,SAAS,OAAO;EACd,MAAM,QAAQ,iBAAiB,QAAQ,QAAQ,IAAI,MAAM,OAAO,KAAK,CAAC;EAEtE,IAAI,OAAO;GACT,SAAS,qCAAqC,MAAM;GACpD;GACA,UAAU;EACZ,CAAC;EAED,IAAI;GACF,OAAO,SAAS,eAAe,OAAO;IACpC,GAAG;IACH;IACA,QAAQ;IACR,cAAc;GAChB,CAAC;EACH,QAAQ;GACN,OAAO,iBAAiB,OAAO,OAAO,OAAO;EAC/C;CACF;AACF;AAEA,SAAgB,MAAM,SAAyD;CAC7E,MAAM,WAAW,WAAW;CAE5B,OAAO,kBAAkB;EACvB,MAAM;EACN,KAAK,MAAM,KAAK;GACd,OAAO,EAAE,SAAS,WAAW,MAAM,MAAM,UAAU,GAAG,EAAE;EAC1D;EACA,WAAW,MAAM,KAAK;GACpB,OAAO;IAAE,MAAM;IAAQ,OAAO,WAAW,MAAM,OAAO,UAAU,GAAG;GAAE;EACvE;CACF,CAAC;AACH"}