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