]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
markup: Goldmark log "Raw HTML omitted" warning also for inline HTML
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 5 Nov 2024 08:29:10 +0000 (09:29 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 5 Nov 2024 10:40:42 +0000 (11:40 +0100)
Fixes #13013

markup/goldmark/goldmark_integration_test.go
markup/goldmark/hugocontext/hugocontext.go

index 8b7cc5a543253938d53930f29b98ae5cdb0b0ad3..82579069bb2323234669ad4153a689e8e3088c26 100644 (file)
@@ -804,7 +804,7 @@ H~2~0
 }
 
 // Issue 12997.
-func TestGoldmarkRawHTMLWarning(t *testing.T) {
+func TestGoldmarkRawHTMLWarningBlocks(t *testing.T) {
        files := `
 -- hugo.toml --
 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
@@ -827,3 +827,27 @@ title: "p1"
        b.AssertFileContent("public/p1/index.html", "! <!-- raw HTML omitted -->")
        b.AssertLogContains("! WARN")
 }
+
+func TestGoldmarkRawHTMLWarningInline(t *testing.T) {
+       files := `
+-- hugo.toml --
+disableKinds = ['home','rss','section','sitemap','taxonomy','term']
+markup.goldmark.renderer.unsafe = false
+-- content/p1.md --
+---
+title: "p1"
+---
+<em>raw HTML</em>
+-- layouts/_default/single.html --
+{{ .Content }}
+`
+
+       b := hugolib.Test(t, files, hugolib.TestOptWarn())
+
+       b.AssertFileContent("public/p1/index.html", "<!-- raw HTML omitted -->")
+       b.AssertLogContains("WARN  Raw HTML omitted from \"/content/p1.md\"; see https://gohugo.io/getting-started/configuration-markup/#rendererunsafe\nYou can suppress this warning by adding the following to your site configuration:\nignoreLogs = ['warning-goldmark-raw-html']")
+
+       b = hugolib.Test(t, strings.ReplaceAll(files, "markup.goldmark.renderer.unsafe = false", "markup.goldmark.renderer.unsafe = true"), hugolib.TestOptWarn())
+       b.AssertFileContent("public/p1/index.html", "! <!-- raw HTML omitted -->")
+       b.AssertLogContains("! WARN")
+}
index 4971456be4205c7bf0539438408a228010f4fd80..a10e095efb984efb25742749fed709268b4da03f 100644 (file)
@@ -145,6 +145,7 @@ func (r *hugoContextRenderer) SetOption(name renderer.OptionName, value any) {
 
 func (r *hugoContextRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
        reg.Register(kindHugoContext, r.handleHugoContext)
+       reg.Register(ast.KindRawHTML, r.renderRawHTML)
        reg.Register(ast.KindHTMLBlock, r.renderHTMLBlock)
 }
 
@@ -155,16 +156,25 @@ func (r *hugoContextRenderer) stripHugoCtx(b []byte) ([]byte, bool) {
        return hugoCtxRe.ReplaceAll(b, nil), true
 }
 
+func (r *hugoContextRenderer) logRawHTMLEmittedWarn(w util.BufWriter) {
+       r.logger.Warnidf(constants.WarnGoldmarkRawHTML, "Raw HTML omitted from %q; see https://gohugo.io/getting-started/configuration-markup/#rendererunsafe", r.getPage(w))
+}
+
+func (r *hugoContextRenderer) getPage(w util.BufWriter) any {
+       var p any
+       ctx, ok := w.(*render.Context)
+       if ok {
+               p, _ = render.GetPageAndPageInner(ctx)
+       }
+       return p
+}
+
+// HTML rendering based on Goldmark implementation.
 func (r *hugoContextRenderer) renderHTMLBlock(
        w util.BufWriter, source []byte, node ast.Node, entering bool,
 ) (ast.WalkStatus, error) {
        n := node.(*ast.HTMLBlock)
        if entering {
-               var p any
-               ctx, ok := w.(*render.Context)
-               if ok {
-                       p, _ = render.GetPageAndPageInner(ctx)
-               }
                if r.Unsafe {
                        l := n.Lines().Len()
                        for i := 0; i < l; i++ {
@@ -173,12 +183,12 @@ func (r *hugoContextRenderer) renderHTMLBlock(
                                var stripped bool
                                linev, stripped = r.stripHugoCtx(linev)
                                if stripped {
-                                       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", p)
+                                       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", r.getPage(w))
                                }
                                r.Writer.SecureWrite(w, linev)
                        }
                } else {
-                       r.logger.Warnidf(constants.WarnGoldmarkRawHTML, "Raw HTML omitted from %q; see https://gohugo.io/getting-started/configuration-markup/#rendererunsafe", p)
+                       r.logRawHTMLEmittedWarn(w)
                        _, _ = w.WriteString("<!-- raw HTML omitted -->\n")
                }
        } else {
@@ -194,6 +204,26 @@ func (r *hugoContextRenderer) renderHTMLBlock(
        return ast.WalkContinue, nil
 }
 
+func (r *hugoContextRenderer) renderRawHTML(
+       w util.BufWriter, source []byte, node ast.Node, entering bool,
+) (ast.WalkStatus, error) {
+       if !entering {
+               return ast.WalkSkipChildren, nil
+       }
+       if r.Unsafe {
+               n := node.(*ast.RawHTML)
+               l := n.Segments.Len()
+               for i := 0; i < l; i++ {
+                       segment := n.Segments.At(i)
+                       _, _ = w.Write(segment.Value(source))
+               }
+               return ast.WalkSkipChildren, nil
+       }
+       r.logRawHTMLEmittedWarn(w)
+       _, _ = w.WriteString("<!-- raw HTML omitted -->")
+       return ast.WalkSkipChildren, nil
+}
+
 func (r *hugoContextRenderer) handleHugoContext(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
        if !entering {
                return ast.WalkContinue, nil