]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Add some more KaTeX options
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 11 Aug 2024 13:25:10 +0000 (15:25 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 11 Aug 2024 17:03:27 +0000 (19:03 +0200)
And fix the options handling.

Closes #12745
Fixes #12746

internal/warpc/katex.go
tpl/transform/transform.go
tpl/transform/transform_integration_test.go

index 0a85d94b6a84d5ee191e14755c9a34dcd359bebb..2412467a98dfeb7d2ce8e8cbad847e6e6afa2ef3 100644 (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
-       DisplayMode  bool   `json:"displayMode"`
-       ThrowOnError bool   `json:"throwOnError"`
+       // 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"`
 }
 
 type KatexOutput struct {
index 10a91671d54fdd688ca7143b22dede4c45572a0a..bcec9346bba9c7cc62dabd6c9bfebbdf48e5d862 100644 (file)
@@ -211,13 +211,14 @@ func (ns *Namespace) ToMath(ctx context.Context, args ...any) (template.HTML, er
        katexInput := warpc.KatexInput{
                Expression: expression,
                Options: warpc.KatexOptions{
-                       Output:       "mathml",
-                       ThrowOnError: false,
+                       Output:           "mathml",
+                       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
                }
        }
index b030417a234fdea54e3b3c42d424a8c7729ea84c..9e5221c9ae6536c96328c82192933ffd633d1f7f 100644 (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>
+       `)
+}