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)
}
+
+func (s RenderedHTML) PrintableValue() any {
+ return template.HTML(s)
+}
c := qt.New(t)
// Validate that it will behave like a string in Hugo settings.
- c.Assert(cast.ToString(RenderedString("Hugo")), qt.Equals, "Hugo")
- c.Assert(template.HTML(RenderedString("Hugo")), qt.Equals, template.HTML("Hugo"))
+ c.Assert(cast.ToString(RenderedHTML("Hugo")), qt.Equals, "Hugo")
+ c.Assert(template.HTML(RenderedHTML("Hugo")), qt.Equals, template.HTML("Hugo"))
}
P1: {{ $p.Content }}
`,
- "_default/_markup/render-link.html", `html-link: {{ .Destination | safeURL }}|Text: {{ .Text | safeHTML }}|Plain: {{ .PlainText | safeHTML }}`,
- "_default/_markup/render-image.html", `html-image: {{ .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 }}|Plain: {{ .PlainText | safeHTML }}`,
)
b.WithContent("p1.md", `---
contentToRenderv := args[sidx]
- if _, ok := contentToRenderv.(hstring.RenderedString); ok {
+ if _, ok := contentToRenderv.(hstring.RenderedHTML); ok {
// This content is already rendered, this is potentially
// a infinite recursion.
return "", errors.New("text is already rendered, repeating it may cause infinite recursion")
Title() string
// The rendered (HTML) text.
- Text() hstring.RenderedString
+ Text() hstring.RenderedHTML
// The plain variant of Text.
PlainText() string
// The blockquote 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".
// Type "alert" indicates that this is a GitHub type alert.
// Anchor is the HTML id assigned to the heading.
Anchor() string
// 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() string
type GetRendererFunc func(t RendererType, id any) any
type TableCell struct {
- Text hstring.RenderedString
+ Text hstring.RenderedHTML
Alignment string // left, center, or right
}
BaseContext: render.NewBaseContext(ctx, renderer, n, src, nil, ordinal),
typ: typ,
alertType: alertType,
- text: hstring.RenderedString(text),
+ text: hstring.RenderedHTML(text),
AttributesHolder: attributes.New(n.Attributes(), attributes.AttributesOwnerGeneral),
}
type blockquoteContext struct {
hooks.BaseContext
- text hstring.RenderedString
+ text hstring.RenderedHTML
alertType string
typ string
return c.alertType
}
-func (c *blockquoteContext) Text() hstring.RenderedString {
+func (c *blockquoteContext) Text() hstring.RenderedHTML {
return c.text
}
block = true
title = true
-- layouts/_default/_markup/render-blockquote.html --
-Blockquote: |{{ .Text | safeHTML }}|{{ .Type }}|
+Blockquote: |{{ .Text }}|{{ .Type }}|
-- layouts/_default/_markup/render-blockquote-alert.html --
-{{ $text := .Text | safeHTML }}
+{{ $text := .Text }}
Blockquote Alert: |{{ $text }}|{{ .Type }}|
Blockquote Alert Attributes: |{{ $text }}|{{ .Attributes }}|
Blockquote Alert Page: |{{ $text }}|{{ .Page.Title }}|{{ .PageInner.Title }}|
{{- range $k, $v := .Attributes -}}
{{- printf " %s=%q" $k $v | safeHTMLAttr -}}
{{- end -}}
->{{ .Text | safeHTML }}</h{{ .Level }}>
+>{{ .Text }}</h{{ .Level }}>
`
b := hugolib.Test(t, files)
{{ .Content }}
-- layouts/_default/_markup/render-heading.html --
<h{{ .Level }} id="{{ .Anchor | safeURL }}">
- {{ .Text | safeHTML }}
+ {{ .Text }}
<a class="anchor" href="#{{ .Anchor | safeURL }}">#</a>
</h{{ .Level }}>
-- 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>
`
-- config.toml --
-- layouts/_default/_markup/render-heading.html --
<h{{ .Level }} id="{{ .Anchor | safeURL }}">
- {{ .Text | safeHTML }}
+ {{ .Text }}
<a class="anchor" href="#{{ .Anchor | safeURL }}">#</a>
</h{{ .Level }}>
-- 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 --
{{ .Content }}
`
if withHook {
files += `-- layouts/_default/_markup/render-link.html --
-<a href="{{ .Destination | safeURL }}">{{ .Text | safeHTML }}</a>`
+<a href="{{ .Destination | safeURL }}">{{ .Text }}</a>`
}
return hugolib.NewIntegrationTestBuilder(
pageInner any
destination string
title string
- text hstring.RenderedString
+ text hstring.RenderedHTML
plainText string
*attributes.AttributesHolder
}
return ctx.pageInner
}
-func (ctx linkContext) Text() hstring.RenderedString {
+func (ctx linkContext) Text() hstring.RenderedHTML {
return ctx.text
}
pageInner any
level int
anchor string
- text hstring.RenderedString
+ text hstring.RenderedHTML
plainText string
*attributes.AttributesHolder
}
return ctx.anchor
}
-func (ctx headingContext) Text() hstring.RenderedString {
+func (ctx headingContext) Text() hstring.RenderedHTML {
return ctx.text
}
pageInner: pageInner,
destination: string(n.Destination),
title: string(n.Title),
- text: hstring.RenderedString(text),
+ text: hstring.RenderedHTML(text),
plainText: string(n.Text(source)),
AttributesHolder: attributes.New(attrs, attributes.AttributesOwnerGeneral),
},
pageInner: pageInner,
destination: string(n.Destination),
title: string(n.Title),
- text: hstring.RenderedString(text),
+ text: hstring.RenderedHTML(text),
plainText: string(n.Text(source)),
AttributesHolder: attributes.Empty,
},
page: page,
pageInner: pageInner,
destination: url,
- text: hstring.RenderedString(label),
+ text: hstring.RenderedHTML(label),
plainText: label,
AttributesHolder: attributes.Empty,
},
pageInner: pageInner,
level: n.Level,
anchor: string(anchor),
- text: hstring.RenderedString(text),
+ text: hstring.RenderedHTML(text),
plainText: string(n.Text(source)),
AttributesHolder: attributes.New(n.Attributes(), attributes.AttributesOwnerGeneral),
},
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 {
table.THead[len(table.THead)-1] = append(table.THead[len(table.THead)-1], cell)
| Item | In Stock | Price |
| :---------------- | :------: | ----: |
| Python Hat | True | 23.99 |
-| SQL Hat | True | 23.99 |
+| SQL **Hat** | True | 23.99 |
| Codecademy Tee | False | 19.99 |
| Codecademy Hoodie | False | 42.99 |
{.foo foo="bar"}
b.AssertFileContent("public/p1/index.html",
"Attributes: map[class:foo foo:bar]|",
"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",
{{- printf " %s=%q" $k $v | safeHTMLAttr -}}
{{- end -}}
{{- end -}}
- >{{ .Text | safeHTML }}</a>
+ >{{ .Text }}</a>
{{- /**/ -}}
<tr>
{{- range . }}
<th {{ printf "style=%q" (printf "text-align: %s" .Alignment) | safeHTMLAttr }}>
- {{- .Text | safeHTML -}}
+ {{- .Text -}}
</th>
{{- end }}
</tr>
<tr>
{{- range . }}
<td {{ printf "style=%q" (printf "text-align: %s" .Alignment) | safeHTMLAttr }}>
- {{- .Text | safeHTML -}}
+ {{- .Text -}}
</td>
{{- end }}
</tr>