Create default link and image render hooks

Fixes #11933
This commit is contained in:
Bjørn Erik Pedersen
2024-01-30 11:43:20 +01:00
parent 80595bbe3e
commit 5b7cb258ec
15 changed files with 229 additions and 42 deletions

View File

@@ -25,6 +25,10 @@ type FileInfo interface {
Filename() string
}
type IsInternalTemplateProvider interface {
IsInternalTemplate() bool
}
type ParseInfo struct {
// Set for shortcode templates with any {{ .Inner }}
IsInner bool

View File

@@ -0,0 +1,15 @@
{{- $u := urls.Parse .Destination -}}
{{- $src := $u.String -}}
{{- if not $u.IsAbs -}}
{{- with or (.Page.Resources.Get $u.Path) (resources.Get $u.Path) -}}
{{- $src = .RelPermalink -}}
{{- end -}}
{{- end -}}
{{- $attributes := dict "alt" .Text "src" $src "title" .Title -}}
<img
{{- range $k, $v := $attributes -}}
{{- if $v -}}
{{- printf " %s=%q" $k $v | safeHTMLAttr -}}
{{- end -}}
{{- end -}}>
{{- /**/ -}}

View File

@@ -0,0 +1,26 @@
{{- $u := urls.Parse .Destination -}}
{{- $href := $u.String -}}
{{- if not $u.IsAbs -}}
{{- with or
($.Page.GetPage $u.Path)
($.Page.Resources.Get $u.Path)
(resources.Get $u.Path)
-}}
{{- $href = .RelPermalink -}}
{{- with $u.RawQuery -}}
{{- $href = printf "%s?%s" $href . -}}
{{- end -}}
{{- with $u.Fragment -}}
{{- $href = printf "%s#%s" $href . -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- $attributes := dict "href" $href "title" .Title -}}
<a
{{- range $k, $v := $attributes -}}
{{- if $v -}}
{{- printf " %s=%q" $k $v | safeHTMLAttr -}}
{{- end -}}
{{- end -}}
>{{ .Text | safeHTML }}</a>
{{- /**/ -}}

View File

@@ -55,6 +55,7 @@ const (
shortcodesPathPrefix = "shortcodes/"
internalPathPrefix = "_internal/"
embeddedPathPrefix = "_embedded/"
baseFileBase = "baseof"
)
@@ -517,11 +518,19 @@ func (t *templateHandler) findLayout(d layouts.LayoutDescriptor, f output.Format
func (t *templateHandler) newTemplateInfo(name, tpl string) templateInfo {
var isText bool
var isEmbedded bool
if strings.HasPrefix(name, embeddedPathPrefix) {
isEmbedded = true
name = strings.TrimPrefix(name, embeddedPathPrefix)
}
name, isText = t.nameIsText(name)
return templateInfo{
name: name,
isText: isText,
template: tpl,
name: name,
isText: isText,
isEmbedded: isEmbedded,
template: tpl,
}
}
@@ -772,7 +781,7 @@ func (t *templateHandler) loadEmbedded() error {
}
if _, found := t.Lookup(templateName); !found {
if err := t.AddTemplate(templateName, templ); err != nil {
if err := t.AddTemplate(embeddedPathPrefix+templateName, templ); err != nil {
return err
}
}
@@ -781,7 +790,7 @@ func (t *templateHandler) loadEmbedded() error {
// TODO(bep) avoid reparsing these aliases
for _, alias := range aliases {
alias = internalPathPrefix + alias
if err := t.AddTemplate(alias, templ); err != nil {
if err := t.AddTemplate(embeddedPathPrefix+alias, templ); err != nil {
return err
}
}
@@ -1026,6 +1035,8 @@ func (t *templateNamespace) parse(info templateInfo) (*templateState, error) {
return ts, nil
}
var _ tpl.IsInternalTemplateProvider = (*templateState)(nil)
type templateState struct {
tpl.Template
@@ -1037,6 +1048,10 @@ type templateState struct {
baseInfo templateInfo // Set when a base template is used.
}
func (t *templateState) IsInternalTemplate() bool {
return t.info.isEmbedded
}
func (t *templateState) GetIdentity() identity.Identity {
return t.id
}

View File

@@ -24,9 +24,10 @@ import (
var _ identity.Identity = (*templateInfo)(nil)
type templateInfo struct {
name string
template string
isText bool // HTML or plain text template.
name string
template string
isText bool // HTML or plain text template.
isEmbedded bool
meta *hugofs.FileMeta
}