Debian Codegraph @

markup/goldmark/codeblocks/codeblocks_integration_test.go

raw
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 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 -- hugo.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/_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/_markup/render-codeblock-go.html --
49 Go Code: {{ .Inner | safeHTML }}|
50 Go Language: {{ .Type }}|
51 -- layouts/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 §§§go
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 for range 6 {
91
92 b := hugolib.Test(t, files)
93
94 b.AssertFileContent("public/p1/index.html", `
95 Goat SVG:<svg class='diagram'
96 Goat Attribute: 600|
97
98 Go Language: go|
99 Go Code: fmt.Println("Hello, World!");
100
101 Go Code: fmt.Println("Hello, Golang!");
102 Go Language: go|
103
104
105 `,
106 "Goat SVG:<svg class='diagram' xmlns='http://www.w3.org/2000/svg' version='1.1' height='25' width='40'",
107 "Goat Attribute: 600|",
108 "<h2 id=\"go-code\">Go Code</h2>\nGo Code: fmt.Println(\"Hello, World!\");\n|\nGo Language: go|",
109 "<h2 id=\"golang-code\">Golang Code</h2>\nGo Code: fmt.Println(\"Hello, Golang!\");\n|\nGo Language: go|",
110 "<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\">&#34;l1&#34;</span><span class=\"p\">;</span>\n</span></span><span class=\"line hl\"><span class=\"ln\">33</span>",
111 )
112 }
113 }
114
115 func TestHighlightCodeblock(t *testing.T) {
116 t.Parallel()
117
118 files := `
119 -- hugo.toml --
120 [markup]
121 [markup.highlight]
122 anchorLineNos = false
123 codeFences = true
124 guessSyntax = false
125 hl_Lines = ''
126 lineAnchors = ''
127 lineNoStart = 1
128 lineNos = false
129 lineNumbersInTable = true
130 noClasses = false
131 style = 'monokai'
132 tabWidth = 4
133 -- layouts/_markup/render-codeblock.html --
134 {{ $result := transform.HighlightCodeBlock . }}
135 Inner: |{{ $result.Inner | safeHTML }}|
136 Wrapped: |{{ $result.Wrapped | safeHTML }}|
137 -- layouts/single.html --
138 {{ .Content }}
139 -- content/p1.md --
140 ---
141 title: "p1"
142 ---
143
144 ## Go Code
145
146 §§§go
147 fmt.Println("Hello, World!");
148 §§§
149
150 ## Plain Code
151
152 §§§
153 plain <code> & text
154 §§§
155
156 `
157
158 b := hugolib.Test(t, files)
159
160 b.AssertFileContent("public/p1/index.html",
161 "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\">&#34;Hello, World!&#34;</span><span class=\"p\">);</span></span></span>|",
162 "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\">&#34;Hello, World!&#34;</span><span class=\"p\">);</span></span></span></code></pre></div>|",
163 "Inner: |plain &lt;code&gt; &amp; text|",
164 "Wrapped: |<pre tabindex=\"0\"><code>plain &lt;code&gt; &amp; text</code></pre>|",
165 )
166 }
167
168 func TestCodeblocksBugs(t *testing.T) {
169 t.Parallel()
170
171 files := `
172 -- hugo.toml --
173 -- layouts/_markup/render-codeblock.html --
174 {{ .Position | safeHTML }}
175 -- layouts/single.html --
176 {{ .Content }}
177 -- content/p1.md --
178 ---
179 title: "p1"
180 ---
181
182 ## Issue 9627
183
184 §§§text
185 {{</* foo */>}}
186 §§§
187
188 `
189
190 b := hugolib.Test(t, files)
191
192 b.AssertFileContent("public/p1/index.html",
193 "p1.md:7:1",
194 )
195 }
196
197 func TestCodeChomp(t *testing.T) {
198 t.Parallel()
199
200 files := `
201 -- hugo.toml --
202 -- content/p1.md --
203 ---
204 title: "p1"
205 ---
206
207 §§§bash
208 echo "p1";
209 §§§
210 -- layouts/single.html --
211 {{ .Content }}
212 -- layouts/_markup/render-codeblock.html --
213 |{{ .Inner | safeHTML }}|
214
215 `
216
217 b := hugolib.Test(t, files)
218
219 b.AssertFileContent("public/p1/index.html", "|echo \"p1\";|")
220 }
221
222 func TestCodePosition(t *testing.T) {
223 t.Parallel()
224
225 files := `
226 -- hugo.toml --
227 -- content/p1.md --
228 ---
229 title: "p1"
230 ---
231
232 ## Code
233
234 §§§
235 echo "p1";
236 §§§
237 -- layouts/single.html --
238 {{ .Content }}
239 -- layouts/_markup/render-codeblock.html --
240 Position: {{ .Position | safeHTML }}
241
242
243 `
244
245 b := hugolib.Test(t, files)
246
247 b.AssertFileContent("public/p1/index.html", filepath.FromSlash("Position: \"/content/p1.md:7:1\""))
248 }
249
250 // Issue 10118
251 func TestAttributes(t *testing.T) {
252 t.Parallel()
253
254 files := `
255 -- hugo.toml --
256 -- content/p1.md --
257 ---
258 title: "p1"
259 ---
260
261 ## Issue 10118
262
263 §§§ {foo="bar"}
264 Hello, World!
265 §§§
266
267 -- layouts/single.html --
268 {{ .Content }}
269 -- layouts/_markup/render-codeblock.html --
270 Attributes: {{ .Attributes }}|Type: {{ .Type }}|
271 `
272
273 b := hugolib.Test(t, files)
274
275 b.AssertFileContent("public/p1/index.html", "<h2 id=\"issue-10118\">Issue 10118</h2>\nAttributes: map[foo:bar]|Type: |")
276 }
277
278 // Issue 9571
279 // Issue 14909
280 func TestAttributesChroma(t *testing.T) {
281 t.Parallel()
282
283 files := `
284 -- hugo.toml --
285 -- content/p1.md --
286 ---
287 title: "p1"
288 ---
289
290 ## Code
291
292 §§§LANGUAGE {style=monokai class=my-class tabWidth=8}
293 echo "p1";
294 §§§
295 -- layouts/single.html --
296 {{ .Content }}
297 -- layouts/_markup/render-codeblock.html --
298 Attributes: {{ .Attributes }}|Options: {{ .Options }}|
299
300
301 `
302 testLanguage := func(language, expect string) {
303 b := hugolib.Test(t, strings.ReplaceAll(files, "LANGUAGE", language))
304
305 b.AssertFileContent("public/p1/index.html", expect)
306 }
307
308 testLanguage("bash", "Attributes: map[class:my-class]|Options: map[style:monokai tabWidth:8]|")
309 testLanguage("hugo", "Attributes: map[class:my-class]|Options: map[style:monokai tabWidth:8]|")
310 testLanguage("", "Attributes: map[class:my-class]|Options: map[style:monokai tabWidth:8]|")
311 }
312
313 func TestPanics(t *testing.T) {
314 files := `
315 -- hugo.toml --
316 [markup]
317 [markup.goldmark]
318 [markup.goldmark.parser]
319 autoHeadingID = true
320 autoHeadingIDType = "github"
321 [markup.goldmark.parser.attribute]
322 block = true
323 title = true
324 -- content/p1.md --
325 ---
326 title: "p1"
327 ---
328
329 BLOCK
330
331 Common
332
333 -- layouts/single.html --
334 {{ .Content }}
335
336
337 `
338
339 for _, test := range []struct {
340 name string
341 markdown string
342 }{
343 {"issue-9819", "asdf\n: {#myid}"},
344 } {
345 t.Run(test.name, func(t *testing.T) {
346 t.Parallel()
347 b := hugolib.Test(t, strings.ReplaceAll(files, "BLOCK", test.markdown))
348
349 b.AssertFileContent("public/p1/index.html", "Common")
350 })
351 }
352 }
353
354 // Issue 10835
355 func TestAttributesValidation(t *testing.T) {
356 t.Parallel()
357
358 files := `
359 -- hugo.toml --
360 disableKinds = ["taxonomy", "term"]
361 -- content/p1.md --
362 ---
363 title: "p1"
364 ---
365
366 ## Issue 10835
367
368 §§§bash { color=red dimensions=300x200 }
369 Hello, World!
370 §§§
371
372 -- layouts/home.html --
373 -- layouts/single.html --
374 {{ .Content }}
375 -- layouts/_markup/render-codeblock.html --
376 Attributes: {{ .Attributes }}|Type: {{ .Type }}|
377 `
378
379 b, err := hugolib.TestE(t, files)
380
381 b.Assert(err, qt.Not(qt.IsNil))
382 b.Assert(err.Error(), qt.Contains, "p1.md:7:9\": failed to parse Markdown attributes; you may need to quote the values")
383 }
384
385 func TestCodeblockPosition(t *testing.T) {
386 t.Parallel()
387
388 files := `
389 -- hugo.toml --
390 -- layouts/_markup/render-codeblock.html --
391 {{ .Position | safeHTML }}
392 -- layouts/single.html --
393 {{ .Content }}
394 -- content/p1.md --
395 ---
396 title: "p1"
397 ---
398
399 ## Simple
400
401 §§§text
402 Some code.
403 §§§
404
405 `
406
407 b := hugolib.Test(t, files)
408
409 b.AssertFileContent("public/p1/index.html", "p1.md:7:1")
410 }
411
412 func TestCodeblockLangEscape(t *testing.T) {
413 t.Parallel()
414
415 files := `
416 -- hugo.toml --
417 -- layouts/single.html --
418 {{ .Content }}
419 -- content/p1.md --
420 ---
421 title: "p1"
422 ---
423
424 ## Simple
425
426 §§§a"><script>A</script>
427 Some code.
428 §§§
429
430 §§§a"><script>B</script> {hl_inline=true}
431 Some code.
432 §§§
433
434 `
435
436 b := hugolib.Test(t, files)
437
438 b.AssertFileContent("public/p1/index.html", "! <script>")
439 }