Skip to content

Commit

Permalink
fix(renderer): fix panic in ToC when doc has no section (#285)
Browse files Browse the repository at this point in the history
fixes #284

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon authored Feb 8, 2019
1 parent 2387309 commit f7ee178
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
27 changes: 15 additions & 12 deletions pkg/renderer/html5/table_of_contents.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,21 @@ type TableOfContentsSection struct {
Level int
Href string
Title template.HTML
Subelements *template.HTML
Subelements template.HTML
}

func renderTableOfContents(ctx *renderer.Context, m types.TableOfContentsMacro) ([]byte, error) {
result := bytes.NewBuffer(nil)
renderedSections, err := renderTableOfContentsSections(ctx, ctx.Document.Elements, 1)
if err != nil {
return nil, errors.Wrapf(err, "error while rendering table of content")
}
if renderedSections == template.HTML("") {
// nothing to render (document has no section)
return []byte{}, nil
}
result := bytes.NewBuffer(nil)
err = tableOfContentTmpl.Execute(result, TableOfContents{
Content: *renderedSections,
Content: renderedSections,
})
if err != nil {
return nil, errors.Wrapf(err, "error while rendering table of content")
Expand All @@ -62,25 +66,25 @@ func renderTableOfContents(ctx *renderer.Context, m types.TableOfContentsMacro)
return result.Bytes(), nil
}

func renderTableOfContentsSections(ctx *renderer.Context, elements []interface{}, currentLevel int) (*template.HTML, error) {
func renderTableOfContentsSections(ctx *renderer.Context, elements []interface{}, currentLevel int) (template.HTML, error) {
sections := make([]TableOfContentsSection, 0)
for _, element := range elements {
log.Debugf("traversing document element of type %T", element)
switch section := element.(type) {
case types.Section:
renderedTitle, err := renderElement(ctx, section.Title.Elements)
if err != nil {
return nil, errors.Wrapf(err, "error while rendering table of content section")
return template.HTML(""), errors.Wrapf(err, "error while rendering table of content section")
}
tocLevels, err := ctx.Document.Attributes.GetTOCLevels()
if err != nil {
return nil, errors.Wrapf(err, "error while rendering table of content section")
return template.HTML(""), errors.Wrapf(err, "error while rendering table of content section")
}
var renderedChildSections *template.HTML
var renderedChildSections template.HTML
if currentLevel < *tocLevels {
renderedChildSections, err = renderTableOfContentsSections(ctx, section.Elements, currentLevel+1)
if err != nil {
return nil, errors.Wrapf(err, "error while rendering table of content section")
return template.HTML(""), errors.Wrapf(err, "error while rendering table of content section")
}
}
var id string
Expand All @@ -97,17 +101,16 @@ func renderTableOfContentsSections(ctx *renderer.Context, elements []interface{}
}
}
if len(sections) == 0 {
return nil, nil
return template.HTML(""), nil
}
resultBuf := bytes.NewBuffer(nil)
err := tableOfContentSectionSetTmpl.Execute(resultBuf, TableOfContentsSectionGroup{
Level: sections[0].Level,
Elements: sections,
})
if err != nil {
return nil, errors.Wrap(err, "failed to render document TOC")
return template.HTML(""), errors.Wrap(err, "failed to render document TOC")
}
log.Debugf("retrieved sections for TOC: %+v", sections)
result := template.HTML(resultBuf.String())
return &result, nil
return template.HTML(resultBuf.String()), nil
}
13 changes: 13 additions & 0 deletions pkg/renderer/html5/table_of_contents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,18 @@ A preamble...
verify(GinkgoT(), expectedResult, actualContent)
})

It("document with no section", func() {
actualContent := `= sect0
:toc:
level 1 sections not exists.`

expectedResult := `<div class="paragraph">
<p>level 1 sections not exists.</p>
</div>`
verify(GinkgoT(), expectedResult, actualContent)

})
})

})

0 comments on commit f7ee178

Please sign in to comment.