page: Add Prev and Next fields

This commit is contained in:
Adnan Maolood 2021-05-12 15:10:40 -04:00
parent 74a4582a8b
commit a32b5c7493

12
dir.go
View file

@ -32,6 +32,8 @@ type Page struct {
Path string `yaml:"-"`
Content string `yaml:"-"`
Params map[string]string
Prev *Page `yaml:"-"`
Next *Page `yaml:"-"`
}
// NewDir returns a new Dir with the given path.
@ -263,6 +265,16 @@ func (d *Dir) sort() {
sort.Slice(d.Pages, func(i, j int) bool {
return d.Pages[i].Date.After(d.Pages[j].Date)
})
for i := range d.Pages {
if i-1 > 0 {
d.Pages[i].Prev = d.Pages[i-1]
}
if i+1 < len(d.Pages) {
d.Pages[i].Next = d.Pages[i+1]
}
}
// Sort subdirectories
for _, d := range d.Dirs {
d.sort()