{"version":3,"file":"docx-exporter.cjs","names":[],"sources":["../src/docx-exporter/index.ts"],"sourcesContent":["import type {\n  BlockConfig,\n  BlockFromConfigNoChildren,\n  Exporter,\n} from \"@blocknote/core\";\nimport { plainContentToString } from \"@blocknote/core\";\nimport { AlignmentType, ImportedXmlComponent, Paragraph, TextRun } from \"docx\";\nimport { mml2omml } from \"mathml2omml\";\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 a native Word equation (OMML): KaTeX renders the LaTeX\n// to MathML, which is then converted to OMML. Invalid LaTeX comes back as a\n// typed error, for the mappings to render as a placeholder.\nfunction latexToDocxEquation(\n  latex: string,\n  inline: boolean,\n): { error?: undefined; equation: ImportedXmlComponent } | { error: string } {\n  const mathML = latexToMathML(latex, inline);\n  if (mathML.error !== undefined) {\n    return { error: mathML.error };\n  }\n\n  // `fromXmlString` parses the XML *document*, returning a nameless wrapper\n  // component around the `m:oMath` root element - unwrap it, or it would\n  // serialize as an (invalid) `<undefined>` element.\n  const imported = ImportedXmlComponent.fromXmlString(\n    mml2omml(mathML.mathML),\n  ) as any;\n  return { equation: imported.root[0] as ImportedXmlComponent };\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) {\n  return new TextRun({\n    text: getMathExporterDictionary(exporter).invalid_formula(source),\n    italics: true,\n    color: \"999999\",\n  });\n}\n\n/**\n * DOCX block mapping for `@blocknote/math-block` that renders math blocks as\n * native (editable) Word equations. Invalid LaTeX renders an error\n * placeholder (mirroring the editor):\n *\n * ```ts\n * import { mathBlockMapping } from \"@blocknote/math-block/docx-exporter\";\n *\n * new DOCXExporter(schema, {\n *   ...docxDefaultSchemaMappings,\n *   blockMapping: {\n *     ...docxDefaultSchemaMappings.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) {\n  const source = plainContentToString(block.content);\n  if (!source.trim()) {\n    return new Paragraph({});\n  }\n\n  const result = latexToDocxEquation(source, false);\n  if (result.error !== undefined) {\n    return new Paragraph({\n      alignment: AlignmentType.CENTER,\n      children: [errorText(exporter, source)],\n    });\n  }\n\n  return new Paragraph({\n    alignment: AlignmentType.CENTER,\n    children: [result.equation as any],\n  });\n}\n\n/**\n * DOCX inline content mapping for `@blocknote/math-block` that renders\n * inline math as native (editable) Word equations. Invalid LaTeX renders an\n * error placeholder (mirroring the editor):\n *\n * ```ts\n * import { inlineMathMapping } from \"@blocknote/math-block/docx-exporter\";\n *\n * new DOCXExporter(schema, {\n *   ...docxDefaultSchemaMappings,\n *   inlineContentMapping: {\n *     ...docxDefaultSchemaMappings.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) {\n  const source = inlineContent.content;\n  if (!source.trim()) {\n    return new TextRun({ text: \"\" });\n  }\n\n  const result = latexToDocxEquation(source, true);\n  if (result.error !== undefined) {\n    return errorText(exporter, source);\n  }\n\n  return result.equation as any;\n}\n"],"mappings":"sOAuBA,SAAS,EACP,EACA,EAC2E,CAC3E,IAAM,EAAS,EAAA,EAAc,EAAO,CAAM,EAW1C,OAVI,EAAO,QAAU,IAAA,GAUd,CAAE,SAHQ,EAAA,qBAAqB,eAAA,EACpC,EAAA,SAAA,CAAS,EAAO,MAAM,CAEL,CAAA,CAAS,KAAK,EAA2B,EATnD,CAAE,MAAO,EAAO,KAAM,CAUjC,CAMA,SAAS,EACP,EACA,EACA,CACA,OAAO,IAAI,EAAA,QAAQ,CACjB,KAAM,EAAA,EAA0B,CAAQ,CAAC,CAAC,gBAAgB,CAAM,EAChE,QAAS,GACT,MAAO,QACT,CAAC,CACH,CAmBA,SAAgB,EACd,EACA,EACA,CACA,IAAM,GAAA,EAAS,EAAA,qBAAA,CAAqB,EAAM,OAAO,EACjD,GAAI,CAAC,EAAO,KAAK,EACf,OAAO,IAAI,EAAA,UAAU,CAAC,CAAC,EAGzB,IAAM,EAAS,EAAoB,EAAQ,EAAK,EAQhD,OAPI,EAAO,QAAU,IAAA,GAOd,IAAI,EAAA,UAAU,CACnB,UAAW,EAAA,cAAc,OACzB,SAAU,CAAC,EAAO,QAAe,CACnC,CAAC,EATQ,IAAI,EAAA,UAAU,CACnB,UAAW,EAAA,cAAc,OACzB,SAAU,CAAC,EAAU,EAAU,CAAM,CAAC,CACxC,CAAC,CAOL,CAmBA,SAAgB,EACd,EACA,EACA,CACA,IAAM,EAAS,EAAc,QAC7B,GAAI,CAAC,EAAO,KAAK,EACf,OAAO,IAAI,EAAA,QAAQ,CAAE,KAAM,EAAG,CAAC,EAGjC,IAAM,EAAS,EAAoB,EAAQ,EAAI,EAK/C,OAJI,EAAO,QAAU,IAAA,GAId,EAAO,SAHL,EAAU,EAAU,CAAM,CAIrC"}