diff options
author | Bjørn Erik Pedersen <[email protected]> | 2024-11-03 13:37:11 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2024-11-03 14:17:10 +0100 |
commit | 62a96cef7fbe496271e8cca0f234193f6106a771 (patch) | |
tree | 305df5dcc416bb3e6189b75fc70444f315ae9514 | |
parent | 30d9aea8607e8143968d73d11a8c1c0e51d34343 (diff) | |
download | hugo-62a96cef7fbe496271e8cca0f234193f6106a771.tar.gz hugo-62a96cef7fbe496271e8cca0f234193f6106a771.zip |
markup/goldmark: Add warning (using Warnidf) on Goldmark <!-- raw HTML omitted -->
Fixes #12997
-rw-r--r-- | common/constants/constants.go | 1 | ||||
-rw-r--r-- | markup/goldmark/goldmark_integration_test.go | 25 | ||||
-rw-r--r-- | markup/goldmark/hugocontext/hugocontext.go | 13 |
3 files changed, 34 insertions, 5 deletions
diff --git a/common/constants/constants.go b/common/constants/constants.go index 752aef72c..48813e79b 100644 --- a/common/constants/constants.go +++ b/common/constants/constants.go @@ -22,6 +22,7 @@ const ( WarnFrontMatterParamsOverrides = "warning-frontmatter-params-overrides" WarnRenderShortcodesInHTML = "warning-rendershortcodes-in-html" + WarnGoldmarkRawHTML = "warning-goldmark-raw-html" ) // Field/method names with special meaning. diff --git a/markup/goldmark/goldmark_integration_test.go b/markup/goldmark/goldmark_integration_test.go index 19b18692e..8b7cc5a54 100644 --- a/markup/goldmark/goldmark_integration_test.go +++ b/markup/goldmark/goldmark_integration_test.go @@ -802,3 +802,28 @@ H~2~0 "<p>1<sup>st</sup></p>", ) } + +// Issue 12997. +func TestGoldmarkRawHTMLWarning(t *testing.T) { + files := ` +-- hugo.toml -- +disableKinds = ['home','rss','section','sitemap','taxonomy','term'] +markup.goldmark.renderer.unsafe = false +-- content/p1.md -- +--- +title: "p1" +--- +<div>Some raw HTML</div> +-- 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") +} diff --git a/markup/goldmark/hugocontext/hugocontext.go b/markup/goldmark/hugocontext/hugocontext.go index 223c30c91..912e9eb3c 100644 --- a/markup/goldmark/hugocontext/hugocontext.go +++ b/markup/goldmark/hugocontext/hugocontext.go @@ -159,6 +159,14 @@ func (r *hugoContextRenderer) renderHTMLBlock( w util.BufWriter, source []byte, node ast.Node, entering bool, ) (ast.WalkStatus, error) { n := node.(*ast.HTMLBlock) + var p any + ctx, ok := w.(*render.Context) + if ok { + p, _ = render.GetPageAndPageInner(ctx) + } + if !r.Unsafe { + r.logger.Warnidf(constants.WarnGoldmarkRawHTML, "Raw HTML omitted from %q; see https://gohugo.io/getting-started/configuration-markup/#rendererunsafe", p) + } if entering { if r.Unsafe { l := n.Lines().Len() @@ -168,11 +176,6 @@ func (r *hugoContextRenderer) renderHTMLBlock( var stripped bool linev, stripped = r.stripHugoCtx(linev) if stripped { - var p any - ctx, ok := w.(*render.Context) - if ok { - p, _ = render.GetPageAndPageInner(ctx) - } 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) } |