mathml

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 28, 2026 License: MIT Imports: 13 Imported by: 0

README

goldmark-mathml

goldmark-mathml is a Goldmark extension that parses GitHub-style mathematical expressions and renders them server-side as native MathML.

It emits no client-side JavaScript. The TeX-to-MathML conversion is performed by an embedded copy of Temml running in the pure-Go Goja JavaScript runtime.

Usage

package main

import (
    "os"

    mathml "github.com/filippo-agent/goldmark-mathml"
    "github.com/yuin/goldmark"
    "github.com/yuin/goldmark/extension"
)

func main() {
    markdown := goldmark.New(goldmark.WithExtensions(
        extension.GFM,
        mathml.New(),
    ))
    if err := markdown.Convert([]byte("Euler wrote $e^{i\\pi}+1=0$."), os.Stdout); err != nil {
        panic(err)
    }
}

The extension and its renderer are safe for concurrent use. Initialized rendering runtimes are pooled and reused.

Syntax

The supported syntax follows GitHub's documented mathematical-expression format:

Inline math: $x^2 + y^2$

Inline math containing Markdown-significant characters: $`x_{*}`$

$$
x^2 + y^2 = z^2
$$

```math
x^2 + y^2 = z^2
```

A display expression may also place both $$ delimiters on one otherwise empty line:

$$x^2 + y^2 = z^2$$

To write a literal dollar inside an expression, use GitHub's backtick form and escape the dollar for TeX:

$`\sqrt{\$4}`$

To write a literal dollar outside math on a line that also contains an expression, use GitHub's documented HTML escape:

To split <span>$</span>100 in half, calculate $100/2$.

Ordinary code spans and code fences are never interpreted as math. A display block cannot interrupt an existing Markdown paragraph; separate it from prose with a blank line.

Errors

Invalid TeX causes Goldmark rendering to return an error. This makes malformed expressions visible in tests and build pipelines instead of silently emitting broken markup.

Third-party code

The embedded internal/temml/temml.min.js is Temml v0.13.4. Its license is in internal/temml/LICENSE.

Documentation

Overview

Package mathml provides a Goldmark extension that parses GitHub-style mathematical expressions and renders them server-side as native MathML.

Index

Constants

This section is empty.

Variables

View Source
var (
	KindInline = ast.NewNodeKind("MathInline")
	KindBlock  = ast.NewNodeKind("MathBlock")
)

Functions

func New

func New() goldmark.Extender

New returns a Goldmark extension for GitHub-style mathematical expressions. It supports $...$ and $`...`$ inline math, $$...$$ display math, and fenced code blocks whose language is "math". Expressions are rendered to MathML on the server; no client-side JavaScript is emitted.

Types

type Block

type Block struct {
	ast.BaseBlock
	Equation []byte
	// contains filtered or unexported fields
}

Block is a display mathematical expression.

func (*Block) Dump

func (n *Block) Dump(source []byte, level int)

func (*Block) IsRaw

func (*Block) IsRaw() bool

func (*Block) Kind

func (*Block) Kind() ast.NodeKind

type Inline

type Inline struct {
	ast.BaseInline
	Equation []byte
	Display  bool
}

Inline is an inline mathematical expression. Display is true for an expression delimited by double dollar signs in a paragraph.

func (*Inline) Dump

func (n *Inline) Dump(source []byte, level int)

func (*Inline) Inline

func (*Inline) Inline()

func (*Inline) IsBlank

func (*Inline) IsBlank([]byte) bool

func (*Inline) Kind

func (*Inline) Kind() ast.NodeKind

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL