Debian Codegraph @

markup/goldmark/toc_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 goldmark_test
15
16 import (
17 "strings"
18 "testing"
19
20 "github.com/gohugoio/hugo/hugolib"
21 )
22
23 func TestTableOfContents(t *testing.T) {
24 t.Parallel()
25
26 files := `
27 -- hugo.toml --
28 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
29 enableEmoji = false
30
31 [markup.tableOfContents]
32 startLevel = 2
33 endLevel = 4
34 ordered = false
35
36 [markup.goldmark.extensions]
37 strikethrough = false
38
39 [markup.goldmark.extensions.typographer]
40 disable = true
41
42 [markup.goldmark.parser]
43 autoHeadingID = false
44 autoHeadingIDType = 'github'
45
46 [markup.goldmark.renderer]
47 unsafe = false
48 xhtml = false
49 -- layouts/single.html --
50 {{ .TableOfContents }}
51 -- content/p1.md --
52 ---
53 title: p1 (basic)
54 ---
55 # Title
56 ## Section 1
57 ### Section 1.1
58 ### Section 1.2
59 #### Section 1.2.1
60 ##### Section 1.2.1.1
61 -- content/p2.md --
62 ---
63 title: p2 (markdown)
64 ---
65 ## Some *emphasized* text
66 ## Some ` + "`" + `inline` + "`" + ` code
67 ## Something to escape A < B && C > B
68 ---
69 -- content/p3.md --
70 ---
71 title: p3 (image)
72 ---
73 ## An image ![kitten](a.jpg)
74 -- content/p4.md --
75 ---
76 title: p4 (raw html)
77 ---
78 ## Some <span>raw</span> HTML
79 -- content/p5.md --
80 ---
81 title: p5 (typographer)
82 ---
83 ## Some "typographer" markup
84 -- content/p6.md --
85 ---
86 title: p6 (strikethrough)
87 ---
88 ## Some ~~deleted~~ text
89 -- content/p7.md --
90 ---
91 title: p7 (emoji)
92 ---
93 ## A :snake: emoji
94 -- content/p8.md --
95 ---
96 title: p8 (link)
97 ---
98 ## A [link](https://example.org)
99 `
100
101 b := hugolib.Test(t, files)
102
103 // basic
104 b.AssertFileContentExact("public/p1/index.html", `<nav id="TableOfContents">
105 <ul>
106 <li><a href="#">Section 1</a>
107 <ul>
108 <li><a href="#">Section 1.1</a></li>
109 <li><a href="#">Section 1.2</a>
110 <ul>
111 <li><a href="#">Section 1.2.1</a></li>
112 </ul>
113 </li>
114 </ul>
115 </li>
116 </ul>
117 </nav>`)
118
119 // markdown
120 b.AssertFileContent("public/p2/index.html",
121 `<li><a href="#">Some <em>emphasized</em> text</a></li>`,
122 `<li><a href="#">Some <code>inline</code> code</a></li>`,
123 `<li><a href="#">Something to escape A &lt; B &amp;&amp; C &gt; B</a></li>`,
124 )
125
126 // image
127 b.AssertFileContent("public/p3/index.html",
128 `<li><a href="#">An image <img src="a.jpg" alt="kitten"></a></li>`,
129 )
130
131 // raw html
132 b.AssertFileContent("public/p4/index.html",
133 `<li><a href="#">Some raw HTML</a></li>`,
134 )
135
136 // typographer
137 b.AssertFileContent("public/p5/index.html",
138 `<li><a href="#">Some &quot;typographer&quot; markup</a></li>`,
139 )
140
141 // strikethrough
142 b.AssertFileContent("public/p6/index.html",
143 `<li><a href="#">Some ~~deleted~~ text</a></li>`,
144 )
145
146 // emoji
147 b.AssertFileContent("public/p7/index.html",
148 `<li><a href="#">A :snake: emoji</a></li>`,
149 )
150
151 // link
152 b.AssertFileContent("public/p8/index.html",
153 `<li><a href="#">A link</a></li>`,
154 )
155 }
156
157 func TestTableOfContentsAdvanced(t *testing.T) {
158 t.Parallel()
159
160 files := `
161 -- hugo.toml --
162 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
163 enableEmoji = true
164
165 [markup.tableOfContents]
166 startLevel = 2
167 endLevel = 3
168 ordered = true
169
170 [markup.goldmark.extensions]
171 strikethrough = true
172
173 [markup.goldmark.extensions.typographer]
174 disable = false
175
176 [markup.goldmark.parser]
177 autoHeadingID = true
178 autoHeadingIDType = 'github'
179
180 [markup.goldmark.renderer]
181 unsafe = true
182 xhtml = true
183 -- layouts/single.html --
184 {{ .TableOfContents }}
185 -- content/p1.md --
186 ---
187 title: p1 (basic)
188 ---
189 # Title
190 ## Section 1
191 ### Section 1.1
192 ### Section 1.2
193 #### Section 1.2.1
194 ##### Section 1.2.1.1
195 -- content/p2.md --
196 ---
197 title: p2 (markdown)
198 ---
199 ## Some *emphasized* text
200 ## Some ` + "`" + `inline` + "`" + ` code
201 ## Something to escape A < B && C > B
202 ---
203 -- content/p3.md --
204 ---
205 title: p3 (image)
206 ---
207 ## An image ![kitten](a.jpg)
208 -- content/p4.md --
209 ---
210 title: p4 (raw html)
211 ---
212 ## Some <span>raw</span> HTML
213 -- content/p5.md --
214 ---
215 title: p5 (typographer)
216 ---
217 ## Some "typographer" markup
218 -- content/p6.md --
219 ---
220 title: p6 (strikethrough)
221 ---
222 ## Some ~~deleted~~ text
223 -- content/p7.md --
224 ---
225 title: p7 (emoji)
226 ---
227 ## A :snake: emoji
228 -- content/p8.md --
229 ---
230 title: p8 (link)
231 ---
232 ## A [link](https://example.org)
233 `
234
235 b := hugolib.Test(t, files)
236
237 // basic
238 b.AssertFileContentExact("public/p1/index.html", `<nav id="TableOfContents">
239 <ol>
240 <li><a href="#section-1">Section 1</a>
241 <ol>
242 <li><a href="#section-11">Section 1.1</a></li>
243 <li><a href="#section-12">Section 1.2</a></li>
244 </ol>
245 </li>
246 </ol>
247 </nav>`)
248
249 // markdown
250 b.AssertFileContent("public/p2/index.html",
251 `<li><a href="#some-emphasized-text">Some <em>emphasized</em> text</a></li>`,
252 `<li><a href="#some-inline-code">Some <code>inline</code> code</a></li>`,
253 `<li><a href="#something-to-escape-a--b--c--b">Something to escape A &lt; B &amp;&amp; C &gt; B</a></li>`,
254 )
255
256 // image
257 b.AssertFileContent("public/p3/index.html",
258 `<li><a href="#an-image-kitten">An image <img src="a.jpg" alt="kitten"/></a></li>`,
259 )
260
261 // raw html
262 b.AssertFileContent("public/p4/index.html",
263 `<li><a href="#some-raw-html">Some <span>raw</span> HTML</a></li>`,
264 )
265
266 // typographer
267 b.AssertFileContent("public/p5/index.html",
268 `<li><a href="#some-typographer-markup">Some &ldquo;typographer&rdquo; markup</a></li>`,
269 )
270
271 // strikethrough
272 b.AssertFileContent("public/p6/index.html",
273 `<li><a href="#some-deleted-text">Some <del>deleted</del> text</a></li>`,
274 )
275
276 // emoji
277 b.AssertFileContent("public/p7/index.html",
278 `<li><a href="#a-snake-emoji">A &#x1f40d; emoji</a></li>`,
279 )
280
281 // link
282 b.AssertFileContent("public/p8/index.html",
283 `<li><a href="#a-link">A link</a></li>`,
284 )
285 }
286
287 func TestIssue13416(t *testing.T) {
288 t.Parallel()
289
290 files := `
291 -- hugo.toml --
292 disableKinds = ['page','rss','section','sitemap','taxonomy','term']
293 -- layouts/home.html --
294 Content:{{ .Content }}|
295 -- layouts/_markup/render-heading.html --
296 -- content/_index.md --
297 ---
298 title: home
299 ---
300 #
301 `
302
303 b := hugolib.Test(t, files)
304
305 b.AssertFileExists("public/index.html", true)
306 }
307
308 // Issue 12605
309 func TestTableOfContentsWithGoldmarkExtras(t *testing.T) {
310 t.Parallel()
311
312 files := `
313 -- hugo.toml --
314 disableKinds = ['page','rss','section','sitemap','taxonomy','term']
315 [markup.goldmark.extensions]
316 strikethrough = false
317 [markup.goldmark.extensions.extras.delete]
318 enable = true
319 [markup.goldmark.extensions.extras.insert]
320 enable = true
321 [markup.goldmark.extensions.extras.mark]
322 enable = true
323 [markup.goldmark.extensions.extras.subscript]
324 enable = true
325 [markup.goldmark.extensions.extras.superscript]
326 enable = true
327
328 -- content/_index.md --
329 ---
330 title: home
331 ---
332 ## ~~deleted~~
333
334 ## ++inserted++
335
336 ## ==marked==
337
338 ## H~2~O
339
340 ## 1^st^
341 -- layouts/home.html --
342 {{ .TableOfContents }}
343 `
344
345 b := hugolib.Test(t, files)
346
347 b.AssertFileContent("public/index.html",
348 `<li><a href="#deleted"><del>deleted</del></a></li>`,
349 `<li><a href="#inserted"><ins>inserted</ins></a></li>`,
350 `<li><a href="#marked"><mark>marked</mark></a></li>`,
351 `<li><a href="#h2o">H<sub>2</sub>O</a></li>`,
352 `<li><a href="#1st">1<sup>st</sup></a></li>`,
353 )
354
355 files = strings.ReplaceAll(files, "enable = true", "enable = false")
356
357 b = hugolib.Test(t, files)
358
359 b.AssertFileContent("public/index.html",
360 `<li><a href="#deleted">~~deleted~~</a></li>`,
361 `<li><a href="#inserted">++inserted++</a></li>`,
362 `<li><a href="#marked">==marked==</a></li>`,
363 `<li><a href="#h2o">H~2~O</a></li>`,
364 `<li><a href="#1st">1^st^</a></li>`,
365 )
366 }
367
368 // Issue 14677
369 func TestTableOfContentsWithPassthrough(t *testing.T) {
370 t.Parallel()
371
372 files := `
373 -- hugo.toml --
374 disableKinds = ['page','rss','section','sitemap','taxonomy','term']
375 [markup.goldmark.extensions.passthrough]
376 enable = true
377 [markup.goldmark.extensions.passthrough.delimiters]
378 inline = [['$', '$']]
379 -- content/_index.md --
380 ---
381 title: home
382 ---
383 ## Heading with **$a$**
384
385 ## Heading with $b$
386
387 ## Heading with *$x + y$*
388
389 -- layouts/home.html --
390 {{ .TableOfContents }}
391 `
392
393 b := hugolib.Test(t, files)
394
395 b.AssertFileContent("public/index.html",
396 `<li><a href="#heading-with">Heading with <strong>$a$</strong></a></li>`,
397 `<li><a href="#heading-with-1">Heading with $b$</a></li>`,
398 `<li><a href="#heading-with-2">Heading with <em>$x + y$</em></a></li>`,
399 )
400 }