Add some more KaTeX options

And fix the options handling.

Closes #12745
Fixes #12746
This commit is contained in:
Bjørn Erik Pedersen 2024-08-11 15:25:10 +02:00
parent 946e6af0bb
commit 891aa00fe1
3 changed files with 48 additions and 6 deletions

View file

@ -13,10 +13,30 @@ type KatexInput struct {
Options KatexOptions `json:"options"`
}
// KatexOptions defines the options for the KaTeX rendering.
// See https://katex.org/docs/options.html
type KatexOptions struct {
Output string `json:"output"` // html, mathml (default), htmlAndMathml
// html, mathml (default), htmlAndMathml
Output string `json:"output"`
// If true, display math in display mode, false in inline mode.
DisplayMode bool `json:"displayMode"`
ThrowOnError bool `json:"throwOnError"`
// 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"`
}
type KatexOutput struct {

View file

@ -212,12 +212,13 @@ func (ns *Namespace) ToMath(ctx context.Context, args ...any) (template.HTML, er
Expression: expression,
Options: warpc.KatexOptions{
Output: "mathml",
ThrowOnError: false,
MinRuleThickness: 0.04,
ErrorColor: "#cc0000",
},
}
if len(args) > 1 {
if err := mapstructure.WeakDecode(args[1], &katexInput); err != nil {
if err := mapstructure.WeakDecode(args[1], &katexInput.Options); err != nil {
return "", err
}
}

View file

@ -149,3 +149,24 @@ disableKinds = ['page','rss','section','sitemap','taxonomy','term']
<span class="katex"><math
`)
}
func TestToMathMacros(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
-- layouts/index.html --
{{ $macros := dict
"\\addBar" "\\bar{#1}"
"\\bold" "\\mathbf{#1}"
}}
{{ $opts := dict "macros" $macros }}
{{ transform.ToMath "\\addBar{y} + \\bold{H}" $opts }}
`
b := hugolib.Test(t, files)
b.AssertFileContent("public/index.html", `
<mi>y</mi>
`)
}