{"version":3,"file":"typst-exporter.cjs","names":[],"sources":["../src/typst-exporter/index.ts"],"sourcesContent":["import type {\n  BlockConfig,\n  BlockFromConfigNoChildren,\n  Exporter,\n} from \"@blocknote/core\";\nimport { plainContentToString } from \"@blocknote/core\";\nimport { errorPlaceholder, strLit } from \"@blocknote/xl-typst-exporter\";\nimport { tex2typst } from \"tex2typst\";\n\nimport { latexToMathML } from \"../exporterHelpers/latexToMathML.js\";\nimport { getMathExporterDictionary } from \"../i18n/dictionary.js\";\n\ntype MathBlock = BlockFromConfigNoChildren<\n  BlockConfig<\"mathBlock\", {}, \"plain\">,\n  any,\n  any\n>;\n\ntype InlineMath = { type: \"math\"; content: string };\n\n// Converts LaTeX to Typst math notation (the content of a `$...$` equation).\n// tex2typst is a lightweight translator that passes some invalid LaTeX\n// through, so the LaTeX is validated with KaTeX first - the same parser (and\n// typed error) the other exporter mappings use. Invalid LaTeX is expected\n// (the source is user input), so it comes back as a typed error rather than\n// thrown.\nfunction latexToTypstMath(\n  latex: string,\n): { error?: undefined; math: string } | { error: string } {\n  const validation = latexToMathML(latex, true);\n  if (validation.error !== undefined) {\n    return { error: validation.error };\n  }\n  return { math: tex2typst(latex) };\n}\n\n// Mirrors the editor, which shows the error state in the preview\n// placeholder, identifying the formula by its source. The parser's message\n// is deliberately NOT rendered: it's authoring detail (and untranslated\n// English) - the editor is where the author sees and fixes it.\nfunction errorText(\n  exporter: Exporter<any, any, any, any, any, any, any>,\n  source: string,\n): string {\n  return errorPlaceholder(\n    getMathExporterDictionary(exporter).invalid_formula(source),\n  );\n}\n\n/**\n * Typst block mapping for `@blocknote/math-block` that renders math blocks\n * as native Typst equations (real text, not images - the LaTeX is converted\n * to Typst math notation with `tex2typst`). The equation carries the LaTeX\n * source as its alt text, as PDF/UA requires formulas to have one. Invalid\n * LaTeX renders an error placeholder (mirroring the editor).\n *\n * Known limitation: `tex2typst` does not translate every valid KaTeX\n * command - an untranslatable one (e.g. `\\coloneqq`) passes validation here\n * but fails the Typst *compile* with an \"unknown variable\" error, failing\n * the export loudly (deliberately: a valid formula the pipeline can't\n * render must not silently become something else):\n *\n * ```ts\n * import { mathBlockMapping } from \"@blocknote/math-block/typst-exporter\";\n *\n * new TypstExporter(schema, {\n *   ...typstDefaultSchemaMappings,\n *   blockMapping: {\n *     ...typstDefaultSchemaMappings.blockMapping,\n *     mathBlock: mathBlockMapping,\n *   },\n * });\n * ```\n */\nexport function mathBlockMapping(\n  block: MathBlock,\n  exporter: Exporter<any, any, any, any, any, any, any>,\n): string {\n  const source = plainContentToString(block.content);\n  if (!source.trim()) {\n    return \"\";\n  }\n\n  const result = latexToTypstMath(source);\n  if (result.error !== undefined) {\n    return `#align(center)[#${errorText(exporter, source)}]`;\n  }\n\n  // A block equation is a display equation, centered by default (matching the\n  // editor and the other exporters).\n  return `#math.equation(block: true, alt: ${strLit(source)}, $ ${\n    result.math\n  } $)`;\n}\n\n/**\n * Typst inline content mapping for `@blocknote/math-block` that renders\n * inline math as native Typst equations flowing with the text - see\n * {@link mathBlockMapping} for the conversion and alt-text details. Invalid\n * LaTeX renders an error placeholder (mirroring the editor):\n *\n * ```ts\n * import { inlineMathMapping } from \"@blocknote/math-block/typst-exporter\";\n *\n * new TypstExporter(schema, {\n *   ...typstDefaultSchemaMappings,\n *   inlineContentMapping: {\n *     ...typstDefaultSchemaMappings.inlineContentMapping,\n *     math: inlineMathMapping,\n *   },\n * });\n * ```\n */\nexport function inlineMathMapping(\n  inlineContent: InlineMath,\n  exporter: Exporter<any, any, any, any, any, any, any>,\n): string {\n  const source = inlineContent.content;\n  if (!source.trim()) {\n    // Inline mappings return Typst *markup*; an empty string literal\n    // renders nothing.\n    return `#\"\"`;\n  }\n\n  const result = latexToTypstMath(source);\n  if (result.error !== undefined) {\n    return `#${errorText(exporter, source)}`;\n  }\n\n  return `#math.equation(alt: ${strLit(source)}, $${result.math}$)`;\n}\n"],"mappings":"4PA0BA,SAAS,EACP,EACyD,CACzD,IAAM,EAAa,EAAA,EAAc,EAAO,EAAI,EAI5C,OAHI,EAAW,QAAU,IAAA,GAGlB,CAAE,MAAA,EAAM,EAAA,UAAA,CAAU,CAAK,CAAE,EAFvB,CAAE,MAAO,EAAW,KAAM,CAGrC,CAMA,SAAS,EACP,EACA,EACQ,CACR,OAAA,EAAO,EAAA,iBAAA,CACL,EAAA,EAA0B,CAAQ,CAAC,CAAC,gBAAgB,CAAM,CAC5D,CACF,CA2BA,SAAgB,EACd,EACA,EACQ,CACR,IAAM,GAAA,EAAS,EAAA,qBAAA,CAAqB,EAAM,OAAO,EACjD,GAAI,CAAC,EAAO,KAAK,EACf,MAAO,GAGT,IAAM,EAAS,EAAiB,CAAM,EAOtC,OANI,EAAO,QAAU,IAAA,GAMd,qCAAA,EAAoC,EAAA,OAAA,CAAO,CAAM,EAAE,MACxD,EAAO,KACR,KAPQ,mBAAmB,EAAU,EAAU,CAAM,EAAE,EAQ1D,CAoBA,SAAgB,EACd,EACA,EACQ,CACR,IAAM,EAAS,EAAc,QAC7B,GAAI,CAAC,EAAO,KAAK,EAGf,MAAO,MAGT,IAAM,EAAS,EAAiB,CAAM,EAKtC,OAJI,EAAO,QAAU,IAAA,GAId,wBAAA,EAAuB,EAAA,OAAA,CAAO,CAAM,EAAE,KAAK,EAAO,KAAK,IAHrD,IAAI,EAAU,EAAU,CAAM,GAIzC"}