Skip to content

Commit

Permalink
add support for default expanded tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Herkarl committed Jan 25, 2024
1 parent 9eb9bd4 commit f3446b2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
26 changes: 17 additions & 9 deletions pages/pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Resp struct {
Body string `json:"body"` // Main content of the page.
Sidebar string `json:"sidebar"` // The sidebar of the page.
Sort *int `json:"sort"` // The order that the tab should appear in on the page
Expanded bool `json:"expanded"` // Should the Nav-tree rooted in this node always be expanded one step when loaded?
Anchors []anchor.Anchor `json:"anchors"` // The list of anchors to headers in the body.
Nav []*Node `json:"nav,omitempty"`
}
Expand All @@ -41,10 +42,11 @@ type Node struct {

// Meta defines the attributes to be loaded from the meta.toml file
type Meta struct {
Image string
Title string
Message string
Sort *int
Image string
Title string
Message string
Sort *int
Expanded bool
}

// NewNode creates a new node with it's path, slug and page title.
Expand Down Expand Up @@ -82,18 +84,22 @@ func (n *Node) AddNode(root []string, p string, title string, paths []string, ac
n.Sort = sort
return
}

// Parent folder.
parent := paths[0]

// Have we already created the parent?
if n.hasNode(parent) {
if len(root) == 0 {
if n.getNode(parent).Expanded {
n.getNode(parent).AddNode(root, p, title, paths[1:], false, expanded, sort)
}
return
}
if root[0] == parent {
n.getNode(parent).AddNode(root[1:], p, title, paths[1:], false, false, sort)
if root[0] == parent || n.getNode(parent).Expanded {
n.getNode(parent).AddNode(root[1:], p, title, paths[1:], false, expanded, sort)
} else if len(paths) == 1 {
n.getNode(parent).AddNode(root, p, title, []string{}, false, false, sort)
n.getNode(parent).AddNode(root, p, title, []string{}, false, expanded, sort)
}
return
}
Expand All @@ -105,7 +111,7 @@ func (n *Node) AddNode(root []string, p string, title string, paths []string, ac
title,
paths[1:],
len(root) == 1 && root[0] == parent,
len(root) > 1 && root[0] == parent,
expanded || (len(root) > 1 && root[0] == parent),
sort,
)
}
Expand Down Expand Up @@ -221,7 +227,8 @@ func parseDir(root, dir string) (*Resp, error) {

// Parse meta data from a toml file.
var meta = Meta{
Sort: nil, // all pages without a sort-tag should be after the pages with a sort-tag, but should keep their internal order
Sort: nil, // all pages without a sort-tag should be after the pages with a sort-tag, but should keep their internal order
Expanded: false,
}
if _, err := toml.DecodeFile(metaPath, &meta); err != nil {
return nil, err
Expand All @@ -237,6 +244,7 @@ func parseDir(root, dir string) (*Resp, error) {
Body: body,
Sidebar: sidebar,
Anchors: anchs,
Expanded: meta.Expanded,
Sort: meta.Sort,
}, nil
}
2 changes: 1 addition & 1 deletion taitan.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func handler(res http.ResponseWriter, req *http.Request) {
responses.Resps[slug].Title,
strings.FieldsFunc(slug, func(c rune) bool { return c == '/' }),
false,
false,
responses.Resps[slug].Expanded,
responses.Resps[slug].Sort,
)
// }
Expand Down

0 comments on commit f3446b2

Please sign in to comment.