Debian Codegraph @

markup/tableofcontents/tableofcontents_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 tableofcontents_test
15
16 import (
17 "strings"
18 "testing"
19
20 qt "github.com/frankban/quicktest"
21 "github.com/gohugoio/hugo/hugolib"
22 )
23
24 // Issue #10776
25 func TestHeadingsLevel(t *testing.T) {
26 t.Parallel()
27
28 files := `
29 -- hugo.toml --
30 disableKinds = ['page','rss','section','sitemap','taxonomy','term']
31 -- layouts/home.html --
32 {{ range .Fragments.HeadingsMap }}
33 {{ printf "%s|%d|%s" .ID .Level .Title }}
34 {{ end }}
35 -- content/_index.md --
36 ## Heading L2
37 ### Heading L3
38 ##### Heading L5
39 `
40
41 b := hugolib.Test(t, files)
42 b.AssertFileContent("public/index.html",
43 "heading-l2|2|Heading L2",
44 "heading-l3|3|Heading L3",
45 "heading-l5|5|Heading L5",
46 )
47 }
48
49 // Issue #13107
50 func TestToHTMLArgTypes(t *testing.T) {
51 t.Parallel()
52
53 files := `
54 -- hugo.toml --
55 disableKinds = ['home','section','rss','sitemap','taxonomy','term']
56 -- layouts/single.html --
57 {{ .Fragments.ToHTML .Params.toc.startLevel .Params.toc.endLevel false }}
58 -- content/json.md --
59 {
60 "title": "json",
61 "params": {
62 "toc": {
63 "startLevel": 2,
64 "endLevel": 4
65 }
66 }
67 }
68 CONTENT
69 -- content/toml.md --
70 +++
71 title = 'toml'
72 [params.toc]
73 startLevel = 2
74 endLevel = 4
75 +++
76 CONTENT
77 -- content/yaml.md --
78 ---
79 title: yaml
80 params:
81 toc:
82 startLevel: 2
83 endLevel: 4
84 ---
85 CONTENT
86 `
87
88 content := `
89 # Level One
90 ## Level Two
91 ### Level Three
92 #### Level Four
93 ##### Level Five
94 ###### Level Six
95 `
96
97 want := `
98 <nav id="TableOfContents">
99 <ul>
100 <li><a href="#level-two">Level Two</a>
101 <ul>
102 <li><a href="#level-three">Level Three</a>
103 <ul>
104 <li><a href="#level-four">Level Four</a></li>
105 </ul>
106 </li>
107 </ul>
108 </li>
109 </ul>
110 </nav>
111 `
112
113 files = strings.ReplaceAll(files, "CONTENT", content)
114
115 b := hugolib.Test(t, files)
116 b.AssertFileContentEquals("public/json/index.html", strings.TrimSpace(want))
117 b.AssertFileContentEquals("public/toml/index.html", strings.TrimSpace(want))
118 b.AssertFileContentEquals("public/yaml/index.html", strings.TrimSpace(want))
119
120 files = strings.ReplaceAll(files, `2`, `"x"`)
121
122 b, err := hugolib.TestE(t, files)
123 b.Assert(err, qt.Not(qt.IsNil))
124 b.Assert(err.Error(), qt.Contains, `error calling ToHTML: startLevel: unable to cast "x" of type string`)
125 }
126
127 func TestHeadingsNilpointerIssue11843(t *testing.T) {
128 t.Parallel()
129 files := `
130 -- hugo.toml --
131 -- layouts/home.html --
132 {{ $h := index .Fragments.HeadingsMap "bad_id" }}
133 {{ if not $h }}OK{{ end }}
134 `
135 b := hugolib.Test(t, files)
136
137 b.AssertFileContent("public/index.html", "OK")
138 }
139
140 func TestHeadingIDEscaped(t *testing.T) {
141 t.Parallel()
142
143 files := `
144 -- hugo.toml --
145 disableKinds = ['home','section','rss','sitemap','taxonomy','term']
146 -- layouts/page.html --
147 TOC: {{ .TableOfContents }}|
148 Fragments: {{ .Fragments.ToHTML 2 3 false }}|
149 Content: {{ .Content }}|
150 -- content/p1.md --
151 ---
152 title: p1
153 ---
154 ## Intro {id="x\"><img src=x onerror=alert(1)><a class=\""}
155 ## Amp {id="a&b<c>"}
156 `
157
158 b := hugolib.Test(t, files)
159 b.AssertFileContent("public/p1/index.html",
160 `<li><a href="#x&#34;&gt;&lt;img src=x onerror=alert(1)&gt;&lt;a class=&#34;">Intro</a></li>`,
161 `<li><a href="#a&amp;b&lt;c&gt;">Amp</a></li>`,
162 `<h2 id="x&quot;&gt;&lt;img src=x onerror=alert(1)&gt;&lt;a class=&quot;">Intro</h2>`,
163 )
164 b.AssertFileContent("public/p1/index.html", "! <img src=x")
165 }