Skip to content

Commit

Permalink
Add some more KaTeX options
Browse files Browse the repository at this point in the history
And fix the options handling.

Closes #12745
Fixes #12746
  • Loading branch information
bep committed Aug 11, 2024
1 parent 946e6af commit 891aa00
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
26 changes: 23 additions & 3 deletions internal/warpc/katex.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions tpl/transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
21 changes: 21 additions & 0 deletions tpl/transform/transform_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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>
`)
}

0 comments on commit 891aa00

Please sign in to comment.