From 9ad26b69ad091ab51f4010c7cafd1daeed095ae7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 26 May 2025 10:22:13 +0200 Subject: [PATCH] Fix it so e.g. de in layouts/_shortcodes/de.html is not interpreted as a language code Fixes #13740 --- common/paths/pathparser.go | 11 ++++++----- common/paths/pathparser_test.go | 11 ++++++++++- tpl/tplimpl/shortcodes_integration_test.go | 4 ++-- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/common/paths/pathparser.go b/common/paths/pathparser.go index b0a2f9fc4..d10523a84 100644 --- a/common/paths/pathparser.go +++ b/common/paths/pathparser.go @@ -120,11 +120,11 @@ func (pp *PathParser) parse(component, s string) (*Path, error) { return p, nil } -func (pp *PathParser) parseIdentifier(component, s string, p *Path, i, lastDot int) { +func (pp *PathParser) parseIdentifier(component, s string, p *Path, i, lastDot, numDots int) { if p.posContainerHigh != -1 { return } - mayHaveLang := p.posIdentifierLanguage == -1 && pp.LanguageIndex != nil + mayHaveLang := numDots > 1 && p.posIdentifierLanguage == -1 && pp.LanguageIndex != nil mayHaveLang = mayHaveLang && (component == files.ComponentFolderContent || component == files.ComponentFolderLayouts) mayHaveOutputFormat := component == files.ComponentFolderLayouts mayHaveKind := p.posIdentifierKind == -1 && mayHaveOutputFormat @@ -167,7 +167,6 @@ func (pp *PathParser) parseIdentifier(component, s string, p *Path, i, lastDot i if langFound { p.identifiersKnown = append(p.identifiersKnown, id) p.posIdentifierLanguage = len(p.identifiersKnown) - 1 - } } @@ -234,19 +233,21 @@ func (pp *PathParser) doParse(component, s string, p *Path) (*Path, error) { p.s = s slashCount := 0 lastDot := 0 + lastSlashIdx := strings.LastIndex(s, "/") + numDots := strings.Count(s[lastSlashIdx+1:], ".") for i := len(s) - 1; i >= 0; i-- { c := s[i] switch c { case '.': - pp.parseIdentifier(component, s, p, i, lastDot) + pp.parseIdentifier(component, s, p, i, lastDot, numDots) lastDot = i case '/': slashCount++ if p.posContainerHigh == -1 { if lastDot > 0 { - pp.parseIdentifier(component, s, p, i, lastDot) + pp.parseIdentifier(component, s, p, i, lastDot, numDots) } p.posContainerHigh = i + 1 } else if p.posContainerLow == -1 { diff --git a/common/paths/pathparser_test.go b/common/paths/pathparser_test.go index a6194e756..ad76b9367 100644 --- a/common/paths/pathparser_test.go +++ b/common/paths/pathparser_test.go @@ -563,11 +563,20 @@ func TestParseLayouts(t *testing.T) { c.Assert(p.Type(), qt.Equals, TypePartial) }, }, + { + "Shortcode lang in root", + "/_shortcodes/no.html", + func(c *qt.C, p *Path) { + c.Assert(p.Type(), qt.Equals, TypeShortcode) + c.Assert(p.Lang(), qt.Equals, "") + c.Assert(p.NameNoIdentifier(), qt.Equals, "no") + }, + }, } for _, test := range tests { c.Run(test.name, func(c *qt.C) { - if test.name != "Baseof" { + if test.name != "Shortcode lang in root" { // return } test.assert(c, testParser.Parse(files.ComponentFolderLayouts, test.path)) diff --git a/tpl/tplimpl/shortcodes_integration_test.go b/tpl/tplimpl/shortcodes_integration_test.go index e884c9a8d..275e8cc15 100644 --- a/tpl/tplimpl/shortcodes_integration_test.go +++ b/tpl/tplimpl/shortcodes_integration_test.go @@ -730,16 +730,16 @@ SHORTCODE b.Assert(err.Error(), qt.Contains, `no compatible template found for shortcode "mymarkdown" in [/_shortcodes/mymarkdown.md]; note that to use plain text template shortcodes in HTML you need to use the shortcode {{% delimiter`) } -func TestShortcodeOnlyLanguageInBaseIssue13699(t *testing.T) { +func TestShortcodeOnlyLanguageInBaseIssue13699And13740(t *testing.T) { t.Parallel() files := ` -- hugo.toml -- baseURL = 'https://example.org/' +disableLanguages = ['de'] [languages] [languages.en] weight = 1 -disableLanguages = ['de'] [languages.de] weight = 2 -- layouts/_shortcodes/de.html --