github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/markup/goldmark/toc_test.go (about)

     1  // Copyright 2023 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_test
    15  
    16  import (
    17  	"strings"
    18  	"testing"
    19  
    20  	"github.com/gohugoio/hugo/common/loggers"
    21  	"github.com/gohugoio/hugo/config/testconfig"
    22  	"github.com/gohugoio/hugo/markup/converter/hooks"
    23  	"github.com/gohugoio/hugo/markup/goldmark"
    24  
    25  	"github.com/gohugoio/hugo/markup/converter"
    26  
    27  	qt "github.com/frankban/quicktest"
    28  )
    29  
    30  var nopGetRenderer = func(t hooks.RendererType, id any) any { return nil }
    31  
    32  func TestToc(t *testing.T) {
    33  	c := qt.New(t)
    34  
    35  	content := `
    36  # Header 1
    37  
    38  ## First h2---now with typography!
    39  
    40  Some text.
    41  
    42  ### H3
    43  
    44  Some more text.
    45  
    46  ## Second h2
    47  
    48  And then some.
    49  
    50  ### Second H3
    51  
    52  #### First H4
    53  
    54  `
    55  	p, err := goldmark.Provider.New(
    56  		converter.ProviderConfig{
    57  			Conf:   testconfig.GetTestConfig(nil, nil),
    58  			Logger: loggers.NewDefault(),
    59  		})
    60  	c.Assert(err, qt.IsNil)
    61  	conv, err := p.New(converter.DocumentContext{})
    62  	c.Assert(err, qt.IsNil)
    63  	b, err := conv.Convert(converter.RenderContext{Src: []byte(content), RenderTOC: true, GetRenderer: nopGetRenderer})
    64  	c.Assert(err, qt.IsNil)
    65  	tocHTML := b.(converter.TableOfContentsProvider).TableOfContents().ToHTML(2, 3, false)
    66  	got := string(tocHTML)
    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  	safeP, _ := goldmark.Provider.New(
    87  		converter.ProviderConfig{
    88  			Conf:   safeConf(),
    89  			Logger: loggers.NewDefault(),
    90  		})
    91  	unsafeP, _ := goldmark.Provider.New(
    92  		converter.ProviderConfig{
    93  			Conf:   unsafeConf(),
    94  			Logger: loggers.NewDefault(),
    95  		})
    96  	safeConv, _ := safeP.New(converter.DocumentContext{})
    97  	unsafeConv, _ := unsafeP.New(converter.DocumentContext{})
    98  
    99  	content := strings.Join([]string{
   100  		"# A < B & C > D",
   101  		"# A < B & C > D <div>foo</div>",
   102  		"# *EMPHASIS*",
   103  		"# `echo codeblock`",
   104  	}, "\n")
   105  	// content := ""
   106  	b, err := safeConv.Convert(converter.RenderContext{Src: []byte(content), RenderTOC: true, GetRenderer: nopGetRenderer})
   107  	c.Assert(err, qt.IsNil)
   108  	tocHTML := b.(converter.TableOfContentsProvider).TableOfContents().ToHTML(1, 2, false)
   109  	got := string(tocHTML)
   110  	c.Assert(got, qt.Equals, `<nav id="TableOfContents">
   111    <ul>
   112      <li><a href="#a--b--c--d">A &lt; B &amp; C &gt; D</a></li>
   113      <li><a href="#a--b--c--d-divfoodiv">A &lt; B &amp; C &gt; D <!-- raw HTML omitted -->foo<!-- raw HTML omitted --></a></li>
   114      <li><a href="#emphasis"><em>EMPHASIS</em></a></li>
   115      <li><a href="#echo-codeblock"><code>echo codeblock</code></a></li>
   116    </ul>
   117  </nav>`, qt.Commentf(got))
   118  
   119  	b, err = unsafeConv.Convert(converter.RenderContext{Src: []byte(content), RenderTOC: true, GetRenderer: nopGetRenderer})
   120  	c.Assert(err, qt.IsNil)
   121  	tocHTML = b.(converter.TableOfContentsProvider).TableOfContents().ToHTML(1, 2, false)
   122  	got = string(tocHTML)
   123  	c.Assert(got, qt.Equals, `<nav id="TableOfContents">
   124    <ul>
   125      <li><a href="#a--b--c--d">A &lt; B &amp; C &gt; D</a></li>
   126      <li><a href="#a--b--c--d-divfoodiv">A &lt; B &amp; C &gt; D <div>foo</div></a></li>
   127      <li><a href="#emphasis"><em>EMPHASIS</em></a></li>
   128      <li><a href="#echo-codeblock"><code>echo codeblock</code></a></li>
   129    </ul>
   130  </nav>`, qt.Commentf(got))
   131  }