github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/markup/highlight/highlight_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 highlight provides code highlighting.
    15  package highlight
    16  
    17  import (
    18  	"testing"
    19  
    20  	qt "github.com/frankban/quicktest"
    21  )
    22  
    23  func TestHighlight(t *testing.T) {
    24  	c := qt.New(t)
    25  
    26  	lines := `LINE1
    27  LINE2
    28  LINE3
    29  LINE4
    30  LINE5
    31  `
    32  	coalesceNeeded := `GET /foo HTTP/1.1
    33  Content-Type: application/json
    34  User-Agent: foo
    35  
    36  {
    37    "hello": "world"
    38  }`
    39  
    40  	c.Run("Basic", func(c *qt.C) {
    41  		cfg := DefaultConfig
    42  		cfg.NoClasses = false
    43  		h := New(cfg)
    44  
    45  		result, _ := h.Highlight(`echo "Hugo Rocks!"`, "bash", "")
    46  		c.Assert(result, qt.Equals, `<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="nb">echo</span> <span class="s2">&#34;Hugo Rocks!&#34;</span></code></pre></div>`)
    47  		result, _ = h.Highlight(`echo "Hugo Rocks!"`, "unknown", "")
    48  		c.Assert(result, qt.Equals, `<pre tabindex="0"><code class="language-unknown" data-lang="unknown">echo &#34;Hugo Rocks!&#34;</code></pre>`)
    49  	})
    50  
    51  	c.Run("Highlight lines, default config", func(c *qt.C) {
    52  		cfg := DefaultConfig
    53  		cfg.NoClasses = false
    54  		h := New(cfg)
    55  
    56  		result, _ := h.Highlight(lines, "bash", "linenos=table,hl_lines=2 4-5,linenostart=3")
    57  		c.Assert(result, qt.Contains, "<div class=\"highlight\"><div class=\"chroma\">\n<table class=\"lntable\"><tr><td class=\"lntd\">\n<pre tabindex=\"0\" class=\"chroma\"><code><span class")
    58  		c.Assert(result, qt.Contains, "<span class=\"hl\"><span class=\"lnt\">4")
    59  
    60  		result, _ = h.Highlight(lines, "bash", "linenos=inline,hl_lines=2")
    61  		c.Assert(result, qt.Contains, "<span class=\"ln\">2</span>LINE2\n</span>")
    62  		c.Assert(result, qt.Not(qt.Contains), "<table")
    63  
    64  		result, _ = h.Highlight(lines, "bash", "linenos=true,hl_lines=2")
    65  		c.Assert(result, qt.Contains, "<table")
    66  		c.Assert(result, qt.Contains, "<span class=\"hl\"><span class=\"lnt\">2\n</span>")
    67  	})
    68  
    69  	c.Run("Highlight lines, linenumbers default on", func(c *qt.C) {
    70  		cfg := DefaultConfig
    71  		cfg.NoClasses = false
    72  		cfg.LineNos = true
    73  		h := New(cfg)
    74  
    75  		result, _ := h.Highlight(lines, "bash", "")
    76  		c.Assert(result, qt.Contains, "<span class=\"lnt\">2\n</span>")
    77  		result, _ = h.Highlight(lines, "bash", "linenos=false,hl_lines=2")
    78  		c.Assert(result, qt.Not(qt.Contains), "class=\"lnt\"")
    79  	})
    80  
    81  	c.Run("Highlight lines, linenumbers default on, anchorlinenumbers default on", func(c *qt.C) {
    82  		cfg := DefaultConfig
    83  		cfg.NoClasses = false
    84  		cfg.LineNos = true
    85  		cfg.AnchorLineNos = true
    86  		h := New(cfg)
    87  
    88  		result, _ := h.Highlight(lines, "bash", "")
    89  		// From Chroma v0.8.2 this is linkable: https://github.com/alecthomas/chroma/commit/ab61726cdb54d5a98b6efe7ed76af6aa0698ab4a
    90  		c.Assert(result, qt.Contains, "<span class=\"lnt\" id=\"2\"><a style=\"outline: none; text-decoration:none; color:inherit\" href=\"#2\">2</a>\n</span>")
    91  		result, _ = h.Highlight(lines, "bash", "lineanchors=test")
    92  		result, _ = h.Highlight(lines, "bash", "anchorlinenos=false,hl_lines=2")
    93  		c.Assert(result, qt.Not(qt.Contains), "id=\"2\"")
    94  	})
    95  
    96  	c.Run("Highlight lines, linenumbers default on, linenumbers in table default off", func(c *qt.C) {
    97  		cfg := DefaultConfig
    98  		cfg.NoClasses = false
    99  		cfg.LineNos = true
   100  		cfg.LineNumbersInTable = false
   101  		h := New(cfg)
   102  
   103  		result, _ := h.Highlight(lines, "bash", "")
   104  		c.Assert(result, qt.Contains, "<span class=\"ln\">2</span>LINE2\n<")
   105  		result, _ = h.Highlight(lines, "bash", "linenos=table")
   106  		c.Assert(result, qt.Contains, "<span class=\"lnt\">1\n</span>")
   107  	})
   108  
   109  	c.Run("No language", func(c *qt.C) {
   110  		cfg := DefaultConfig
   111  		cfg.NoClasses = false
   112  		cfg.LineNos = true
   113  		h := New(cfg)
   114  
   115  		result, _ := h.Highlight(lines, "", "")
   116  		c.Assert(result, qt.Equals, "<pre tabindex=\"0\"><code>LINE1\nLINE2\nLINE3\nLINE4\nLINE5\n</code></pre>")
   117  	})
   118  
   119  	c.Run("No language, guess syntax", func(c *qt.C) {
   120  		cfg := DefaultConfig
   121  		cfg.NoClasses = false
   122  		cfg.GuessSyntax = true
   123  		cfg.LineNos = true
   124  		cfg.LineNumbersInTable = false
   125  		h := New(cfg)
   126  
   127  		result, _ := h.Highlight(lines, "", "")
   128  		c.Assert(result, qt.Contains, "<span class=\"ln\">2</span>LINE2\n<")
   129  	})
   130  
   131  	c.Run("No language, Escape HTML string", func(c *qt.C) {
   132  		cfg := DefaultConfig
   133  		cfg.NoClasses = false
   134  		h := New(cfg)
   135  
   136  		result, _ := h.Highlight("Escaping less-than in code block? <fail>", "", "")
   137  		c.Assert(result, qt.Contains, "&lt;fail&gt;")
   138  	})
   139  
   140  	c.Run("Highlight lines, default config", func(c *qt.C) {
   141  		cfg := DefaultConfig
   142  		cfg.NoClasses = false
   143  		h := New(cfg)
   144  
   145  		result, _ := h.Highlight(coalesceNeeded, "http", "linenos=true,hl_lines=2")
   146  		c.Assert(result, qt.Contains, "hello")
   147  		c.Assert(result, qt.Contains, "}")
   148  	})
   149  }