// Copyright 2019 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 requiredF 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 hugolib import ( "fmt" "strings" "testing" qt "github.com/frankban/quicktest" ) func TestRenderHooksRSS(t *testing.T) { files := ` -- hugo.toml -- baseURL = "https://example.org" disableKinds = ["taxonomy", "term"] -- layouts/index.html -- {{ $p := site.GetPage "p1.md" }} {{ $p2 := site.GetPage "p2.md" }} P1: {{ $p.Content }} P2: {{ $p2.Content }} -- layouts/index.xml -- {{ $p2 := site.GetPage "p2.md" }} {{ $p3 := site.GetPage "p3.md" }} P2: {{ $p2.Content }} P3: {{ $p3.Content }} -- layouts/_default/_markup/render-link.html -- html-link: {{ .Destination | safeURL }}| -- layouts/_default/_markup/render-link.rss.xml -- xml-link: {{ .Destination | safeURL }}| -- layouts/_default/_markup/render-heading.html -- html-heading: {{ .Text }}| -- layouts/_default/_markup/render-heading.rss.xml -- xml-heading: {{ .Text }}| -- content/p1.md -- --- title: "p1" --- P1. [I'm an inline-style link](https://www.gohugo.io) # Heading in p1 -- content/p2.md -- --- title: "p2" --- P2. [I'm an inline-style link](https://www.bep.is) # Heading in p2 -- content/p3.md -- --- title: "p3" outputs: ["rss"] --- P3. [I'm an inline-style link](https://www.example.org) ` b := Test(t, files) b.AssertFileContent("public/index.html", ` P1:
P1. html-link: https://www.gohugo.io|
html-heading: Heading in p1| html-heading: Heading in p2| `) b.AssertFileContent("public/index.xml", ` P2:P2. xml-link: https://www.bep.is|
P3:P3. xml-link: https://www.example.org|
xml-heading: Heading in p2| `) } // Issue 13242. func TestRenderHooksRSSOnly(t *testing.T) { files := ` -- hugo.toml -- baseURL = "https://example.org" disableKinds = ["taxonomy", "term"] -- layouts/index.html -- {{ $p := site.GetPage "p1.md" }} {{ $p2 := site.GetPage "p2.md" }} P1: {{ $p.Content }} P2: {{ $p2.Content }} -- layouts/index.xml -- {{ $p2 := site.GetPage "p2.md" }} {{ $p3 := site.GetPage "p3.md" }} P2: {{ $p2.Content }} P3: {{ $p3.Content }} -- layouts/_default/_markup/render-link.rss.xml -- xml-link: {{ .Destination | safeURL }}| -- layouts/_default/_markup/render-heading.rss.xml -- xml-heading: {{ .Text }}| -- content/p1.md -- --- title: "p1" --- P1. [I'm an inline-style link](https://www.gohugo.io) # Heading in p1 -- content/p2.md -- --- title: "p2" --- P2. [I'm an inline-style link](https://www.bep.is) # Heading in p2 -- content/p3.md -- --- title: "p3" outputs: ["rss"] --- P3. [I'm an inline-style link](https://www.example.org) ` b := Test(t, files) b.AssertFileContent("public/index.html", ` P1:P2. xml-link: https://www.bep.is|
P3:P3. xml-link: https://www.example.org|
xml-heading: Heading in p2| `) } // https://github.com/gohugoio/hugo/issues/6629 func TestRenderLinkWithMarkupInText(t *testing.T) { b := newTestSitesBuilder(t) b.WithConfigFile("toml", ` baseURL="https://example.org" [markup] [markup.goldmark] [markup.goldmark.renderer] unsafe = true `) b.WithTemplates("index.html", ` {{ $p := site.GetPage "p1.md" }} P1: {{ $p.Content }} `, "_default/_markup/render-link.html", `html-link: {{ .Destination | safeURL }}|Text: {{ .Text }}|Plain: {{ .PlainText | safeHTML }}`, "_default/_markup/render-image.html", `html-image: {{ .Destination | safeURL }}|Text: {{ .Text }}|Plain: {{ .PlainText | safeHTML }}`, ) b.WithContent("p1.md", `--- title: "p1" --- START: [**should be bold**](https://gohugo.io)END Some regular **markup**. Image: ![HelloSTART: html-link: https://gohugo.io|Text: should be bold|Plain: should be boldEND
Some regular markup.
html-image: image.jpg|Text: Hello
Goodbye|Plain: Hello GoodbyeEND
P2", "")
})
}
func TestRenderHooksDefaultEscape(t *testing.T) {
files := `
-- hugo.toml --
[markup.goldmark.extensions.typographer]
disable = true
[markup.goldmark.renderHooks.image]
enableDefault = ENABLE
[markup.goldmark.renderHooks.link]
enableDefault = ENABLE
[markup.goldmark.parser]
wrapStandAloneImageWithinParagraph = false
[markup.goldmark.parser.attribute]
block = true
title = true
-- content/_index.md --
---
title: "Home"
---
Link: [text-"<>&](/destination-"<> 'title-"<>&')
Image: 
{class=">" id="baz"}
-- layouts/index.html --
{{ .Content }}
`
for _, enabled := range []bool{true, false} {
enabled := enabled
t.Run(fmt.Sprint(enabled), func(t *testing.T) {
t.Parallel()
b := Test(t, strings.ReplaceAll(files, "ENABLE", fmt.Sprint(enabled)))
// The escaping is slightly different between the two.
if enabled {
b.AssertFileContent("public/index.html",
"Link: text-"<>&",
"img src=\"/destination-%22%3C%3E\" alt=\"alt-"<>&\" title=\"title-"<>&\">",
"><script>",
)
} else {
b.AssertFileContent("public/index.html",
"Link: text-"<>&",
"Image:
",
)
}
})
}
}
// Issue 13410.
func TestRenderHooksMultilineTitlePlainText(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
-- content/p1.md --
---
title: "p1"
---
First line.
Second line.
----------------
-- layouts/_default/_markup/render-heading.html --
Plain text: {{ .PlainText }}|Text: {{ .Text }}|
-- layouts/_default/single.html --
Content: {{ .Content}}|
}
`
b := Test(t, files)
b.AssertFileContent("public/p1/index.html",
"Content: Plain text: First line.\nSecond line.|",
"|Text: First line.\nSecond line.||\n",
)
}
func TestContentOutputReuseRenderHooksAndShortcodesHTMLOnly(t *testing.T) {
files := `
-- hugo.toml --
-- layouts/index.html --
HTML: {{ .Title }}|{{ .Content }}|
-- layouts/index.xml --
XML: {{ .Title }}|{{ .Content }}|
-- layouts/_markup/render-heading.html --
Render heading.
-- layouts/shortcodes/myshortcode.html --
My shortcode.
-- content/_index.md --
---
title: "Home"
---
## Heading
{{< myshortcode >}}
`
b := Test(t, files)
s := b.H.Sites[0]
b.Assert(s.home.pageOutputTemplateVariationsState.Load(), qt.Equals, uint32(1))
b.AssertFileContent("public/index.html", "HTML: Home|Render heading.\nMy shortcode.\n|")
b.AssertFileContent("public/index.xml", "XML: Home|Render heading.\nMy shortcode.\n|")
}
func TestContentOutputNoReuseRenderHooksInBothHTMLAnXML(t *testing.T) {
files := `
-- hugo.toml --
disableKinds = ["taxonomy", "term"]
-- layouts/index.html --
HTML: {{ .Title }}|{{ .Content }}|
-- layouts/index.xml --
XML: {{ .Title }}|{{ .Content }}|
-- layouts/_markup/render-heading.html --
Render heading.
-- layouts/_markup/render-heading.xml --
Render heading XML.
-- content/_index.md --
---
title: "Home"
---
## Heading
`
b := Test(t, files)
s := b.H.Sites[0]
b.Assert(s.home.pageOutputTemplateVariationsState.Load() > 1, qt.IsTrue)
b.AssertFileContentExact("public/index.xml", "XML: Home|Render heading XML.|")
b.AssertFileContentExact("public/index.html", "HTML: Home|Render heading.|")
}
func TestContentOutputNoReuseShortcodesInBothHTMLAnXML(t *testing.T) {
files := `
-- hugo.toml --
disableKinds = ["taxonomy", "term"]
-- layouts/index.html --
HTML: {{ .Title }}|{{ .Content }}|
-- layouts/index.xml --
XML: {{ .Title }}|{{ .Content }}|
-- layouts/_markup/render-heading.html --
Render heading.
-- layouts/shortcodes/myshortcode.html --
My shortcode HTML.
-- layouts/shortcodes/myshortcode.xml --
My shortcode XML.
-- content/_index.md --
---
title: "Home"
---
## Heading
{{< myshortcode >}}
`
b := Test(t, files)
b.AssertFileContentExact("public/index.xml", "My shortcode XML.")
b.AssertFileContentExact("public/index.html", "My shortcode HTML.")
s := b.H.Sites[0]
b.Assert(s.home.pageOutputTemplateVariationsState.Load() > 1, qt.IsTrue)
}