From: Niels Widger Date: Sat, 18 Jan 2014 16:42:01 +0000 (-0500) Subject: Add new TableOfContents Page variable (Markdown only) X-Git-Tag: v0.10~47 X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=f62e3e9940fafb89f88e0ee6b8987b89bbfd281e;p=brevno-suite%2Fhugo 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 --- diff --git a/hugolib/page.go b/hugolib/page.go index 950022d3..5b625bba 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -34,22 +34,23 @@ import ( ) type Page struct { - Status string - Images []string - RawContent []byte - Content template.HTML - Summary template.HTML - Truncated bool - plain string // TODO should be []byte - Params map[string]interface{} - contentType string - Draft bool - Aliases []string - Tmpl bundle.Template - Markup string - renderable bool - layout string - linkTitle string + Status string + Images []string + RawContent []byte + Content template.HTML + Summary template.HTML + TableOfContents template.HTML + Truncated bool + plain string // TODO should be []byte + Params map[string]interface{} + contentType string + Draft bool + Aliases []string + Tmpl bundle.Template + Markup string + renderable bool + layout string + linkTitle string PageMeta File Position @@ -624,6 +625,16 @@ func (page *Page) Convert() error { 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) { b := new(bytes.Buffer) b.ReadFrom(lines) @@ -631,6 +642,7 @@ func (page *Page) convertMarkdown(lines io.Reader) { page.Content = template.HTML(string(blackfriday.MarkdownCommon(RemoveSummaryDivider(content)))) summary, truncated := getSummaryString(content, "markdown") page.Summary = template.HTML(string(summary)) + page.TableOfContents = getTableOfContents(RemoveSummaryDivider(content)) page.Truncated = truncated } diff --git a/hugolib/page_toc_test.go b/hugolib/page_toc_test.go new file mode 100644 index 00000000..cdc93462 --- /dev/null +++ b/hugolib/page_toc_test.go @@ -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 := ` +` + + if toc != expected { + t.Errorf("Expected table of contents: %s, got: %s", expected, toc) + } +}