page: Add Prev and Next fields

This commit is contained in:
adnano 2021-05-12 15:10:40 -04:00
parent f68f86594b
commit 5fc28c9b78

12
dir.go
View file

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