github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/markup/goldmark/codeblocks/integration_test.go (about) 1 // Copyright 2022 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 codeblocks_test 15 16 import ( 17 "path/filepath" 18 "strings" 19 "testing" 20 21 qt "github.com/frankban/quicktest" 22 23 "github.com/gohugoio/hugo/hugolib" 24 ) 25 26 func TestCodeblocks(t *testing.T) { 27 t.Parallel() 28 29 files := ` 30 -- config.toml -- 31 [markup] 32 [markup.highlight] 33 anchorLineNos = false 34 codeFences = true 35 guessSyntax = false 36 hl_Lines = '' 37 lineAnchors = '' 38 lineNoStart = 1 39 lineNos = false 40 lineNumbersInTable = true 41 noClasses = false 42 style = 'monokai' 43 tabWidth = 4 44 -- layouts/_default/_markup/render-codeblock-goat.html -- 45 {{ $diagram := diagrams.Goat .Inner }} 46 Goat SVG:{{ substr $diagram.Wrapped 0 100 | safeHTML }} }}| 47 Goat Attribute: {{ .Attributes.width}}| 48 -- layouts/_default/_markup/render-codeblock-go.html -- 49 Go Code: {{ .Inner | safeHTML }}| 50 Go Language: {{ .Type }}| 51 -- layouts/_default/single.html -- 52 {{ .Content }} 53 -- content/p1.md -- 54 --- 55 title: "p1" 56 --- 57 58 ## Ascii Diagram 59 60 §§§goat { width="600" } 61 ---> 62 §§§ 63 64 ## Go Code 65 66 §§§go 67 fmt.Println("Hello, World!"); 68 §§§ 69 70 ## Golang Code 71 72 §§§golang 73 fmt.Println("Hello, Golang!"); 74 §§§ 75 76 ## Bash Code 77 78 §§§bash { linenos=inline,hl_lines=[2,"5-6"],linenostart=32 class=blue } 79 echo "l1"; 80 echo "l2"; 81 echo "l3"; 82 echo "l4"; 83 echo "l5"; 84 echo "l6"; 85 echo "l7"; 86 echo "l8"; 87 §§§ 88 ` 89 90 b := hugolib.NewIntegrationTestBuilder( 91 hugolib.IntegrationTestConfig{ 92 T: t, 93 TxtarString: files, 94 NeedsOsFS: false, 95 }, 96 ).Build() 97 98 b.AssertFileContent("public/p1/index.html", ` 99 Goat SVG:<svg class='diagram' 100 Goat Attribute: 600| 101 102 Go Language: go| 103 Go Code: fmt.Println("Hello, World!"); 104 105 Go Code: fmt.Println("Hello, Golang!"); 106 Go Language: golang| 107 108 109 `, 110 "Goat SVG:<svg class='diagram' xmlns='http://www.w3.org/2000/svg' version='1.1' height='25' width='40'", 111 "Goat Attribute: 600|", 112 "<h2 id=\"go-code\">Go Code</h2>\nGo Code: fmt.Println(\"Hello, World!\");\n|\nGo Language: go|", 113 "<h2 id=\"golang-code\">Golang Code</h2>\nGo Code: fmt.Println(\"Hello, Golang!\");\n|\nGo Language: golang|", 114 "<h2 id=\"bash-code\">Bash Code</h2>\n<div class=\"highlight blue\"><pre tabindex=\"0\" class=\"chroma\"><code class=\"language-bash\" data-lang=\"bash\"><span class=\"line\"><span class=\"ln\">32</span><span class=\"cl\"><span class=\"nb\">echo</span> <span class=\"s2\">"l1"</span><span class=\"p\">;</span>\n</span></span><span class=\"line hl\"><span class=\"ln\">33</span>", 115 ) 116 } 117 118 func TestHighlightCodeblock(t *testing.T) { 119 t.Parallel() 120 121 files := ` 122 -- config.toml -- 123 [markup] 124 [markup.highlight] 125 anchorLineNos = false 126 codeFences = true 127 guessSyntax = false 128 hl_Lines = '' 129 lineAnchors = '' 130 lineNoStart = 1 131 lineNos = false 132 lineNumbersInTable = true 133 noClasses = false 134 style = 'monokai' 135 tabWidth = 4 136 -- layouts/_default/_markup/render-codeblock.html -- 137 {{ $result := transform.HighlightCodeBlock . }} 138 Inner: |{{ $result.Inner | safeHTML }}| 139 Wrapped: |{{ $result.Wrapped | safeHTML }}| 140 -- layouts/_default/single.html -- 141 {{ .Content }} 142 -- content/p1.md -- 143 --- 144 title: "p1" 145 --- 146 147 ## Go Code 148 149 §§§go 150 fmt.Println("Hello, World!"); 151 §§§ 152 153 ` 154 155 b := hugolib.NewIntegrationTestBuilder( 156 hugolib.IntegrationTestConfig{ 157 T: t, 158 TxtarString: files, 159 NeedsOsFS: false, 160 }, 161 ).Build() 162 163 b.AssertFileContent("public/p1/index.html", 164 "Inner: |<span class=\"line\"><span class=\"cl\"><span class=\"nx\">fmt</span><span class=\"p\">.</span><span class=\"nf\">Println</span><span class=\"p\">(</span><span class=\"s\">"Hello, World!"</span><span class=\"p\">);</span></span></span>|", 165 "Wrapped: |<div class=\"highlight\"><pre tabindex=\"0\" class=\"chroma\"><code class=\"language-go\" data-lang=\"go\"><span class=\"line\"><span class=\"cl\"><span class=\"nx\">fmt</span><span class=\"p\">.</span><span class=\"nf\">Println</span><span class=\"p\">(</span><span class=\"s\">"Hello, World!"</span><span class=\"p\">);</span></span></span></code></pre></div>|", 166 ) 167 } 168 169 func TestCodeblocksBugs(t *testing.T) { 170 t.Parallel() 171 172 files := ` 173 -- config.toml -- 174 -- layouts/_default/_markup/render-codeblock.html -- 175 {{ .Position | safeHTML }} 176 -- layouts/_default/single.html -- 177 {{ .Content }} 178 -- content/p1.md -- 179 --- 180 title: "p1" 181 --- 182 183 ## Issue 9627 184 185 §§§text 186 {{</* foo */>}} 187 §§§ 188 189 ` 190 191 b := hugolib.NewIntegrationTestBuilder( 192 hugolib.IntegrationTestConfig{ 193 T: t, 194 TxtarString: files, 195 NeedsOsFS: false, 196 }, 197 ).Build() 198 199 b.AssertFileContent("public/p1/index.html", ` 200 # Issue 9627: For the Position in code blocks we try to match the .Inner with the original source. This isn't always possible. 201 p1.md:0:0 202 `, 203 ) 204 } 205 206 func TestCodeChomp(t *testing.T) { 207 t.Parallel() 208 209 files := ` 210 -- config.toml -- 211 -- content/p1.md -- 212 --- 213 title: "p1" 214 --- 215 216 §§§bash 217 echo "p1"; 218 §§§ 219 -- layouts/_default/single.html -- 220 {{ .Content }} 221 -- layouts/_default/_markup/render-codeblock.html -- 222 |{{ .Inner | safeHTML }}| 223 224 ` 225 226 b := hugolib.NewIntegrationTestBuilder( 227 hugolib.IntegrationTestConfig{ 228 T: t, 229 TxtarString: files, 230 NeedsOsFS: false, 231 }, 232 ).Build() 233 234 b.AssertFileContent("public/p1/index.html", "|echo \"p1\";|") 235 } 236 237 func TestCodePosition(t *testing.T) { 238 t.Parallel() 239 240 files := ` 241 -- config.toml -- 242 -- content/p1.md -- 243 --- 244 title: "p1" 245 --- 246 247 ## Code 248 249 §§§ 250 echo "p1"; 251 §§§ 252 -- layouts/_default/single.html -- 253 {{ .Content }} 254 -- layouts/_default/_markup/render-codeblock.html -- 255 Position: {{ .Position | safeHTML }} 256 257 258 ` 259 260 b := hugolib.NewIntegrationTestBuilder( 261 hugolib.IntegrationTestConfig{ 262 T: t, 263 TxtarString: files, 264 }, 265 ).Build() 266 267 b.AssertFileContent("public/p1/index.html", filepath.FromSlash("Position: \"/content/p1.md:7:1\"")) 268 } 269 270 // Issue 10118 271 func TestAttributes(t *testing.T) { 272 t.Parallel() 273 274 files := ` 275 -- config.toml -- 276 -- content/p1.md -- 277 --- 278 title: "p1" 279 --- 280 281 ## Issue 10118 282 283 §§§ {foo="bar"} 284 Hello, World! 285 §§§ 286 287 -- layouts/_default/single.html -- 288 {{ .Content }} 289 -- layouts/_default/_markup/render-codeblock.html -- 290 Attributes: {{ .Attributes }}|Type: {{ .Type }}| 291 ` 292 293 b := hugolib.NewIntegrationTestBuilder( 294 hugolib.IntegrationTestConfig{ 295 T: t, 296 TxtarString: files, 297 }, 298 ).Build() 299 300 b.AssertFileContent("public/p1/index.html", "<h2 id=\"issue-10118\">Issue 10118</h2>\nAttributes: map[foo:bar]|Type: |") 301 } 302 303 // Issue 9571 304 func TestAttributesChroma(t *testing.T) { 305 t.Parallel() 306 307 files := ` 308 -- config.toml -- 309 -- content/p1.md -- 310 --- 311 title: "p1" 312 --- 313 314 ## Code 315 316 §§§LANGUAGE {style=monokai} 317 echo "p1"; 318 §§§ 319 -- layouts/_default/single.html -- 320 {{ .Content }} 321 -- layouts/_default/_markup/render-codeblock.html -- 322 Attributes: {{ .Attributes }}|Options: {{ .Options }}| 323 324 325 ` 326 testLanguage := func(language, expect string) { 327 b := hugolib.NewIntegrationTestBuilder( 328 hugolib.IntegrationTestConfig{ 329 T: t, 330 TxtarString: strings.ReplaceAll(files, "LANGUAGE", language), 331 }, 332 ).Build() 333 334 b.AssertFileContent("public/p1/index.html", expect) 335 } 336 337 testLanguage("bash", "Attributes: map[]|Options: map[style:monokai]|") 338 testLanguage("hugo", "Attributes: map[style:monokai]|Options: map[]|") 339 } 340 341 func TestPanics(t *testing.T) { 342 343 files := ` 344 -- config.toml -- 345 [markup] 346 [markup.goldmark] 347 [markup.goldmark.parser] 348 autoHeadingID = true 349 autoHeadingIDType = "github" 350 [markup.goldmark.parser.attribute] 351 block = true 352 title = true 353 -- content/p1.md -- 354 --- 355 title: "p1" 356 --- 357 358 BLOCK 359 360 Common 361 362 -- layouts/_default/single.html -- 363 {{ .Content }} 364 365 366 ` 367 368 for _, test := range []struct { 369 name string 370 markdown string 371 }{ 372 {"issue-9819", "asdf\n: {#myid}"}, 373 } { 374 test := test 375 t.Run(test.name, func(t *testing.T) { 376 t.Parallel() 377 b := hugolib.NewIntegrationTestBuilder( 378 hugolib.IntegrationTestConfig{ 379 T: t, 380 TxtarString: strings.ReplaceAll(files, "BLOCK", test.markdown), 381 }, 382 ).Build() 383 384 b.AssertFileContent("public/p1/index.html", "Common") 385 }) 386 } 387 388 } 389 390 // Issue 10835 391 func TestAttributesValidation(t *testing.T) { 392 t.Parallel() 393 394 files := ` 395 -- hugo.toml -- 396 disableKinds = ["taxonomy", "term"] 397 -- content/p1.md -- 398 --- 399 title: "p1" 400 --- 401 402 ## Issue 10835 403 404 §§§bash { color=red dimensions=300x200 } 405 Hello, World! 406 §§§ 407 408 -- layouts/index.html -- 409 -- layouts/_default/single.html -- 410 {{ .Content }} 411 -- layouts/_default/_markup/render-codeblock.html -- 412 Attributes: {{ .Attributes }}|Type: {{ .Type }}| 413 ` 414 415 b, err := hugolib.NewIntegrationTestBuilder( 416 hugolib.IntegrationTestConfig{ 417 T: t, 418 TxtarString: files, 419 }, 420 ).BuildE() 421 422 b.Assert(err, qt.Not(qt.IsNil)) 423 b.Assert(err.Error(), qt.Contains, "p1.md:7:9\": failed to parse Markdown attributes; you may need to quote the values") 424 425 }