Skip to content

Commit

Permalink
feature: adding back next and prev
Browse files Browse the repository at this point in the history
  • Loading branch information
paganotoni committed May 25, 2024
1 parent eef9c3a commit cd8a548
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 27 deletions.
3 changes: 3 additions & 0 deletions internal/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ type document struct {
// content
markdown []byte
section *section

prev *document
next *document
}

func (doc document) String() string {
Expand Down
55 changes: 34 additions & 21 deletions internal/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,28 @@ func Generate(srcFolder, destination string, s *site) error {
return err
}

type docData struct {
Config config.Site
Site *site

Title string
SectionName string
Description string
Keywords string
Link string

Markdown []byte
Style template.CSS
JS template.JS

NextLink string
NextTitle string
PrevLink string
PrevTitle string
}

for _, doc := range v.Documents {
bb := bytes.NewBuffer([]byte{})
err = pageTmpl.Execute(bb, struct {
Config config.Site
Site *site

Title string
SectionName string
Description string
Keywords string
Link string

Markdown []byte
Style template.CSS
JS template.JS

NextLink string
NextTitle string
PrevLink string
PrevTitle string
}{
d := docData{
Config: conf,
Site: s,

Expand All @@ -89,8 +90,20 @@ func Generate(srcFolder, destination string, s *site) error {

Style: style,
JS: docoJS,
})
}

if doc.next != nil {
d.NextLink = doc.next.Link()
d.NextTitle = doc.next.Title
}

if doc.prev != nil {
d.PrevLink = doc.prev.Link()
d.PrevTitle = doc.prev.Title
}

bb := bytes.NewBuffer([]byte{})
err = pageTmpl.Execute(bb, d)
if err != nil {
return err
}
Expand Down
36 changes: 30 additions & 6 deletions internal/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ func NewSite(folder string) (*site, error) {
sort.Sort(site.Sections[i].Documents)
}

// Adding next and previous to documents
docs := site.Documents()
for i := range docs {
if i != 0 {
docs[i].prev = docs[i-1]
}

if i < len(docs)-1 {
docs[i].next = docs[i+1]
}
}

return site, err
}

Expand All @@ -91,6 +103,16 @@ type site struct {
Sections sections
}

func (s *site) Documents() (docs []*document) {
for j := range s.Sections {
for i := range s.Sections[j].Documents {
docs = append(docs, &s.Sections[j].Documents[i])
}
}

return docs
}

func (s *site) String() string {
pp := "Site: \n"
for _, sec := range s.Sections {
Expand Down Expand Up @@ -125,13 +147,15 @@ func (s *site) Add(path string, doc document) error {
}

for i, v := range s.Sections {
if v.Name == secName {
doc.section = &v
v.Documents = append(v.Documents, doc)
s.Sections[i] = v

return nil
if v.Name != secName {
continue
}

doc.section = &v
v.Documents = append(v.Documents, doc)
s.Sections[i] = v

return nil
}

sec := section{
Expand Down

0 comments on commit cd8a548

Please sign in to comment.