hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

toc_test.go (4127B)

    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/converter/hooks"
   22 	"github.com/gohugoio/hugo/markup/markup_config"
   23 
   24 	"github.com/gohugoio/hugo/common/loggers"
   25 
   26 	"github.com/gohugoio/hugo/markup/converter"
   27 
   28 	qt "github.com/frankban/quicktest"
   29 )
   30 
   31 var nopGetRenderer = func(t hooks.RendererType, id any) any { return nil }
   32 
   33 func TestToc(t *testing.T) {
   34 	c := qt.New(t)
   35 
   36 	content := `
   37 # Header 1
   38 
   39 ## First h2---now with typography!
   40 
   41 Some text.
   42 
   43 ### H3
   44 
   45 Some more text.
   46 
   47 ## Second h2
   48 
   49 And then some.
   50 
   51 ### Second H3
   52 
   53 #### First H4
   54 
   55 `
   56 	p, err := Provider.New(
   57 		converter.ProviderConfig{
   58 			MarkupConfig: markup_config.Default,
   59 			Logger:       loggers.NewErrorLogger(),
   60 		})
   61 	c.Assert(err, qt.IsNil)
   62 	conv, err := p.New(converter.DocumentContext{})
   63 	c.Assert(err, qt.IsNil)
   64 	b, err := conv.Convert(converter.RenderContext{Src: []byte(content), RenderTOC: true, GetRenderer: nopGetRenderer})
   65 	c.Assert(err, qt.IsNil)
   66 	got := b.(converter.TableOfContentsProvider).TableOfContents().ToHTML(2, 3, false)
   67 	c.Assert(got, qt.Equals, `<nav id="TableOfContents">
   68   <ul>
   69     <li><a href="#first-h2---now-with-typography">First h2&mdash;now with typography!</a>
   70       <ul>
   71         <li><a href="#h3">H3</a></li>
   72       </ul>
   73     </li>
   74     <li><a href="#second-h2">Second h2</a>
   75       <ul>
   76         <li><a href="#second-h3">Second H3</a></li>
   77       </ul>
   78     </li>
   79   </ul>
   80 </nav>`, qt.Commentf(got))
   81 }
   82 
   83 func TestEscapeToc(t *testing.T) {
   84 	c := qt.New(t)
   85 
   86 	defaultConfig := markup_config.Default
   87 
   88 	safeConfig := defaultConfig
   89 	unsafeConfig := defaultConfig
   90 
   91 	safeConfig.Goldmark.Renderer.Unsafe = false
   92 	unsafeConfig.Goldmark.Renderer.Unsafe = true
   93 
   94 	safeP, _ := Provider.New(
   95 		converter.ProviderConfig{
   96 			MarkupConfig: safeConfig,
   97 			Logger:       loggers.NewErrorLogger(),
   98 		})
   99 	unsafeP, _ := Provider.New(
  100 		converter.ProviderConfig{
  101 			MarkupConfig: unsafeConfig,
  102 			Logger:       loggers.NewErrorLogger(),
  103 		})
  104 	safeConv, _ := safeP.New(converter.DocumentContext{})
  105 	unsafeConv, _ := unsafeP.New(converter.DocumentContext{})
  106 
  107 	content := strings.Join([]string{
  108 		"# A < B & C > D",
  109 		"# A < B & C > D <div>foo</div>",
  110 		"# *EMPHASIS*",
  111 		"# `echo codeblock`",
  112 	}, "\n")
  113 	// content := ""
  114 	b, err := safeConv.Convert(converter.RenderContext{Src: []byte(content), RenderTOC: true, GetRenderer: nopGetRenderer})
  115 	c.Assert(err, qt.IsNil)
  116 	got := b.(converter.TableOfContentsProvider).TableOfContents().ToHTML(1, 2, false)
  117 	c.Assert(got, qt.Equals, `<nav id="TableOfContents">
  118   <ul>
  119     <li><a href="#a--b--c--d">A &lt; B &amp; C &gt; D</a></li>
  120     <li><a href="#a--b--c--d-divfoodiv">A &lt; B &amp; C &gt; D <!-- raw HTML omitted -->foo<!-- raw HTML omitted --></a></li>
  121     <li><a href="#emphasis"><em>EMPHASIS</em></a></li>
  122     <li><a href="#echo-codeblock"><code>echo codeblock</code></a></li>
  123   </ul>
  124 </nav>`, qt.Commentf(got))
  125 
  126 	b, err = unsafeConv.Convert(converter.RenderContext{Src: []byte(content), RenderTOC: true, GetRenderer: nopGetRenderer})
  127 	c.Assert(err, qt.IsNil)
  128 	got = b.(converter.TableOfContentsProvider).TableOfContents().ToHTML(1, 2, false)
  129 	c.Assert(got, qt.Equals, `<nav id="TableOfContents">
  130   <ul>
  131     <li><a href="#a--b--c--d">A &lt; B &amp; C &gt; D</a></li>
  132     <li><a href="#a--b--c--d-divfoodiv">A &lt; B &amp; C &gt; D <div>foo</div></a></li>
  133     <li><a href="#emphasis"><em>EMPHASIS</em></a></li>
  134     <li><a href="#echo-codeblock"><code>echo codeblock</code></a></li>
  135   </ul>
  136 </nav>`, qt.Commentf(got))
  137 }