Allow hook template per section/type
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 2 Jun 2020 16:08:17 +0000 (18:08 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 2 Jun 2020 17:02:10 +0000 (19:02 +0200)
Fixes #7349

docs/content/en/getting-started/configuration-markup.md
hugolib/content_render_hooks_test.go
output/layout.go
output/layout_test.go

index df4449bbf40f53e550e9b98426877237991e804b..99d1e989d52634066284fceaad6e2c176ef80a97 100644 (file)
@@ -87,6 +87,8 @@ Note that this is only supported with the [Goldmark](#goldmark) renderer.
 
 Render Hooks allow custom templates to override markdown rendering functionality. You can do this by creating templates with base names `render-{feature}` in `layouts/_default/_markup`.
 
+You can also create type/section specific hooks in `layouts/[type/section]/_markup`, e.g.: `layouts/blog/_markup`.{{< new-in "0.71.0" >}}
+
 The features currently supported are:
 
 * `image`
index f161a46a268e987c7cd28a52bb86f5629f12d9b4..17d273a33fdd50e32ab5a150a2a8c59a6bcdf240 100644 (file)
@@ -50,6 +50,7 @@ Inner Block: {{ .Inner | .Page.RenderString (dict "display" "block" ) }}
        b.WithTemplatesAdded("docs/_markup/render-link.html", `Link docs section: {{ .Text | safeHTML }}|END`)
        b.WithTemplatesAdded("_default/_markup/render-image.html", `IMAGE: {{ .Page.Title }}||{{ .Destination | safeURL }}|Title: {{ .Title | safeHTML }}|Text: {{ .Text | safeHTML }}|END`)
        b.WithTemplatesAdded("_default/_markup/render-heading.html", `HEADING: {{ .Page.Title }}||Level: {{ .Level }}|Anchor: {{ .Anchor | safeURL }}|Text: {{ .Text | safeHTML }}|END`)
+       b.WithTemplatesAdded("docs/_markup/render-heading.html", `Docs Level: {{ .Level }}|END`)
 
        b.WithContent("customview/p1.md", `---
 title: Custom View
@@ -133,7 +134,15 @@ some text
 ## Heading Level 2
 
 ### Heading Level 3
-`)
+`,
+               "docs/p8.md", `---
+title: Doc With Heading
+---
+
+# Docs lvl 1
+
+`,
+       )
 
        for i := 1; i <= 30; i++ {
                // Add some content with no shortcodes or links, i.e no templates needed.
@@ -146,7 +155,7 @@ title: No Template
        }
        counters := &testCounters{}
        b.Build(BuildCfg{testCounters: counters})
-       b.Assert(int(counters.contentRenderCounter), qt.Equals, 44)
+       b.Assert(int(counters.contentRenderCounter), qt.Equals, 45)
 
        b.AssertFileContent("public/blog/p1/index.html", `
 <p>Cool Page|https://www.google.com|Title: Google's Homepage|Text: First Link|END</p>
@@ -194,6 +203,10 @@ SHORT3|
        b.AssertFileContent("public/blog/p4/index.html", `IMAGE EDITED: /images/Dragster.jpg|`)
        b.AssertFileContent("public/blog/p6/index.html", "<p>Inner Link: EDITED: https://www.gohugo.io|</p>")
        b.AssertFileContent("public/blog/p7/index.html", "HEADING: With Headings||Level: 1|Anchor: heading-level-1|Text: Heading Level 1|END<p>some text</p>\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) {
index e59404684138d8b6847f8c9d11ea81d745e090ed..09ac7b2f62ab2b5edcbafe9278fc18a137758def 100644 (file)
@@ -134,15 +134,16 @@ func resolvePageTemplate(d LayoutDescriptor, f Format) []string {
 
        b := &layoutBuilder{d: d, f: f}
 
+       if !d.RenderingHook && d.Layout != "" {
+               b.addLayoutVariations(d.Layout)
+       }
+       if d.Type != "" {
+               b.addTypeVariations(d.Type)
+       }
+
        if d.RenderingHook {
                b.addLayoutVariations(d.Kind)
-       } else {
-               if d.Layout != "" {
-                       b.addLayoutVariations(d.Layout)
-               }
-               if d.Type != "" {
-                       b.addTypeVariations(d.Type)
-               }
+               b.addSectionType()
        }
 
        switch d.Kind {
index 8f26bb6c48c3459df646e924fb28e95924ed7d7b..d5b6c7bcaeb2ca163fac6305b9737cb60b318ded 100644 (file)
@@ -128,9 +128,8 @@ func TestLayout(t *testing.T) {
                        []string{"404.html.html", "404.html"}, 2},
                {"404, HTML baseof", LayoutDescriptor{Kind: "404", Baseof: true}, "", htmlFormat,
                        []string{"404-baseof.html.html", "baseof.html.html", "404-baseof.html", "baseof.html", "_default/404-baseof.html.html", "_default/baseof.html.html", "_default/404-baseof.html", "_default/baseof.html"}, 8},
-               // We may add type support ... later.
                {"Content hook", LayoutDescriptor{Kind: "render-link", RenderingHook: true, Layout: "mylayout", Section: "blog"}, "", ampType,
-                       []string{"_default/_markup/render-link.amp.html", "_default/_markup/render-link.html"}, 2},
+                       []string{"blog/_markup/render-link.amp.html", "blog/_markup/render-link.html", "_default/_markup/render-link.amp.html", "_default/_markup/render-link.html"}, 4},
        } {
                c.Run(this.name, func(c *qt.C) {
                        l := NewLayoutHandler()