hugo/internal/warpc/katex.go
Bjørn Erik Pedersen e1e1baa1bd Improve Katex error handling and fix handling of large expressions
* Make throwOnError=true the new default
* Handle JS errors as part of the RPC request/response flow
* Return a new Result type with .Err on it

This enables constructs on the form:

```handlebars
{{ with transform.ToMath "c = \\foo{a^2 + b^2}" }}
	{{ with .Err }}
	 	{{ warnf "error: %s" . }}
	{{ else }}
		{{ . }}
	{{ end }}
{{ end }}
```

Note that the new `Result` type behaves like `template.HTML` (or a string if needed) when printed, but it will panic if in a error state.

Closes #12748
2024-08-12 13:50:18 +02:00

47 lines
1.2 KiB
Go

package warpc
import (
_ "embed"
)
//go:embed wasm/renderkatex.wasm
var katexWasm []byte
// See https://katex.org/docs/options.html
type KatexInput struct {
Expression string `json:"expression"`
Options KatexOptions `json:"options"`
}
// KatexOptions defines the options for the KaTeX rendering.
// See https://katex.org/docs/options.html
type KatexOptions struct {
// html, mathml (default), htmlAndMathml
Output string `json:"output"`
// If true, display math in display mode, false in inline mode.
DisplayMode bool `json:"displayMode"`
// Render \tags on the left side instead of the right.
Leqno bool `json:"leqno"`
// If true, render flush left with a 2em left margin.
Fleqn bool `json:"fleqn"`
// The color used for typesetting errors.
// A color string given in the format "#XXX" or "#XXXXXX"
ErrorColor string `json:"errorColor"`
// A collection of custom macros.
Macros map[string]string `json:"macros,omitempty"`
// Specifies a minimum thickness, in ems, for fraction lines.
MinRuleThickness float64 `json:"minRuleThickness"`
// If true, KaTeX will throw a ParseError when it encounters an unsupported command.
ThrowOnError bool `json:"throwOnError"`
}
type KatexOutput struct {
Output string `json:"output"`
}