| 1 |
// Copyright 2024 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package transform_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"strings" |
| 19 |
"testing" |
| 20 |
|
| 21 |
qt "github.com/frankban/quicktest" |
| 22 |
"github.com/gohugoio/hugo/hugolib" |
| 23 |
) |
| 24 |
|
| 25 |
// Issue #11698 |
| 26 |
func TestMarkdownifyIssue11698(t *testing.T) { |
| 27 |
t.Parallel() |
| 28 |
|
| 29 |
files := ` |
| 30 |
-- hugo.toml -- |
| 31 |
disableKinds = ['home','section','rss','sitemap','taxonomy','term'] |
| 32 |
[markup.goldmark.parser.attribute] |
| 33 |
title = true |
| 34 |
block = true |
| 35 |
-- layouts/single.html -- |
| 36 |
_{{ markdownify .RawContent }}_ |
| 37 |
-- content/p1.md -- |
| 38 |
--- |
| 39 |
title: p1 |
| 40 |
--- |
| 41 |
foo bar |
| 42 |
-- content/p2.md -- |
| 43 |
--- |
| 44 |
title: p2 |
| 45 |
--- |
| 46 |
foo |
| 47 |
|
| 48 |
**bar** |
| 49 |
-- content/p3.md -- |
| 50 |
--- |
| 51 |
title: p3 |
| 52 |
--- |
| 53 |
## foo |
| 54 |
|
| 55 |
bar |
| 56 |
-- content/p4.md -- |
| 57 |
--- |
| 58 |
title: p4 |
| 59 |
--- |
| 60 |
foo |
| 61 |
{#bar} |
| 62 |
` |
| 63 |
|
| 64 |
b := hugolib.Test(t, files) |
| 65 |
|
| 66 |
b.AssertFileContent("public/p1/index.html", "_foo bar_") |
| 67 |
b.AssertFileContent("public/p2/index.html", "_<p>foo</p>\n<p><strong>bar</strong></p>\n_") |
| 68 |
b.AssertFileContent("public/p3/index.html", "_<h2 id=\"foo\">foo</h2>\n<p>bar</p>\n_") |
| 69 |
b.AssertFileContent("public/p4/index.html", "_<p id=\"bar\">foo</p>\n_") |
| 70 |
} |
| 71 |
|
| 72 |
func TestXMLEscape(t *testing.T) { |
| 73 |
t.Parallel() |
| 74 |
|
| 75 |
files := ` |
| 76 |
-- hugo.toml -- |
| 77 |
disableKinds = ['section','sitemap','taxonomy','term'] |
| 78 |
-- content/p1.md -- |
| 79 |
--- |
| 80 |
title: p1 |
| 81 |
--- |
| 82 |
a **b** ` + "\v" + ` c |
| 83 |
<!--more--> |
| 84 |
` |
| 85 |
b := hugolib.Test(t, files) |
| 86 |
|
| 87 |
b.AssertFileContent("public/index.xml", ` |
| 88 |
<description><p>a <strong>b</strong> c</p></description> |
| 89 |
`) |
| 90 |
} |
| 91 |
|
| 92 |
// Issue #9642 |
| 93 |
func TestHighlightError(t *testing.T) { |
| 94 |
t.Parallel() |
| 95 |
|
| 96 |
files := ` |
| 97 |
-- hugo.toml -- |
| 98 |
disableKinds = ['page','rss','section','sitemap','taxonomy','term'] |
| 99 |
-- layouts/home.html -- |
| 100 |
{{ highlight "a" "b" 0 }} |
| 101 |
` |
| 102 |
b, err := hugolib.TestE(t, files) |
| 103 |
b.Assert(err.Error(), qt.Contains, "error calling highlight: invalid Highlight option: 0") |
| 104 |
} |
| 105 |
|
| 106 |
// transform.Highlight: LANG is optional and may be set via the type option, and |
| 107 |
// the code option overrides CODE — consistent with transform.HighlightCodeBlock. |
| 108 |
// See issue 11872. |
| 109 |
func TestHighlightTypeAndCodeOptions(t *testing.T) { |
| 110 |
t.Parallel() |
| 111 |
|
| 112 |
files := ` |
| 113 |
-- hugo.toml -- |
| 114 |
disableKinds = ['page','rss','section','sitemap','taxonomy','term'] |
| 115 |
[markup.highlight] |
| 116 |
noClasses = false |
| 117 |
-- layouts/home.html -- |
| 118 |
lang:{{ transform.Highlight "i = 42" "go" }} |
| 119 |
type:{{ transform.Highlight "i = 42" (dict "type" "go") }} |
| 120 |
code:{{ transform.Highlight "" (dict "type" "go" "code" "i = 42") }} |
| 121 |
` |
| 122 |
|
| 123 |
b := hugolib.Test(t, files) |
| 124 |
|
| 125 |
want := `<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-go" data-lang="go"><span class="line"><span class="cl"><span class="nx">i</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">42</span></span></span></code></pre></div>` |
| 126 |
|
| 127 |
b.AssertFileContent("public/index.html", |
| 128 |
"lang:"+want, |
| 129 |
"type:"+want, |
| 130 |
"code:"+want, |
| 131 |
) |
| 132 |
} |
| 133 |
|
| 134 |
// Issue #11884 |
| 135 |
func TestUnmarshalCSVLazyDecoding(t *testing.T) { |
| 136 |
t.Parallel() |
| 137 |
|
| 138 |
files := ` |
| 139 |
-- hugo.toml -- |
| 140 |
disableKinds = ['page','rss','section','sitemap','taxonomy','term'] |
| 141 |
-- assets/pets.csv -- |
| 142 |
name,description,age |
| 143 |
Spot,a nice dog,3 |
| 144 |
Rover,"a big dog",5 |
| 145 |
Felix,a "malicious" cat,7 |
| 146 |
Bella,"an "evil" cat",9 |
| 147 |
Scar,"a "dead cat",11 |
| 148 |
-- layouts/home.html -- |
| 149 |
{{ $opts := dict "lazyQuotes" true }} |
| 150 |
{{ $data := resources.Get "pets.csv" | transform.Unmarshal $opts }} |
| 151 |
{{ printf "%v" $data | safeHTML }} |
| 152 |
` |
| 153 |
b := hugolib.Test(t, files) |
| 154 |
|
| 155 |
b.AssertFileContent("public/index.html", ` |
| 156 |
[[name description age] [Spot a nice dog 3] [Rover a big dog 5] [Felix a "malicious" cat 7] [Bella an "evil" cat 9] [Scar a "dead cat 11]] |
| 157 |
`) |
| 158 |
} |
| 159 |
|
| 160 |
func TestToMath(t *testing.T) { |
| 161 |
files := ` |
| 162 |
-- hugo.toml -- |
| 163 |
disableKinds = ['page','rss','section','sitemap','taxonomy','term'] |
| 164 |
-- layouts/home.html -- |
| 165 |
{{ transform.ToMath "c = \\pm\\sqrt{a^2 + b^2}" }} |
| 166 |
` |
| 167 |
b := hugolib.Test(t, files) |
| 168 |
|
| 169 |
b.AssertFileContent("public/index.html", ` |
| 170 |
<span class="katex"><math |
| 171 |
`) |
| 172 |
} |
| 173 |
|
| 174 |
func TestToMathError(t *testing.T) { |
| 175 |
t.Run("Default", func(t *testing.T) { |
| 176 |
files := ` |
| 177 |
-- hugo.toml -- |
| 178 |
disableKinds = ['page','rss','section','sitemap','taxonomy','term'] |
| 179 |
-- layouts/home.html -- |
| 180 |
{{ transform.ToMath "c = \\foo{a^2 + b^2}" }} |
| 181 |
` |
| 182 |
b, err := hugolib.TestE(t, files, hugolib.TestOptWarn()) |
| 183 |
|
| 184 |
b.Assert(err, qt.IsNotNil) |
| 185 |
b.Assert(err.Error(), qt.Contains, "KaTeX parse error: Undefined control sequence: \\foo") |
| 186 |
}) |
| 187 |
|
| 188 |
t.Run("Disable ThrowOnError", func(t *testing.T) { |
| 189 |
files := ` |
| 190 |
-- hugo.toml -- |
| 191 |
disableKinds = ['page','rss','section','sitemap','taxonomy','term'] |
| 192 |
-- layouts/home.html -- |
| 193 |
{{ $opts := dict "throwOnError" false }} |
| 194 |
{{ transform.ToMath "c = \\foo{a^2 + b^2}" $opts }} |
| 195 |
` |
| 196 |
b, err := hugolib.TestE(t, files, hugolib.TestOptWarn()) |
| 197 |
|
| 198 |
b.Assert(err, qt.IsNil) |
| 199 |
b.AssertFileContent("public/index.html", `#cc0000`) // Error color |
| 200 |
}) |
| 201 |
|
| 202 |
t.Run("Handle in template", func(t *testing.T) { |
| 203 |
files := ` |
| 204 |
-- hugo.toml -- |
| 205 |
disableKinds = ['page','rss','section','sitemap','taxonomy','term'] |
| 206 |
-- layouts/home.html -- |
| 207 |
{{ with try (transform.ToMath "c = \\foo{a^2 + b^2}") }} |
| 208 |
{{ with .Err }} |
| 209 |
{{ warnf "error: %s" . }} |
| 210 |
{{ else }} |
| 211 |
{{ .Value }} |
| 212 |
{{ end }} |
| 213 |
{{ end }} |
| 214 |
` |
| 215 |
b, err := hugolib.TestE(t, files, hugolib.TestOptWarn()) |
| 216 |
|
| 217 |
b.Assert(err, qt.IsNil) |
| 218 |
b.AssertLogContains("WARN error: template: home.html:1:22: executing \"home.html\" at <transform.ToMath>: error calling ToMath: KaTeX parse error: Undefined control sequence: \\foo at position 5: c = \\̲f̲o̲o̲{a^2 + b^2}") |
| 219 |
}) |
| 220 |
|
| 221 |
// See issue 13239. |
| 222 |
t.Run("Handle in template, old Err construct", func(t *testing.T) { |
| 223 |
files := ` |
| 224 |
-- hugo.toml -- |
| 225 |
disableKinds = ['page','rss','section','sitemap','taxonomy','term'] |
| 226 |
-- layouts/home.html -- |
| 227 |
{{ with transform.ToMath "c = \\pm\\sqrt{a^2 + b^2}" }} |
| 228 |
{{ with .Err }} |
| 229 |
{{ warnf "error: %s" . }} |
| 230 |
{{ else }} |
| 231 |
{{ . }} |
| 232 |
{{ end }} |
| 233 |
{{ end }} |
| 234 |
` |
| 235 |
b, err := hugolib.TestE(t, files, hugolib.TestOptWarn()) |
| 236 |
|
| 237 |
b.Assert(err, qt.IsNotNil) |
| 238 |
b.Assert(err.Error(), qt.Contains, "the return type of transform.ToMath was changed in Hugo v0.141.0 and the error handling replaced with a new try keyword, see https://gohugo.io/functions/go-template/try/") |
| 239 |
}) |
| 240 |
} |
| 241 |
|
| 242 |
func TestToMathBigAndManyExpressions(t *testing.T) { |
| 243 |
filesTemplate := ` |
| 244 |
-- hugo.toml -- |
| 245 |
disableKinds = ['rss','section','sitemap','taxonomy','term'] |
| 246 |
[markup.goldmark.extensions.passthrough] |
| 247 |
enable = true |
| 248 |
[markup.goldmark.extensions.passthrough.delimiters] |
| 249 |
block = [['\[', '\]'], ['$$', '$$']] |
| 250 |
inline = [['\(', '\)'], ['$', '$']] |
| 251 |
-- content/p1.md -- |
| 252 |
P1_CONTENT |
| 253 |
-- layouts/home.html -- |
| 254 |
Home. |
| 255 |
-- layouts/single.html -- |
| 256 |
Content: {{ .Content }}| |
| 257 |
-- layouts/_markup/render-passthrough.html -- |
| 258 |
{{ $opts := dict "throwOnError" false "displayMode" true }} |
| 259 |
{{ transform.ToMath .Inner $opts }} |
| 260 |
` |
| 261 |
|
| 262 |
t.Run("Very large file with many complex KaTeX expressions", func(t *testing.T) { |
| 263 |
files := strings.ReplaceAll(filesTemplate, "P1_CONTENT", "sourcefilename: testdata/large-katex.md") |
| 264 |
b := hugolib.Test(t, files) |
| 265 |
b.AssertFileContent("public/p1/index.html", ` |
| 266 |
<span class="katex"><math |
| 267 |
`) |
| 268 |
}) |
| 269 |
|
| 270 |
t.Run("Large and complex expression", func(t *testing.T) { |
| 271 |
// This is pulled from the file above, which times out for some reason. |
| 272 |
largeAndComplexeExpressions := `\begin{align*} \frac{\pi^2}{6}&=\frac{4}{3}\frac{(\arcsin 1)^2}{2}\\ &=\frac{4}{3}\int_0^1\frac{\arcsin x}{\sqrt{1-x^2}}\,dx\\ &=\frac{4}{3}\int_0^1\frac{x+\sum_{n=1}^{\infty}\frac{(2n-1)!!}{(2n)!!}\frac{x^{2n+1}}{2n+1}}{\sqrt{1-x^2}}\,dx\\ &=\frac{4}{3}\int_0^1\frac{x}{\sqrt{1-x^2}}\,dx +\frac{4}{3}\sum_{n=1}^{\infty}\frac{(2n-1)!!}{(2n)!!(2n+1)}\int_0^1x^{2n}\frac{x}{\sqrt{1-x^2}}\,dx\\ &=\frac{4}{3}+\frac{4}{3}\sum_{n=1}^{\infty}\frac{(2n-1)!!}{(2n)!!(2n+1)}\left[\frac{(2n)!!}{(2n+1)!!}\right]\\ &=\frac{4}{3}\sum_{n=0}^{\infty}\frac{1}{(2n+1)^2}\\ &=\frac{4}{3}\left(\sum_{n=1}^{\infty}\frac{1}{n^2}-\frac{1}{4}\sum_{n=1}^{\infty}\frac{1}{n^2}\right)\\ &=\sum_{n=1}^{\infty}\frac{1}{n^2} \end{align*}` |
| 273 |
files := strings.ReplaceAll(filesTemplate, "P1_CONTENT", fmt.Sprintf(`--- |
| 274 |
title: p1 |
| 275 |
--- |
| 276 |
|
| 277 |
$$%s$$ |
| 278 |
`, largeAndComplexeExpressions)) |
| 279 |
|
| 280 |
b := hugolib.Test(t, files) |
| 281 |
b.AssertFileContent("public/p1/index.html", ` |
| 282 |
<span class="katex"><math |
| 283 |
`) |
| 284 |
}) |
| 285 |
} |
| 286 |
|
| 287 |
// Issue #13406. |
| 288 |
func TestToMathRenderHookPosition(t *testing.T) { |
| 289 |
filesTemplate := ` |
| 290 |
-- hugo.toml -- |
| 291 |
disableKinds = ['rss','section','sitemap','taxonomy','term'] |
| 292 |
[markup.goldmark.extensions.passthrough] |
| 293 |
enable = true |
| 294 |
[markup.goldmark.extensions.passthrough.delimiters] |
| 295 |
block = [['\[', '\]'], ['$$', '$$']] |
| 296 |
inline = [['\(', '\)'], ['$', '$']] |
| 297 |
-- content/p1.md -- |
| 298 |
--- |
| 299 |
title: p1 |
| 300 |
--- |
| 301 |
|
| 302 |
Block: |
| 303 |
|
| 304 |
$$1+2$$ |
| 305 |
|
| 306 |
Some inline $1+3$ math. |
| 307 |
|
| 308 |
-- layouts/home.html -- |
| 309 |
Home. |
| 310 |
-- layouts/single.html -- |
| 311 |
Content: {{ .Content }}| |
| 312 |
-- layouts/_markup/render-passthrough.html -- |
| 313 |
{{ $opts := dict "throwOnError" true "displayMode" true }} |
| 314 |
{{- with try (transform.ToMath .Inner $opts ) }} |
| 315 |
{{- with .Err }} |
| 316 |
{{ errorf "KaTeX: %s: see %s." . $.Position }} |
| 317 |
{{- else }} |
| 318 |
{{- .Value }} |
| 319 |
{{- end }} |
| 320 |
{{- end -}} |
| 321 |
|
| 322 |
` |
| 323 |
|
| 324 |
// Block math. |
| 325 |
files := strings.Replace(filesTemplate, "$$1+2$$", "$$\\foo1+2$$", 1) |
| 326 |
b, err := hugolib.TestE(t, files) |
| 327 |
b.Assert(err, qt.IsNotNil) |
| 328 |
b.AssertLogContains("p1.md:7:1") |
| 329 |
|
| 330 |
// Inline math. |
| 331 |
files = strings.Replace(filesTemplate, "$1+3$", "$\\foo1+3$", 1) |
| 332 |
b, err = hugolib.TestE(t, files) |
| 333 |
b.Assert(err, qt.IsNotNil) |
| 334 |
b.AssertLogContains("p1.md:9:13") |
| 335 |
} |
| 336 |
|
| 337 |
func TestToMathMacros(t *testing.T) { |
| 338 |
files := ` |
| 339 |
-- hugo.toml -- |
| 340 |
disableKinds = ['page','rss','section','sitemap','taxonomy','term'] |
| 341 |
-- layouts/home.html -- |
| 342 |
{{ $macros := dict |
| 343 |
"\\addBar" "\\bar{#1}" |
| 344 |
"\\bold" "\\mathbf{#1}" |
| 345 |
}} |
| 346 |
{{ $opts := dict "macros" $macros }} |
| 347 |
{{ transform.ToMath "\\addBar{y} + \\bold{H}" $opts }} |
| 348 |
` |
| 349 |
b := hugolib.Test(t, files) |
| 350 |
|
| 351 |
b.AssertFileContent("public/index.html", ` |
| 352 |
<mi>y</mi> |
| 353 |
`) |
| 354 |
} |
| 355 |
|
| 356 |
// Issue #12977 |
| 357 |
func TestUnmarshalWithIndentedYAML(t *testing.T) { |
| 358 |
t.Parallel() |
| 359 |
|
| 360 |
files := ` |
| 361 |
-- hugo.toml -- |
| 362 |
disableKinds = ['page','rss','section','sitemap','taxonomy','term'] |
| 363 |
-- layouts/home.html -- |
| 364 |
{{ $yaml := "\n a:\n b: 1\n c:\n d: 2\n" }} |
| 365 |
{{ $yaml | transform.Unmarshal | encoding.Jsonify }} |
| 366 |
` |
| 367 |
|
| 368 |
b := hugolib.Test(t, files) |
| 369 |
|
| 370 |
b.AssertFileExists("public/index.html", true) |
| 371 |
b.AssertFileContent("public/index.html", `{"a":{"b":1},"c":{"d":2}}`) |
| 372 |
} |
| 373 |
|
| 374 |
func TestPortableText(t *testing.T) { |
| 375 |
files := ` |
| 376 |
-- hugo.toml -- |
| 377 |
-- assets/sample.json -- |
| 378 |
[ |
| 379 |
{ |
| 380 |
"_key": "a", |
| 381 |
"_type": "block", |
| 382 |
"children": [ |
| 383 |
{ |
| 384 |
"_key": "b", |
| 385 |
"_type": "span", |
| 386 |
"marks": [], |
| 387 |
"text": "Heading 2" |
| 388 |
} |
| 389 |
], |
| 390 |
"markDefs": [], |
| 391 |
"style": "h2" |
| 392 |
} |
| 393 |
] |
| 394 |
-- layouts/home.html -- |
| 395 |
{{ $markdown := resources.Get "sample.json" | transform.Unmarshal | transform.PortableText }} |
| 396 |
Markdown: {{ $markdown }}| |
| 397 |
|
| 398 |
` |
| 399 |
b := hugolib.Test(t, files) |
| 400 |
|
| 401 |
b.AssertFileContent("public/index.html", "Markdown: ## Heading 2\n|") |
| 402 |
} |
| 403 |
|
| 404 |
func TestUnmarshalCSV(t *testing.T) { |
| 405 |
t.Parallel() |
| 406 |
|
| 407 |
files := ` |
| 408 |
-- hugo.toml -- |
| 409 |
disableKinds = ['page','rss','section','sitemap','taxonomy','term'] |
| 410 |
-- layouts/all.html -- |
| 411 |
{{ $opts := OPTS }} |
| 412 |
{{ with resources.Get "pets.csv" | transform.Unmarshal $opts }} |
| 413 |
{{ jsonify . }} |
| 414 |
{{ end }} |
| 415 |
-- assets/pets.csv -- |
| 416 |
DATA |
| 417 |
` |
| 418 |
|
| 419 |
// targetType = map |
| 420 |
f := strings.ReplaceAll(files, "OPTS", `dict "targetType" "map"`) |
| 421 |
f = strings.ReplaceAll(f, "DATA", |
| 422 |
"name,type,breed,age\nSpot,dog,Collie,3\nFelix,cat,Malicious,7", |
| 423 |
) |
| 424 |
b := hugolib.Test(t, f) |
| 425 |
b.AssertFileContent("public/index.html", |
| 426 |
`[{"age":"3","breed":"Collie","name":"Spot","type":"dog"},{"age":"7","breed":"Malicious","name":"Felix","type":"cat"}]`, |
| 427 |
) |
| 428 |
|
| 429 |
// targetType = map (no data) |
| 430 |
f = strings.ReplaceAll(files, "OPTS", `dict "targetType" "map"`) |
| 431 |
f = strings.ReplaceAll(f, "DATA", "") |
| 432 |
b = hugolib.Test(t, f) |
| 433 |
b.AssertFileContent("public/index.html", "") |
| 434 |
|
| 435 |
// targetType = slice |
| 436 |
f = strings.ReplaceAll(files, "OPTS", `dict "targetType" "slice"`) |
| 437 |
f = strings.ReplaceAll(f, "DATA", |
| 438 |
"name,type,breed,age\nSpot,dog,Collie,3\nFelix,cat,Malicious,7", |
| 439 |
) |
| 440 |
b = hugolib.Test(t, f) |
| 441 |
b.AssertFileContent("public/index.html", |
| 442 |
`[["name","type","breed","age"],["Spot","dog","Collie","3"],["Felix","cat","Malicious","7"]]`, |
| 443 |
) |
| 444 |
|
| 445 |
// targetType = slice (no data) |
| 446 |
f = strings.ReplaceAll(files, "OPTS", `dict "targetType" "slice"`) |
| 447 |
f = strings.ReplaceAll(f, "DATA", "") |
| 448 |
b = hugolib.Test(t, f) |
| 449 |
b.AssertFileContent("public/index.html", "") |
| 450 |
|
| 451 |
// targetType not specified |
| 452 |
f = strings.ReplaceAll(files, "OPTS", "dict") |
| 453 |
f = strings.ReplaceAll(f, "DATA", |
| 454 |
"name,type,breed,age\nSpot,dog,Collie,3\nFelix,cat,Malicious,7", |
| 455 |
) |
| 456 |
b = hugolib.Test(t, f) |
| 457 |
b.AssertFileContent("public/index.html", |
| 458 |
`[["name","type","breed","age"],["Spot","dog","Collie","3"],["Felix","cat","Malicious","7"]]`, |
| 459 |
) |
| 460 |
|
| 461 |
// targetType not specified (no data) |
| 462 |
f = strings.ReplaceAll(files, "OPTS", "dict") |
| 463 |
f = strings.ReplaceAll(f, "DATA", "") |
| 464 |
b = hugolib.Test(t, f) |
| 465 |
b.AssertFileContent("public/index.html", "") |
| 466 |
|
| 467 |
// targetType = foo |
| 468 |
f = strings.ReplaceAll(files, "OPTS", `dict "targetType" "foo"`) |
| 469 |
_, err := hugolib.TestE(t, f) |
| 470 |
if err == nil { |
| 471 |
t.Errorf("expected error") |
| 472 |
} else { |
| 473 |
if !strings.Contains(err.Error(), `invalid targetType: expected either slice or map, received foo`) { |
| 474 |
t.Log(err.Error()) |
| 475 |
t.Errorf("error message does not match expected error message") |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
// targetType = foo (no data) |
| 480 |
f = strings.ReplaceAll(files, "OPTS", `dict "targetType" "foo"`) |
| 481 |
f = strings.ReplaceAll(f, "DATA", "") |
| 482 |
_, err = hugolib.TestE(t, f) |
| 483 |
if err == nil { |
| 484 |
t.Errorf("expected error") |
| 485 |
} else { |
| 486 |
if !strings.Contains(err.Error(), `invalid targetType: expected either slice or map, received foo`) { |
| 487 |
t.Log(err.Error()) |
| 488 |
t.Errorf("error message does not match expected error message") |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
// targetType = map (error: expected at least a header row and one data row) |
| 493 |
f = strings.ReplaceAll(files, "OPTS", `dict "targetType" "map"`) |
| 494 |
_, err = hugolib.TestE(t, f) |
| 495 |
if err == nil { |
| 496 |
t.Errorf("expected error") |
| 497 |
} else { |
| 498 |
if !strings.Contains(err.Error(), `expected at least a header row and one data row`) { |
| 499 |
t.Log(err.Error()) |
| 500 |
t.Errorf("error message does not match expected error message") |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
// targetType = map (error: header row contains duplicate field names) |
| 505 |
f = strings.ReplaceAll(files, "OPTS", `dict "targetType" "map"`) |
| 506 |
f = strings.ReplaceAll(f, "DATA", |
| 507 |
"name,name,breed,age\nSpot,dog,Collie,3\nFelix,cat,Malicious,7", |
| 508 |
) |
| 509 |
_, err = hugolib.TestE(t, f) |
| 510 |
if err == nil { |
| 511 |
t.Errorf("expected error") |
| 512 |
} else { |
| 513 |
if !strings.Contains(err.Error(), `header row contains duplicate field names`) { |
| 514 |
t.Log(err.Error()) |
| 515 |
t.Errorf("error message does not match expected error message") |
| 516 |
} |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
// Issue 13729 |
| 521 |
func TestToMathStrictMode(t *testing.T) { |
| 522 |
t.Parallel() |
| 523 |
|
| 524 |
files := ` |
| 525 |
-- hugo.toml -- |
| 526 |
disableKinds = ['page','rss','section','sitemap','taxonomy','term'] |
| 527 |
-- layouts/all.html -- |
| 528 |
{{ transform.ToMath "a %" dict }} |
| 529 |
-- foo -- |
| 530 |
` |
| 531 |
|
| 532 |
// strict mode: default |
| 533 |
f := strings.ReplaceAll(files, "dict", "") |
| 534 |
b, err := hugolib.TestE(t, f) |
| 535 |
b.Assert(err.Error(), qt.Contains, "[commentAtEnd]") |
| 536 |
|
| 537 |
// strict mode: error |
| 538 |
f = strings.ReplaceAll(files, "dict", `(dict "strict" "error")`) |
| 539 |
b, err = hugolib.TestE(t, f) |
| 540 |
b.Assert(err.Error(), qt.Contains, "[commentAtEnd]") |
| 541 |
|
| 542 |
// strict mode: ignore |
| 543 |
f = strings.ReplaceAll(files, "dict", `(dict "strict" "ignore")`) |
| 544 |
b = hugolib.Test(t, f, hugolib.TestOptWarn()) |
| 545 |
b.AssertLogMatches("") |
| 546 |
b.AssertFileContent("public/index.html", `<annotation encoding="application/x-tex">a %</annotation>`) |
| 547 |
|
| 548 |
// strict: warn |
| 549 |
f = strings.ReplaceAll(files, "dict", `(dict "strict" "warn")`) |
| 550 |
b = hugolib.Test(t, f, hugolib.TestOptWarn()) |
| 551 |
b.AssertLogMatches("[commentAtEnd]") |
| 552 |
b.AssertFileContent("public/index.html", `<annotation encoding="application/x-tex">a %</annotation>`) |
| 553 |
|
| 554 |
// strict mode: invalid value |
| 555 |
f = strings.ReplaceAll(files, "dict", `(dict "strict" "foo")`) |
| 556 |
b, err = hugolib.TestE(t, f) |
| 557 |
b.Assert(err.Error(), qt.Contains, "invalid strict mode") |
| 558 |
} |
| 559 |
|
| 560 |
func TestHTMLToMarkdown(t *testing.T) { |
| 561 |
t.Parallel() |
| 562 |
|
| 563 |
markdown := ` |
| 564 |
# Heading |
| 565 |
|
| 566 |
Some **bold** text. |
| 567 |
|
| 568 |
A [link](https://example.com). |
| 569 |
|
| 570 |
An image: |
| 571 |
|
| 572 |
 |
| 573 |
|
| 574 |
A list: |
| 575 |
|
| 576 |
- Item 1 |
| 577 |
- Item 2 |
| 578 |
- Item 2a |
| 579 |
- Item 2b |
| 580 |
|
| 581 |
A table: |
| 582 |
|
| 583 |
| Header 1 | Header 2 | |
| 584 |
|----------|----------| |
| 585 |
| Cell 1 | Cell 2 | |
| 586 |
| Cell 3 | Cell 4 | |
| 587 |
|
| 588 |
A blockquote: |
| 589 |
|
| 590 |
> This is a quote. |
| 591 |
` |
| 592 |
files := ` |
| 593 |
-- hugo.toml -- |
| 594 |
disableKinds = ['rss','section','sitemap','taxonomy','term'] |
| 595 |
-- layouts/all.html -- |
| 596 |
All html. |
| 597 |
-- layouts/all.markdown -- |
| 598 |
{{ .Content | transform.HTMLToMarkdown | safeHTML }} |
| 599 |
-- content/p1.md -- |
| 600 |
--- |
| 601 |
title: p1 |
| 602 |
outputs: ["html", "markdown"] |
| 603 |
--- |
| 604 |
` |
| 605 |
|
| 606 |
files += markdown |
| 607 |
|
| 608 |
b := hugolib.Test(t, files) |
| 609 |
|
| 610 |
b.AssertFileContent("public/p1/index.html", `All html.`) |
| 611 |
|
| 612 |
// There are some white space differences, so we cannot do an exact match. |
| 613 |
b.AssertFileContent("public/p1/index.md", markdown) |
| 614 |
} |
| 615 |
|
| 616 |
// See https://github.com/goccy/go-yaml/issues/461 |
| 617 |
func TestUnmarshalExcessiveYAMLStructureShouldFail(t *testing.T) { |
| 618 |
t.Parallel() |
| 619 |
|
| 620 |
files := ` |
| 621 |
-- hugo.toml -- |
| 622 |
disableKinds = ['page','rss','section','sitemap','taxonomy','term'] |
| 623 |
-- assets/ddos.yaml -- |
| 624 |
a: &a [_, _, _, _, _, _, _, _, _, _, _, _, _, _, _] |
| 625 |
b: &b [*a, *a, *a, *a, *a, *a, *a, *a, *a, *a] |
| 626 |
c: &c [*b, *b, *b, *b, *b, *b, *b, *b, *b, *b] |
| 627 |
d: &d [*c, *c, *c, *c, *c, *c, *c, *c, *c, *c] |
| 628 |
e: &e [*d, *d, *d, *d, *d, *d, *d, *d, *d, *d] |
| 629 |
f: &f [*e, *e, *e, *e, *e, *e, *e, *e, *e, *e] |
| 630 |
g: &g [*f, *f, *f, *f, *f, *f, *f, *f, *f, *f] |
| 631 |
h: &h [*g, *g, *g, *g, *g, *g, *g, *g, *g, *g] |
| 632 |
i: &i [*h, *h, *h, *h, *h, *h, *h, *h, *h, *h] |
| 633 |
-- layouts/home.html -- |
| 634 |
{{ $m := resources.Get "ddos.yaml" | transform.Unmarshal }} |
| 635 |
{{ printf "Length: %d" (len $m) }} |
| 636 |
` |
| 637 |
|
| 638 |
b, err := hugolib.TestE(t, files) |
| 639 |
|
| 640 |
b.Assert(err, qt.IsNotNil) |
| 641 |
b.Assert(err.Error(), qt.Contains, "too many YAML aliases for non-scalar nodes") |
| 642 |
} |