hugo

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

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

integration_test.go (7747B)

    1 // Copyright 2022 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 codeblocks_test
   15 
   16 import (
   17 	"path/filepath"
   18 	"strings"
   19 	"testing"
   20 
   21 	"github.com/gohugoio/hugo/hugolib"
   22 )
   23 
   24 func TestCodeblocks(t *testing.T) {
   25 	t.Parallel()
   26 
   27 	files := `
   28 -- config.toml --
   29 [markup]
   30   [markup.highlight]
   31     anchorLineNos = false
   32     codeFences = true
   33     guessSyntax = false
   34     hl_Lines = ''
   35     lineAnchors = ''
   36     lineNoStart = 1
   37     lineNos = false
   38     lineNumbersInTable = true
   39     noClasses = false
   40     style = 'monokai'
   41     tabWidth = 4
   42 -- layouts/_default/_markup/render-codeblock-goat.html --
   43 {{ $diagram := diagrams.Goat .Inner }}
   44 Goat SVG:{{ substr $diagram.Wrapped 0 100 | safeHTML }}  }}|
   45 Goat Attribute: {{ .Attributes.width}}|
   46 -- layouts/_default/_markup/render-codeblock-go.html --
   47 Go Code: {{ .Inner | safeHTML }}|
   48 Go Language: {{ .Type }}|
   49 -- layouts/_default/single.html --
   50 {{ .Content }}
   51 -- content/p1.md --
   52 ---
   53 title: "p1"
   54 ---
   55 
   56 ## Ascii Diagram
   57 
   58 §§§goat { width="600" }
   59 --->
   60 §§§
   61 
   62 ## Go Code
   63 
   64 §§§go
   65 fmt.Println("Hello, World!");
   66 §§§
   67 
   68 ## Golang Code
   69 
   70 §§§golang
   71 fmt.Println("Hello, Golang!");
   72 §§§
   73 
   74 ## Bash Code
   75 
   76 §§§bash { linenos=inline,hl_lines=[2,"5-6"],linenostart=32 class=blue }
   77 echo "l1";
   78 echo "l2";
   79 echo "l3";
   80 echo "l4";
   81 echo "l5";
   82 echo "l6";
   83 echo "l7";
   84 echo "l8";
   85 §§§
   86 `
   87 
   88 	b := hugolib.NewIntegrationTestBuilder(
   89 		hugolib.IntegrationTestConfig{
   90 			T:           t,
   91 			TxtarString: files,
   92 			NeedsOsFS:   false,
   93 		},
   94 	).Build()
   95 
   96 	b.AssertFileContent("public/p1/index.html", `
   97 Goat SVG:<svg class='diagram'
   98 Goat Attribute: 600|
   99 
  100 Go Language: go|
  101 Go Code: fmt.Println("Hello, World!");
  102 
  103 Go Code: fmt.Println("Hello, Golang!");
  104 Go Language: golang|
  105 
  106 
  107 	`,
  108 		"Goat SVG:<svg class='diagram' xmlns='http://www.w3.org/2000/svg' version='1.1' height='25' width='40'",
  109 		"Goat Attribute: 600|",
  110 		"<h2 id=\"go-code\">Go Code</h2>\nGo Code: fmt.Println(\"Hello, World!\");\n|\nGo Language: go|",
  111 		"<h2 id=\"golang-code\">Golang Code</h2>\nGo Code: fmt.Println(\"Hello, Golang!\");\n|\nGo Language: golang|",
  112 		"<h2 id=\"bash-code\">Bash Code</h2>\n<div class=\"highlight blue\"><pre tabindex=\"0\" class=\"chroma\"><code class=\"language-bash\" data-lang=\"bash\"><span class=\"line\"><span class=\"ln\">32</span><span class=\"cl\"><span class=\"nb\">echo</span> <span class=\"s2\">&#34;l1&#34;</span><span class=\"p\">;</span>\n</span></span><span class=\"line hl\"><span class=\"ln\">33</span>",
  113 	)
  114 }
  115 
  116 func TestHighlightCodeblock(t *testing.T) {
  117 	t.Parallel()
  118 
  119 	files := `
  120 -- config.toml --
  121 [markup]
  122 [markup.highlight]
  123 anchorLineNos = false
  124 codeFences = true
  125 guessSyntax = false
  126 hl_Lines = ''
  127 lineAnchors = ''
  128 lineNoStart = 1
  129 lineNos = false
  130 lineNumbersInTable = true
  131 noClasses = false
  132 style = 'monokai'
  133 tabWidth = 4
  134 -- layouts/_default/_markup/render-codeblock.html --
  135 {{ $result := transform.HighlightCodeBlock . }}
  136 Inner: |{{ $result.Inner | safeHTML }}|
  137 Wrapped: |{{ $result.Wrapped | safeHTML }}|
  138 -- layouts/_default/single.html --
  139 {{ .Content }}
  140 -- content/p1.md --
  141 ---
  142 title: "p1"
  143 ---
  144 
  145 ## Go Code
  146 
  147 §§§go
  148 fmt.Println("Hello, World!");
  149 §§§
  150 
  151 `
  152 
  153 	b := hugolib.NewIntegrationTestBuilder(
  154 		hugolib.IntegrationTestConfig{
  155 			T:           t,
  156 			TxtarString: files,
  157 			NeedsOsFS:   false,
  158 		},
  159 	).Build()
  160 
  161 	b.AssertFileContent("public/p1/index.html",
  162 		"Inner: |<span class=\"line\"><span class=\"cl\"><span class=\"nx\">fmt</span><span class=\"p\">.</span><span class=\"nf\">Println</span><span class=\"p\">(</span><span class=\"s\">&#34;Hello, World!&#34;</span><span class=\"p\">);</span></span></span>|",
  163 		"Wrapped: |<div class=\"highlight\"><pre tabindex=\"0\" class=\"chroma\"><code class=\"language-go\" data-lang=\"go\"><span class=\"line\"><span class=\"cl\"><span class=\"nx\">fmt</span><span class=\"p\">.</span><span class=\"nf\">Println</span><span class=\"p\">(</span><span class=\"s\">&#34;Hello, World!&#34;</span><span class=\"p\">);</span></span></span></code></pre></div>|",
  164 	)
  165 }
  166 
  167 func TestCodeblocksBugs(t *testing.T) {
  168 	t.Parallel()
  169 
  170 	files := `
  171 -- config.toml --
  172 -- layouts/_default/_markup/render-codeblock.html --
  173 {{ .Position | safeHTML }}
  174 -- layouts/_default/single.html --
  175 {{ .Content }}
  176 -- content/p1.md --
  177 ---
  178 title: "p1"
  179 ---
  180 
  181 ## Issue 9627
  182 
  183 §§§text
  184 {{</* foo */>}}
  185 §§§
  186 
  187 `
  188 
  189 	b := hugolib.NewIntegrationTestBuilder(
  190 		hugolib.IntegrationTestConfig{
  191 			T:           t,
  192 			TxtarString: files,
  193 			NeedsOsFS:   false,
  194 		},
  195 	).Build()
  196 
  197 	b.AssertFileContent("public/p1/index.html", `
  198 # Issue 9627: For the Position in code blocks we try to match the .Inner with the original source. This isn't always possible.
  199 p1.md:0:0
  200 	`,
  201 	)
  202 }
  203 
  204 func TestCodeChomp(t *testing.T) {
  205 	t.Parallel()
  206 
  207 	files := `
  208 -- config.toml --
  209 -- content/p1.md --
  210 ---
  211 title: "p1"
  212 ---
  213 
  214 §§§bash
  215 echo "p1";
  216 §§§
  217 -- layouts/_default/single.html --
  218 {{ .Content }}
  219 -- layouts/_default/_markup/render-codeblock.html --
  220 |{{ .Inner | safeHTML }}|
  221 
  222 `
  223 
  224 	b := hugolib.NewIntegrationTestBuilder(
  225 		hugolib.IntegrationTestConfig{
  226 			T:           t,
  227 			TxtarString: files,
  228 			NeedsOsFS:   false,
  229 		},
  230 	).Build()
  231 
  232 	b.AssertFileContent("public/p1/index.html", "|echo \"p1\";|")
  233 }
  234 
  235 func TestCodePosition(t *testing.T) {
  236 	t.Parallel()
  237 
  238 	files := `
  239 -- config.toml --
  240 -- content/p1.md --
  241 ---
  242 title: "p1"
  243 ---
  244 
  245 ##   Code
  246 
  247 §§§
  248 echo "p1";
  249 §§§
  250 -- layouts/_default/single.html --
  251 {{ .Content }}
  252 -- layouts/_default/_markup/render-codeblock.html --
  253 Position: {{ .Position | safeHTML }}
  254 
  255 
  256 `
  257 
  258 	b := hugolib.NewIntegrationTestBuilder(
  259 		hugolib.IntegrationTestConfig{
  260 			T:           t,
  261 			TxtarString: files,
  262 		},
  263 	).Build()
  264 
  265 	b.AssertFileContent("public/p1/index.html", filepath.FromSlash("Position: \"/content/p1.md:7:1\""))
  266 }
  267 
  268 // Issue 9571
  269 func TestAttributesChroma(t *testing.T) {
  270 	t.Parallel()
  271 
  272 	files := `
  273 -- config.toml --
  274 -- content/p1.md --
  275 ---
  276 title: "p1"
  277 ---
  278 
  279 ##   Code
  280 
  281 §§§LANGUAGE {style=monokai}
  282 echo "p1";
  283 §§§
  284 -- layouts/_default/single.html --
  285 {{ .Content }}
  286 -- layouts/_default/_markup/render-codeblock.html --
  287 Attributes: {{ .Attributes }}|Options: {{ .Options }}|
  288 
  289 
  290 `
  291 	testLanguage := func(language, expect string) {
  292 		b := hugolib.NewIntegrationTestBuilder(
  293 			hugolib.IntegrationTestConfig{
  294 				T:           t,
  295 				TxtarString: strings.ReplaceAll(files, "LANGUAGE", language),
  296 			},
  297 		).Build()
  298 
  299 		b.AssertFileContent("public/p1/index.html", expect)
  300 	}
  301 
  302 	testLanguage("bash", "Attributes: map[]|Options: map[style:monokai]|")
  303 	testLanguage("hugo", "Attributes: map[style:monokai]|Options: map[]|")
  304 }
  305 
  306 func TestPanics(t *testing.T) {
  307 
  308 	files := `
  309 -- config.toml --
  310 [markup]
  311 [markup.goldmark]
  312 [markup.goldmark.parser]
  313 autoHeadingID = true
  314 autoHeadingIDType = "github"
  315 [markup.goldmark.parser.attribute]
  316 block = true
  317 title = true
  318 -- content/p1.md --
  319 ---
  320 title: "p1"
  321 ---
  322 
  323 BLOCK
  324 
  325 Common
  326 
  327 -- layouts/_default/single.html --
  328 {{ .Content }}
  329 
  330 
  331 `
  332 
  333 	for _, test := range []struct {
  334 		name     string
  335 		markdown string
  336 	}{
  337 		{"issue-9819", "asdf\n: {#myid}"},
  338 	} {
  339 		t.Run(test.name, func(t *testing.T) {
  340 			t.Parallel()
  341 			b := hugolib.NewIntegrationTestBuilder(
  342 				hugolib.IntegrationTestConfig{
  343 					T:           t,
  344 					TxtarString: strings.ReplaceAll(files, "BLOCK", test.markdown),
  345 				},
  346 			).Build()
  347 
  348 			b.AssertFileContent("public/p1/index.html", "Common")
  349 		})
  350 	}
  351 
  352 }