Skip to content

Commit cf9e690

Browse files
committed
tpl: Fix the case for a shortcode in a nested folder only
Fixes #13605
1 parent 8a2830f commit cf9e690

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

Diff for: hugolib/shortcode.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -677,13 +677,7 @@ Loop:
677677

678678
// Used to check if the template expects inner content,
679679
// so just pick one arbitrarily with the same name.
680-
q := tplimpl.TemplateQuery{
681-
Path: "",
682-
Name: sc.name,
683-
Category: tplimpl.CategoryShortcode,
684-
Consider: nil,
685-
}
686-
templ := s.s.TemplateStore.LookupShortcode(q)
680+
templ := s.s.TemplateStore.LookupShortcodeByName(sc.name)
687681
if templ == nil {
688682
return nil, fmt.Errorf("%s: template for shortcode %q not found", errorPrefix, sc.name)
689683
}

Diff for: tpl/tplimpl/templatestore.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ func NewStore(opts StoreOptions, siteOpts SiteOptions) (*TemplateStore, error) {
122122
treeMain: doctree.NewSimpleTree[map[nodeKey]*TemplInfo](),
123123
treeShortcodes: doctree.NewSimpleTree[map[string]map[TemplateDescriptor]*TemplInfo](),
124124
templatesByPath: maps.NewCache[string, *TemplInfo](),
125+
shortcodesByName: maps.NewCache[string, *TemplInfo](),
125126
cacheLookupPartials: maps.NewCache[string, *TemplInfo](),
126127

127128
// Note that the funcs passed below is just for name validation.
@@ -419,9 +420,10 @@ type TemplateStore struct {
419420
siteOpts SiteOptions
420421
htmlFormat output.Format
421422

422-
treeMain *doctree.SimpleTree[map[nodeKey]*TemplInfo]
423-
treeShortcodes *doctree.SimpleTree[map[string]map[TemplateDescriptor]*TemplInfo]
424-
templatesByPath *maps.Cache[string, *TemplInfo]
423+
treeMain *doctree.SimpleTree[map[nodeKey]*TemplInfo]
424+
treeShortcodes *doctree.SimpleTree[map[string]map[TemplateDescriptor]*TemplInfo]
425+
templatesByPath *maps.Cache[string, *TemplInfo]
426+
shortcodesByName *maps.Cache[string, *TemplInfo]
425427

426428
dh descriptorHandler
427429

@@ -576,6 +578,15 @@ func (s *TemplateStore) LookupPartial(pth string) *TemplInfo {
576578
return ti
577579
}
578580

581+
func (s *TemplateStore) LookupShortcodeByName(name string) *TemplInfo {
582+
name = strings.ToLower(name)
583+
ti, _ := s.shortcodesByName.Get(name)
584+
if ti == nil {
585+
return nil
586+
}
587+
return ti
588+
}
589+
579590
func (s *TemplateStore) LookupShortcode(q TemplateQuery) *TemplInfo {
580591
q.init()
581592
k1 := s.key(q.Path)
@@ -1039,6 +1050,7 @@ func (s *TemplateStore) insertShortcode(pi *paths.Path, fi hugofs.FileMetaInfo,
10391050

10401051
m1[d] = ti
10411052

1053+
s.shortcodesByName.Set(k2, ti)
10421054
s.setTemplateByPath(pi.Path(), ti)
10431055

10441056
if fi != nil {

Diff for: tpl/tplimpl/templatestore_integration_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,28 @@ single.html
11271127
b.AssertFileContent("public/s3/index.html", "single.html") // fail
11281128
}
11291129

1130+
func TestIssue13605(t *testing.T) {
1131+
t.Parallel()
1132+
1133+
files := `
1134+
-- hugo.toml --
1135+
disableKinds = ['home','rss','section','sitemap','taxonomy','term']
1136+
-- content/s1/p1.md --
1137+
---
1138+
title: p1
1139+
---
1140+
{{< sc >}}
1141+
-- layouts/s1/_shortcodes/sc.html --
1142+
layouts/s1/_shortcodes/sc.html
1143+
-- layouts/single.html --
1144+
{{ .Content }}
1145+
`
1146+
1147+
b := hugolib.Test(t, files)
1148+
1149+
b.AssertFileContent("public/s1/p1/index.html", "layouts/s1/_shortcodes/sc.html")
1150+
}
1151+
11301152
func TestSkipDotFiles(t *testing.T) {
11311153
t.Parallel()
11321154

0 commit comments

Comments
 (0)