| 1 |
// Copyright 2025 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 requiredF 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 hugolib |
| 15 |
|
| 16 |
import ( |
| 17 |
"testing" |
| 18 |
|
| 19 |
qt "github.com/frankban/quicktest" |
| 20 |
) |
| 21 |
|
| 22 |
func TestRenderString(t *testing.T) { |
| 23 |
t.Parallel() |
| 24 |
|
| 25 |
files := ` |
| 26 |
-- hugo.toml -- |
| 27 |
baseURL = "http://example.com/" |
| 28 |
-- layouts/home.html -- |
| 29 |
{{ $p := site.GetPage "p1.md" }} |
| 30 |
{{ $optBlock := dict "display" "block" }} |
| 31 |
{{ $optOrg := dict "markup" "org" }} |
| 32 |
RSTART:{{ "**Bold Markdown**" | $p.RenderString }}:REND |
| 33 |
RSTART:{{ "**Bold Block Markdown**" | $p.RenderString $optBlock }}:REND |
| 34 |
RSTART:{{ "/italic org mode/" | $p.RenderString $optOrg }}:REND |
| 35 |
RSTART:{{ "## Header2" | $p.RenderString }}:REND |
| 36 |
-- layouts/_markup/render-heading.html -- |
| 37 |
Hook Heading: {{ .Level }} |
| 38 |
-- content/p1.md -- |
| 39 |
--- |
| 40 |
title: "p1" |
| 41 |
--- |
| 42 |
` |
| 43 |
b := Test(t, files) |
| 44 |
|
| 45 |
b.AssertFileContent("public/index.html", ` |
| 46 |
RSTART:<strong>Bold Markdown</strong>:REND |
| 47 |
RSTART:<p><strong>Bold Block Markdown</strong></p> |
| 48 |
RSTART:<em>italic org mode</em>:REND |
| 49 |
RSTART:Hook Heading: 2:REND |
| 50 |
`) |
| 51 |
} |
| 52 |
|
| 53 |
// https://github.com/gohugoio/hugo/issues/6882 |
| 54 |
func TestRenderStringOnListPage(t *testing.T) { |
| 55 |
t.Parallel() |
| 56 |
|
| 57 |
files := ` |
| 58 |
-- hugo.toml -- |
| 59 |
baseURL = "http://example.com/" |
| 60 |
-- layouts/home.html -- |
| 61 |
{{ .RenderString "**Hello**" }} |
| 62 |
-- layouts/list.html -- |
| 63 |
{{ .RenderString "**Hello**" }} |
| 64 |
-- layouts/single.html -- |
| 65 |
{{ .RenderString "**Hello**" }} |
| 66 |
-- content/mysection/p1.md -- |
| 67 |
FOO |
| 68 |
` |
| 69 |
b := Test(t, files) |
| 70 |
|
| 71 |
for _, filename := range []string{ |
| 72 |
"index.html", |
| 73 |
"mysection/index.html", |
| 74 |
"categories/index.html", |
| 75 |
"tags/index.html", |
| 76 |
"mysection/p1/index.html", |
| 77 |
} { |
| 78 |
b.AssertFileContent("public/"+filename, `<strong>Hello</strong>`) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
// Issue 9433 |
| 83 |
func TestRenderStringOnPageNotBackedByAFile(t *testing.T) { |
| 84 |
t.Parallel() |
| 85 |
|
| 86 |
files := ` |
| 87 |
-- hugo.toml -- |
| 88 |
disableKinds = ["page", "section", "taxonomy", "term"] |
| 89 |
-- layouts/home.html -- |
| 90 |
{{ .RenderString "**Hello**" }} |
| 91 |
-- content/p1.md -- |
| 92 |
` |
| 93 |
b, err := TestE(t, files) // Removed WithLogger(logger) |
| 94 |
b.Assert(err, qt.IsNil) |
| 95 |
} |
| 96 |
|
| 97 |
func TestRenderStringWithShortcode(t *testing.T) { |
| 98 |
t.Parallel() |
| 99 |
|
| 100 |
filesTemplate := ` |
| 101 |
-- hugo.toml -- |
| 102 |
title = "Hugo Rocks!" |
| 103 |
enableInlineShortcodes = true |
| 104 |
-- content/p1/index.md -- |
| 105 |
--- |
| 106 |
title: "P1" |
| 107 |
--- |
| 108 |
## First |
| 109 |
-- layouts/_shortcodes/mark1.md -- |
| 110 |
{{ .Inner }} |
| 111 |
-- layouts/_shortcodes/mark2.md -- |
| 112 |
1. Item Mark2 1 |
| 113 |
1. Item Mark2 2 |
| 114 |
1. Item Mark2 2-1 |
| 115 |
1. Item Mark2 3 |
| 116 |
-- layouts/_shortcodes/myhthml.html -- |
| 117 |
Title: {{ .Page.Title }} |
| 118 |
TableOfContents: {{ .Page.TableOfContents }} |
| 119 |
Page Type: {{ printf "%T" .Page }} |
| 120 |
-- layouts/single.html -- |
| 121 |
{{ .RenderString "Markdown: {{% mark2 %}}|HTML: {{< myhthml >}}|Inline: {{< foo.inline >}}{{ site.Title }}{{< /foo.inline >}}|" }} |
| 122 |
HasShortcode: mark2:{{ .HasShortcode "mark2" }}:true |
| 123 |
HasShortcode: foo:{{ .HasShortcode "foo" }}:false |
| 124 |
|
| 125 |
` |
| 126 |
|
| 127 |
t.Run("Basic", func(t *testing.T) { |
| 128 |
b := Test(t, filesTemplate) |
| 129 |
|
| 130 |
b.AssertFileContent("public/p1/index.html", |
| 131 |
"<p>Markdown: 1. Item Mark2 1</p>\n<ol>\n<li>Item Mark2 2\n<ol>\n<li>Item Mark2 2-1</li>\n</ol>\n</li>\n<li>Item Mark2 3|", |
| 132 |
"<a href=\"#first\">First</a>", // ToC |
| 133 |
` |
| 134 |
HTML: Title: P1 |
| 135 |
Inline: Hugo Rocks! |
| 136 |
HasShortcode: mark2:true:true |
| 137 |
HasShortcode: foo:false:false |
| 138 |
Page Type: *hugolib.pageForShortcode`, |
| 139 |
) |
| 140 |
}) |
| 141 |
|
| 142 |
t.Run("Edit shortcode", func(t *testing.T) { |
| 143 |
b := TestRunning(t, filesTemplate) |
| 144 |
|
| 145 |
b.EditFiles("layouts/_shortcodes/myhthml.html", "Edit shortcode").Build() |
| 146 |
|
| 147 |
b.AssertFileContent("public/p1/index.html", |
| 148 |
`Edit shortcode`, |
| 149 |
) |
| 150 |
}) |
| 151 |
} |
| 152 |
|
| 153 |
// Issue 9959 |
| 154 |
func TestRenderStringWithShortcodeInPageWithNoContentFile(t *testing.T) { |
| 155 |
t.Parallel() |
| 156 |
|
| 157 |
files := ` |
| 158 |
-- hugo.toml -- |
| 159 |
-- layouts/_shortcodes/myshort.html -- |
| 160 |
Page Kind: {{ .Page.Kind }} |
| 161 |
-- layouts/home.html -- |
| 162 |
Short: {{ .RenderString "{{< myshort >}}" }} |
| 163 |
Has myshort: {{ .HasShortcode "myshort" }} |
| 164 |
Has other: {{ .HasShortcode "other" }} |
| 165 |
|
| 166 |
` |
| 167 |
|
| 168 |
b := Test(t, files) |
| 169 |
|
| 170 |
b.AssertFileContent("public/index.html", |
| 171 |
` |
| 172 |
Page Kind: home |
| 173 |
Has myshort: true |
| 174 |
Has other: false |
| 175 |
`) |
| 176 |
} |
| 177 |
|
| 178 |
func TestRenderStringWithShortcodeIssue10654(t *testing.T) { |
| 179 |
t.Parallel() |
| 180 |
|
| 181 |
files := ` |
| 182 |
-- hugo.toml -- |
| 183 |
timeout = '300ms' |
| 184 |
-- content/p1.md -- |
| 185 |
--- |
| 186 |
title: "P1" |
| 187 |
--- |
| 188 |
{{< toc >}} |
| 189 |
|
| 190 |
## Heading 1 |
| 191 |
|
| 192 |
{{< noop >}} |
| 193 |
{{ not a shortcode |
| 194 |
{{< /noop >}} |
| 195 |
} |
| 196 |
-- layouts/_shortcodes/noop.html -- |
| 197 |
{{ .Inner | $.Page.RenderString }} |
| 198 |
-- layouts/_shortcodes/toc.html -- |
| 199 |
{{ .Page.TableOfContents }} |
| 200 |
-- layouts/single.html -- |
| 201 |
{{ .Content }} |
| 202 |
` |
| 203 |
|
| 204 |
b := Test(t, files) |
| 205 |
|
| 206 |
b.AssertFileContent("public/p1/index.html", `TableOfContents`) |
| 207 |
} |