github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/markup/goldmark/toc_test.go (about) 1 // Copyright 2019 The Hugo Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 // Package goldmark converts Markdown to HTML using Goldmark. 15 package goldmark 16 17 import ( 18 "strings" 19 "testing" 20 21 "github.com/gohugoio/hugo/markup/markup_config" 22 23 "github.com/gohugoio/hugo/common/loggers" 24 25 "github.com/gohugoio/hugo/markup/converter" 26 27 qt "github.com/frankban/quicktest" 28 ) 29 30 func TestToc(t *testing.T) { 31 c := qt.New(t) 32 33 content := ` 34 # Header 1 35 36 ## First h2---now with typography! 37 38 Some text. 39 40 ### H3 41 42 Some more text. 43 44 ## Second h2 45 46 And then some. 47 48 ### Second H3 49 50 #### First H4 51 52 ` 53 p, err := Provider.New( 54 converter.ProviderConfig{ 55 MarkupConfig: markup_config.Default, 56 Logger: loggers.NewErrorLogger(), 57 }) 58 c.Assert(err, qt.IsNil) 59 conv, err := p.New(converter.DocumentContext{}) 60 c.Assert(err, qt.IsNil) 61 b, err := conv.Convert(converter.RenderContext{Src: []byte(content), RenderTOC: true}) 62 c.Assert(err, qt.IsNil) 63 got := b.(converter.TableOfContentsProvider).TableOfContents().ToHTML(2, 3, false) 64 c.Assert(got, qt.Equals, `<nav id="TableOfContents"> 65 <ul> 66 <li><a href="#first-h2---now-with-typography">First h2—now with typography!</a> 67 <ul> 68 <li><a href="#h3">H3</a></li> 69 </ul> 70 </li> 71 <li><a href="#second-h2">Second h2</a> 72 <ul> 73 <li><a href="#second-h3">Second H3</a></li> 74 </ul> 75 </li> 76 </ul> 77 </nav>`, qt.Commentf(got)) 78 } 79 80 func TestEscapeToc(t *testing.T) { 81 c := qt.New(t) 82 83 defaultConfig := markup_config.Default 84 85 safeConfig := defaultConfig 86 unsafeConfig := defaultConfig 87 88 safeConfig.Goldmark.Renderer.Unsafe = false 89 unsafeConfig.Goldmark.Renderer.Unsafe = true 90 91 safeP, _ := Provider.New( 92 converter.ProviderConfig{ 93 MarkupConfig: safeConfig, 94 Logger: loggers.NewErrorLogger(), 95 }) 96 unsafeP, _ := Provider.New( 97 converter.ProviderConfig{ 98 MarkupConfig: unsafeConfig, 99 Logger: loggers.NewErrorLogger(), 100 }) 101 safeConv, _ := safeP.New(converter.DocumentContext{}) 102 unsafeConv, _ := unsafeP.New(converter.DocumentContext{}) 103 104 content := strings.Join([]string{ 105 "# A < B & C > D", 106 "# A < B & C > D <div>foo</div>", 107 "# *EMPHASIS*", 108 "# `echo codeblock`", 109 }, "\n") 110 // content := "" 111 b, err := safeConv.Convert(converter.RenderContext{Src: []byte(content), RenderTOC: true}) 112 c.Assert(err, qt.IsNil) 113 got := b.(converter.TableOfContentsProvider).TableOfContents().ToHTML(1, 2, false) 114 c.Assert(got, qt.Equals, `<nav id="TableOfContents"> 115 <ul> 116 <li><a href="#a--b--c--d">A < B & C > D</a></li> 117 <li><a href="#a--b--c--d-divfoodiv">A < B & C > D <!-- raw HTML omitted -->foo<!-- raw HTML omitted --></a></li> 118 <li><a href="#emphasis"><em>EMPHASIS</em></a></li> 119 <li><a href="#echo-codeblock"><code>echo codeblock</code></a></li> 120 </ul> 121 </nav>`, qt.Commentf(got)) 122 123 b, err = unsafeConv.Convert(converter.RenderContext{Src: []byte(content), RenderTOC: true}) 124 c.Assert(err, qt.IsNil) 125 got = b.(converter.TableOfContentsProvider).TableOfContents().ToHTML(1, 2, false) 126 c.Assert(got, qt.Equals, `<nav id="TableOfContents"> 127 <ul> 128 <li><a href="#a--b--c--d">A < B & C > D</a></li> 129 <li><a href="#a--b--c--d-divfoodiv">A < B & C > D <div>foo</div></a></li> 130 <li><a href="#emphasis"><em>EMPHASIS</em></a></li> 131 <li><a href="#echo-codeblock"><code>echo codeblock</code></a></li> 132 </ul> 133 </nav>`, qt.Commentf(got)) 134 }