1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
|
// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hugolib
import (
"fmt"
"path/filepath"
"github.com/gohugoio/hugo/resources/page"
"reflect"
"strings"
"testing"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/deps"
)
func TestTaxonomiesCountOrder(t *testing.T) {
t.Parallel()
taxonomies := make(map[string]string)
taxonomies["tag"] = "tags"
taxonomies["category"] = "categories"
cfg, fs := newTestCfg()
cfg.Set("taxonomies", taxonomies)
const pageContent = `---
tags: ['a', 'B', 'c']
categories: 'd'
---
YAML frontmatter with tags and categories taxonomy.`
writeSource(t, fs, filepath.Join("content", "page.md"), pageContent)
s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
st := make([]string, 0)
for _, t := range s.Taxonomies()["tags"].ByCount() {
st = append(st, t.Page().Title()+":"+t.Name)
}
expect := []string{"a:a", "B:b", "c:c"}
if !reflect.DeepEqual(st, expect) {
t.Fatalf("ordered taxonomies mismatch, expected\n%v\ngot\n%q", expect, st)
}
}
//
func TestTaxonomiesWithAndWithoutContentFile(t *testing.T) {
for _, uglyURLs := range []bool{false, true} {
uglyURLs := uglyURLs
t.Run(fmt.Sprintf("uglyURLs=%t", uglyURLs), func(t *testing.T) {
t.Parallel()
doTestTaxonomiesWithAndWithoutContentFile(t, uglyURLs)
})
}
}
func doTestTaxonomiesWithAndWithoutContentFile(t *testing.T, uglyURLs bool) {
siteConfig := `
baseURL = "http://example.com/blog"
uglyURLs = %t
paginate = 1
defaultContentLanguage = "en"
[Taxonomies]
tag = "tags"
category = "categories"
other = "others"
empty = "empties"
permalinked = "permalinkeds"
[permalinks]
permalinkeds = "/perma/:slug/"
`
pageTemplate := `---
title: "%s"
tags:
%s
categories:
%s
others:
%s
permalinkeds:
%s
---
# Doc
`
siteConfig = fmt.Sprintf(siteConfig, uglyURLs)
b := newTestSitesBuilder(t).WithConfigFile("toml", siteConfig)
b.WithContent(
"p1.md", fmt.Sprintf(pageTemplate, "t1/c1", "- Tag1", "- cAt1", "- o1", "- Pl1"),
"p2.md", fmt.Sprintf(pageTemplate, "t2/c1", "- tag2", "- cAt1", "- o1", "- Pl1"),
"p3.md", fmt.Sprintf(pageTemplate, "t2/c12", "- tag2", "- cat2", "- o1", "- Pl1"),
"p4.md", fmt.Sprintf(pageTemplate, "Hello World", "", "", "- \"Hello Hugo world\"", "- Pl1"),
"categories/_index.md", newTestPage("Category Terms", "2017-01-01", 10),
"tags/Tag1/_index.md", newTestPage("Tag1 List", "2017-01-01", 10),
// https://github.com/gohugoio/hugo/issues/5847
"/tags/not-used/_index.md", newTestPage("Unused Tag List", "2018-01-01", 10),
)
b.Build(BuildCfg{})
// So what we have now is:
// 1. categories with terms content page, but no content page for the only c1 category
// 2. tags with no terms content page, but content page for one of 2 tags (tag1)
// 3. the "others" taxonomy with no content pages.
// 4. the "permalinkeds" taxonomy with permalinks configuration.
pathFunc := func(s string) string {
if uglyURLs {
return strings.Replace(s, "/index.html", ".html", 1)
}
return s
}
// 1.
b.AssertFileContent(pathFunc("public/categories/cat1/index.html"), "List", "cAt1")
b.AssertFileContent(pathFunc("public/categories/index.html"), "Taxonomy Term Page", "Category Terms")
// 2.
b.AssertFileContent(pathFunc("public/tags/tag2/index.html"), "List", "tag2")
b.AssertFileContent(pathFunc("public/tags/tag1/index.html"), "List", "Tag1")
b.AssertFileContent(pathFunc("public/tags/index.html"), "Taxonomy Term Page", "Tags")
// 3.
b.AssertFileContent(pathFunc("public/others/o1/index.html"), "List", "o1")
b.AssertFileContent(pathFunc("public/others/index.html"), "Taxonomy Term Page", "Others")
// 4.
b.AssertFileContent(pathFunc("public/perma/pl1/index.html"), "List", "Pl1")
// This looks kind of funky, but the taxonomy terms do not have a permalinks definition,
// for good reasons.
b.AssertFileContent(pathFunc("public/permalinkeds/index.html"), "Taxonomy Term Page", "Permalinkeds")
s := b.H.Sites[0]
// Make sure that each page.KindTaxonomyTerm page has an appropriate number
// of page.KindTaxonomy pages in its Pages slice.
taxonomyTermPageCounts := map[string]int{
"tags": 3,
"categories": 2,
"others": 2,
"empties": 0,
"permalinkeds": 1,
}
for taxonomy, count := range taxonomyTermPageCounts {
msg := qt.Commentf(taxonomy)
term := s.getPage(page.KindTaxonomyTerm, taxonomy)
b.Assert(term, qt.Not(qt.IsNil), msg)
b.Assert(len(term.Pages()), qt.Equals, count, msg)
for _, p := range term.Pages() {
b.Assert(p.Kind(), qt.Equals, page.KindTaxonomy)
}
}
cat1 := s.getPage(page.KindTaxonomy, "categories", "cat1")
b.Assert(cat1, qt.Not(qt.IsNil))
if uglyURLs {
b.Assert(cat1.RelPermalink(), qt.Equals, "/blog/categories/cat1.html")
} else {
b.Assert(cat1.RelPermalink(), qt.Equals, "/blog/categories/cat1/")
}
pl1 := s.getPage(page.KindTaxonomy, "permalinkeds", "pl1")
permalinkeds := s.getPage(page.KindTaxonomyTerm, "permalinkeds")
b.Assert(pl1, qt.Not(qt.IsNil))
b.Assert(permalinkeds, qt.Not(qt.IsNil))
if uglyURLs {
b.Assert(pl1.RelPermalink(), qt.Equals, "/blog/perma/pl1.html")
b.Assert(permalinkeds.RelPermalink(), qt.Equals, "/blog/permalinkeds.html")
} else {
b.Assert(pl1.RelPermalink(), qt.Equals, "/blog/perma/pl1/")
b.Assert(permalinkeds.RelPermalink(), qt.Equals, "/blog/permalinkeds/")
}
helloWorld := s.getPage(page.KindTaxonomy, "others", "hello-hugo-world")
b.Assert(helloWorld, qt.Not(qt.IsNil))
b.Assert(helloWorld.Title(), qt.Equals, "Hello Hugo world")
// Issue #2977
b.AssertFileContent(pathFunc("public/empties/index.html"), "Taxonomy Term Page", "Empties")
}
// https://github.com/gohugoio/hugo/issues/5513
// https://github.com/gohugoio/hugo/issues/5571
func TestTaxonomiesPathSeparation(t *testing.T) {
t.Parallel()
config := `
baseURL = "https://example.com"
[taxonomies]
"news/tag" = "news/tags"
"news/category" = "news/categories"
"t1/t2/t3" = "t1/t2/t3s"
"s1/s2/s3" = "s1/s2/s3s"
`
pageContent := `
+++
title = "foo"
"news/categories" = ["a", "b", "c", "d/e", "f/g/h"]
"t1/t2/t3s" = ["t4/t5", "t4/t5/t6"]
+++
Content.
`
b := newTestSitesBuilder(t)
b.WithConfigFile("toml", config)
b.WithContent("page.md", pageContent)
b.WithContent("news/categories/b/_index.md", `
---
title: "This is B"
---
`)
b.WithContent("news/categories/f/g/h/_index.md", `
---
title: "This is H"
---
`)
b.WithContent("t1/t2/t3s/t4/t5/_index.md", `
---
title: "This is T5"
---
`)
b.WithContent("s1/s2/s3s/_index.md", `
---
title: "This is S3s"
---
`)
b.CreateSites().Build(BuildCfg{})
s := b.H.Sites[0]
filterbyKind := func(kind string) page.Pages {
var pages page.Pages
for _, p := range s.Pages() {
if p.Kind() == kind {
pages = append(pages, p)
}
}
return pages
}
ta := filterbyKind(page.KindTaxonomy)
te := filterbyKind(page.KindTaxonomyTerm)
b.Assert(len(te), qt.Equals, 4)
b.Assert(len(ta), qt.Equals, 7)
b.AssertFileContent("public/news/categories/a/index.html", "Taxonomy List Page 1|a|Hello|https://example.com/news/categories/a/|")
b.AssertFileContent("public/news/categories/b/index.html", "Taxonomy List Page 1|This is B|Hello|https://example.com/news/categories/b/|")
b.AssertFileContent("public/news/categories/d/e/index.html", "Taxonomy List Page 1|d/e|Hello|https://example.com/news/categories/d/e/|")
b.AssertFileContent("public/news/categories/f/g/h/index.html", "Taxonomy List Page 1|This is H|Hello|https://example.com/news/categories/f/g/h/|")
b.AssertFileContent("public/t1/t2/t3s/t4/t5/index.html", "Taxonomy List Page 1|This is T5|Hello|https://example.com/t1/t2/t3s/t4/t5/|")
b.AssertFileContent("public/t1/t2/t3s/t4/t5/t6/index.html", "Taxonomy List Page 1|t4/t5/t6|Hello|https://example.com/t1/t2/t3s/t4/t5/t6/|")
b.AssertFileContent("public/news/categories/index.html", "Taxonomy Term Page 1|News/Categories|Hello|https://example.com/news/categories/|")
b.AssertFileContent("public/t1/t2/t3s/index.html", "Taxonomy Term Page 1|T1/T2/T3s|Hello|https://example.com/t1/t2/t3s/|")
b.AssertFileContent("public/s1/s2/s3s/index.html", "Taxonomy Term Page 1|This is S3s|Hello|https://example.com/s1/s2/s3s/|")
}
// https://github.com/gohugoio/hugo/issues/5719
func TestTaxonomiesNextGenLoops(t *testing.T) {
b := newTestSitesBuilder(t).WithSimpleConfigFile()
b.WithTemplatesAdded("index.html", `
<h1>Tags</h1>
<ul>
{{ range .Site.Taxonomies.tags }}
<li><a href="{{ .Page.Permalink }}">{{ .Page.Title }}</a> {{ .Count }}</li>
{{ end }}
</ul>
`)
b.WithTemplatesAdded("_default/terms.html", `
<h1>Terms</h1>
<ul>
{{ range .Data.Terms.Alphabetical }}
<li><a href="{{ .Page.Permalink }}">{{ .Page.Title }}</a> {{ .Count }}</li>
{{ end }}
</ul>
`)
for i := 0; i < 10; i++ {
b.WithContent(fmt.Sprintf("page%d.md", i+1), `
---
Title: "Taxonomy!"
tags: ["Hugo Rocks!", "Rocks I say!" ]
categories: ["This is Cool", "And new" ]
---
Content.
`)
}
b.CreateSites().Build(BuildCfg{})
b.AssertFileContent("public/index.html", `<li><a href="http://example.com/tags/hugo-rocks/">Hugo Rocks!</a> 10</li>`)
b.AssertFileContent("public/categories/index.html", `<li><a href="http://example.com/categories/this-is-cool/">This is Cool</a> 10</li>`)
b.AssertFileContent("public/tags/index.html", `<li><a href="http://example.com/tags/rocks-i-say/">Rocks I say!</a> 10</li>`)
}
// Issue 6213
func TestTaxonomiesNotForDrafts(t *testing.T) {
t.Parallel()
b := newTestSitesBuilder(t)
b.WithContent("draft.md", `---
title: "Draft"
draft: true
categories: ["drafts"]
---
`,
"regular.md", `---
title: "Not Draft"
categories: ["regular"]
---
`)
b.Build(BuildCfg{})
s := b.H.Sites[0]
b.Assert(b.CheckExists("public/categories/regular/index.html"), qt.Equals, true)
b.Assert(b.CheckExists("public/categories/drafts/index.html"), qt.Equals, false)
reg, _ := s.getPageNew(nil, "categories/regular")
dra, _ := s.getPageNew(nil, "categories/draft")
b.Assert(reg, qt.Not(qt.IsNil))
b.Assert(dra, qt.IsNil)
}
func TestTaxonomiesIndexDraft(t *testing.T) {
t.Parallel()
b := newTestSitesBuilder(t)
b.WithContent(
"categories/_index.md", `---
title: "The Categories"
draft: true
---
Content.
`,
"page.md", `---
title: "The Page"
categories: ["cool"]
---
Content.
`,
)
b.WithTemplates("index.html", `
{{ range .Site.Pages }}
{{ .RelPermalink }}|{{ .Title }}|{{ .WordCount }}|{{ .Content }}|
{{ end }}
`)
b.Build(BuildCfg{})
b.AssertFileContentFn("public/index.html", func(s string) bool {
return !strings.Contains(s, "categories")
})
}
// https://github.com/gohugoio/hugo/issues/6173
func TestTaxonomiesWithBundledResources(t *testing.T) {
b := newTestSitesBuilder(t)
b.WithTemplates("_default/list.html", `
List {{ .Title }}:
{{ range .Resources }}
Resource: {{ .RelPermalink }}|{{ .MediaType }}
{{ end }}
`)
b.WithContent("p1.md", `---
title: Page
categories: ["funny"]
---
`,
"categories/_index.md", "---\ntitle: Categories Page\n---",
"categories/data.json", "Category data",
"categories/funny/_index.md", "---\ntitle: Funnny Category\n---",
"categories/funny/funnydata.json", "Category funny data",
)
b.Build(BuildCfg{})
b.AssertFileContent("public/categories/index.html", `Resource: /categories/data.json|application/json`)
b.AssertFileContent("public/categories/funny/index.html", `Resource: /categories/funny/funnydata.json|application/json`)
}
func TestTaxonomiesRemoveOne(t *testing.T) {
b := newTestSitesBuilder(t).Running()
b.WithTemplates("index.html", `
{{ $cats := .Site.Taxonomies.categories.cats }}
{{ if $cats }}
Len cats: {{ len $cats }}
{{ range $cats }}
Cats:|{{ .Page.RelPermalink }}|
{{ end }}
{{ end }}
{{ $funny := .Site.Taxonomies.categories.funny }}
{{ if $funny }}
Len funny: {{ len $funny }}
{{ range $funny }}
Funny:|{{ .Page.RelPermalink }}|
{{ end }}
{{ end }}
`)
b.WithContent("p1.md", `---
title: Page
categories: ["funny", "cats"]
---
`, "p2.md", `---
title: Page2
categories: ["funny", "cats"]
---
`,
)
b.Build(BuildCfg{})
b.AssertFileContent("public/index.html", `
Len cats: 2
Len funny: 2
Cats:|/p1/|
Cats:|/p2/|
Funny:|/p1/|
Funny:|/p2/|`)
// Remove one category from one of the pages.
b.EditFiles("content/p1.md", `---
title: Page
categories: ["funny"]
---
`)
b.Build(BuildCfg{})
b.AssertFileContent("public/index.html", `
Len cats: 1
Len funny: 2
Cats:|/p2/|
Funny:|/p1/|
Funny:|/p2/|`)
}
//https://github.com/gohugoio/hugo/issues/6590
func TestTaxonomiesListPages(t *testing.T) {
b := newTestSitesBuilder(t)
b.WithTemplates("_default/list.html", `
{{ template "print-taxo" "categories.cats" }}
{{ template "print-taxo" "categories.funny" }}
{{ define "print-taxo" }}
{{ $node := index site.Taxonomies (split $ ".") }}
{{ if $node }}
Len {{ $ }}: {{ len $node }}
{{ range $node }}
{{ $ }}:|{{ .Page.RelPermalink }}|
{{ end }}
{{ else }}
{{ $ }} not found.
{{ end }}
{{ end }}
`)
b.WithContent("_index.md", `---
title: Home
categories: ["funny", "cats"]
---
`, "blog/p1.md", `---
title: Page1
categories: ["funny"]
---
`, "blog/_index.md", `---
title: Blog Section
categories: ["cats"]
---
`,
)
b.Build(BuildCfg{})
b.AssertFileContent("public/index.html", `
Len categories.cats: 2
categories.cats:|/blog/|
categories.cats:|/|
Len categories.funny: 2
categories.funny:|/|
categories.funny:|/blog/p1/|
`)
}
|