diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-04-19 18:06:40 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-04-21 22:02:56 +0200 |
commit | 4d26ab33dcef704086f43828d1dfb4b8beae2593 (patch) | |
tree | 986c2a878326238ad62dc13c76ed311fef1a855e /hugolib/hugo_sites_build_test.go | |
parent | d6a2024e6b675ca30629bbbc7e8a6592defc942c (diff) |
Make .Content (almost) always available in shortcodes
This resolves some surprising behaviour when reading other pages' content from shortcodes. Before this commit, that behaviour was undefined. Note that this has never been an issue from regular templates.
It will still not be possible to get **the current shortcode's page's rendered content**. That would have impressed Einstein.
The new and well defined rules are:
* `.Page.Content` from a shortcode will be empty. The related `.Page.Truncated` `.Page.Summary`, `.Page.WordCount`, `.Page.ReadingTime`, `.Page.Plain` and `.Page.PlainWords` will also have empty values.
* For _other pages_ (retrieved via `.Page.Site.GetPage`, `.Site.Pages` etc.) the `.Content` is there to use as you please as long as you don't have infinite content recursion in your shortcode/content setup. See below.
* `.Page.TableOfContents` is good to go (but does not support shortcodes in headlines; this is unchanged)
If you get into a situation of infinite recursion, the `.Content` will be empty. Run `hugo -v` for more information.
Fixes #4632
Fixes #4653
Fixes #4655
Diffstat (limited to 'hugolib/hugo_sites_build_test.go')
-rw-r--r-- | hugolib/hugo_sites_build_test.go | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/hugolib/hugo_sites_build_test.go b/hugolib/hugo_sites_build_test.go index 0515def4e..87eb2cb29 100644 --- a/hugolib/hugo_sites_build_test.go +++ b/hugolib/hugo_sites_build_test.go @@ -378,9 +378,9 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) { b.AssertFileContent("public/en/tags/tag1/index.html", "Tag1|Hello|http://example.com/blog/en/tags/tag1/") // Check Blackfriday config - require.True(t, strings.Contains(string(doc1fr.content), "«"), string(doc1fr.content)) - require.False(t, strings.Contains(string(doc1en.content), "«"), string(doc1en.content)) - require.True(t, strings.Contains(string(doc1en.content), "“"), string(doc1en.content)) + require.True(t, strings.Contains(string(doc1fr.content()), "«"), string(doc1fr.content())) + require.False(t, strings.Contains(string(doc1en.content()), "«"), string(doc1en.content())) + require.True(t, strings.Contains(string(doc1en.content()), "“"), string(doc1en.content())) // Check that the drafts etc. are not built/processed/rendered. assertShouldNotBuild(t, b.H) @@ -630,9 +630,9 @@ func assertShouldNotBuild(t *testing.T, sites *HugoSites) { for _, p := range s.rawAllPages { // No HTML when not processed require.Equal(t, p.shouldBuild(), bytes.Contains(p.workContent, []byte("</")), p.BaseFileName()+": "+string(p.workContent)) - require.Equal(t, p.shouldBuild(), p.content != "", p.BaseFileName()) + require.Equal(t, p.shouldBuild(), p.content() != "", p.BaseFileName()) - require.Equal(t, p.shouldBuild(), p.content != "", p.BaseFileName()) + require.Equal(t, p.shouldBuild(), p.content() != "", p.BaseFileName()) } } @@ -753,6 +753,29 @@ var tocShortcode = ` {{ .Page.TableOfContents }} ` +func TestSelfReferencedContentInShortcode(t *testing.T) { + t.Parallel() + + b := newMultiSiteTestDefaultBuilder(t) + + var ( + shortcode = `{{- .Page.Content -}}{{- .Page.Summary -}}{{- .Page.Plain -}}{{- .Page.PlainWords -}}{{- .Page.WordCount -}}{{- .Page.ReadingTime -}}` + + page = `--- +title: sctest +--- +Empty:{{< mycontent >}}: +` + ) + + b.WithTemplatesAdded("layouts/shortcodes/mycontent.html", shortcode) + b.WithContent("post/simple.en.md", page) + + b.CreateSites().Build(BuildCfg{}) + + b.AssertFileContent("public/en/post/simple/index.html", "Empty:[]00:") +} + var tocPageSimple = `--- title: tocTest publishdate: "2000-01-01" |