mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-26 22:04:32 +02:00
Pass .RenderShortcodes' Page to render hooks as .PageInner
The main use case for this is to resolve links and resources (e.g. images) relative to the included `Page`. A typical `include` would similar to this: ```handlebars {{ with site.GetPage (.Get 0) }} {{ .RenderShortcodes }} {{ end }} ``` And when used in a Markdown file: ```markdown {{% include "/posts/p1" %}} ``` Any render hook triggered while rendering `/posts/p1` will get `/posts/p1` when calling `.PageInner`. Note that * This is only relevant for shortcodes included with `{{%` that calls `.RenderShortcodes`. * `.PageInner` is available in all render hooks that, before this commit, received `.Page`. * `.PageInner` will fall back to the value of `.Page` if not relevant and will always have a value. Fixes #12356
This commit is contained in:
@@ -200,3 +200,99 @@ Myshort Original.
|
||||
b.Build()
|
||||
b.AssertFileContent("public/p1/index.html", "Edited")
|
||||
}
|
||||
|
||||
func TestRenderShortcodesNestedPageContextIssue12356(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
files := `
|
||||
-- hugo.toml --
|
||||
disableKinds = ["taxonomy", "term", "rss", "sitemap", "robotsTXT", "404"]
|
||||
-- layouts/_default/_markup/render-image.html --
|
||||
{{- with .PageInner.Resources.Get .Destination -}}Image: {{ .RelPermalink }}|{{- end -}}
|
||||
-- layouts/_default/_markup/render-link.html --
|
||||
{{- with .PageInner.GetPage .Destination -}}Link: {{ .RelPermalink }}|{{- end -}}
|
||||
-- layouts/_default/_markup/render-heading.html --
|
||||
Heading: {{ .PageInner.Title }}: {{ .PlainText }}|
|
||||
-- layouts/_default/_markup/render-codeblock.html --
|
||||
CodeBlock: {{ .PageInner.Title }}: {{ .Type }}|
|
||||
-- layouts/_default/list.html --
|
||||
Content:{{ .Content }}|
|
||||
Fragments: {{ with .Fragments }}{{.Identifiers }}{{ end }}|
|
||||
-- layouts/_default/single.html --
|
||||
Content:{{ .Content }}|
|
||||
-- layouts/shortcodes/include.html --
|
||||
{{ with site.GetPage (.Get 0) }}
|
||||
{{ .RenderShortcodes }}
|
||||
{{ end }}
|
||||
-- content/markdown/_index.md --
|
||||
---
|
||||
title: "Markdown"
|
||||
---
|
||||
# H1
|
||||
|{{% include "/posts/p1" %}}|
|
||||

|
||||
|
||||
§§§go
|
||||
fmt.Println("Hello")
|
||||
§§§
|
||||
|
||||
-- content/markdown2/_index.md --
|
||||
---
|
||||
title: "Markdown 2"
|
||||
---
|
||||
|{{< include "/posts/p1" >}}|
|
||||
-- content/html/_index.html --
|
||||
---
|
||||
title: "HTML"
|
||||
---
|
||||
|{{% include "/posts/p1" %}}|
|
||||
|
||||
-- content/posts/p1/index.md --
|
||||
---
|
||||
title: "p1"
|
||||
---
|
||||
## H2-p1
|
||||

|
||||

|
||||
[p2](p2)
|
||||
|
||||
§§§bash
|
||||
echo "Hello"
|
||||
§§§
|
||||
|
||||
-- content/posts/p2/index.md --
|
||||
---
|
||||
title: "p2"
|
||||
---
|
||||
-- content/posts/p1/pixel1.png --
|
||||
iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
|
||||
-- content/posts/p1/pixel2.png --
|
||||
iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
|
||||
-- content/markdown/pixel3.png --
|
||||
iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
|
||||
-- content/html/pixel4.png --
|
||||
iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
|
||||
|
||||
`
|
||||
|
||||
b := Test(t, files)
|
||||
|
||||
b.AssertFileContent("public/markdown/index.html",
|
||||
// Images.
|
||||
"Image: /posts/p1/pixel1.png|\nImage: /posts/p1/pixel2.png|\n|\nImage: /markdown/pixel3.png|</p>\n|",
|
||||
// Links.
|
||||
"Link: /posts/p2/|",
|
||||
// Code blocks
|
||||
"CodeBlock: p1: bash|", "CodeBlock: Markdown: go|",
|
||||
// Headings.
|
||||
"Heading: Markdown: H1|", "Heading: p1: H2-p1|",
|
||||
// Fragments.
|
||||
"Fragments: [h1 h2-p1]|",
|
||||
// Check that the special context markup is not rendered.
|
||||
"! hugo_ctx",
|
||||
)
|
||||
|
||||
b.AssertFileContent("public/markdown2/index.html", "! hugo_ctx", "Content:<p>|\n \n\n|</p>\n|")
|
||||
|
||||
b.AssertFileContent("public/html/index.html", "! hugo_ctx")
|
||||
}
|
||||
|
Reference in New Issue
Block a user