github.com/dominikszabo/hugo-ds-clean@v0.47.1/helpers/content_renderer_test.go (about)

     1  // Copyright 2015 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 helpers
    15  
    16  import (
    17  	"bytes"
    18  	"regexp"
    19  	"testing"
    20  
    21  	"github.com/spf13/viper"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  // Renders a codeblock using Blackfriday
    26  func (c ContentSpec) render(input string) string {
    27  	ctx := &RenderingContext{Cfg: c.cfg, Config: c.BlackFriday}
    28  	render := c.getHTMLRenderer(0, ctx)
    29  
    30  	buf := &bytes.Buffer{}
    31  	render.BlockCode(buf, []byte(input), "html")
    32  	return buf.String()
    33  }
    34  
    35  // Renders a codeblock using Mmark
    36  func (c ContentSpec) renderWithMmark(input string) string {
    37  	ctx := &RenderingContext{Cfg: c.cfg, Config: c.BlackFriday}
    38  	render := c.getMmarkHTMLRenderer(0, ctx)
    39  
    40  	buf := &bytes.Buffer{}
    41  	render.BlockCode(buf, []byte(input), "html", []byte(""), false, false)
    42  	return buf.String()
    43  }
    44  
    45  func TestCodeFence(t *testing.T) {
    46  	assert := require.New(t)
    47  
    48  	type test struct {
    49  		enabled         bool
    50  		input, expected string
    51  	}
    52  
    53  	// Pygments 2.0 and 2.1 have slightly different outputs so only do partial matching
    54  	data := []test{
    55  		{true, "<html></html>", `(?s)^<div class="highlight">\n?<pre.*><code class="language-html" data-lang="html">.*?</code></pre>\n?</div>\n?$`},
    56  		{false, "<html></html>", `(?s)^<pre.*><code class="language-html">.*?</code></pre>\n$`},
    57  	}
    58  
    59  	for _, useClassic := range []bool{false, true} {
    60  		for i, d := range data {
    61  			v := viper.New()
    62  			v.Set("pygmentsStyle", "monokai")
    63  			v.Set("pygmentsUseClasses", true)
    64  			v.Set("pygmentsCodeFences", d.enabled)
    65  			v.Set("pygmentsUseClassic", useClassic)
    66  
    67  			c, err := NewContentSpec(v)
    68  			assert.NoError(err)
    69  
    70  			result := c.render(d.input)
    71  
    72  			expectedRe, err := regexp.Compile(d.expected)
    73  
    74  			if err != nil {
    75  				t.Fatal("Invalid regexp", err)
    76  			}
    77  			matched := expectedRe.MatchString(result)
    78  
    79  			if !matched {
    80  				t.Errorf("Test %d failed. BlackFriday enabled:%t, Expected:\n%q got:\n%q", i, d.enabled, d.expected, result)
    81  			}
    82  
    83  			result = c.renderWithMmark(d.input)
    84  			matched = expectedRe.MatchString(result)
    85  			if !matched {
    86  				t.Errorf("Test %d failed. Mmark enabled:%t, Expected:\n%q got:\n%q", i, d.enabled, d.expected, result)
    87  			}
    88  		}
    89  	}
    90  }
    91  
    92  func TestBlackfridayTaskList(t *testing.T) {
    93  	c := newTestContentSpec()
    94  
    95  	for i, this := range []struct {
    96  		markdown        string
    97  		taskListEnabled bool
    98  		expect          string
    99  	}{
   100  		{`
   101  TODO:
   102  
   103  - [x] On1
   104  - [X] On2
   105  - [ ] Off
   106  
   107  END
   108  `, true, `<p>TODO:</p>
   109  
   110  <ul class="task-list">
   111  <li><label><input type="checkbox" checked disabled class="task-list-item"> On1</label></li>
   112  <li><label><input type="checkbox" checked disabled class="task-list-item"> On2</label></li>
   113  <li><label><input type="checkbox" disabled class="task-list-item"> Off</label></li>
   114  </ul>
   115  
   116  <p>END</p>
   117  `},
   118  		{`- [x] On1`, false, `<ul>
   119  <li>[x] On1</li>
   120  </ul>
   121  `},
   122  		{`* [ ] Off
   123  
   124  END`, true, `<ul class="task-list">
   125  <li><label><input type="checkbox" disabled class="task-list-item"> Off</label></li>
   126  </ul>
   127  
   128  <p>END</p>
   129  `},
   130  	} {
   131  		blackFridayConfig := c.BlackFriday
   132  		blackFridayConfig.TaskLists = this.taskListEnabled
   133  		ctx := &RenderingContext{Content: []byte(this.markdown), PageFmt: "markdown", Config: blackFridayConfig}
   134  
   135  		result := string(c.RenderBytes(ctx))
   136  
   137  		if result != this.expect {
   138  			t.Errorf("[%d] got \n%v but expected \n%v", i, result, this.expect)
   139  		}
   140  	}
   141  }