Skip to content
/ hugo Public
forked from gohugoio/hugo

Commit

Permalink
Exclude event attributes when rendering markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
jmooring committed Feb 4, 2022
1 parent 3336762 commit 7f7a497
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
52 changes: 52 additions & 0 deletions hugolib/content_render_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,55 @@ disableKinds = ["page", "section", "taxonomy", "term"]
b.BuildE(BuildCfg{})
b.Assert(int(logger.LogCounters().WarnCounter.Count()), qt.Equals, 0)
}

func TestAttributeExclusion(t *testing.T) {
b := newTestSitesBuilder(t)

b.WithConfigFile("toml", `
[markup.goldmark.renderer]
unsafe = false
[markup.goldmark.parser.attribute]
block = true
title = true
`)

b.WithTemplates("_default/single.html", "{{ .Content }}")

b.WithContent("p1.md", `---
title: "p1"
---
## Heading {class="a" onclick="alert('heading')" linenos="inline"}
> Blockquote
{class="b" ondblclick="alert('blockquote')" LINENOS="inline"}
~~~bash {id="c" onmouseover="alert('code fence')" linenos="true" linenostart=2 hl_lines=[3] hl_style="dracula"}
javascript
security
issue
~~~
`,
)

b.Build(BuildCfg{})

b.AssertFileContent("public/p1/index.html", `
<h2 class="a" id="heading">Heading</h2>
<blockquote class="b"><p>Blockquote</p>
</blockquote>
<div class="highlight" id="c"><div style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="display:block;width:100%;background-color:#3d3f4a"><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span></span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">javascript
security
<span style="display:block;width:100%;background-color:#3d3f4a">issue
</span></code></pre></td></tr></table>
</div>
</div>
`)
}
3 changes: 2 additions & 1 deletion markup/goldmark/render_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ func renderAttributes(w util.BufWriter, skipClass bool, attributes ...ast.Attrib
continue
}

if attributeExcludes[string(attr.Name)] {
a := strings.ToLower(string(attr.Name))
if attributeExcludes[a] || strings.HasPrefix(a, "on") {
continue
}

Expand Down

0 comments on commit 7f7a497

Please sign in to comment.