github.com/neohugo/neohugo@v0.123.8/hugolib/content_render_hooks_test.go (about) 1 // Copyright 2019 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 "strings" 18 "testing" 19 ) 20 21 func TestRenderHooksRSS(t *testing.T) { 22 files := ` 23 -- hugo.toml -- 24 baseURL = "https://example.org" 25 disableKinds = ["taxonomy", "term"] 26 -- layouts/index.html -- 27 {{ $p := site.GetPage "p1.md" }} 28 {{ $p2 := site.GetPage "p2.md" }} 29 P1: {{ $p.Content }} 30 P2: {{ $p2.Content }} 31 -- layouts/index.xml -- 32 {{ $p2 := site.GetPage "p2.md" }} 33 {{ $p3 := site.GetPage "p3.md" }} 34 P2: {{ $p2.Content }} 35 P3: {{ $p3.Content }} 36 -- layouts/_default/_markup/render-link.html -- 37 html-link: {{ .Destination | safeURL }}| 38 -- layouts/_default/_markup/render-link.rss.xml -- 39 xml-link: {{ .Destination | safeURL }}| 40 -- layouts/_default/_markup/render-heading.html -- 41 html-heading: {{ .Text }}| 42 -- layouts/_default/_markup/render-heading.rss.xml -- 43 xml-heading: {{ .Text }}| 44 -- content/p1.md -- 45 --- 46 title: "p1" 47 --- 48 P1. [I'm an inline-style link](https://www.gohugo.io) 49 50 # Heading in p1 51 52 -- content/p2.md -- 53 --- 54 title: "p2" 55 --- 56 P2. [I'm an inline-style link](https://www.bep.is) 57 58 # Heading in p2 59 60 -- content/p3.md -- 61 --- 62 title: "p3" 63 outputs: ["rss"] 64 --- 65 P3. [I'm an inline-style link](https://www.example.org) 66 ` 67 b := Test(t, files) 68 69 b.AssertFileContent("public/index.html", ` 70 P1: <p>P1. html-link: https://www.gohugo.io|</p> 71 html-heading: Heading in p1| 72 html-heading: Heading in p2| 73 `) 74 b.AssertFileContent("public/index.xml", ` 75 P2: <p>P2. xml-link: https://www.bep.is|</p> 76 P3: <p>P3. xml-link: https://www.example.org|</p> 77 xml-heading: Heading in p2| 78 `) 79 } 80 81 // https://github.com/gohugoio/hugo/issues/6629 82 func TestRenderLinkWithMarkupInText(t *testing.T) { 83 b := newTestSitesBuilder(t) 84 b.WithConfigFile("toml", ` 85 86 baseURL="https://example.org" 87 88 [markup] 89 [markup.goldmark] 90 [markup.goldmark.renderer] 91 unsafe = true 92 93 `) 94 95 b.WithTemplates("index.html", ` 96 {{ $p := site.GetPage "p1.md" }} 97 P1: {{ $p.Content }} 98 99 `, 100 "_default/_markup/render-link.html", `html-link: {{ .Destination | safeURL }}|Text: {{ .Text | safeHTML }}|Plain: {{ .PlainText | safeHTML }}`, 101 "_default/_markup/render-image.html", `html-image: {{ .Destination | safeURL }}|Text: {{ .Text | safeHTML }}|Plain: {{ .PlainText | safeHTML }}`, 102 ) 103 104 b.WithContent("p1.md", `--- 105 title: "p1" 106 --- 107 108 START: [**should be bold**](https://gohugo.io)END 109 110 Some regular **markup**. 111 112 Image: 113 114 ![Hello<br> Goodbye](image.jpg)END 115 116 `) 117 118 b.Build(BuildCfg{}) 119 120 b.AssertFileContent("public/index.html", ` 121 P1: <p>START: html-link: https://gohugo.io|Text: <strong>should be bold</strong>|Plain: should be boldEND</p> 122 <p>Some regular <strong>markup</strong>.</p> 123 <p>html-image: image.jpg|Text: Hello<br> Goodbye|Plain: Hello GoodbyeEND</p> 124 `) 125 } 126 127 func TestRenderHookContentFragmentsOnSelf(t *testing.T) { 128 files := ` 129 -- hugo.toml -- 130 baseURL = "https://example.org" 131 disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT"] 132 -- content/p1.md -- 133 --- 134 title: "p1" 135 --- 136 137 ## A {#z} 138 ## B 139 ## C 140 141 -- content/p2.md -- 142 --- 143 title: "p2" 144 --- 145 146 ## D 147 ## E 148 ## F 149 150 -- layouts/_default/_markup/render-heading.html -- 151 Heading: {{ .Text }}| 152 {{ with .Page }} 153 Self Fragments: {{ .Fragments.Identifiers }}| 154 {{ end }} 155 {{ with (site.GetPage "p1.md") }} 156 P1 Fragments: {{ .Fragments.Identifiers }}| 157 {{ end }} 158 -- layouts/_default/single.html -- 159 {{ .Content}} 160 ` 161 162 b := Test(t, files) 163 164 b.AssertFileContent("public/p1/index.html", ` 165 Self Fragments: [b c z] 166 P1 Fragments: [b c z] 167 `) 168 b.AssertFileContent("public/p2/index.html", ` 169 Self Fragments: [d e f] 170 P1 Fragments: [b c z] 171 `) 172 } 173 174 func TestDefaultRenderHooksMultilingual(t *testing.T) { 175 files := ` 176 -- hugo.toml -- 177 baseURL = "https://example.org" 178 disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT"] 179 defaultContentLanguage = "nn" 180 defaultContentLanguageInSubdir = true 181 [markup] 182 [markup.goldmark] 183 duplicateResourceFiles = false 184 [markup.goldmark.renderhooks] 185 [markup.goldmark.renderhooks.link] 186 #enableDefault = false 187 [markup.goldmark.renderhooks.image] 188 #enableDefault = false 189 [languages] 190 [languages.en] 191 weight = 1 192 [languages.nn] 193 weight = 2 194 -- content/p1/index.md -- 195 --- 196 title: "p1" 197 --- 198 [P2](p2) 199 ![Pixel](pixel.png) 200 -- content/p2/index.md -- 201 --- 202 title: "p2" 203 --- 204 [P1](p1) 205 ![Pixel](pixel.jpg) 206 -- content/p1/index.en.md -- 207 --- 208 title: "p1 en" 209 --- 210 [P2](p2) 211 ![Pixel](pixel.png) 212 -- content/p2/index.en.md -- 213 --- 214 title: "p2 en" 215 --- 216 [P1](p1) 217 ![Pixel](pixel.png) 218 219 -- content/p1/pixel.nn.png -- 220 iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg== 221 -- content/p2/pixel.png -- 222 iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg== 223 -- layouts/_default/single.html -- 224 {{ .Title }}|{{ .Content }}|$ 225 226 ` 227 228 t.Run("Default multilingual", func(t *testing.T) { 229 b := Test(t, files) 230 231 b.AssertFileContent("public/nn/p1/index.html", 232 "p1|<p><a href=\"/nn/p2/\">P2</a\n></p>", "<img alt=\"Pixel\" src=\"/nn/p1/pixel.nn.png\">") 233 b.AssertFileContent("public/en/p1/index.html", 234 "p1 en|<p><a href=\"/en/p2/\">P2</a\n></p>", "<img alt=\"Pixel\" src=\"/nn/p1/pixel.nn.png\">") 235 }) 236 237 t.Run("Disabled", func(t *testing.T) { 238 b := Test(t, strings.ReplaceAll(files, "#enableDefault = false", "enableDefault = false")) 239 240 b.AssertFileContent("public/nn/p1/index.html", 241 "p1|<p><a href=\"p2\">P2</a>", "<img src=\"pixel.png\" alt=\"Pixel\">") 242 }) 243 }