aboutsummaryrefslogtreecommitdiffhomepage
path: root/resources/page/page_markup_integration_test.go
blob: fc3c6a5691e644a84a1e0b07478740687aa33ae8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Copyright 2024 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package page_test

import (
	"testing"

	"github.com/gohugoio/hugo/hugolib"
	"github.com/gohugoio/hugo/markup/asciidocext"
	"github.com/gohugoio/hugo/markup/rst"
)

func TestPageMarkupMethods(t *testing.T) {
	t.Parallel()

	files := `
-- hugo.toml --
summaryLength=2
-- content/p1.md --
---
title: "Post 1"
date: "2020-01-01"
---
{{% foo %}}
-- layouts/shortcodes/foo.html --
Two *words*.
{{/* Test that markup scope is set in all relevant constructs. */}}
{{ if eq hugo.Context.MarkupScope "foo" }}

## Heading 1
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.
### Heading 2
Exercitation quis est consectetur occaecat nostrud. Ullamco aute mollit aliqua est amet. Exercitation ullamco consectetur dolor labore et non irure eu cillum Lorem.
{{ end }}
-- layouts/index.html --
Home.
{{ .Content }}
-- layouts/_default/single.html --
Single.
Page.ContentWithoutSummmary: {{ .ContentWithoutSummary }}|
{{ template "render-scope" (dict "page" . "scope" "main") }}
{{ template "render-scope" (dict "page" . "scope" "foo") }}
{{ define "render-scope" }}
{{ $c := .page.Markup .scope }}
{{ with $c.Render }}
{{ $.scope }}: Content: {{ .Content }}|
 {{ $.scope }}: ContentWithoutSummary: {{ .ContentWithoutSummary }}|
{{ $.scope }}: Plain: {{ .Plain }}|
{{ $.scope }}: PlainWords: {{ .PlainWords }}|
{{ $.scope }}: WordCount: {{ .WordCount }}|
{{ $.scope }}: FuzzyWordCount: {{ .FuzzyWordCount }}|
{{ $.scope }}: ReadingTime: {{ .ReadingTime }}|
{{ $.scope }}: Len: {{ .Len }}|
{{ $.scope }}: Summary: {{ with .Summary }}{{ . }}{{ else }}nil{{ end }}|
{{ end }}
{{ $.scope }}: Fragments: {{ $c.Fragments.Identifiers }}|
{{ end }}



`

	b := hugolib.Test(t, files)

	// Main scope.
	b.AssertFileContent("public/p1/index.html",
		"Page.ContentWithoutSummmary: |",
		"main: Content: <p>Two <em>words</em>.</p>\n|",
		"main: ContentWithoutSummary: |",
		"main: Plain: Two words.\n|",
		"PlainWords: [Two words.]|\nmain: WordCount: 2|\nmain: FuzzyWordCount: 100|\nmain: ReadingTime: 1|",
		"main: Summary: <p>Two <em>words</em>.</p>|\n\nmain: Fragments: []|",
		"main: Len: 27|",
	)

	// Foo scope (has more content).
	b.AssertFileContent("public/p1/index.html",
		"foo: Content: <p>Two <em>words</em>.</p>\n<h2",
		"foo: ContentWithoutSummary: <h2",
		"Plain: Two words.\nHeading 1",
		"PlainWords: [Two words. Heading 1",
		"foo: WordCount: 81|\nfoo: FuzzyWordCount: 100|\nfoo: ReadingTime: 1|\nfoo: Len: 622|",
		"foo: Summary: <p>Two <em>words</em>.</p>|",
		"foo: Fragments: [heading-1 heading-2]|",
	)
}

func TestPageMarkupScope(t *testing.T) {
	t.Parallel()

	files := `
-- hugo.toml --
disableKinds = ["taxonomy", "term", "rss", "section"]
-- content/p1.md --
---
title: "Post 1"
date: "2020-01-01"
---

# P1

{{< foo >}}

Begin:{{% includerendershortcodes "p2" %}}:End
Begin:{{< includecontent "p3" >}}:End

-- content/p2.md --
---
title: "Post 2"
date: "2020-01-02"
---

# P2
-- content/p3.md --
---
title: "Post 3"
date: "2020-01-03"
---

# P3

{{< foo >}}

-- layouts/index.html --
Home.
{{ with site.GetPage "p1" }}
	{{ with .Markup "home" }}
	 	{{ .Render.Content }}
	{{ end }}
{{ end }}
-- layouts/_default/single.html --
Single.
{{ with .Markup  }}
	{{ with .Render }}
	 	{{ .Content }}
	{{ end }}
{{ end }}
-- layouts/_default/_markup/render-heading.html --
Render heading: title: {{ .Text}} scope: {{ hugo.Context.MarkupScope }}|
-- layouts/shortcodes/foo.html --
Foo scope: {{ hugo.Context.MarkupScope }}|
-- layouts/shortcodes/includerendershortcodes.html --
{{ $p := site.GetPage (.Get 0) }}
includerendershortcodes: {{ hugo.Context.MarkupScope }}|{{ $p.Markup.RenderShortcodes }}|
-- layouts/shortcodes/includecontent.html --
{{ $p := site.GetPage (.Get 0) }}
includecontent: {{ hugo.Context.MarkupScope }}|{{ $p.Markup.Render.Content }}|

`

	b := hugolib.Test(t, files)

	b.AssertFileContent("public/p1/index.html", "Render heading: title: P1 scope: |", "Foo scope: |")

	b.AssertFileContent("public/index.html",
		"Render heading: title: P1 scope: home|",
		"Foo scope: home|",
		"Begin:\nincluderendershortcodes: home|</p>\nRender heading: title: P2 scope: home|<p>|:End",
		"Begin:\nincludecontent: home|Render heading: title: P3 scope: home|Foo scope: home|\n|\n:End",
	)
}

