Skip to content

Commit 013c8cf

Browse files
jmooringbep
authored andcommitted
tpl/transform: Expose the KaTeX strict option
Closes #13729
1 parent e25db38 commit 013c8cf

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

hugolib/integrationtest_builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ func (s *IntegrationTestBuilder) AssertLogContains(els ...string) {
263263
}
264264
}
265265

266-
// AssertLogNotContains asserts that the last build log does matches the given regular expressions.
266+
// AssertLogMatches asserts that the last build log matches the given regular expressions.
267267
// The regular expressions can be negated with a "! " prefix.
268268
func (s *IntegrationTestBuilder) AssertLogMatches(expression string) {
269269
s.Helper()

internal/warpc/katex.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,30 @@ type KatexOptions struct {
4545
// A color string given in the format "#XXX" or "#XXXXXX"
4646
ErrorColor string `json:"errorColor"`
4747

48-
// A collection of custom macros.
48+
// A collection of custom macros.
4949
Macros map[string]string `json:"macros,omitempty"`
5050

5151
// Specifies a minimum thickness, in ems, for fraction lines.
5252
MinRuleThickness float64 `json:"minRuleThickness"`
5353

5454
// If true, KaTeX will throw a ParseError when it encounters an unsupported command.
5555
ThrowOnError bool `json:"throwOnError"`
56+
57+
// Controls how KaTeX handles LaTeX features that offer convenience but
58+
// aren't officially supported, one of error (default), ignore, or warn.
59+
//
60+
// - error: Throws an error when convenient, unsupported LaTeX features
61+
// are encountered.
62+
// - ignore: Allows convenient, unsupported LaTeX features without any
63+
// feedback.
64+
// - warn: Emits a warning when convenient, unsupported LaTeX features are
65+
// encountered.
66+
//
67+
// The "newLineInDisplayMode" error code, which flags the use of \\
68+
// or \newline in display mode outside an array or tabular environment, is
69+
// intentionally designed not to throw an error, despite this behavior
70+
// being questionable.
71+
Strict string `json:"strict"`
5672
}
5773

5874
type KatexOutput struct {

tpl/transform/transform.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"context"
2020
"encoding/xml"
2121
"errors"
22+
"fmt"
2223
"html"
2324
"html/template"
2425
"io"
@@ -234,6 +235,7 @@ func (ns *Namespace) ToMath(ctx context.Context, args ...any) (template.HTML, er
234235
MinRuleThickness: 0.04,
235236
ErrorColor: "#cc0000",
236237
ThrowOnError: true,
238+
Strict: "error",
237239
},
238240
}
239241

@@ -243,6 +245,13 @@ func (ns *Namespace) ToMath(ctx context.Context, args ...any) (template.HTML, er
243245
}
244246
}
245247

248+
switch katexInput.Options.Strict {
249+
case "error", "ignore", "warn":
250+
// Valid strict mode, continue
251+
default:
252+
return "", fmt.Errorf("invalid strict mode; expected one of error, ignore, or warn; received %s", katexInput.Options.Strict)
253+
}
254+
246255
s := hashing.HashString(args...)
247256
key := "tomath/" + s[:2] + "/" + s[2:]
248257
fileCache := ns.deps.ResourceSpec.FileCaches.MiscCache()

tpl/transform/transform_integration_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,3 +495,44 @@ DATA
495495
}
496496
}
497497
}
498+
499+
// Issue 13729
500+
func TestToMathStrictMode(t *testing.T) {
501+
t.Parallel()
502+
503+
files := `
504+
-- hugo.toml --
505+
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
506+
-- layouts/all.html --
507+
{{ transform.ToMath "a %" dict }}
508+
-- foo --
509+
`
510+
511+
// strict mode: default
512+
f := strings.ReplaceAll(files, "dict", "")
513+
b, err := hugolib.TestE(t, f)
514+
b.Assert(err.Error(), qt.Contains, "[commentAtEnd]")
515+
516+
// strict mode: error
517+
f = strings.ReplaceAll(files, "dict", `(dict "strict" "error")`)
518+
b, err = hugolib.TestE(t, f)
519+
b.Assert(err.Error(), qt.Contains, "[commentAtEnd]")
520+
521+
// strict mode: ignore
522+
f = strings.ReplaceAll(files, "dict", `(dict "strict" "ignore")`)
523+
b = hugolib.Test(t, f, hugolib.TestOptWarn())
524+
b.AssertLogMatches("")
525+
b.AssertFileContent("public/index.html", `<annotation encoding="application/x-tex">a %</annotation>`)
526+
527+
// strict: warn
528+
// TODO: see https://github.com/gohugoio/hugo/issues/13735
529+
// f = strings.ReplaceAll(files, "dict", `(dict "strict" "warn")`)
530+
// b = hugolib.Test(t, f, hugolib.TestOptWarn())
531+
// b.AssertLogMatches("[commentAtEnd]")
532+
// b.AssertFileContent("public/index.html", `<annotation encoding="application/x-tex">a %</annotation>`)
533+
534+
// strict mode: invalid value
535+
f = strings.ReplaceAll(files, "dict", `(dict "strict" "foo")`)
536+
b, err = hugolib.TestE(t, f)
537+
b.Assert(err.Error(), qt.Contains, "invalid strict mode")
538+
}

0 commit comments

Comments
 (0)