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