]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Fix it so e.g. de in layouts/_shortcodes/de.html is not interpreted as a language...
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 26 May 2025 08:22:13 +0000 (10:22 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 26 May 2025 18:26:56 +0000 (20:26 +0200)
Fixes #13740

common/paths/pathparser.go
common/paths/pathparser_test.go
tpl/tplimpl/shortcodes_integration_test.go

index b0a2f9fc4d0abc3b21b12648388b4fcd9213b84a..d10523a848a92aa3a6b277b362854e3be403f26f 100644 (file)
@@ -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 {
index a6194e756d2e30f5cf836bd56e18bb6606f60b24..ad76b93672676090c17c88e109686b2bb7e1c7e7 100644 (file)
@@ -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))
index e884c9a8d6b9eb1bc988a4992bd613b09d5087de..275e8cc15055b38b9fddf790fa9c9ac71b5f54d8 100644 (file)
@@ -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 --