Debian Codegraph @

markup/highlight/highlight_integration_test.go

raw
1 // Copyright 2024 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_test
15
16 import (
17 "strings"
18 "testing"
19
20 qt "github.com/frankban/quicktest"
21 "github.com/gohugoio/hugo/hugolib"
22 )
23
24 func TestHighlightInline(t *testing.T) {
25 t.Parallel()
26
27 files := `
28 -- hugo.toml --
29 [markup]
30 [markup.highlight]
31 codeFences = true
32 noClasses = false
33 -- content/p1.md --
34 ---
35 title: "p1"
36 ---
37
38 ## Inline in Shortcode
39
40 Inline:{{< highlight emacs "hl_inline=true" >}}(message "this highlight shortcode"){{< /highlight >}}:End.
41 Inline Unknown:{{< highlight foo "hl_inline=true" >}}(message "this highlight shortcode"){{< /highlight >}}:End.
42
43 ## Inline in code block
44
45 Not sure if this makes sense, but add a test for it:
46
47 §§§bash {hl_inline=true}
48 (message "highlight me")
49 §§§
50
51 ## HighlightCodeBlock in hook
52
53 §§§html
54 (message "highlight me 2")
55 §§§
56
57 ## Unknown lexer
58
59 §§§foo {hl_inline=true}
60 (message "highlight me 3")
61 §§§
62
63
64 -- layouts/_markup/render-codeblock-html.html --
65 {{ $opts := dict "hl_inline" true }}
66 {{ $result := transform.HighlightCodeBlock . $opts }}
67 HighlightCodeBlock: Wrapped:{{ $result.Wrapped }}|Inner:{{ $result.Inner }}
68 -- layouts/single.html --
69 {{ .Content }}
70 `
71
72 b := hugolib.Test(t, files)
73
74 b.AssertFileContent("public/p1/index.html",
75 "Inline:<code class=\"code-inline language-emacs\"><span class=\"p\">(</span><span class=\"nf\">message</span> <span class=\"s\">&#34;this highlight shortcode&#34;</span><span class=\"p\">)</span></code>:End.",
76 "Inline Unknown:<code class=\"code-inline language-foo\">(message &#34;this highlight shortcode&#34;)</code>:End.",
77 "Not sure if this makes sense, but add a test for it:</p>\n<code class=\"code-inline language-bash\"><span class=\"o\">(</span>message <span class=\"s2\">&#34;highlight me&#34;</span><span class=\"o\">)</span>\n</code>",
78 "HighlightCodeBlock: Wrapped:<code class=\"code-inline language-html\">(message &#34;highlight me 2&#34;)</code>|Inner:<code class=\"code-inline language-html\">(message &#34;highlight me 2&#34;)</code>",
79 "<code class=\"code-inline language-foo\">(message &#34;highlight me 3&#34;)\n</code>",
80 )
81 }
82
83 // See issue 11872.
84 func TestCodeblockWithTypeOverride(t *testing.T) {
85 t.Parallel()
86
87 files := `
88 -- hugo.toml --
89 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
90 [markup.highlight]
91 noClasses = false # to reduce size of assertion string
92 -- content/p1.md --
93 ---
94 title: p1
95 ---
96 §§§go {style=monokai class=my-class tabWidth=8}
97 i = 42
98 §§§
99 -- content/p2.md --
100 ---
101 title: p2
102 ---
103 §§§{style=monokai class=my-class tabWidth=8}
104 i = 42
105 §§§
106 -- layouts/page.html --
107 {{ .Content }}
108 -- layouts/_markup/render-codeblock.html --
109 {{- $opts := dict }}
110 {{- if not (transform.CanHighlight .Type) }}
111 {{- $opts = dict "type" "text" }}
112 {{- end }}
113 {{- $result := transform.HighlightCodeBlock . $opts }}
114 {{- $result.Wrapped -}}
115 `
116
117 b := hugolib.Test(t, files)
118
119 b.AssertFileContent("public/p1/index.html",
120 `<div class="highlight my-class"><pre tabindex="0" class="chroma"><code class="language-go" data-lang="go"><span class="line"><span class="cl"><span class="nx">i</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">42</span></span></span></code></pre></div>`,
121 )
122 b.AssertFileContent("public/p2/index.html",
123 `<div class="highlight my-class"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">i = 42</span></span></code></pre></div>`,
124 )
125 }
126
127 // Issue #11311
128 func TestIssue11311(t *testing.T) {
129 t.Parallel()
130
131 files := `
132 -- hugo.toml --
133 [markup.highlight]
134 noClasses = false
135 -- content/_index.md --
136 ---
137 title: home
138 ---
139 §§§go
140 xəx := 0
141 §§§
142 -- layouts/home.html --
143 {{ .Content }}
144 `
145
146 b := hugolib.Test(t, files)
147
148 b.AssertFileContent("public/index.html", `
149 <span class="nx">xəx</span>
150 `)
151 }
152
153 func TestHighlightClass(t *testing.T) {
154 t.Parallel()
155
156 files := `
157 -- hugo.toml --
158 [markup.highlight]
159 noClasses = false
160 wrapperClass = "highlight no-prose"
161 -- content/_index.md --
162 ---
163 title: home
164 ---
165 §§§go
166 xəx := 0
167 §§§
168 -- layouts/home.html --
169 {{ .Content }}
170 `
171
172 b := hugolib.Test(t, files)
173
174 b.AssertFileContent("public/index.html", `
175 <div class="highlight no-prose"><pre
176 `)
177 }
178
179 func TestHighlightLineNos(t *testing.T) {
180 t.Parallel()
181
182 files := `
183 -- config.toml --
184 disableKinds = ['page','rss','section','sitemap','taxonomy','term']
185 [markup.highlight]
186 noClasses = false
187 lineNoStart = 42
188 #LINENOS
189 -- content/_index.md --
190 ---
191 title: home
192 ---
193 §§§go
194 aaa
195 §§§
196 -- layouts/index.html --
197 {{ .Content }}
198 `
199
200 b := hugolib.Test(t, files)
201 b.AssertFileContent("public/index.html",
202 `<span class="nx">aaa</span>`,
203 `! <table class="lntable">`,
204 `! 42`,
205 )
206
207 f := strings.ReplaceAll(files, "#LINENOS", `linenos = false`)
208 b = hugolib.Test(t, f)
209 b.AssertFileContent("public/index.html",
210 `<span class="nx">aaa</span>`,
211 `! <table class="lntable">`,
212 `! 42`,
213 )
214
215 f = strings.ReplaceAll(files, "#LINENOS", `linenos = true`)
216 b = hugolib.Test(t, f)
217 b.AssertFileContent("public/index.html",
218 `<span class="nx">aaa</span>`,
219 `<table class="lntable">`,
220 `42`,
221 )
222
223 f = strings.ReplaceAll(files, "#LINENOS", `linenos = "table"`)
224 b = hugolib.Test(t, f)
225 b.AssertFileContent("public/index.html",
226 `<span class="nx">aaa</span>`,
227 `<table class="lntable">`,
228 `42`,
229 )
230
231 f = strings.ReplaceAll(files, "#LINENOS", `linenos = "inline"`)
232 b = hugolib.Test(t, f)
233 b.AssertFileContent("public/index.html",
234 `<span class="nx">aaa</span>`,
235 `! <table class="lntable">`,
236 `42`,
237 )
238
239 want := `.* lineNos must be one of .*`
240
241 f = strings.ReplaceAll(files, "#LINENOS", `linenos = "foo"`)
242 b, err := hugolib.TestE(t, f)
243 b.Assert(err, qt.ErrorMatches, want)
244
245 f = strings.ReplaceAll(files, "#LINENOS", `linenos = 123`)
246 b, err = hugolib.TestE(t, f)
247 b.Assert(err, qt.ErrorMatches, want)
248 }