Skip to content

Commit 641403f

Browse files
committed
Fix Position for passthrough hooks
Fixes #13406
1 parent 24cc255 commit 641403f

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

markup/goldmark/internal/render/context.go

+28-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"strings"
2020
"sync"
2121

22+
"github.com/gohugoio/hugo-goldmark-extensions/passthrough"
2223
bp "github.com/gohugoio/hugo/bufferpool"
2324
east "github.com/yuin/goldmark-emoji/ast"
2425

@@ -163,21 +164,44 @@ func (ctx *RenderContextDataHolder) DocumentContext() converter.DocumentContext
163164
// Note that this is not a copy of the source, but a slice of it,
164165
// so it assumes that the source is not mutated.
165166
func extractSourceSample(n ast.Node, src []byte) []byte {
167+
if n.Type() == ast.TypeInline {
168+
switch n := n.(type) {
169+
case *passthrough.PassthroughInline:
170+
return n.Segment.Value(src)
171+
}
172+
173+
return nil
174+
}
175+
166176
var sample []byte
167177

168-
// Extract a source sample to use for position information.
169-
if nn := n.FirstChild(); nn != nil {
178+
getStartStop := func(n ast.Node) (int, int) {
179+
if n == nil {
180+
return 0, 0
181+
}
182+
170183
var start, stop int
171-
for i := 0; i < nn.Lines().Len() && i < 2; i++ {
172-
line := nn.Lines().At(i)
184+
for i := 0; i < n.Lines().Len() && i < 2; i++ {
185+
line := n.Lines().At(i)
173186
if i == 0 {
174187
start = line.Start
175188
}
176189
stop = line.Stop
177190
}
191+
return start, stop
192+
}
193+
194+
start, stop := getStartStop(n)
195+
if stop == 0 {
196+
// Try first child.
197+
start, stop = getStartStop(n.FirstChild())
198+
}
199+
200+
if stop > 0 {
178201
// We do not mutate the source, so this is safe.
179202
sample = src[start:stop]
180203
}
204+
181205
return sample
182206
}
183207

tpl/transform/transform_integration_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,56 @@ $$%s$$
263263
})
264264
}
265265

266+
// Issue #13406.
267+
func TestToMathRenderHookPosition(t *testing.T) {
268+
filesTemplate := `
269+
-- hugo.toml --
270+
disableKinds = ['rss','section','sitemap','taxonomy','term']
271+
[markup.goldmark.extensions.passthrough]
272+
enable = true
273+
[markup.goldmark.extensions.passthrough.delimiters]
274+
block = [['\[', '\]'], ['$$', '$$']]
275+
inline = [['\(', '\)'], ['$', '$']]
276+
-- content/p1.md --
277+
---
278+
title: p1
279+
---
280+
281+
Block:
282+
283+
$$1+2$$
284+
285+
Some inline $1+3$ math.
286+
287+
-- layouts/index.html --
288+
Home.
289+
-- layouts/_default/single.html --
290+
Content: {{ .Content }}|
291+
-- layouts/_default/_markup/render-passthrough.html --
292+
{{ $opts := dict "throwOnError" true "displayMode" true }}
293+
{{- with try (transform.ToMath .Inner $opts ) }}
294+
{{- with .Err }}
295+
{{ errorf "KaTeX: %s: see %s." . $.Position }}
296+
{{- else }}
297+
{{- .Value }}
298+
{{- end }}
299+
{{- end -}}
300+
301+
`
302+
303+
// Block math.
304+
files := strings.Replace(filesTemplate, "$$1+2$$", "$$\\foo1+2$$", 1)
305+
b, err := hugolib.TestE(t, files)
306+
b.Assert(err, qt.IsNotNil)
307+
b.AssertLogContains("p1.md:6:1")
308+
309+
// Inline math.
310+
files = strings.Replace(filesTemplate, "$1+3$", "$\\foo1+3$", 1)
311+
b, err = hugolib.TestE(t, files)
312+
b.Assert(err, qt.IsNotNil)
313+
b.AssertLogContains("p1.md:8:13")
314+
}
315+
266316
func TestToMathMacros(t *testing.T) {
267317
files := `
268318
-- hugo.toml --

0 commit comments

Comments
 (0)