// 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" "testing" qt "github.com/frankban/quicktest" ) func TestRenderHookEditNestedPartial(t *testing.T) { config := ` baseURL="https://example.org" workingDir="/mywork" ` b := newTestSitesBuilder(t).WithWorkingDir("/mywork").WithConfigFile("toml", config).Running() b.WithTemplates("_default/single.html", "{{ .Content }}") b.WithTemplates("partials/mypartial1.html", `PARTIAL1 {{ partial "mypartial2.html" }}`) b.WithTemplates("partials/mypartial2.html", `PARTIAL2`) b.WithTemplates("_default/_markup/render-link.html", `Link {{ .Text | safeHTML }}|{{ partial "mypartial1.html" . }}END`) b.WithContent("p1.md", `--- title: P1 --- [First Link](https://www.google.com "Google's Homepage") `) b.Build(BuildCfg{}) b.AssertFileContent("public/p1/index.html", `Link First Link|PARTIAL1 PARTIAL2END`) b.EditFiles("layouts/partials/mypartial1.html", `PARTIAL1_EDITED {{ partial "mypartial2.html" }}`) b.Build(BuildCfg{}) b.AssertFileContent("public/p1/index.html", `Link First Link|PARTIAL1_EDITED PARTIAL2END`) b.EditFiles("layouts/partials/mypartial2.html", `PARTIAL2_EDITED`) b.Build(BuildCfg{}) b.AssertFileContent("public/p1/index.html", `Link First Link|PARTIAL1_EDITED PARTIAL2_EDITEDEND`) } func TestRenderHooks(t *testing.T) { config := ` baseURL="https://example.org" workingDir="/mywork" ` b := newTestSitesBuilder(t).WithWorkingDir("/mywork").WithConfigFile("toml", config).Running() b.WithTemplatesAdded("_default/single.html", `{{ .Content }}`) b.WithTemplatesAdded("shortcodes/myshortcode1.html", `{{ partial "mypartial1" }}`) b.WithTemplatesAdded("shortcodes/myshortcode2.html", `{{ partial "mypartial2" }}`) b.WithTemplatesAdded("shortcodes/myshortcode3.html", `SHORT3|`) b.WithTemplatesAdded("shortcodes/myshortcode4.html", `
Cool Page|https://www.google.com|Title: Google's Homepage|Text: First Link|END
Text: Second SHORT3|IMAGE: Cool Page||/images/Dragster.jpg|Title: image title|Text: Drag Racing|END
`) b.AssertFileContent("public/customview/p1/index.html", `.Render: myrender: Custom View|P4: PARTIAL4`) b.AssertFileContent("public/blog/p2/index.html", `PARTIAL ,Cool Page2|https://gohugo.io|Title: |Text: No Whitespace Please|END,`, ) b.AssertFileContent("public/blog/p3/index.html", `PARTIAL3`) // We may add type template support later, keep this for then. b.AssertFileContent("public/docs/docs1/index.html", `Link docs section: Docs 1|END`) b.AssertFileContent("public/blog/p4/index.html", `IMAGE: Cool Page With Image||/images/Dragster.jpg|Title: image title|Text: Drag Racing|END
`) // The regular markdownify func currently gets regular links. b.AssertFileContent("public/blog/p5/index.html", "Inner Link: Inner Link\n") b.AssertFileContent("public/blog/p6/index.html", "Inner Inline: Inner Link: With RenderString|https://www.gohugo.io|Title: Hugo's Homepage|Text: Inner Link|END", "Inner Block:Inner Link: With RenderString|https://www.gohugo.io|Title: Hugo's Homepage|Text: Inner Link|END
", ) b.EditFiles( "layouts/_default/_markup/render-link.html", `EDITED: {{ .Destination | safeURL }}|`, "layouts/_default/_markup/render-image.html", `IMAGE EDITED: {{ .Destination | safeURL }}|`, "layouts/docs/_markup/render-link.html", `DOCS EDITED: {{ .Destination | safeURL }}|`, "layouts/partials/mypartial1.html", `PARTIAL1_EDITED`, "layouts/partials/mypartial3.html", `PARTIAL3_EDITED`, "layouts/partials/mypartial4.html", `PARTIAL4_EDITED`, "layouts/shortcodes/myshortcode3.html", `SHORT3_EDITED|`, ) counters = &testCounters{} b.Build(BuildCfg{testCounters: counters}) // Make sure that only content using the changed templates are re-rendered. b.Assert(int(counters.contentRenderCounter), qt.Equals, 7) b.AssertFileContent("public/customview/p1/index.html", `.Render: myrender: Custom View|P4: PARTIAL4_EDITED`) b.AssertFileContent("public/blog/p1/index.html", `EDITED: https://www.google.com|
`, "SHORT3_EDITED|") b.AssertFileContent("public/blog/p2/index.html", `PARTIAL1_EDITED`) b.AssertFileContent("public/blog/p3/index.html", `PARTIAL3_EDITED`) // We may add type template support later, keep this for then. b.AssertFileContent("public/docs/docs1/index.html", `DOCS EDITED: https://www.google.com|`) b.AssertFileContent("public/blog/p4/index.html", `IMAGE EDITED: /images/Dragster.jpg|`) b.AssertFileContent("public/blog/p6/index.html", "Inner Link: EDITED: https://www.gohugo.io|
") b.AssertFileContent("public/blog/p7/index.html", "HEADING: With Headings||Level: 1|Anchor: heading-level-1|Text: Heading Level 1|ENDsome text
\nHEADING: With Headings||Level: 2|Anchor: heading-level-2|Text: Heading Level 2|ENDHEADING: With Headings||Level: 3|Anchor: heading-level-3|Text: Heading Level 3|END") // https://github.com/gohugoio/hugo/issues/7349 b.AssertFileContent("public/docs/p8/index.html", "Docs Level: 1") } func TestRenderHooksDeleteTemplate(t *testing.T) { config := ` baseURL="https://example.org" workingDir="/mywork" ` b := newTestSitesBuilder(t).WithWorkingDir("/mywork").WithConfigFile("toml", config).Running() b.WithTemplatesAdded("_default/single.html", `{{ .Content }}`) b.WithTemplatesAdded("_default/_markup/render-link.html", `html-render-link`) b.WithContent("p1.md", `--- title: P1 --- [First Link](https://www.google.com "Google's Homepage") `) b.Build(BuildCfg{}) b.AssertFileContent("public/p1/index.html", `html-render-link
`) b.RemoveFiles( "layouts/_default/_markup/render-link.html", ) b.Build(BuildCfg{}) b.AssertFileContent("public/p1/index.html", ``) } func TestRenderHookAddTemplate(t *testing.T) { config := ` baseURL="https://example.org" workingDir="/mywork" ` b := newTestSitesBuilder(t).WithWorkingDir("/mywork").WithConfigFile("toml", config).Running() b.WithTemplatesAdded("_default/single.html", `{{ .Content }}`) b.WithContent("p1.md", `--- title: P1 --- [First Link](https://www.google.com "Google's Homepage") `) b.Build(BuildCfg{}) b.AssertFileContent("public/p1/index.html", ``) b.EditFiles("layouts/_default/_markup/render-link.html", `html-render-link`) b.Build(BuildCfg{}) b.AssertFileContent("public/p1/index.html", `html-render-link
`) } func TestRenderHooksRSS(t *testing.T) { b := newTestSitesBuilder(t) b.WithTemplates("index.html", ` {{ $p := site.GetPage "p1.md" }} P1: {{ $p.Content }} `, "index.xml", ` {{ $p2 := site.GetPage "p2.md" }} {{ $p3 := site.GetPage "p3.md" }} P2: {{ $p2.Content }} P3: {{ $p3.Content }} `, "_default/_markup/render-link.html", `html-link: {{ .Destination | safeURL }}|`, "_default/_markup/render-link.rss.xml", `xml-link: {{ .Destination | safeURL }}|`, ) b.WithContent("p1.md", `--- title: "p1" --- P1. [I'm an inline-style link](https://www.gohugo.io) `, "p2.md", `--- title: "p2" --- P1. [I'm an inline-style link](https://www.bep.is) `, "p3.md", `--- title: "p2" outputs: ["rss"] --- P3. [I'm an inline-style link](https://www.example.org) `, ) b.Build(BuildCfg{}) b.AssertFileContent("public/index.html", "P1:P1. html-link: https://www.gohugo.io|
") b.AssertFileContent("public/index.xml", ` P2:P1. xml-link: https://www.bep.is|
P3:P3. xml-link: https://www.example.org|
`) } // 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 | safeHTML }}|Plain: {{ .PlainText | safeHTML }}`, "_default/_markup/render-image.html", `html-image: {{ .Destination | safeURL }}|Text: {{ .Text | safeHTML }}|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
Bold Block Markdown
RSTART:italic org mode:REND RSTART:Hook Heading: 2:REND `) } // https://github.com/gohugoio/hugo/issues/6882 func TestRenderStringOnListPage(t *testing.T) { renderStringTempl := ` {{ .RenderString "**Hello**" }} ` b := newTestSitesBuilder(t) b.WithContent("mysection/p1.md", `FOO`) b.WithTemplates( "index.html", renderStringTempl, "_default/list.html", renderStringTempl, "_default/single.html", renderStringTempl, ) b.Build(BuildCfg{}) for _, filename := range []string{ "index.html", "mysection/index.html", "categories/index.html", "tags/index.html", "mysection/p1/index.html", } { b.AssertFileContent("public/"+filename, `Hello`) } }