Debian Codegraph @

resources/page/page_markup_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 page_test
15
16 import (
17 "strconv"
18 "strings"
19 "testing"
20
21 "github.com/gohugoio/hugo/hugolib"
22 "github.com/gohugoio/hugo/markup/asciidocext"
23 "github.com/gohugoio/hugo/markup/rst"
24 )
25
26 func TestPageMarkupMethods(t *testing.T) {
27 t.Parallel()
28
29 files := `
30 -- hugo.toml --
31 summaryLength=2
32 -- content/p1.md --
33 ---
34 title: "Post 1"
35 date: "2020-01-01"
36 ---
37 {{% foo %}}
38 -- layouts/_shortcodes/foo.html --
39 Two *words*.
40 {{/* Test that markup scope is set in all relevant constructs. */}}
41 {{ if eq hugo.Context.MarkupScope "foo" }}
42
43 ## Heading 1
44 Sint ad mollit qui Lorem ut occaecat culpa officia. Et consectetur aute voluptate non sit ullamco adipisicing occaecat. Sunt deserunt amet sit ad. Deserunt enim voluptate proident ipsum dolore dolor ut sit velit esse est mollit irure esse. Mollit incididunt veniam laboris magna et excepteur sit duis. Magna adipisicing reprehenderit tempor irure.
45 ### Heading 2
46 Exercitation quis est consectetur occaecat nostrud. Ullamco aute mollit aliqua est amet. Exercitation ullamco consectetur dolor labore et non irure eu cillum Lorem.
47 {{ end }}
48 -- layouts/home.html --
49 Home.
50 {{ .Content }}
51 -- layouts/single.html --
52 Single.
53 Page.ContentWithoutSummmary: {{ .ContentWithoutSummary }}|
54 {{ template "render-scope" (dict "page" . "scope" "main") }}
55 {{ template "render-scope" (dict "page" . "scope" "foo") }}
56 {{ define "render-scope" }}
57 {{ $c := .page.Markup .scope }}
58 {{ with $c.Render }}
59 {{ $.scope }}: Content: {{ .Content }}|
60 {{ $.scope }}: ContentWithoutSummary: {{ .ContentWithoutSummary }}|
61 {{ $.scope }}: Plain: {{ .Plain }}|
62 {{ $.scope }}: PlainWords: {{ .PlainWords }}|
63 {{ $.scope }}: WordCount: {{ .WordCount }}|
64 {{ $.scope }}: FuzzyWordCount: {{ .FuzzyWordCount }}|
65 {{ $.scope }}: ReadingTime: {{ .ReadingTime }}|
66 {{ $.scope }}: Len: {{ .Len }}|
67 {{ $.scope }}: Summary: {{ with .Summary }}{{ . }}{{ else }}nil{{ end }}|
68 {{ end }}
69 {{ $.scope }}: Fragments: {{ $c.Fragments.Identifiers }}|
70 {{ end }}
71
72
73
74 `
75
76 b := hugolib.Test(t, files)
77
78 // Main scope.
79 b.AssertFileContent("public/p1/index.html",
80 "Page.ContentWithoutSummmary: |",
81 "main: Content: <p>Two <em>words</em>.</p>\n|",
82 "main: ContentWithoutSummary: |",
83 "main: Plain: Two words.\n|",
84 "PlainWords: [Two words.]|\nmain: WordCount: 2|\nmain: FuzzyWordCount: 100|\nmain: ReadingTime: 1|",
85 "main: Summary: <p>Two <em>words</em>.</p>|\n\nmain: Fragments: []|",
86 "main: Len: 27|",
87 )
88
89 // Foo scope (has more content).
90 b.AssertFileContent("public/p1/index.html",
91 "foo: Content: <p>Two <em>words</em>.</p>\n<h2",
92 "foo: ContentWithoutSummary: <h2",
93 "Plain: Two words.\nHeading 1",
94 "PlainWords: [Two words. Heading 1",
95 "foo: WordCount: 81|\nfoo: FuzzyWordCount: 100|\nfoo: ReadingTime: 1|\nfoo: Len: 622|",
96 "foo: Summary: <p>Two <em>words</em>.</p>|",
97 "foo: Fragments: [heading-1 heading-2]|",
98 )
99 }
100
101 func TestPageMarkupScope(t *testing.T) {
102 t.Parallel()
103
104 files := `
105 -- hugo.toml --
106 disableKinds = ["taxonomy", "term", "rss", "section"]
107 -- content/p1.md --
108 ---
109 title: "Post 1"
110 date: "2020-01-01"
111 ---
112
113 # P1
114
115 {{< foo >}}
116
117 Begin:{{% includerendershortcodes "p2" %}}:End
118 Begin:{{< includecontent "p3" >}}:End
119
120 -- content/p2.md --
121 ---
122 title: "Post 2"
123 date: "2020-01-02"
124 ---
125
126 # P2
127 -- content/p3.md --
128 ---
129 title: "Post 3"
130 date: "2020-01-03"
131 ---
132
133 # P3
134
135 {{< foo >}}
136
137 -- layouts/home.html --
138 Home.
139 {{ with site.GetPage "p1" }}
140 {{ with .Markup "home" }}
141 {{ .Render.Content }}
142 {{ end }}
143 {{ end }}
144 -- layouts/single.html --
145 Single.
146 {{ with .Markup }}
147 {{ with .Render }}
148 {{ .Content }}
149 {{ end }}
150 {{ end }}
151 -- layouts/_markup/render-heading.html --
152 Render heading: title: {{ .Text}} scope: {{ hugo.Context.MarkupScope }}|
153 -- layouts/_shortcodes/foo.html --
154 Foo scope: {{ hugo.Context.MarkupScope }}|
155 -- layouts/_shortcodes/includerendershortcodes.html --
156 {{ $p := site.GetPage (.Get 0) }}
157 includerendershortcodes: {{ hugo.Context.MarkupScope }}|{{ $p.Markup.RenderShortcodes }}|
158 -- layouts/_shortcodes/includecontent.html --
159 {{ $p := site.GetPage (.Get 0) }}
160 includecontent: {{ hugo.Context.MarkupScope }}|{{ $p.Markup.Render.Content }}|
161
162 `
163
164 b := hugolib.Test(t, files)
165
166 b.AssertFileContentExact("public/p1/index.html", "Render heading: title: P1 scope: |", "Foo scope: |")
167
168 b.AssertFileContentExact("public/index.html",
169 "Begin:\nincludecontent: home|Render heading: title: P3 scope: home|Foo scope: home|\n|\n:End",
170 "Render heading: title: P1 scope: home|",
171 "Foo scope: home|",
172 "Begin:\nincluderendershortcodes: home|</p>\nRender heading: title: P2 scope: home|<p>|:End",
173 )
174 }
175
176 func TestPageContentWithoutSummary(t *testing.T) {
177 t.Parallel()
178
179 files := `
180 -- hugo.toml --
181 summaryLength=5
182 -- content/p1.md --
183 ---
184 title: "Post 1"
185 date: "2020-01-01"
186 ---
187 This is summary.
188 <!--more-->
189 This is content.
190 -- content/p2.md --
191 ---
192 title: "Post 2"
193 date: "2020-01-01"
194 ---
195 This is some content about a summary and more.
196
197 Another paragraph.
198
199 Third paragraph.
200 -- content/p3.md --
201 ---
202 title: "Post 3"
203 date: "2020-01-01"
204 summary: "This is summary in front matter."
205 ---
206 This is content.
207 -- layouts/single.html --
208 Single.
209 Page.Summary: {{ .Summary }}|
210 {{ with .Markup.Render }}
211 Content: {{ .Content }}|
212 ContentWithoutSummary: {{ .ContentWithoutSummary }}|
213 WordCount: {{ .WordCount }}|
214 FuzzyWordCount: {{ .FuzzyWordCount }}|
215 {{ with .Summary }}
216 Summary: {{ . }}|
217 Summary Type: {{ .Type }}|
218 Summary Truncated: {{ .Truncated }}|
219 {{ end }}
220 {{ end }}
221
222 `
223 b := hugolib.Test(t, files)
224
225 b.AssertFileContentExact("public/p1/index.html",
226 "Content: <p>This is summary.</p>\n<p>This is content.</p>",
227 "ContentWithoutSummary: <p>This is content.</p>|",
228 "WordCount: 6|",
229 "FuzzyWordCount: 100|",
230 "Summary: <p>This is summary.</p>|",
231 "Summary Type: manual|",
232 "Summary Truncated: true|",
233 )
234 b.AssertFileContent("public/p2/index.html",
235 "Summary: <p>This is some content about a summary and more.</p>|",
236 "WordCount: 13|",
237 "FuzzyWordCount: 100|",
238 "Summary Type: auto",
239 "Summary Truncated: true",
240 )
241
242 b.AssertFileContentExact("public/p3/index.html",
243 "Summary: This is summary in front matter.|",
244 "ContentWithoutSummary: <p>This is content.</p>\n|",
245 )
246 }
247
248 func TestPageMarkupWithoutSummaryRST(t *testing.T) {
249 t.Parallel()
250 if !rst.Supports() {
251 t.Skip("Skip RST test as not supported")
252 }
253
254 files := `
255 -- hugo.toml --
256 summaryLength=5
257 [security.exec]
258 allow = ["rst", "python"]
259
260 -- content/p1.rst --
261 This is a story about a summary and more.
262
263 Another paragraph.
264 -- content/p2.rst --
265 This is summary.
266 <!--more-->
267 This is content.
268 -- layouts/single.html --
269 Single.
270 Page.Summary: {{ .Summary }}|
271 {{ with .Markup.Render }}
272 Content: {{ .Content }}|
273 ContentWithoutSummary: {{ .ContentWithoutSummary }}|
274 {{ with .Summary }}
275 Summary: {{ . }}|
276 Summary Type: {{ .Type }}|
277 Summary Truncated: {{ .Truncated }}|
278 {{ end }}
279 {{ end }}
280
281 `
282
283 b := hugolib.Test(t, files)
284
285 // Auto summary.
286 b.AssertFileContentExact("public/p1/index.html",
287 "Content: <div class=\"document\">\n\n\n<p>This is a story about a summary and more.</p>\n<p>Another paragraph.</p>\n</div>|",
288 "Summary: <div class=\"document\">\n\n\n<p>This is a story about a summary and more.</p></div>|\nSummary Type: auto|\nSummary Truncated: true|",
289 "ContentWithoutSummary: <div class=\"document\">\n<p>Another paragraph.</p>\n</div>|",
290 )
291
292 // Manual summary.
293 b.AssertFileContentExact("public/p2/index.html",
294 "Content: <div class=\"document\">\n\n\n<p>This is summary.</p>\n<p>This is content.</p>\n</div>|",
295 "ContentWithoutSummary: <div class=\"document\"><p>This is content.</p>\n</div>|",
296 "Summary: <div class=\"document\">\n\n\n<p>This is summary.</p>\n</div>|\nSummary Type: manual|\nSummary Truncated: true|",
297 )
298 }
299
300 func TestPageMarkupWithoutSummaryAsciiDoc(t *testing.T) {
301 t.Parallel()
302 if ok, err := asciidocext.Supports(); !ok {
303 t.Skip(err)
304 }
305
306 files := `
307 -- hugo.toml --
308 summaryLength=5
309 [security.exec]
310 allow = ["asciidoc", "python"]
311
312 -- content/p1.ad --
313 This is a story about a summary and more.
314
315 Another paragraph.
316 -- content/p2.ad --
317 This is summary.
318 <!--more-->
319 This is content.
320 -- layouts/single.html --
321 Single.
322 Page.Summary: {{ .Summary }}|
323 {{ with .Markup.Render }}
324 Content: {{ .Content }}|
325 ContentWithoutSummary: {{ .ContentWithoutSummary }}|
326 {{ with .Summary }}
327 Summary: {{ . }}|
328 Summary Type: {{ .Type }}|
329 Summary Truncated: {{ .Truncated }}|
330 {{ end }}
331 {{ end }}
332
333 `
334
335 b := hugolib.Test(t, files)
336
337 // Auto summary.
338 b.AssertFileContentExact("public/p1/index.html",
339 "Content: <div class=\"paragraph\">\n<p>This is a story about a summary and more.</p>\n</div>\n<div class=\"paragraph\">\n<p>Another paragraph.</p>\n</div>\n|",
340 "Summary: <div class=\"paragraph\">\n<p>This is a story about a summary and more.</p>\n</div>|",
341 "Summary Type: auto|\nSummary Truncated: true|",
342 "ContentWithoutSummary: <div class=\"paragraph\">\n<p>Another paragraph.</p>\n</div>|",
343 )
344
345 // Manual summary.
346 b.AssertFileContentExact("public/p2/index.html",
347 "Content: <div class=\"paragraph\">\n<p>This is summary.</p>\n</div>\n<div class=\"paragraph\">\n<p>This is content.</p>\n</div>|",
348 "ContentWithoutSummary: <div class=\"paragraph\">\n<p>This is content.</p>\n</div>|",
349 "Summary: <div class=\"paragraph\">\n<p>This is summary.</p>\n</div>|\nSummary Type: manual|\nSummary Truncated: true|",
350 )
351 }
352
353 func TestIssue13967(t *testing.T) {
354 t.Parallel()
355
356 files := `
357 -- hugo.toml --
358 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
359 -- layouts/all.html --
360 Title: {{ .Title }}|Summary: {{ .Summary }}|Truncated: {{ .Truncated }}|
361 -- content/p1.md --
362 ---
363 title: p1
364 ---
365 <!--more--> one two three
366 -- content/p2.md --
367 ---
368 title: p2
369 ---
370 one <!--more--> two three
371 -- content/p3.md --
372 ---
373 title: p3
374 ---
375 one two <!--more--> three
376 -- content/p4.md --
377 ---
378 title: p4
379 ---
380 one two three <!--more-->
381 `
382 b := hugolib.Test(t, files)
383
384 b.AssertFileContent("public/p1/index.html", `Title: p1|Summary: |Truncated: true|`)
385 b.AssertFileContent("public/p2/index.html", `Title: p2|Summary: <p>one</p>|Truncated: true|`)
386 b.AssertFileContent("public/p3/index.html", `Title: p3|Summary: <p>one two</p>|Truncated: true|`)
387 b.AssertFileContent("public/p4/index.html", `Title: p4|Summary: <p>one two three</p>|Truncated: false|`)
388 }
389
390 func TestIssue13968(t *testing.T) {
391 t.Parallel()
392
393 files := `
394 -- hugo.toml --
395 disableKinds = ['page','rss','section','sitemap','taxonomy','term']
396 summaryLength = SUMMARY_LENGTH
397 -- layouts/all.html --
398 Title: {{ .Title }}|Summary: {{ .Summary }}|Truncated: {{ .Truncated }}|
399 -- content/_index.md --
400 ---
401 title: home
402 ---
403 one two three
404 `
405
406 tests := []struct {
407 summaryLength int
408 want string
409 }{
410 {0, "Title: home|Summary: |Truncated: true|"},
411 {1, "Title: home|Summary: <p>one two three</p>|Truncated: false|"},
412 {2, "Title: home|Summary: <p>one two three</p>|Truncated: false|"},
413 {3, "Title: home|Summary: <p>one two three</p>|Truncated: false|"},
414 {4, "Title: home|Summary: <p>one two three</p>|Truncated: false|"},
415 }
416
417 for _, tt := range tests {
418 f := strings.ReplaceAll(files, "SUMMARY_LENGTH", strconv.Itoa(tt.summaryLength))
419 b := hugolib.Test(t, f)
420 b.AssertFileContent("public/index.html", tt.want)
421 }
422 }