// Copyright 2017-2022 The MathJax Consortium// SPDX-License-Identifier: Apache-2.0
// Package mathjax converts TeX math to SVG without a JavaScript runtime.//// Its compatibility target is the custom MathJax 3.2.2 component embedded by// D2. Render uses that component's display-mode, 16-pixel em, 8-pixel ex, and// uncached-font-path defaults.
package mathjaximport ()// CompatibilityVersion is the upstream MathJax version whose behavior this// module preserves.constCompatibilityVersion = "3.2.2"var defaultPipeline = pipeline.Pipeline{Compiler: tex.NewCompiler(),Typesetter: svg.NewTypesetter(),}var svgDimensions = regexp.MustCompile(`<svg[^>]+width="([0-9.]+)ex" height="([0-9.]+)ex"[^>]+>`)// Render converts TeX to a bare SVG element using DefaultOptions. TeX syntax// errors are represented by MathJax-compatible merror SVG; non-nil errors// report an invalid option or an internal conversion failure.func ( string) (string, error) {returnRenderWithOptions(, DefaultOptions())}// RenderWithOptions converts TeX to a bare SVG element. The returned string is// the equivalent of the inner HTML of MathJax's mjx-container. TeX syntax// errors are represented by MathJax-compatible merror SVG.func ( string, Options) (string, error) {returndefaultPipeline.Render(, .pipelineOptions())}// Measure renders TeX with DefaultOptions and returns its rounded-up width and// height in pixels. It preserves D2's MathJax 3.2.2 measurement contract: the// serialized SVG dimensions are expressed in ex units and one ex is 8 pixels.func ( string) (, int, error) { , := Render()if != nil {return0, 0, } := svgDimensions.FindStringSubmatch()iflen() != 3 {return0, 0, fmt.Errorf("mathjax-go: SVG dimensions not found") } , := strconv.ParseFloat([1], 64)if != nil {return0, 0, fmt.Errorf("mathjax-go: parse SVG width: %w", ) } , := strconv.ParseFloat([2], 64)if != nil {return0, 0, fmt.Errorf("mathjax-go: parse SVG height: %w", ) }returnint(math.Ceil( * 8)), int(math.Ceil( * 8)), nil}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.