func TestPageMarkupWithoutSummary(t *testing.T) {
	t.Parallel()

	files := `
-- hugo.toml --
summaryLength=5
-- content/p1.md --
---
title: "Post 1"
date: "2020-01-01"
---
This is summary.
<!--more-->
This is content.
-- content/p2.md --
---
title: "Post 2"
date: "2020-01-01"
---
This is some content about a summary and more.

Another paragraph.

Third paragraph.
-- layouts/_default/single.html --
Single.
Page.Summary: {{ .Summary }}|
{{ with .Markup.Render }}
Content: {{ .Content }}|
ContentWithoutSummary: {{ .ContentWithoutSummary }}|
WordCount: {{ .WordCount }}|
FuzzyWordCount: {{ .FuzzyWordCount }}|
{{ with .Summary }}
Summary: {{ . }}|
Summary Type: {{ .Type }}|
Summary Truncated: {{ .Truncated }}|
{{ end }}
{{ end }}

`
	b := hugolib.Test(t, files)

	b.AssertFileContentExact("public/p1/index.html",
		"Content: <p>This is summary.</p>\n<p>This is content.</p>",
		"ContentWithoutSummary: <p>This is content.</p>|",
		"WordCount: 6|",
		"FuzzyWordCount: 100|",
		"Summary: <p>This is summary.</p>|",
		"Summary Type: manual|",
		"Summary Truncated: true|",
	)
	b.AssertFileContent("public/p2/index.html",
		"Summary: <p>This is some content about a summary and more.</p>|",
		"WordCount: 13|",
		"FuzzyWordCount: 100|",
		"Summary Type: auto",
		"Summary Truncated: true",
	)
}

func TestPageMarkupWithoutSummaryRST(t *testing.T) {
	t.Parallel()
	if !rst.Supports() {
		t.Skip("Skip RST test as not supported")
	}

	files := `
-- hugo.toml --
summaryLength=5
[security.exec]
allow = ["rst", "python"]

-- content/p1.rst --
This is a story about a summary and more.

Another paragraph.
-- content/p2.rst --
This is summary.
<!--more-->
This is content.
-- layouts/_default/single.html --
Single.
Page.Summary: {{ .Summary }}|
{{ with .Markup.Render }}
Content: {{ .Content }}|
ContentWithoutSummary: {{ .ContentWithoutSummary }}|
{{ with .Summary }}
Summary: {{ . }}|
Summary Type: {{ .Type }}|
Summary Truncated: {{ .Truncated }}|
{{ end }}
{{ end }}

`

	b := hugolib.Test(t, files)

	// Auto summary.
	b.AssertFileContentExact("public/p1/index.html",
		"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>|",
		"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|",
		"ContentWithoutSummary: <div class=\"document\">\n<p>Another paragraph.</p>\n</div>|",
	)

	// Manual summary.
	b.AssertFileContentExact("public/p2/index.html",
		"Content: <div class=\"document\">\n\n\n<p>This is summary.</p>\n<p>This is content.</p>\n</div>|",
		"ContentWithoutSummary: <div class=\"document\"><p>This is content.</p>\n</div>|",
		"Summary: <div class=\"document\">\n\n\n<p>This is summary.</p>\n</div>|\nSummary Type: manual|\nSummary Truncated: true|",
	)
}

func TestPageMarkupWithoutSummaryAsciidoc(t *testing.T) {
	t.Parallel()
	if !asciidocext.Supports() {
		t.Skip("Skip asiidoc test as not supported")
	}

	files := `
-- hugo.toml --
summaryLength=5
[security.exec]
allow = ["asciidoc", "python"]

-- content/p1.ad --
This is a story about a summary and more.

Another paragraph.
-- content/p2.ad --
This is summary.
<!--more-->
This is content.
-- layouts/_default/single.html --
Single.
Page.Summary: {{ .Summary }}|
{{ with .Markup.Render }}
Content: {{ .Content }}|
ContentWithoutSummary: {{ .ContentWithoutSummary }}|
{{ with .Summary }}
Summary: {{ . }}|
Summary Type: {{ .Type }}|
Summary Truncated: {{ .Truncated }}|
{{ end }}
{{ end }}

`

	b := hugolib.Test(t, files)

	// Auto summary.
	b.AssertFileContentExact("public/p1/index.html",
		"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|",
		"Summary: <div class=\"paragraph\">\n<p>This is a story about a summary and more.</p>\n</div>|",
		"Summary Type: auto|\nSummary Truncated: true|",
		"ContentWithoutSummary: <div class=\"paragraph\">\n<p>Another paragraph.</p>\n</div>|",
	)

	// Manual summary.
	b.AssertFileContentExact("public/p2/index.html",
		"Content: <div class=\"paragraph\">\n<p>This is summary.</p>\n</div>\n<div class=\"paragraph\">\n<p>This is content.</p>\n</div>|",
		"ContentWithoutSummary: <div class=\"paragraph\">\n<p>This is content.</p>\n</div>|",
		"Summary: <div class=\"paragraph\">\n<p>This is summary.</p>\n</div>|\nSummary Type: manual|\nSummary Truncated: true|",
	)
}