markup/tableofcontents: Return template.HTML from .Fragments.ToHTML

Closes #11545
This commit is contained in:
Joe Mooring 2023-10-08 16:59:36 -07:00 committed by Bjørn Erik Pedersen
parent d5d0f420d8
commit 1b5f78b6b7
4 changed files with 21 additions and 17 deletions

View file

@ -109,7 +109,7 @@ LINE1
* Autolink: https://gohugo.io/ * Autolink: https://gohugo.io/
* Strikethrough:~~Hi~~ Hello, world! * Strikethrough:~~Hi~~ Hello, world!
## Table ## Table
| foo | bar | | foo | bar |
@ -137,7 +137,7 @@ That's some text with a footnote.[^1]
## Definition Lists ## Definition Lists
date date
: the datetime assigned to this page. : the datetime assigned to this page.
description description
: the description for the content. : the description for the content.
@ -204,8 +204,8 @@ unsafe = true
toc, ok := b.(converter.TableOfContentsProvider) toc, ok := b.(converter.TableOfContentsProvider)
c.Assert(ok, qt.Equals, true) c.Assert(ok, qt.Equals, true)
tocHTML := toc.TableOfContents().ToHTML(1, 2, false) tocString := string(toc.TableOfContents().ToHTML(1, 2, false))
c.Assert(tocHTML, qt.Contains, "TableOfContents") c.Assert(tocString, qt.Contains, "TableOfContents")
} }
func TestConvertAutoIDAsciiOnly(t *testing.T) { func TestConvertAutoIDAsciiOnly(t *testing.T) {

View file

@ -62,7 +62,8 @@ And then some.
c.Assert(err, qt.IsNil) c.Assert(err, qt.IsNil)
b, err := conv.Convert(converter.RenderContext{Src: []byte(content), RenderTOC: true, GetRenderer: nopGetRenderer}) b, err := conv.Convert(converter.RenderContext{Src: []byte(content), RenderTOC: true, GetRenderer: nopGetRenderer})
c.Assert(err, qt.IsNil) c.Assert(err, qt.IsNil)
got := b.(converter.TableOfContentsProvider).TableOfContents().ToHTML(2, 3, false) tocHTML := b.(converter.TableOfContentsProvider).TableOfContents().ToHTML(2, 3, false)
got := string(tocHTML)
c.Assert(got, qt.Equals, `<nav id="TableOfContents"> c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ul> <ul>
<li><a href="#first-h2---now-with-typography">First h2&mdash;now with typography!</a> <li><a href="#first-h2---now-with-typography">First h2&mdash;now with typography!</a>
@ -104,7 +105,8 @@ func TestEscapeToc(t *testing.T) {
// content := "" // content := ""
b, err := safeConv.Convert(converter.RenderContext{Src: []byte(content), RenderTOC: true, GetRenderer: nopGetRenderer}) b, err := safeConv.Convert(converter.RenderContext{Src: []byte(content), RenderTOC: true, GetRenderer: nopGetRenderer})
c.Assert(err, qt.IsNil) c.Assert(err, qt.IsNil)
got := b.(converter.TableOfContentsProvider).TableOfContents().ToHTML(1, 2, false) tocHTML := b.(converter.TableOfContentsProvider).TableOfContents().ToHTML(1, 2, false)
got := string(tocHTML)
c.Assert(got, qt.Equals, `<nav id="TableOfContents"> c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ul> <ul>
<li><a href="#a--b--c--d">A &lt; B &amp; C &gt; D</a></li> <li><a href="#a--b--c--d">A &lt; B &amp; C &gt; D</a></li>
@ -116,7 +118,8 @@ func TestEscapeToc(t *testing.T) {
b, err = unsafeConv.Convert(converter.RenderContext{Src: []byte(content), RenderTOC: true, GetRenderer: nopGetRenderer}) b, err = unsafeConv.Convert(converter.RenderContext{Src: []byte(content), RenderTOC: true, GetRenderer: nopGetRenderer})
c.Assert(err, qt.IsNil) c.Assert(err, qt.IsNil)
got = b.(converter.TableOfContentsProvider).TableOfContents().ToHTML(1, 2, false) tocHTML = b.(converter.TableOfContentsProvider).TableOfContents().ToHTML(1, 2, false)
got = string(tocHTML)
c.Assert(got, qt.Equals, `<nav id="TableOfContents"> c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ul> <ul>
<li><a href="#a--b--c--d">A &lt; B &amp; C &gt; D</a></li> <li><a href="#a--b--c--d">A &lt; B &amp; C &gt; D</a></li>

View file

@ -14,6 +14,7 @@
package tableofcontents package tableofcontents
import ( import (
"html/template"
"sort" "sort"
"strings" "strings"
@ -131,7 +132,7 @@ func (toc *Fragments) addAt(h *Heading, row, level int) {
} }
// ToHTML renders the ToC as HTML. // ToHTML renders the ToC as HTML.
func (toc *Fragments) ToHTML(startLevel, stopLevel int, ordered bool) string { func (toc *Fragments) ToHTML(startLevel, stopLevel int, ordered bool) template.HTML {
if toc == nil { if toc == nil {
return "" return ""
} }
@ -143,7 +144,7 @@ func (toc *Fragments) ToHTML(startLevel, stopLevel int, ordered bool) string {
ordered: ordered, ordered: ordered,
} }
b.Build() b.Build()
return b.s.String() return template.HTML(b.s.String())
} }
func (toc Fragments) walk(fn func(*Heading)) { func (toc Fragments) walk(fn func(*Heading)) {

View file

@ -45,7 +45,7 @@ func TestToc(t *testing.T) {
toc.addAt(&Heading{Title: "1-H3-1", ID: "1-h2-2"}, 0, 2) toc.addAt(&Heading{Title: "1-H3-1", ID: "1-h2-2"}, 0, 2)
toc.addAt(&Heading{Title: "Heading 2", ID: "h1-2"}, 1, 0) toc.addAt(&Heading{Title: "Heading 2", ID: "h1-2"}, 1, 0)
got := toc.ToHTML(1, -1, false) got := string(toc.ToHTML(1, -1, false))
c.Assert(got, qt.Equals, `<nav id="TableOfContents"> c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ul> <ul>
<li><a href="#h1-1">Heading 1</a> <li><a href="#h1-1">Heading 1</a>
@ -62,7 +62,7 @@ func TestToc(t *testing.T) {
</ul> </ul>
</nav>`, qt.Commentf(got)) </nav>`, qt.Commentf(got))
got = toc.ToHTML(1, 1, false) got = string(toc.ToHTML(1, 1, false))
c.Assert(got, qt.Equals, `<nav id="TableOfContents"> c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ul> <ul>
<li><a href="#h1-1">Heading 1</a></li> <li><a href="#h1-1">Heading 1</a></li>
@ -70,7 +70,7 @@ func TestToc(t *testing.T) {
</ul> </ul>
</nav>`, qt.Commentf(got)) </nav>`, qt.Commentf(got))
got = toc.ToHTML(1, 2, false) got = string(toc.ToHTML(1, 2, false))
c.Assert(got, qt.Equals, `<nav id="TableOfContents"> c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ul> <ul>
<li><a href="#h1-1">Heading 1</a> <li><a href="#h1-1">Heading 1</a>
@ -83,7 +83,7 @@ func TestToc(t *testing.T) {
</ul> </ul>
</nav>`, qt.Commentf(got)) </nav>`, qt.Commentf(got))
got = toc.ToHTML(2, 2, false) got = string(toc.ToHTML(2, 2, false))
c.Assert(got, qt.Equals, `<nav id="TableOfContents"> c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ul> <ul>
<li><a href="#1-h2-1">1-H2-1</a></li> <li><a href="#1-h2-1">1-H2-1</a></li>
@ -91,7 +91,7 @@ func TestToc(t *testing.T) {
</ul> </ul>
</nav>`, qt.Commentf(got)) </nav>`, qt.Commentf(got))
got = toc.ToHTML(1, -1, true) got = string(toc.ToHTML(1, -1, true))
c.Assert(got, qt.Equals, `<nav id="TableOfContents"> c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ol> <ol>
<li><a href="#h1-1">Heading 1</a> <li><a href="#h1-1">Heading 1</a>
@ -118,7 +118,7 @@ func TestTocMissingParent(t *testing.T) {
toc.addAt(&Heading{Title: "H3", ID: "h3"}, 1, 2) toc.addAt(&Heading{Title: "H3", ID: "h3"}, 1, 2)
toc.addAt(&Heading{Title: "H3", ID: "h3"}, 1, 2) toc.addAt(&Heading{Title: "H3", ID: "h3"}, 1, 2)
got := toc.ToHTML(1, -1, false) got := string(toc.ToHTML(1, -1, false))
c.Assert(got, qt.Equals, `<nav id="TableOfContents"> c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ul> <ul>
<li> <li>
@ -139,7 +139,7 @@ func TestTocMissingParent(t *testing.T) {
</ul> </ul>
</nav>`, qt.Commentf(got)) </nav>`, qt.Commentf(got))
got = toc.ToHTML(3, 3, false) got = string(toc.ToHTML(3, 3, false))
c.Assert(got, qt.Equals, `<nav id="TableOfContents"> c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ul> <ul>
<li><a href="#h3">H3</a></li> <li><a href="#h3">H3</a></li>
@ -147,7 +147,7 @@ func TestTocMissingParent(t *testing.T) {
</ul> </ul>
</nav>`, qt.Commentf(got)) </nav>`, qt.Commentf(got))
got = toc.ToHTML(1, -1, true) got = string(toc.ToHTML(1, -1, true))
c.Assert(got, qt.Equals, `<nav id="TableOfContents"> c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ol> <ol>
<li> <li>