Debian Codegraph @

hugolib/page_permalink_test.go

raw
1 // Copyright 2025 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 hugolib
15
16 import (
17 "fmt"
18 "html/template"
19 "testing"
20
21 qt "github.com/frankban/quicktest"
22 )
23
24 func TestPermalink(t *testing.T) {
25 t.Parallel()
26
27 tests := []struct {
28 file string
29 base template.URL
30 slug string
31 url string
32 uglyURLs bool
33 canonifyURLs bool
34 expectedAbs string
35 expectedRel string
36 }{
37 {"x/y/z/boofar.md", "/", "", "", false, false, "/x/y/z/boofar/", "/x/y/z/boofar/"},
38 {"x/y/z/boofar.md", "/", "", "", false, false, "/x/y/z/boofar/", "/x/y/z/boofar/"},
39 // Issue #1174
40 {"x/y/z/boofar.md", "http://gopher.com/", "", "", false, true, "http://gopher.com/x/y/z/boofar/", "/x/y/z/boofar/"},
41 {"x/y/z/boofar.md", "http://gopher.com/", "", "", true, true, "http://gopher.com/x/y/z/boofar.html", "/x/y/z/boofar.html"},
42 {"x/y/z/boofar.md", "/", "boofar", "", false, false, "/x/y/z/boofar/", "/x/y/z/boofar/"},
43 {"x/y/z/boofar.md", "http://barnew/", "", "", false, false, "http://barnew/x/y/z/boofar/", "/x/y/z/boofar/"},
44 {"x/y/z/boofar.md", "http://barnew/", "boofar", "", false, false, "http://barnew/x/y/z/boofar/", "/x/y/z/boofar/"},
45 {"x/y/z/boofar.md", "/", "", "", true, false, "/x/y/z/boofar.html", "/x/y/z/boofar.html"},
46 {"x/y/z/boofar.md", "/", "", "", true, false, "/x/y/z/boofar.html", "/x/y/z/boofar.html"},
47 {"x/y/z/boofar.md", "/", "boofar", "", true, false, "/x/y/z/boofar.html", "/x/y/z/boofar.html"},
48 {"x/y/z/boofar.md", "http://barnew/", "", "", true, false, "http://barnew/x/y/z/boofar.html", "/x/y/z/boofar.html"},
49 {"x/y/z/boofar.md", "http://barnew/", "boofar", "", true, false, "http://barnew/x/y/z/boofar.html", "/x/y/z/boofar.html"},
50 {"x/y/z/boofar.md", "http://barnew/boo/", "booslug", "", true, false, "http://barnew/boo/x/y/z/booslug.html", "/boo/x/y/z/booslug.html"},
51 {"x/y/z/boofar.md", "http://barnew/boo/", "booslug", "", false, true, "http://barnew/boo/x/y/z/booslug/", "/x/y/z/booslug/"},
52 {"x/y/z/boofar.md", "http://barnew/boo/", "booslug", "", false, false, "http://barnew/boo/x/y/z/booslug/", "/boo/x/y/z/booslug/"},
53 {"x/y/z/boofar.md", "http://barnew/boo/", "booslug", "", true, true, "http://barnew/boo/x/y/z/booslug.html", "/x/y/z/booslug.html"},
54 {"x/y/z/boofar.md", "http://barnew/boo", "booslug", "", true, true, "http://barnew/boo/x/y/z/booslug.html", "/x/y/z/booslug.html"},
55 // Issue #4666
56 {"x/y/z/boo-makeindex.md", "http://barnew/boo", "", "", true, true, "http://barnew/boo/x/y/z/boo-makeindex.html", "/x/y/z/boo-makeindex.html"},
57
58 // test URL overrides
59 {"x/y/z/boofar.md", "/", "", "/z/y/q/", false, false, "/z/y/q/", "/z/y/q/"},
60 // test URL override with expands
61 {"x/y/z/boofar.md", "/", "test", "/z/:slug/", false, false, "/z/test/", "/z/test/"},
62 }
63
64 for i, test := range tests {
65 t.Run(fmt.Sprintf("%s-%d", test.file, i), func(t *testing.T) {
66 t.Parallel()
67 c := qt.New(t)
68
69 files := fmt.Sprintf(`
70 -- hugo.toml --
71 baseURL = %q
72 uglyURLs = %t
73 canonifyURLs = %t
74 -- content/%s --
75 ---
76 title: Page
77 slug: %q
78 url: %q
79 output: ["HTML"]
80 ---
81 `, test.base, test.uglyURLs, test.canonifyURLs, test.file, test.slug, test.url)
82
83 b := Test(t, files)
84 s := b.H.Sites[0]
85 c.Assert(len(s.RegularPages()), qt.Equals, 1)
86 p := s.RegularPages()[0]
87 u := p.Permalink()
88
89 expected := test.expectedAbs
90 if u != expected {
91 t.Fatalf("[%d] Expected abs url: %s, got: %s", i, expected, u)
92 }
93
94 u = p.RelPermalink()
95
96 expected = test.expectedRel
97 if u != expected {
98 t.Errorf("[%d] Expected rel url: %s, got: %s", i, expected, u)
99 }
100 })
101 }
102 }
103
104 func TestRelativeURLInFrontMatter(t *testing.T) {
105 t.Parallel()
106
107 files := `
108 -- hugo.toml --
109 baseURL = "https://example.com"
110 defaultContentLanguage = "en"
111 defaultContentLanguageInSubdir = false
112
113 [Languages]
114 [Languages.en]
115 weight = 10
116 contentDir = "content/en"
117 [Languages.nn]
118 weight = 20
119 contentDir = "content/nn"
120 -- layouts/single.html --
121 Single: {{ .Title }}|Hello|{{ .Lang }}|RelPermalink: {{ .RelPermalink }}|Permalink: {{ .Permalink }}|
122 -- layouts/list.html --
123 List Page 1|{{ .Title }}|Hello|{{ .Permalink }}|
124 -- content/en/blog/page1.md --
125 ---
126 title: "A page"
127 url: "myblog/p1/"
128 ---
129
130 Some content.
131 -- content/en/blog/page2.md --
132 ---
133 title: "A page"
134 url: "../../../../../myblog/p2/"
135 ---
136
137 Some content.
138 -- content/en/blog/page3.md --
139 ---
140 title: "A page"
141 url: "../myblog/../myblog/p3/"
142 ---
143
144 Some content.
145 -- content/en/blog/_index.md --
146 ---
147 title: "A page"
148 url: "this-is-my-english-blog"
149 ---
150
151 Some content.
152 -- content/nn/blog/page1.md --
153 ---
154 title: "A page"
155 url: "myblog/p1/"
156 ---
157
158 Some content.
159 -- content/nn/blog/_index.md --
160 ---
161 title: "A page"
162 url: "this-is-my-blog"
163 ---
164
165 Some content.
166 `
167 b := Test(t, files)
168
169 b.AssertFileContent("public/nn/myblog/p1/index.html", "Single: A page|Hello|nn|RelPermalink: /nn/myblog/p1/|")
170 b.AssertFileContent("public/nn/this-is-my-blog/index.html", "List Page 1|A page|Hello|https://example.com/nn/this-is-my-blog/|")
171 b.AssertFileContent("public/this-is-my-english-blog/index.html", "List Page 1|A page|Hello|https://example.com/this-is-my-english-blog/|")
172 b.AssertFileContent("public/myblog/p1/index.html", "Single: A page|Hello|en|RelPermalink: /myblog/p1/|Permalink: https://example.com/myblog/p1/|")
173 b.AssertFileContent("public/myblog/p2/index.html", "Single: A page|Hello|en|RelPermalink: /myblog/p2/|Permalink: https://example.com/myblog/p2/|")
174 b.AssertFileContent("public/myblog/p3/index.html", "Single: A page|Hello|en|RelPermalink: /myblog/p3/|Permalink: https://example.com/myblog/p3/|")
175 }
176
177 // Issue 13869
178 func TestPermalinksSliceFromModule13869(t *testing.T) {
179 t.Parallel()
180
181 files := `
182 -- hugo.toml --
183 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
184 theme = 'foo'
185 [permalinks]
186 _merge = 'deep'
187 -- content/books/my-book.md --
188 ---
189 title: My Book
190 ---
191 -- layouts/page.html --
192 RelPermalink: {{ .RelPermalink }}
193 -- themes/foo/hugo.toml --
194 [[permalinks]]
195 pattern = '/shelf/:slug/'
196 [permalinks.target]
197 path = '/books/**'
198 `
199
200 b := Test(t, files)
201
202 b.AssertFileContent("public/shelf/my-book/index.html", "RelPermalink: /shelf/my-book/")
203 }
204
205 // See issue 14352.
206 func TestSlugInBranchPagesCascadesToDescendants(t *testing.T) {
207 t.Parallel()
208
209 files := `
210 -- hugo.toml --
211 defaultContentLanguage = "en"
212 [languages.en]
213 weight = 1
214 [languages.es]
215 weight = 2
216 [taxonomies]
217 tag = "tags"
218 -- content/help/_index.en.md --
219 ---
220 title: Help
221 ---
222 -- content/help/_index.es.md --
223 ---
224 title: Ayuda
225 slug: ayuda
226 ---
227 -- content/help/how-to-frob/index.en.md --
228 ---
229 title: How to frob
230 tags: [red]
231 ---
232 -- content/help/how-to-frob/index.es.md --
233 ---
234 title: Cómo frobear
235 slug: como-frobear
236 tags: [red]
237 ---
238 -- content/help/advanced/_index.en.md --
239 ---
240 title: Advanced
241 ---
242 -- content/help/advanced/_index.es.md --
243 ---
244 title: Avanzado
245 slug: avanzado
246 ---
247 -- content/help/advanced/p1.en.md --
248 ---
249 title: P1
250 ---
251 -- content/help/advanced/p1.es.md --
252 ---
253 title: P1
254 ---
255 -- content/help/deep/dir/p2.es.md --
256 ---
257 title: P2
258 ---
259 -- content/tags/_index.es.md --
260 ---
261 title: Etiquetas
262 slug: etiquetas
263 ---
264 -- content/tags/red/_index.es.md --
265 ---
266 title: Rojo
267 slug: rojo
268 ---
269 -- layouts/all.html --
270 {{ .Kind }}|{{ .Path }}|{{ .RelPermalink }}|{{ range .Pages }}{{ .RelPermalink }}|{{ end }}
271 `
272
273 b := Test(t, files)
274
275 b.AssertFileContent("public/help/index.html", "section|/help|/help/|/help/advanced/|/help/how-to-frob/|")
276 b.AssertFileContent("public/help/advanced/p1/index.html", "page|/help/advanced/p1|/help/advanced/p1/|")
277 b.AssertFileContent("public/tags/red/index.html", "term|/tags/red|/tags/red/|/help/how-to-frob/|")
278
279 b.AssertFileContent("public/es/ayuda/index.html", "section|/help|/es/ayuda/|/es/ayuda/avanzado/|/es/ayuda/como-frobear/|")
280 b.AssertFileContent("public/es/ayuda/index.xml", "<link>https://example.org/es/ayuda/</link>")
281 b.AssertFileContent("public/es/ayuda/como-frobear/index.html", "page|/help/how-to-frob|/es/ayuda/como-frobear/|")
282 b.AssertFileContent("public/es/ayuda/avanzado/index.html", "section|/help/advanced|/es/ayuda/avanzado/|/es/ayuda/avanzado/p1/|")
283 b.AssertFileContent("public/es/ayuda/avanzado/p1/index.html", "page|/help/advanced/p1|/es/ayuda/avanzado/p1/|")
284 b.AssertFileContent("public/es/ayuda/deep/dir/p2/index.html", "page|/help/deep/dir/p2|/es/ayuda/deep/dir/p2/|")
285 b.AssertFileContent("public/es/etiquetas/index.html", "taxonomy|/tags|/es/etiquetas/|/es/etiquetas/rojo/|")
286 b.AssertFileContent("public/es/etiquetas/rojo/index.html", "term|/tags/red|/es/etiquetas/rojo/|/es/ayuda/como-frobear/|")
287 }
288
289 func TestSlugInBranchPagesDisablePathToLowerUglyURLs(t *testing.T) {
290 t.Parallel()
291
292 files := `
293 -- hugo.toml --
294 disablePathToLower = true
295 uglyURLs = true
296 -- content/help/_index.md --
297 ---
298 title: Help
299 slug: Ayuda
300 ---
301 -- content/help/p1.md --
302 ---
303 title: P1
304 ---
305 -- layouts/all.html --
306 {{ .Kind }}|{{ .RelPermalink }}|{{ range .Pages }}{{ .RelPermalink }}|{{ end }}
307 `
308
309 b := Test(t, files)
310
311 b.AssertFileContent("public/Ayuda/index.html", "section|/Ayuda/index.html|/Ayuda/p1.html|")
312 b.AssertFileContent("public/Ayuda/p1.html", "page|/Ayuda/p1.html|")
313 }
314
315 func BenchmarkTargetPathDir(b *testing.B) {
316 files := `
317 -- content/help/_index.md --
318 ---
319 title: Help
320 slug: Ayuda
321 ---
322 -- content/help/advanced/_index.md --
323 ---
324 title: Advanced
325 slug: Avanzado
326 ---
327 -- content/help/advanced/p1.md --
328 ---
329 title: P1
330 ---
331 `
332
333 bb := Test(b, files)
334 p, err := bb.H.Sites[0].GetPage("help/advanced/p1")
335 bb.Assert(err, qt.IsNil)
336 bb.Assert(p, qt.IsNotNil)
337 b.ResetTimer()
338 for b.Loop() {
339 _ = targetPathDir(p.(*pageState))
340 }
341 }