Skip to content

Commit c523cac

Browse files
committed
Handle KaTeX warnings
Fixes #13735
1 parent bff5d19 commit c523cac

File tree

9 files changed

+62
-6
lines changed

9 files changed

+62
-6
lines changed

hugolib/site.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ func NewHugoSites(cfg deps.DepsCfg) (*HugoSites, error) {
204204
// Katex is relatively slow.
205205
PoolSize: 8,
206206
Infof: logger.InfoCommand("wasm").Logf,
207+
Warnf: logger.WarnCommand("wasm").Logf,
207208
},
208209
),
209210
}

internal/warpc/js/common.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ export function readInput(handle) {
44
let currentLine = [];
55
const buffer = new Uint8Array(buffSize);
66

7+
// These are not implemented by QuickJS.
8+
console.warn = (value) => {
9+
console.log(value);
10+
};
11+
12+
console.error = (value) => {
13+
throw new Error(value);
14+
};
15+
716
// Read all the available bytes
817
while (true) {
918
// Stdin file descriptor

internal/warpc/js/greet.bundle.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/warpc/js/renderkatex.bundle.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/warpc/js/renderkatex.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ const render = function (input) {
77
const expression = data.expression;
88
const options = data.options;
99
const header = input.header;
10+
header.warnings = [];
11+
12+
if (options.strict == 'warn') {
13+
// By default, KaTeX will write to console.warn, that's a little hard to handle.
14+
options.strict = (errorCode, errorMsg, token) => {
15+
header.warnings.push(`katex: ${errorCode}: ${errorMsg} at ${token}`);
16+
};
17+
}
1018
// Any error thrown here will be caught by the common.js readInput function.
1119
const output = katex.renderToString(expression, options);
1220
writeOutput({ header: header, data: { output: output } });

internal/warpc/warpc.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ type Header struct {
5151

5252
// Set in the response if there was an error.
5353
Err string `json:"err"`
54+
55+
// Warnings is a list of warnings that may be returned in the response.
56+
Warnings []string `json:"warnings,omitempty"`
5457
}
5558

5659
type Message[T any] struct {
@@ -155,6 +158,14 @@ func (p *dispatcherPool[Q, R]) Execute(ctx context.Context, q Message[Q]) (Messa
155158
}
156159

157160
resp, err := call.response, p.Err()
161+
if len(resp.Header.Warnings) > 0 {
162+
for _, w := range resp.Header.Warnings {
163+
if w != "" {
164+
p.opts.Warnf("%s", w)
165+
}
166+
}
167+
}
168+
158169
if err == nil && resp.Header.Err != "" {
159170
err = errors.New(resp.Header.Err)
160171
}
@@ -270,6 +281,8 @@ type Options struct {
270281

271282
Infof func(format string, v ...any)
272283

284+
Warnf func(format string, v ...any)
285+
273286
// E.g. quickjs wasm. May be omitted if not needed.
274287
Runtime Binary
275288

@@ -325,6 +338,7 @@ type dispatcherPool[Q, R any] struct {
325338
counter atomic.Uint32
326339
dispatchers []*dispatcher[Q, R]
327340
close func() error
341+
opts Options
328342

329343
errc chan error
330344
donec chan struct{}
@@ -355,6 +369,11 @@ func newDispatcher[Q, R any](opts Options) (*dispatcherPool[Q, R], error) {
355369
// noop
356370
}
357371
}
372+
if opts.Warnf == nil {
373+
opts.Warnf = func(format string, v ...any) {
374+
// noop
375+
}
376+
}
358377

359378
if opts.Memory <= 0 {
360379
// 32 MiB
@@ -466,6 +485,7 @@ func newDispatcher[Q, R any](opts Options) (*dispatcherPool[Q, R], error) {
466485

467486
dp := &dispatcherPool[Q, R]{
468487
dispatchers: make([]*dispatcher[Q, R], len(inOuts)),
488+
opts: opts,
469489

470490
errc: make(chan error, 10),
471491
donec: make(chan struct{}),

internal/warpc/wasm/greet.wasm

137 Bytes
Binary file not shown.

internal/warpc/wasm/renderkatex.wasm

507 Bytes
Binary file not shown.

tpl/transform/transform_integration_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,3 +536,21 @@ disableKinds = ['page','rss','section','sitemap','taxonomy','term']
536536
b, err = hugolib.TestE(t, f)
537537
b.Assert(err.Error(), qt.Contains, "invalid strict mode")
538538
}
539+
540+
// Issue 13735.
541+
func TestToMathStrictModeWarn(t *testing.T) {
542+
t.Parallel()
543+
544+
files := `
545+
-- hugo.toml --
546+
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
547+
-- layouts/all.html --
548+
{{ transform.ToMath "a %" (dict "strict" "warn") }}
549+
-- foo --
550+
`
551+
552+
b := hugolib.Test(t, files, hugolib.TestOptWarn())
553+
554+
b.AssertLogMatches("commentAtEnd")
555+
b.AssertFileContent("public/index.html", `<annotation encoding="application/x-tex">a %</annotation>`)
556+
}

0 commit comments

Comments
 (0)