mirror of
https://github.com/gohugoio/hugo.git
synced 2025-03-22 14:44:23 +00:00
Add new TableOfContents Page variable (Markdown only)
Added TableOfContents field to hugolib.Page struct. New function getTableOfContents is used in convertMarkdown to set the TableOfContents field. Added new test file hugolib/page_toc_test.go with a simple test of the new functionality. Conflicts: hugolib/page.go
This commit is contained in:
parent
4f1807c7a7
commit
f62e3e9940
2 changed files with 79 additions and 16 deletions
|
@ -34,22 +34,23 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Page struct {
|
type Page struct {
|
||||||
Status string
|
Status string
|
||||||
Images []string
|
Images []string
|
||||||
RawContent []byte
|
RawContent []byte
|
||||||
Content template.HTML
|
Content template.HTML
|
||||||
Summary template.HTML
|
Summary template.HTML
|
||||||
Truncated bool
|
TableOfContents template.HTML
|
||||||
plain string // TODO should be []byte
|
Truncated bool
|
||||||
Params map[string]interface{}
|
plain string // TODO should be []byte
|
||||||
contentType string
|
Params map[string]interface{}
|
||||||
Draft bool
|
contentType string
|
||||||
Aliases []string
|
Draft bool
|
||||||
Tmpl bundle.Template
|
Aliases []string
|
||||||
Markup string
|
Tmpl bundle.Template
|
||||||
renderable bool
|
Markup string
|
||||||
layout string
|
renderable bool
|
||||||
linkTitle string
|
layout string
|
||||||
|
linkTitle string
|
||||||
PageMeta
|
PageMeta
|
||||||
File
|
File
|
||||||
Position
|
Position
|
||||||
|
@ -624,6 +625,16 @@ func (page *Page) Convert() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getTableOfContents(content []byte) template.HTML {
|
||||||
|
htmlFlags := 0
|
||||||
|
htmlFlags |= blackfriday.HTML_SKIP_SCRIPT
|
||||||
|
htmlFlags |= blackfriday.HTML_TOC
|
||||||
|
htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
|
||||||
|
renderer := blackfriday.HtmlRenderer(htmlFlags, "", "")
|
||||||
|
|
||||||
|
return template.HTML(string(blackfriday.Markdown(content, renderer, 0)))
|
||||||
|
}
|
||||||
|
|
||||||
func (page *Page) convertMarkdown(lines io.Reader) {
|
func (page *Page) convertMarkdown(lines io.Reader) {
|
||||||
b := new(bytes.Buffer)
|
b := new(bytes.Buffer)
|
||||||
b.ReadFrom(lines)
|
b.ReadFrom(lines)
|
||||||
|
@ -631,6 +642,7 @@ func (page *Page) convertMarkdown(lines io.Reader) {
|
||||||
page.Content = template.HTML(string(blackfriday.MarkdownCommon(RemoveSummaryDivider(content))))
|
page.Content = template.HTML(string(blackfriday.MarkdownCommon(RemoveSummaryDivider(content))))
|
||||||
summary, truncated := getSummaryString(content, "markdown")
|
summary, truncated := getSummaryString(content, "markdown")
|
||||||
page.Summary = template.HTML(string(summary))
|
page.Summary = template.HTML(string(summary))
|
||||||
|
page.TableOfContents = getTableOfContents(RemoveSummaryDivider(content))
|
||||||
page.Truncated = truncated
|
page.Truncated = truncated
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
51
hugolib/page_toc_test.go
Normal file
51
hugolib/page_toc_test.go
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
package hugolib
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTableOfContents(t *testing.T) {
|
||||||
|
text := `
|
||||||
|
Blah blah blah blah blah.
|
||||||
|
|
||||||
|
## AA
|
||||||
|
|
||||||
|
Blah blah blah blah blah.
|
||||||
|
|
||||||
|
### AAA
|
||||||
|
|
||||||
|
Blah blah blah blah blah.
|
||||||
|
|
||||||
|
## BB
|
||||||
|
|
||||||
|
Blah blah blah blah blah.
|
||||||
|
|
||||||
|
### BBB
|
||||||
|
|
||||||
|
Blah blah blah blah blah.
|
||||||
|
`
|
||||||
|
|
||||||
|
markdown := RemoveSummaryDivider([]byte(text))
|
||||||
|
toc := string(getTableOfContents(markdown))
|
||||||
|
|
||||||
|
expected := `<nav>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
<ul>
|
||||||
|
<li><a href="#toc_0">AA</a>
|
||||||
|
<ul>
|
||||||
|
<li><a href="#toc_1">AAA</a></li>
|
||||||
|
</ul></li>
|
||||||
|
<li><a href="#toc_2">BB</a>
|
||||||
|
<ul>
|
||||||
|
<li><a href="#toc_3">BBB</a></li>
|
||||||
|
</ul></li>
|
||||||
|
</ul></li>
|
||||||
|
</ul>
|
||||||
|
</nav>
|
||||||
|
`
|
||||||
|
|
||||||
|
if toc != expected {
|
||||||
|
t.Errorf("Expected table of contents: %s, got: %s", expected, toc)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue