diff options
author | Joe Mooring <[email protected]> | 2023-10-08 16:59:36 -0700 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2023-10-09 18:42:03 +0200 |
commit | 1b5f78b6b7335b02b6207a637498c4c8817999d1 (patch) | |
tree | b124d86ff9eef7356594eba3d8b5d14008277e87 | |
parent | d5d0f420d8e900126d1a733ca7a9d751288b8b26 (diff) | |
download | hugo-1b5f78b6b7335b02b6207a637498c4c8817999d1.tar.gz hugo-1b5f78b6b7335b02b6207a637498c4c8817999d1.zip |
markup/tableofcontents: Return template.HTML from .Fragments.ToHTML
Closes #11545
-rw-r--r-- | markup/goldmark/convert_test.go | 8 | ||||
-rw-r--r-- | markup/goldmark/toc_test.go | 9 | ||||
-rw-r--r-- | markup/tableofcontents/tableofcontents.go | 5 | ||||
-rw-r--r-- | markup/tableofcontents/tableofcontents_test.go | 16 |
4 files changed, 21 insertions, 17 deletions
diff --git a/markup/goldmark/convert_test.go b/markup/goldmark/convert_test.go index ed791448b..6d73b301f 100644 --- a/markup/goldmark/convert_test.go +++ b/markup/goldmark/convert_test.go @@ -109,7 +109,7 @@ LINE1 * Autolink: https://gohugo.io/ * Strikethrough:~~Hi~~ Hello, world! - + ## Table | foo | bar | @@ -137,7 +137,7 @@ That's some text with a footnote.[^1] ## Definition Lists date -: the datetime assigned to this page. +: the datetime assigned to this page. description : the description for the content. @@ -204,8 +204,8 @@ unsafe = true toc, ok := b.(converter.TableOfContentsProvider) c.Assert(ok, qt.Equals, true) - tocHTML := toc.TableOfContents().ToHTML(1, 2, false) - c.Assert(tocHTML, qt.Contains, "TableOfContents") + tocString := string(toc.TableOfContents().ToHTML(1, 2, false)) + c.Assert(tocString, qt.Contains, "TableOfContents") } func TestConvertAutoIDAsciiOnly(t *testing.T) { diff --git a/markup/goldmark/toc_test.go b/markup/goldmark/toc_test.go index f7f7bb7a0..1b846877b 100644 --- a/markup/goldmark/toc_test.go +++ b/markup/goldmark/toc_test.go @@ -62,7 +62,8 @@ And then some. c.Assert(err, qt.IsNil) b, err := conv.Convert(converter.RenderContext{Src: []byte(content), RenderTOC: true, GetRenderer: nopGetRenderer}) 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"> <ul> <li><a href="#first-h2---now-with-typography">First h2—now with typography!</a> @@ -104,7 +105,8 @@ func TestEscapeToc(t *testing.T) { // content := "" b, err := safeConv.Convert(converter.RenderContext{Src: []byte(content), RenderTOC: true, GetRenderer: nopGetRenderer}) 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"> <ul> <li><a href="#a--b--c--d">A < B & C > 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}) 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"> <ul> <li><a href="#a--b--c--d">A < B & C > D</a></li> diff --git a/markup/tableofcontents/tableofcontents.go b/markup/tableofcontents/tableofcontents.go index 5afb36067..9109ba87c 100644 --- a/markup/tableofcontents/tableofcontents.go +++ b/markup/tableofcontents/tableofcontents.go @@ -14,6 +14,7 @@ package tableofcontents import ( + "html/template" "sort" "strings" @@ -131,7 +132,7 @@ func (toc *Fragments) addAt(h *Heading, row, level int) { } // 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 { return "" } @@ -143,7 +144,7 @@ func (toc *Fragments) ToHTML(startLevel, stopLevel int, ordered bool) string { ordered: ordered, } b.Build() - return b.s.String() + return template.HTML(b.s.String()) } func (toc Fragments) walk(fn func(*Heading)) { diff --git a/markup/tableofcontents/tableofcontents_test.go b/markup/tableofcontents/tableofcontents_test.go index adbda4b00..ac9ad0eed 100644 --- a/markup/tableofcontents/tableofcontents_test.go +++ b/markup/tableofcontents/tableofcontents_test.go @@ -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: "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"> <ul> <li><a href="#h1-1">Heading 1</a> @@ -62,7 +62,7 @@ func TestToc(t *testing.T) { </ul> </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"> <ul> <li><a href="#h1-1">Heading 1</a></li> @@ -70,7 +70,7 @@ func TestToc(t *testing.T) { </ul> </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"> <ul> <li><a href="#h1-1">Heading 1</a> @@ -83,7 +83,7 @@ func TestToc(t *testing.T) { </ul> </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"> <ul> <li><a href="#1-h2-1">1-H2-1</a></li> @@ -91,7 +91,7 @@ func TestToc(t *testing.T) { </ul> </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"> <ol> <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) - got := toc.ToHTML(1, -1, false) + got := string(toc.ToHTML(1, -1, false)) c.Assert(got, qt.Equals, `<nav id="TableOfContents"> <ul> <li> @@ -139,7 +139,7 @@ func TestTocMissingParent(t *testing.T) { </ul> </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"> <ul> <li><a href="#h3">H3</a></li> @@ -147,7 +147,7 @@ func TestTocMissingParent(t *testing.T) { </ul> </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"> <ol> <li> |