shithub: hugo

Download patch

ref: 56550d1e449f45ebee398ac8a9e3b9818b3ee60e
parent: 7881b0965f8b83d03379e9ed102cd0c3bce297e2
author: Bjørn Erik Pedersen <[email protected]>
date: Mon Apr 15 11:17:46 EDT 2019

hugolib: Fix shortcode namespace issue

Fixes #5863

--- a/hugolib/shortcode_test.go
+++ b/hugolib/shortcode_test.go
@@ -1126,3 +1126,32 @@
 
 	}
 }
+
+// https://github.com/gohugoio/hugo/issues/5863
+func TestShortcodeNamespaced(t *testing.T) {
+	t.Parallel()
+	assert := require.New(t)
+
+	builder := newTestSitesBuilder(t).WithSimpleConfigFile()
+
+	builder.WithContent("page.md", `---
+title: "Hugo Rocks!"
+---
+
+# doc
+
+   hello: {{< hello >}}
+   test/hello: {{< test/hello >}}
+
+`).WithTemplatesAdded(
+		"layouts/shortcodes/hello.html", `hello`,
+		"layouts/shortcodes/test/hello.html", `test/hello`).CreateSites().Build(BuildCfg{})
+
+	s := builder.H.Sites[0]
+	assert.Equal(1, len(s.RegularPages()))
+
+	builder.AssertFileContent("public/page/index.html",
+		"hello: hello",
+		"test/hello: test/hello",
+	)
+}
--- a/tpl/tplimpl/shortcodes.go
+++ b/tpl/tplimpl/shortcodes.go
@@ -152,9 +152,9 @@
 }
 
 func isShortcode(name string) bool {
-	return strings.Contains(name, "shortcodes/")
+	return strings.Contains(name, shortcodesPathPrefix)
 }
 
 func isInternal(name string) bool {
-	return strings.HasPrefix(name, "_internal/")
+	return strings.HasPrefix(name, internalPathPrefix)
 }
--- a/tpl/tplimpl/shortcodes_test.go
+++ b/tpl/tplimpl/shortcodes_test.go
@@ -85,8 +85,12 @@
 
 	})
 
-	t.Run("Template", func(t *testing.T) {
+	t.Run("Name", func(t *testing.T) {
 		assert := require.New(t)
+
+		assert.Equal("foo.html", templateBaseName(templateShortcode, "shortcodes/foo.html"))
+		assert.Equal("foo.html", templateBaseName(templateShortcode, "_internal/shortcodes/foo.html"))
+		assert.Equal("test/foo.html", templateBaseName(templateShortcode, "shortcodes/test/foo.html"))
 
 		assert.True(true)
 
--- a/tpl/tplimpl/template.go
+++ b/tpl/tplimpl/template.go
@@ -16,7 +16,6 @@
 import (
 	"fmt"
 	"html/template"
-	"path"
 	"strings"
 	texttemplate "text/template"
 
@@ -112,9 +111,28 @@
 	*deps.Deps
 }
 
+const (
+	shortcodesPathPrefix = "shortcodes/"
+	internalPathPrefix   = "_internal/"
+)
+
+// resolves _internal/shortcodes/param.html => param.html etc.
+func templateBaseName(typ templateType, name string) string {
+	name = strings.TrimPrefix(name, internalPathPrefix)
+	switch typ {
+	case templateShortcode:
+		return strings.TrimPrefix(name, shortcodesPathPrefix)
+	default:
+		panic("not implemented")
+	}
+
+}
+
 func (t *templateHandler) addShortcodeVariant(name string, info tpl.Info, templ tpl.Template) {
-	shortcodename, variants := templateNameAndVariants(path.Base(name))
+	base := templateBaseName(templateShortcode, name)
 
+	shortcodename, variants := templateNameAndVariants(base)
+
 	templs, found := t.shortcodes[shortcodename]
 	if !found {
 		templs = &shortcodeTemplates{}
@@ -204,7 +222,7 @@
 // This currently only applies to shortcodes and what we get here is the
 // shortcode name.
 func (t *templateHandler) LookupVariant(name string, variants tpl.TemplateVariants) (tpl.Template, bool, bool) {
-	name = path.Base(name)
+	name = templateBaseName(templateShortcode, name)
 	s, found := t.shortcodes[name]
 	if !found {
 		return nil, false, false