From 334c055f972a820cba9ed2e4a12d2e038829d8f5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?=
{{ .Inner | safeHTML }}
+`
+
+ b := TestRunning(t, files, TestOptWarn())
+
+ b.AssertNoRenderShortcodesArtifacts()
+ b.AssertFileContentEquals("public/p1/index.html", "Content p1 id-1000.
\ncode_p2
Foo.\n
\ncode_p3
code_p1
")
+ b.EditFileReplaceAll("content/p1.md", "id-1000.", "id-100.").Build()
+ b.AssertNoRenderShortcodesArtifacts()
+ b.AssertFileContentEquals("public/p1/index.html", "Content p1 id-100.
\ncode_p2
Foo.\n
\ncode_p3
code_p1
")
+ b.EditFileReplaceAll("content/p2.md", "code_p2", "codep2").Build()
+ b.AssertNoRenderShortcodesArtifacts()
+ b.AssertFileContentEquals("public/p1/index.html", "Content p1 id-100.
\ncodep2
Foo.\n
\ncode_p3
code_p1
")
+ b.EditFileReplaceAll("content/p3.md", "code_p3", "code_p3_edited").Build()
+ b.AssertNoRenderShortcodesArtifacts()
+ b.AssertFileContentEquals("public/p1/index.html", "Content p1 id-100.
\ncodep2
Foo.\n
\ncode_p3_edited
code_p1
")
+}
diff --git a/markup/goldmark/convert.go b/markup/goldmark/convert.go
index 5c31eee40d9..ea3bbc4ae9b 100644
--- a/markup/goldmark/convert.go
+++ b/markup/goldmark/convert.go
@@ -106,7 +106,7 @@ func newMarkdown(pcfg converter.ProviderConfig) goldmark.Markdown {
renderer.WithNodeRenderers(util.Prioritized(emoji.NewHTMLRenderer(), 200)))
var (
extensions = []goldmark.Extender{
- hugocontext.New(),
+ hugocontext.New(pcfg.Logger),
newLinks(cfg),
newTocExtension(tocRendererOptions),
blockquotes.New(),
diff --git a/markup/goldmark/hugocontext/hugocontext.go b/markup/goldmark/hugocontext/hugocontext.go
index b9c548dac5e..7d0dcaa850f 100644
--- a/markup/goldmark/hugocontext/hugocontext.go
+++ b/markup/goldmark/hugocontext/hugocontext.go
@@ -16,20 +16,24 @@ package hugocontext
import (
"bytes"
"fmt"
+ "regexp"
"strconv"
"github.com/gohugoio/hugo/bufferpool"
+ "github.com/gohugoio/hugo/common/constants"
+ "github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/markup/goldmark/internal/render"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
+ "github.com/yuin/goldmark/renderer/html"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)
-func New() goldmark.Extender {
- return &hugoContextExtension{}
+func New(logger loggers.Logger) goldmark.Extender {
+ return &hugoContextExtension{logger: logger}
}
// Wrap wraps the given byte slice in a Hugo context that used to determine the correct Page
@@ -37,14 +41,15 @@ func New() goldmark.Extender {
func Wrap(b []byte, pid uint64) string {
buf := bufferpool.GetBuffer()
defer bufferpool.PutBuffer(buf)
- buf.Write(prefix)
+ buf.Write(hugoCtxPrefix)
buf.WriteString(" pid=")
buf.WriteString(strconv.FormatUint(pid, 10))
- buf.Write(endDelim)
+ buf.Write(hugoCtxEndDelim)
buf.WriteByte('\n')
buf.Write(b)
- buf.Write(prefix)
- buf.Write(closingDelimAndNewline)
+ buf.WriteByte('\n')
+ buf.Write(hugoCtxPrefix)
+ buf.Write(hugoCtxClosingDelimAndNewline)
return buf.String()
}
@@ -89,9 +94,10 @@ func (h *HugoContext) Kind() ast.NodeKind {
}
var (
- prefix = []byte("{{__hugo_ctx")
- endDelim = []byte("}}")
- closingDelimAndNewline = []byte("/}}\n")
+ hugoCtxPrefix = []byte("{{__hugo_ctx")
+ hugoCtxEndDelim = []byte("}}")
+ hugoCtxClosingDelimAndNewline = []byte("/}}\n")
+ hugoCtxRe = regexp.MustCompile(`{{__hugo_ctx( pid=\d+)?/?}}\n?`)
)
var _ parser.InlineParser = (*hugoContextParser)(nil)
@@ -100,21 +106,26 @@ type hugoContextParser struct{}
func (s *hugoContextParser) Parse(parent ast.Node, block text.Reader, pc parser.Context) ast.Node {
line, _ := block.PeekLine()
- if !bytes.HasPrefix(line, prefix) {
+
+ if !bytes.HasPrefix(line, hugoCtxPrefix) {
return nil
}
- end := bytes.Index(line, endDelim)
+ end := bytes.Index(line, hugoCtxEndDelim)
if end == -1 {
return nil
}
- block.Advance(end + len(endDelim) + 1) // +1 for the newline
+ block.Advance(end + len(hugoCtxEndDelim) + 1) // +1 for the newline
if line[end-1] == '/' {
+ if parent.Kind() == ast.KindParagraph && !parent.HasChildren() {
+ // Remove empty paragraph created by the {{__hugo_ctx/}}.
+ parent.Parent().RemoveChild(parent.Parent(), parent)
+ }
return &HugoContext{Closing: true}
}
- attrBytes := line[len(prefix)+1 : end]
+ attrBytes := line[len(hugoCtxPrefix)+1 : end]
h := &HugoContext{}
h.parseAttrs(attrBytes)
return h
@@ -124,10 +135,67 @@ func (a *hugoContextParser) Trigger() []byte {
return []byte{'{'}
}
-type hugoContextRenderer struct{}
+type hugoContextRenderer struct {
+ logger loggers.Logger
+ html.Config
+}
+
+func (r *hugoContextRenderer) SetOption(name renderer.OptionName, value any) {
+ r.Config.SetOption(name, value)
+}
func (r *hugoContextRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
reg.Register(kindHugoContext, r.handleHugoContext)
+ reg.Register(ast.KindHTMLBlock, r.renderHTMLBlock)
+}
+
+func (r *hugoContextRenderer) stripHugoCtx(b []byte) ([]byte, bool) {
+ if !bytes.Contains(b, hugoCtxPrefix) {
+ return b, false
+ }
+ return hugoCtxRe.ReplaceAll(b, nil), true
+}
+
+func (r *hugoContextRenderer) renderHTMLBlock(
+ w util.BufWriter, source []byte, node ast.Node, entering bool,
+) (ast.WalkStatus, error) {
+ n := node.(*ast.HTMLBlock)
+ if entering {
+ if r.Unsafe {
+ l := n.Lines().Len()
+ for i := 0; i < l; i++ {
+ line := n.Lines().At(i)
+ linev := line.Value(source)
+ // TODO1 add warn with ID.
+ var stripped bool
+ linev, stripped = r.stripHugoCtx(linev)
+ if stripped {
+ var pageContext string
+ ctx, ok := w.(*render.Context)
+ if ok {
+ p, _ := render.GetPageAndPageInnerAsPage(ctx)
+ if p != nil && p.PathInfo() != nil {
+ pageContext = p.PathInfo().Path()
+ }
+ }
+ r.logger.Warnidf(constants.WarnRenderShortcodesInHTML, ".RenderShortcodes detected inside HTML block in %q; this may not be what you intended, see https://gohugo.io/methods/page/rendershortcodes/#limitations", pageContext)
+ }
+ r.Writer.SecureWrite(w, linev)
+ }
+ } else {
+ _, _ = w.WriteString("\n")
+ }
+ } else {
+ if n.HasClosure() {
+ if r.Unsafe {
+ closure := n.ClosureLine
+ r.Writer.SecureWrite(w, closure.Value(source))
+ } else {
+ _, _ = w.WriteString("\n")
+ }
+ }
+ }
+ return ast.WalkContinue, nil
}
func (r *hugoContextRenderer) handleHugoContext(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
@@ -148,7 +216,9 @@ func (r *hugoContextRenderer) handleHugoContext(w util.BufWriter, source []byte,
return ast.WalkContinue, nil
}
-type hugoContextExtension struct{}
+type hugoContextExtension struct {
+ logger loggers.Logger
+}
func (a *hugoContextExtension) Extend(m goldmark.Markdown) {
m.Parser().AddOptions(
@@ -159,7 +229,12 @@ func (a *hugoContextExtension) Extend(m goldmark.Markdown) {
m.Renderer().AddOptions(
renderer.WithNodeRenderers(
- util.Prioritized(&hugoContextRenderer{}, 50),
+ util.Prioritized(&hugoContextRenderer{
+ logger: a.logger,
+ Config: html.Config{
+ Writer: html.DefaultWriter,
+ },
+ }, 50),
),
)
}
diff --git a/markup/goldmark/internal/render/context.go b/markup/goldmark/internal/render/context.go
index b8cf9ba54ef..feab7c535d6 100644
--- a/markup/goldmark/internal/render/context.go
+++ b/markup/goldmark/internal/render/context.go
@@ -18,6 +18,7 @@ import (
"math/bits"
"sync"
+ "github.com/gohugoio/hugo/common/paths"
htext "github.com/gohugoio/hugo/common/text"
"github.com/gohugoio/hugo/markup/converter"
@@ -175,6 +176,11 @@ func extractSourceSample(n ast.Node, src []byte) []byte {
return sample
}
+// A sub set of page.Page.
+type Page interface {
+ PathInfo() *paths.Path
+}
+
// GetPageAndPageInner returns the current page and the inner page for the given context.
func GetPageAndPageInner(rctx *Context) (any, any) {
p := rctx.DocumentContext().Document
@@ -189,6 +195,14 @@ func GetPageAndPageInner(rctx *Context) (any, any) {
return p, p
}
+// GetPageAndPageInnerAsPage returns the current page and the inner page for the given context.
+func GetPageAndPageInnerAsPage(rctx *Context) (Page, Page) {
+ v1, v2 := GetPageAndPageInner(rctx)
+ p1, _ := v1.(Page)
+ p2, _ := v2.(Page)
+ return p1, p2
+}
+
// NewBaseContext creates a new BaseContext.
func NewBaseContext(rctx *Context, renderer any, n ast.Node, src []byte, getSourceSample func() []byte, ordinal int) hooks.BaseContext {
if getSourceSample == nil {