mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-14 20:37:55 -05:00
Make all renderhook Text methods return template.HTML
This commit is contained in:
parent
f738669a4d
commit
6d97ee711e
13 changed files with 50 additions and 36 deletions
|
@ -13,8 +13,22 @@
|
||||||
|
|
||||||
package hstring
|
package hstring
|
||||||
|
|
||||||
type RenderedString string
|
import (
|
||||||
|
"html/template"
|
||||||
|
|
||||||
func (s RenderedString) String() string {
|
"github.com/gohugoio/hugo/common/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ types.PrintableValueProvider = RenderedHTML("")
|
||||||
|
|
||||||
|
// RenderedHTML is a string that represents rendered HTML.
|
||||||
|
// When printed in templates it will be rendered as template.HTML and considered safe.
|
||||||
|
type RenderedHTML string
|
||||||
|
|
||||||
|
func (s RenderedHTML) String() string {
|
||||||
return string(s)
|
return string(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s RenderedHTML) PrintableValue() any {
|
||||||
|
return template.HTML(s)
|
||||||
|
}
|
||||||
|
|
|
@ -25,6 +25,6 @@ func TestRenderedString(t *testing.T) {
|
||||||
c := qt.New(t)
|
c := qt.New(t)
|
||||||
|
|
||||||
// Validate that it will behave like a string in Hugo settings.
|
// Validate that it will behave like a string in Hugo settings.
|
||||||
c.Assert(cast.ToString(RenderedString("Hugo")), qt.Equals, "Hugo")
|
c.Assert(cast.ToString(RenderedHTML("Hugo")), qt.Equals, "Hugo")
|
||||||
c.Assert(template.HTML(RenderedString("Hugo")), qt.Equals, template.HTML("Hugo"))
|
c.Assert(template.HTML(RenderedHTML("Hugo")), qt.Equals, template.HTML("Hugo"))
|
||||||
}
|
}
|
||||||
|
|
|
@ -98,8 +98,8 @@ baseURL="https://example.org"
|
||||||
P1: {{ $p.Content }}
|
P1: {{ $p.Content }}
|
||||||
|
|
||||||
`,
|
`,
|
||||||
"_default/_markup/render-link.html", `html-link: {{ .Destination | safeURL }}|Text: {{ .Text | safeHTML }}|Plain: {{ .PlainText | safeHTML }}`,
|
"_default/_markup/render-link.html", `html-link: {{ .Destination | safeURL }}|Text: {{ .Text }}|Plain: {{ .PlainText | safeHTML }}`,
|
||||||
"_default/_markup/render-image.html", `html-image: {{ .Destination | safeURL }}|Text: {{ .Text | safeHTML }}|Plain: {{ .PlainText | safeHTML }}`,
|
"_default/_markup/render-image.html", `html-image: {{ .Destination | safeURL }}|Text: {{ .Text }}|Plain: {{ .PlainText | safeHTML }}`,
|
||||||
)
|
)
|
||||||
|
|
||||||
b.WithContent("p1.md", `---
|
b.WithContent("p1.md", `---
|
||||||
|
|
|
@ -928,7 +928,7 @@ func (c *cachedContentScope) RenderString(ctx context.Context, args ...any) (tem
|
||||||
|
|
||||||
contentToRenderv := args[sidx]
|
contentToRenderv := args[sidx]
|
||||||
|
|
||||||
if _, ok := contentToRenderv.(hstring.RenderedString); ok {
|
if _, ok := contentToRenderv.(hstring.RenderedHTML); ok {
|
||||||
// This content is already rendered, this is potentially
|
// This content is already rendered, this is potentially
|
||||||
// a infinite recursion.
|
// a infinite recursion.
|
||||||
return "", errors.New("text is already rendered, repeating it may cause infinite recursion")
|
return "", errors.New("text is already rendered, repeating it may cause infinite recursion")
|
||||||
|
|
|
@ -41,7 +41,7 @@ type LinkContext interface {
|
||||||
Title() string
|
Title() string
|
||||||
|
|
||||||
// The rendered (HTML) text.
|
// The rendered (HTML) text.
|
||||||
Text() hstring.RenderedString
|
Text() hstring.RenderedHTML
|
||||||
|
|
||||||
// The plain variant of Text.
|
// The plain variant of Text.
|
||||||
PlainText() string
|
PlainText() string
|
||||||
|
@ -100,7 +100,7 @@ type BlockquoteContext interface {
|
||||||
|
|
||||||
// The blockquote text.
|
// The blockquote text.
|
||||||
// If type is "alert", this will be the alert text.
|
// If type is "alert", this will be the alert text.
|
||||||
Text() hstring.RenderedString
|
Text() hstring.RenderedHTML
|
||||||
|
|
||||||
/// Returns the blockquote type, one of "regular" and "alert".
|
/// Returns the blockquote type, one of "regular" and "alert".
|
||||||
// Type "alert" indicates that this is a GitHub type alert.
|
// Type "alert" indicates that this is a GitHub type alert.
|
||||||
|
@ -166,7 +166,7 @@ type HeadingContext interface {
|
||||||
// Anchor is the HTML id assigned to the heading.
|
// Anchor is the HTML id assigned to the heading.
|
||||||
Anchor() string
|
Anchor() string
|
||||||
// Text is the rendered (HTML) heading text, excluding the heading marker.
|
// Text is the rendered (HTML) heading text, excluding the heading marker.
|
||||||
Text() hstring.RenderedString
|
Text() hstring.RenderedHTML
|
||||||
// PlainText is the unrendered version of Text.
|
// PlainText is the unrendered version of Text.
|
||||||
PlainText() string
|
PlainText() string
|
||||||
|
|
||||||
|
@ -213,7 +213,7 @@ const (
|
||||||
type GetRendererFunc func(t RendererType, id any) any
|
type GetRendererFunc func(t RendererType, id any) any
|
||||||
|
|
||||||
type TableCell struct {
|
type TableCell struct {
|
||||||
Text hstring.RenderedString
|
Text hstring.RenderedHTML
|
||||||
Alignment string // left, center, or right
|
Alignment string // left, center, or right
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -95,7 +95,7 @@ func (r *htmlRenderer) renderBlockquote(w util.BufWriter, src []byte, node ast.N
|
||||||
BaseContext: render.NewBaseContext(ctx, renderer, n, src, nil, ordinal),
|
BaseContext: render.NewBaseContext(ctx, renderer, n, src, nil, ordinal),
|
||||||
typ: typ,
|
typ: typ,
|
||||||
alertType: alertType,
|
alertType: alertType,
|
||||||
text: hstring.RenderedString(text),
|
text: hstring.RenderedHTML(text),
|
||||||
AttributesHolder: attributes.New(n.Attributes(), attributes.AttributesOwnerGeneral),
|
AttributesHolder: attributes.New(n.Attributes(), attributes.AttributesOwnerGeneral),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,7 +134,7 @@ func (r *htmlRenderer) renderBlockquoteDefault(
|
||||||
type blockquoteContext struct {
|
type blockquoteContext struct {
|
||||||
hooks.BaseContext
|
hooks.BaseContext
|
||||||
|
|
||||||
text hstring.RenderedString
|
text hstring.RenderedHTML
|
||||||
alertType string
|
alertType string
|
||||||
typ string
|
typ string
|
||||||
|
|
||||||
|
@ -149,7 +149,7 @@ func (c *blockquoteContext) AlertType() string {
|
||||||
return c.alertType
|
return c.alertType
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *blockquoteContext) Text() hstring.RenderedString {
|
func (c *blockquoteContext) Text() hstring.RenderedHTML {
|
||||||
return c.text
|
return c.text
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,9 +32,9 @@ func TestBlockquoteHook(t *testing.T) {
|
||||||
block = true
|
block = true
|
||||||
title = true
|
title = true
|
||||||
-- layouts/_default/_markup/render-blockquote.html --
|
-- layouts/_default/_markup/render-blockquote.html --
|
||||||
Blockquote: |{{ .Text | safeHTML }}|{{ .Type }}|
|
Blockquote: |{{ .Text }}|{{ .Type }}|
|
||||||
-- layouts/_default/_markup/render-blockquote-alert.html --
|
-- layouts/_default/_markup/render-blockquote-alert.html --
|
||||||
{{ $text := .Text | safeHTML }}
|
{{ $text := .Text }}
|
||||||
Blockquote Alert: |{{ $text }}|{{ .Type }}|
|
Blockquote Alert: |{{ $text }}|{{ .Type }}|
|
||||||
Blockquote Alert Attributes: |{{ $text }}|{{ .Attributes }}|
|
Blockquote Alert Attributes: |{{ $text }}|{{ .Attributes }}|
|
||||||
Blockquote Alert Page: |{{ $text }}|{{ .Page.Title }}|{{ .PageInner.Title }}|
|
Blockquote Alert Page: |{{ $text }}|{{ .Page.Title }}|{{ .PageInner.Title }}|
|
||||||
|
|
|
@ -76,7 +76,7 @@ title: "p1"
|
||||||
{{- range $k, $v := .Attributes -}}
|
{{- range $k, $v := .Attributes -}}
|
||||||
{{- printf " %s=%q" $k $v | safeHTMLAttr -}}
|
{{- printf " %s=%q" $k $v | safeHTMLAttr -}}
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
>{{ .Text | safeHTML }}</h{{ .Level }}>
|
>{{ .Text }}</h{{ .Level }}>
|
||||||
`
|
`
|
||||||
|
|
||||||
b := hugolib.Test(t, files)
|
b := hugolib.Test(t, files)
|
||||||
|
@ -146,11 +146,11 @@ title: "p1"
|
||||||
{{ .Content }}
|
{{ .Content }}
|
||||||
-- layouts/_default/_markup/render-heading.html --
|
-- layouts/_default/_markup/render-heading.html --
|
||||||
<h{{ .Level }} id="{{ .Anchor | safeURL }}">
|
<h{{ .Level }} id="{{ .Anchor | safeURL }}">
|
||||||
{{ .Text | safeHTML }}
|
{{ .Text }}
|
||||||
<a class="anchor" href="#{{ .Anchor | safeURL }}">#</a>
|
<a class="anchor" href="#{{ .Anchor | safeURL }}">#</a>
|
||||||
</h{{ .Level }}>
|
</h{{ .Level }}>
|
||||||
-- layouts/_default/_markup/render-link.html --
|
-- layouts/_default/_markup/render-link.html --
|
||||||
<a href="{{ .Destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}>{{ .Text | safeHTML }}</a>
|
<a href="{{ .Destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}>{{ .Text }}</a>
|
||||||
|
|
||||||
`
|
`
|
||||||
|
|
||||||
|
@ -236,11 +236,11 @@ func BenchmarkRenderHooks(b *testing.B) {
|
||||||
-- config.toml --
|
-- config.toml --
|
||||||
-- layouts/_default/_markup/render-heading.html --
|
-- layouts/_default/_markup/render-heading.html --
|
||||||
<h{{ .Level }} id="{{ .Anchor | safeURL }}">
|
<h{{ .Level }} id="{{ .Anchor | safeURL }}">
|
||||||
{{ .Text | safeHTML }}
|
{{ .Text }}
|
||||||
<a class="anchor" href="#{{ .Anchor | safeURL }}">#</a>
|
<a class="anchor" href="#{{ .Anchor | safeURL }}">#</a>
|
||||||
</h{{ .Level }}>
|
</h{{ .Level }}>
|
||||||
-- layouts/_default/_markup/render-link.html --
|
-- layouts/_default/_markup/render-link.html --
|
||||||
<a href="{{ .Destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}>{{ .Text | safeHTML }}</a>
|
<a href="{{ .Destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}>{{ .Text }}</a>
|
||||||
-- layouts/_default/single.html --
|
-- layouts/_default/single.html --
|
||||||
{{ .Content }}
|
{{ .Content }}
|
||||||
`
|
`
|
||||||
|
@ -452,7 +452,7 @@ Link https procol: https://www.example.org
|
||||||
|
|
||||||
if withHook {
|
if withHook {
|
||||||
files += `-- layouts/_default/_markup/render-link.html --
|
files += `-- layouts/_default/_markup/render-link.html --
|
||||||
<a href="{{ .Destination | safeURL }}">{{ .Text | safeHTML }}</a>`
|
<a href="{{ .Destination | safeURL }}">{{ .Text }}</a>`
|
||||||
}
|
}
|
||||||
|
|
||||||
return hugolib.NewIntegrationTestBuilder(
|
return hugolib.NewIntegrationTestBuilder(
|
||||||
|
|
|
@ -52,7 +52,7 @@ type linkContext struct {
|
||||||
pageInner any
|
pageInner any
|
||||||
destination string
|
destination string
|
||||||
title string
|
title string
|
||||||
text hstring.RenderedString
|
text hstring.RenderedHTML
|
||||||
plainText string
|
plainText string
|
||||||
*attributes.AttributesHolder
|
*attributes.AttributesHolder
|
||||||
}
|
}
|
||||||
|
@ -69,7 +69,7 @@ func (ctx linkContext) PageInner() any {
|
||||||
return ctx.pageInner
|
return ctx.pageInner
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx linkContext) Text() hstring.RenderedString {
|
func (ctx linkContext) Text() hstring.RenderedHTML {
|
||||||
return ctx.text
|
return ctx.text
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,7 +100,7 @@ type headingContext struct {
|
||||||
pageInner any
|
pageInner any
|
||||||
level int
|
level int
|
||||||
anchor string
|
anchor string
|
||||||
text hstring.RenderedString
|
text hstring.RenderedHTML
|
||||||
plainText string
|
plainText string
|
||||||
*attributes.AttributesHolder
|
*attributes.AttributesHolder
|
||||||
}
|
}
|
||||||
|
@ -121,7 +121,7 @@ func (ctx headingContext) Anchor() string {
|
||||||
return ctx.anchor
|
return ctx.anchor
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx headingContext) Text() hstring.RenderedString {
|
func (ctx headingContext) Text() hstring.RenderedHTML {
|
||||||
return ctx.text
|
return ctx.text
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,7 +199,7 @@ func (r *hookedRenderer) renderImage(w util.BufWriter, source []byte, node ast.N
|
||||||
pageInner: pageInner,
|
pageInner: pageInner,
|
||||||
destination: string(n.Destination),
|
destination: string(n.Destination),
|
||||||
title: string(n.Title),
|
title: string(n.Title),
|
||||||
text: hstring.RenderedString(text),
|
text: hstring.RenderedHTML(text),
|
||||||
plainText: string(n.Text(source)),
|
plainText: string(n.Text(source)),
|
||||||
AttributesHolder: attributes.New(attrs, attributes.AttributesOwnerGeneral),
|
AttributesHolder: attributes.New(attrs, attributes.AttributesOwnerGeneral),
|
||||||
},
|
},
|
||||||
|
@ -288,7 +288,7 @@ func (r *hookedRenderer) renderLink(w util.BufWriter, source []byte, node ast.No
|
||||||
pageInner: pageInner,
|
pageInner: pageInner,
|
||||||
destination: string(n.Destination),
|
destination: string(n.Destination),
|
||||||
title: string(n.Title),
|
title: string(n.Title),
|
||||||
text: hstring.RenderedString(text),
|
text: hstring.RenderedHTML(text),
|
||||||
plainText: string(n.Text(source)),
|
plainText: string(n.Text(source)),
|
||||||
AttributesHolder: attributes.Empty,
|
AttributesHolder: attributes.Empty,
|
||||||
},
|
},
|
||||||
|
@ -355,7 +355,7 @@ func (r *hookedRenderer) renderAutoLink(w util.BufWriter, source []byte, node as
|
||||||
page: page,
|
page: page,
|
||||||
pageInner: pageInner,
|
pageInner: pageInner,
|
||||||
destination: url,
|
destination: url,
|
||||||
text: hstring.RenderedString(label),
|
text: hstring.RenderedHTML(label),
|
||||||
plainText: label,
|
plainText: label,
|
||||||
AttributesHolder: attributes.Empty,
|
AttributesHolder: attributes.Empty,
|
||||||
},
|
},
|
||||||
|
@ -442,7 +442,7 @@ func (r *hookedRenderer) renderHeading(w util.BufWriter, source []byte, node ast
|
||||||
pageInner: pageInner,
|
pageInner: pageInner,
|
||||||
level: n.Level,
|
level: n.Level,
|
||||||
anchor: string(anchor),
|
anchor: string(anchor),
|
||||||
text: hstring.RenderedString(text),
|
text: hstring.RenderedHTML(text),
|
||||||
plainText: string(n.Text(source)),
|
plainText: string(n.Text(source)),
|
||||||
AttributesHolder: attributes.New(n.Attributes(), attributes.AttributesOwnerGeneral),
|
AttributesHolder: attributes.New(n.Attributes(), attributes.AttributesOwnerGeneral),
|
||||||
},
|
},
|
||||||
|
|
|
@ -132,7 +132,7 @@ func (r *htmlRenderer) renderCell(w util.BufWriter, source []byte, node ast.Node
|
||||||
alignment = "left"
|
alignment = "left"
|
||||||
}
|
}
|
||||||
|
|
||||||
cell := hooks.TableCell{Text: hstring.RenderedString(text), Alignment: alignment}
|
cell := hooks.TableCell{Text: hstring.RenderedHTML(text), Alignment: alignment}
|
||||||
|
|
||||||
if node.Parent().Kind() == gast.KindTableHeader {
|
if node.Parent().Kind() == gast.KindTableHeader {
|
||||||
table.THead[len(table.THead)-1] = append(table.THead[len(table.THead)-1], cell)
|
table.THead[len(table.THead)-1] = append(table.THead[len(table.THead)-1], cell)
|
||||||
|
|
|
@ -32,7 +32,7 @@ title = true
|
||||||
| Item | In Stock | Price |
|
| Item | In Stock | Price |
|
||||||
| :---------------- | :------: | ----: |
|
| :---------------- | :------: | ----: |
|
||||||
| Python Hat | True | 23.99 |
|
| Python Hat | True | 23.99 |
|
||||||
| SQL Hat | True | 23.99 |
|
| SQL **Hat** | True | 23.99 |
|
||||||
| Codecademy Tee | False | 19.99 |
|
| Codecademy Tee | False | 19.99 |
|
||||||
| Codecademy Hoodie | False | 42.99 |
|
| Codecademy Hoodie | False | 42.99 |
|
||||||
{.foo foo="bar"}
|
{.foo foo="bar"}
|
||||||
|
@ -61,7 +61,7 @@ Attributes: {{ .Attributes }}|
|
||||||
b.AssertFileContent("public/p1/index.html",
|
b.AssertFileContent("public/p1/index.html",
|
||||||
"Attributes: map[class:foo foo:bar]|",
|
"Attributes: map[class:foo foo:bar]|",
|
||||||
"table-0-thead: 0: 0: left: Item| 1: center: In Stock| 2: right: Price|$",
|
"table-0-thead: 0: 0: left: Item| 1: center: In Stock| 2: right: Price|$",
|
||||||
"table-0-tbody: 0: 0: left: Python Hat| 1: center: True| 2: right: 23.99| 1: 0: left: SQL Hat| 1: center: True| 2: right: 23.99| 2: 0: left: Codecademy Tee| 1: center: False| 2: right: 19.99| 3: 0: left: Codecademy Hoodie| 1: center: False| 2: right: 42.99|$",
|
"table-0-tbody: 0: 0: left: Python Hat| 1: center: True| 2: right: 23.99| 1: 0: left: SQL <strong>Hat</strong>| 1: center: True| 2: right: 23.99| 2: 0: left: Codecademy Tee| 1: center: False| 2: right: 19.99| 3: 0: left: Codecademy Hoodie| 1: center: False| 2: right: 42.99|$",
|
||||||
)
|
)
|
||||||
|
|
||||||
b.AssertFileContent("public/p1/index.html",
|
b.AssertFileContent("public/p1/index.html",
|
||||||
|
|
|
@ -25,5 +25,5 @@
|
||||||
{{- printf " %s=%q" $k $v | safeHTMLAttr -}}
|
{{- printf " %s=%q" $k $v | safeHTMLAttr -}}
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
>{{ .Text | safeHTML }}</a>
|
>{{ .Text }}</a>
|
||||||
{{- /**/ -}}
|
{{- /**/ -}}
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
<tr>
|
<tr>
|
||||||
{{- range . }}
|
{{- range . }}
|
||||||
<th {{ printf "style=%q" (printf "text-align: %s" .Alignment) | safeHTMLAttr }}>
|
<th {{ printf "style=%q" (printf "text-align: %s" .Alignment) | safeHTMLAttr }}>
|
||||||
{{- .Text | safeHTML -}}
|
{{- .Text -}}
|
||||||
</th>
|
</th>
|
||||||
{{- end }}
|
{{- end }}
|
||||||
</tr>
|
</tr>
|
||||||
|
@ -20,7 +20,7 @@
|
||||||
<tr>
|
<tr>
|
||||||
{{- range . }}
|
{{- range . }}
|
||||||
<td {{ printf "style=%q" (printf "text-align: %s" .Alignment) | safeHTMLAttr }}>
|
<td {{ printf "style=%q" (printf "text-align: %s" .Alignment) | safeHTMLAttr }}>
|
||||||
{{- .Text | safeHTML -}}
|
{{- .Text -}}
|
||||||
</td>
|
</td>
|
||||||
{{- end }}
|
{{- end }}
|
||||||
</tr>
|
</tr>
|
||||||
|
|
Loading…
Reference in a new issue