markup/highlight: Escape lang in default code block rendering

This commit is contained in:
Bjørn Erik Pedersen
2026-06-18 13:48:46 +02:00
parent e8988c3141
commit ce1a7e0bce
2 changed files with 31 additions and 0 deletions
@@ -408,3 +408,32 @@ Some code.
b.AssertFileContent("public/p1/index.html", "p1.md:7:1")
}
func TestCodeblockLangEscape(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
-- layouts/single.html --
{{ .Content }}
-- content/p1.md --
---
title: "p1"
---
## Simple
§§§a"><script>A</script>
Some code.
§§§
§§§a"><script>B</script> {hl_inline=true}
Some code.
§§§
`
b := hugolib.Test(t, files)
b.AssertFileContent("public/p1/index.html", "! <script>")
}
+2
View File
@@ -275,6 +275,7 @@ func (p *preWrapper) Start(code bool, styleAttr string) string {
}
func inlineCodeAttrs(lang string) string {
lang = gohtml.EscapeString(lang)
return fmt.Sprintf(` class="code-inline language-%s"`, lang)
}
@@ -282,6 +283,7 @@ func WritePreStart(w io.Writer, language, styleAttr string) {
fmt.Fprintf(w, `<pre tabindex="0"%s>`, styleAttr)
fmt.Fprint(w, "<code")
if language != "" {
language = gohtml.EscapeString(language)
fmt.Fprint(w, ` class="language-`+language+`"`)
fmt.Fprint(w, ` data-lang="`+language+`"`)
}