mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-29 22:29:56 +02:00
tpl: Narrow down the usage of plain text shortcodes when rendering HTML
After this commit, if you want to resolve `layouts/_shortcodes/myshortcode.txt` when rendering HTML content, you need to use the `{{%` shortcode delimiter: ``` {{% myshortcode %}} ``` This should be what people would do anyway, but we have also as part of this improved the error message to inform about what needs to be done. Note that this is not relevant for partials. Fixes #13698
This commit is contained in:
@@ -19,6 +19,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
@@ -608,7 +609,7 @@ func (s *TemplateStore) LookupShortcodeByName(name string) *TemplInfo {
|
||||
return ti
|
||||
}
|
||||
|
||||
func (s *TemplateStore) LookupShortcode(q TemplateQuery) *TemplInfo {
|
||||
func (s *TemplateStore) LookupShortcode(q TemplateQuery) (*TemplInfo, error) {
|
||||
q.init()
|
||||
k1 := s.key(q.Path)
|
||||
|
||||
@@ -630,13 +631,15 @@ func (s *TemplateStore) LookupShortcode(q TemplateQuery) *TemplInfo {
|
||||
}
|
||||
|
||||
for k, vv := range v {
|
||||
best.candidates = append(best.candidates, vv)
|
||||
if !q.Consider(vv) {
|
||||
continue
|
||||
}
|
||||
|
||||
weight := s.dh.compareDescriptors(q.Category, vv.subCategory == SubCategoryEmbedded, q.Desc, k)
|
||||
weight.distance = distance
|
||||
if best.isBetter(weight, vv) {
|
||||
isBetter := best.isBetter(weight, vv)
|
||||
if isBetter {
|
||||
best.updateValues(weight, k2, k, vv)
|
||||
}
|
||||
}
|
||||
@@ -644,8 +647,21 @@ func (s *TemplateStore) LookupShortcode(q TemplateQuery) *TemplInfo {
|
||||
return false, nil
|
||||
})
|
||||
|
||||
// Any match will do.
|
||||
return best.templ
|
||||
if best.w.w1 <= 0 {
|
||||
var err error
|
||||
if s := best.candidatesAsStringSlice(); s != nil {
|
||||
msg := fmt.Sprintf("no compatible template found for shortcode %q in %s", q.Name, s)
|
||||
if !q.Desc.IsPlainText {
|
||||
msg += "; note that to use plain text template shortcodes in HTML you need to use the shortcode {{% delimiter"
|
||||
}
|
||||
err = errors.New(msg)
|
||||
} else {
|
||||
err = fmt.Errorf("no template found for shortcode %q", q.Name)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return best.templ, nil
|
||||
}
|
||||
|
||||
// PrintDebug is for testing/debugging only.
|
||||
@@ -1817,10 +1833,11 @@ type TextTemplatHandler interface {
|
||||
}
|
||||
|
||||
type bestMatch struct {
|
||||
templ *TemplInfo
|
||||
desc TemplateDescriptor
|
||||
w weight
|
||||
key string
|
||||
templ *TemplInfo
|
||||
desc TemplateDescriptor
|
||||
w weight
|
||||
key string
|
||||
candidates []*TemplInfo
|
||||
|
||||
// settings.
|
||||
defaultOutputformat string
|
||||
@@ -1831,6 +1848,18 @@ func (best *bestMatch) reset() {
|
||||
best.w = weight{}
|
||||
best.desc = TemplateDescriptor{}
|
||||
best.key = ""
|
||||
best.candidates = nil
|
||||
}
|
||||
|
||||
func (best *bestMatch) candidatesAsStringSlice() []string {
|
||||
if len(best.candidates) == 0 {
|
||||
return nil
|
||||
}
|
||||
candidates := make([]string, len(best.candidates))
|
||||
for i, v := range best.candidates {
|
||||
candidates[i] = v.PathInfo.Path()
|
||||
}
|
||||
return candidates
|
||||
}
|
||||
|
||||
func (best *bestMatch) isBetter(w weight, ti *TemplInfo) bool {
|
||||
@@ -1840,7 +1869,6 @@ func (best *bestMatch) isBetter(w weight, ti *TemplInfo) bool {
|
||||
}
|
||||
|
||||
if w.w1 <= 0 {
|
||||
|
||||
if best.w.w1 <= 0 {
|
||||
return ti.PathInfo.Path() < best.templ.PathInfo.Path()
|
||||
}
|
||||
|
Reference in New Issue
Block a user