块编辑器开发文档

@wordpress/latex-to-mathml

💡 云策文档标注

概述

@wordpress/latex-to-mathml 是一个 WordPress 包,用于将 LaTeX 数学语法转换为 MathML 格式。它支持块级和行内模式,适用于在 WordPress 环境中处理数学公式。

关键要点

  • 安装方式:通过 npm 安装 @wordpress/latex-to-mathml 包。
  • 核心函数:latexToMathML(latex, options),接受 LaTeX 字符串和可选参数,返回 MathML 字符串。
  • 参数选项:displayMode 控制渲染模式,true 为块级(默认),false 为行内。
  • 错误处理:如果 LaTeX 语法无效,函数会抛出错误。
  • 项目背景:此包是 Gutenberg 项目的一部分,采用 monorepo 结构,可独立贡献。

代码示例

import latexToMathML from '@wordpress/latex-to-mathml';

// Convert LaTeX to MathML (display mode, for block equations)
const mathML = latexToMathML( 'E = mc^2' );

// Convert LaTeX to MathML (inline mode)
const inlineMathML = latexToMathML( 'x^2', { displayMode: false } );

注意事项

确保 LaTeX 语法正确,否则转换会失败并抛出错误。此包专为 WordPress 和 Gutenberg 项目设计,但也可用于其他软件项目。


📄 原文内容

Convert LaTeX math syntax to MathML.

Installation

Install the module:

npm install @wordpress/latex-to-mathml --save

Usage

import latexToMathML from '@wordpress/latex-to-mathml';

// Convert LaTeX to MathML (display mode, for block equations)
const mathML = latexToMathML( 'E = mc^2' );

// Convert LaTeX to MathML (inline mode)
const inlineMathML = latexToMathML( 'x^2', { displayMode: false } );

API

latexToMathML( latex, options )

Converts LaTeX math syntax to MathML.

Parameters

  • latex (string): The LaTeX string to convert.
  • options (Object): Optional conversion options.
    • displayMode (boolean): Whether to render in display mode (block) or inline mode. Default: true.

Returns

  • (string): The MathML string.

Throws

  • Will throw an error if the LaTeX syntax is invalid.

Contributing to this package

This is an individual package that’s part of the Gutenberg project. The project is organized as a monorepo. It’s made up of multiple self-contained software packages, each with a specific purpose. The packages in this monorepo are published to npm and used by WordPress as well as other software projects.

To find out more about contributing to this package or Gutenberg as a whole, please read the project’s main contributor guide.