Skip to content

Commit

Permalink
fix home page link handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ihadeed committed Nov 11, 2019
1 parent 62838ee commit a4647ff
Showing 1 changed file with 43 additions and 9 deletions.
52 changes: 43 additions & 9 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ func NewParser(config *ParserConfig) *Parser {
Logger: l,
}

l.WithFields(logrus.Fields{
"rootDir": config.RootDir,
"outDir": config.OutDir,
"baseUrl": config.BaseURL,
"repo": config.Repo,
"siteTitle": config.SiteTitle,
}).Debug("creating new parser")

return &p
}

Expand Down Expand Up @@ -188,10 +196,32 @@ type RenderContext struct {
Link string
}

func (p *Parser) handleHomePage(it *MenuItem) {
if it.Group {
for _, iit := range it.Items {
p.handleHomePage(iit)
}
return
}

if it.Link == "" || it.Link == "/" {
p.Logger.WithFields(logrus.Fields{
"title": it.Title,
}).Debug("setting page link to baseURL")
it.Link = p.Config.BaseURL
}
}

func (p *Parser) Render() error {
menuItems := p.Config.MenuItems
rndCtxs := make([]*RenderContext, 0)

if p.Config.BaseURL != "" {
for _, it := range menuItems {
p.handleHomePage(it)
}
}

for _, f := range p.Files {
if fc, err := ioutil.ReadFile(f.SourceFile); err != nil {
return err
Expand Down Expand Up @@ -229,22 +259,21 @@ func (p *Parser) Render() error {
Link: f.Path,
}

rndCtxs = append(rndCtxs, &ctx)

menuItem := MenuItem{
Name: f.Name,
Title: f.Title,
Link: f.Path,
}

if f.Path == "" || f.Path == "/" {
if p.Config.BaseURL != "" {
ctx.Link = p.Config.BaseURL
menuItem.Link = ctx.Link
}
}

rndCtxs = append(rndCtxs, &ctx)

if f.AddToMenu {
menuItem := MenuItem{
Name: f.Name,
Title: f.Title,
Link: f.Path,
}

if f.MenuGroup != "" {
for _, mit := range menuItems {
if mit.Group && mit.Name == f.MenuGroup {
Expand Down Expand Up @@ -293,6 +322,11 @@ func (p *Parser) Render() error {
ctx.MenuItems = menuItems
ctx.BaseURL = p.Config.BaseURL

p.Logger.WithFields(logrus.Fields{
"title": ctx.Title,
"link": ctx.Link,
}).Debug("rendering page")

for _, mit := range menuItems {
if mit.Group {
for _, cmit := range mit.Items {
Expand Down

0 comments on commit a4647ff

Please sign in to comment.