Skip to content

Commit

Permalink
markup/goldmark: Add warning (using Warnidf) on Goldmark <!-- raw HTM…
Browse files Browse the repository at this point in the history
…L omitted -->

Fixes #12997
  • Loading branch information
bep committed Nov 3, 2024
1 parent 30d9aea commit 62a96ce
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
1 change: 1 addition & 0 deletions common/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
25 changes: 25 additions & 0 deletions markup/goldmark/goldmark_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
13 changes: 8 additions & 5 deletions markup/goldmark/hugocontext/hugocontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
}

Expand Down

0 comments on commit 62a96ce

Please sign in to comment.