Skip to content

Commit 0a83a1c

Browse files
TIL-10 Separate tag page entries by month
Signed-off-by: Chris Cummer <[email protected]>
1 parent 292b8e1 commit 0a83a1c

File tree

1 file changed

+48
-26
lines changed

1 file changed

+48
-26
lines changed

til.go

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,11 @@ func buildContent() {
289289
buildIndexPage(pages, tagMap)
290290
}
291291

292+
// buildIndexPage creates the main index.md page that is the root of the site
292293
func buildIndexPage(pages []*Page, tagMap *TagMap) {
293294
Info(statusIdxBuild)
294295

295296
content := ""
296-
prevPage := &Page{}
297297

298298
// Write the tag list into the top of the index
299299
for _, tag := range tagMap.SortedTagNames() {
@@ -304,28 +304,12 @@ func buildIndexPage(pages []*Page, tagMap *TagMap) {
304304
)
305305
}
306306

307-
// Write the page list into the middle of the index
308-
// This is sorted by month, in reverse-chronological order
309-
for _, page := range pages {
310-
if !page.IsContentPage() {
311-
continue
312-
}
313-
314-
// This breaks the page list up by month
315-
if prevPage.CreatedMonth() != page.CreatedMonth() {
316-
content += "\n\n"
317-
}
318-
319-
content += fmt.Sprintf("* %s\n", page.Link())
320-
321-
prevPage = page
322-
}
323-
307+
// Write the page list into the middle of the page
308+
content += pagesToHTMLUnorderedList(pages)
324309
content += fmt.Sprintf("\n")
325310

326311
// Write the footer content into the bottom of the index
327312
content += fmt.Sprintf("\n")
328-
content += fmt.Sprintf("\n")
329313
content += footer()
330314

331315
// And write the file to disk
@@ -339,6 +323,8 @@ func buildIndexPage(pages []*Page, tagMap *TagMap) {
339323
if err != nil {
340324
Fail(err)
341325
}
326+
327+
ll.Print(fmt.Sprintf("%s %s\n", Blue("\t->"), filePath))
342328
}
343329

344330
// buildTagPages creates the tag pages, with links to posts tagged with those names
@@ -357,13 +343,8 @@ func buildTagPages(pages []*Page) *TagMap {
357343

358344
content := fmt.Sprintf("## %s\n\n", tagName)
359345

360-
for _, tag := range tagMap.Get(tagName) {
361-
for _, page := range tag.Pages {
362-
if page.IsContentPage() {
363-
content += fmt.Sprintf("* %s\n", page.Link())
364-
}
365-
}
366-
}
346+
// Write the page list into the middle of the page
347+
content += pagesToHTMLUnorderedList(tagMap.PagesFor(tagName))
367348

368349
// Write the footer content into the bottom of the page
369350
content += fmt.Sprintf("\n")
@@ -456,6 +437,30 @@ func loadPages() []*Page {
456437
return pages
457438
}
458439

440+
// pagesToHTMLUnorderedList creates the unordered list of page links that appear
441+
// on the index and tag pages
442+
func pagesToHTMLUnorderedList(pages []*Page) string {
443+
content := ""
444+
prevPage := &Page{}
445+
446+
for _, page := range pages {
447+
if !page.IsContentPage() {
448+
continue
449+
}
450+
451+
// This breaks the page list up by month
452+
if prevPage.CreatedMonth() != page.CreatedMonth() {
453+
content += "\n"
454+
}
455+
456+
content += fmt.Sprintf("* %s\n", page.Link())
457+
458+
prevPage = page
459+
}
460+
461+
return content
462+
}
463+
459464
// push pushes up to the remote git repo
460465
func push() {
461466
Info(statusRepoPush)
@@ -717,6 +722,23 @@ func (tm *TagMap) Len() int {
717722
return len(tm.Tags)
718723
}
719724

725+
// PagesFor returns a flattened slice of pages for a given tag name, sorted
726+
// in reverse-chronological order
727+
func (tm *TagMap) PagesFor(tagName string) []*Page {
728+
pages := []*Page{}
729+
tags := tm.Get(tagName)
730+
731+
for _, tag := range tags {
732+
pages = append(pages, tag.Pages...)
733+
734+
sort.Slice(pages, func(i, j int) bool {
735+
return pages[i].CreatedAt().After(pages[j].CreatedAt())
736+
})
737+
}
738+
739+
return pages
740+
}
741+
720742
// SortedTagNames returns the tag names in alphabetical order
721743
func (tm *TagMap) SortedTagNames() []string {
722744
tagArr := make([]string, tm.Len())

0 commit comments

Comments
 (0)