markup/highlight: Add wrapperClass option

The need comes from Tailwind's typography plugin. There's currently no way to turn that off outside of the markup, see https://github.com/tailwindlabs/tailwindcss-typography/pull/363
This commit is contained in:
Bjørn Erik Pedersen 2024-12-25 18:20:16 +01:00
parent 845b8885de
commit ec0caaec7c
3 changed files with 35 additions and 3 deletions

View file

@ -45,13 +45,18 @@ var DefaultConfig = Config{
NoClasses: true, NoClasses: true,
LineNumbersInTable: true, LineNumbersInTable: true,
TabWidth: 4, TabWidth: 4,
WrapperClass: "highlight",
} }
type Config struct { type Config struct {
Style string Style string
// Enable syntax highlighting of fenced code blocks.
CodeFences bool CodeFences bool
// The class or classes to use for the outermost element of the highlighted code.
WrapperClass string
// Use inline CSS styles. // Use inline CSS styles.
NoClasses bool NoClasses bool

View file

@ -202,7 +202,7 @@ func highlight(fw hugio.FlexiWriter, code, lang string, attributes []attributes.
} }
if !cfg.Hl_inline { if !cfg.Hl_inline {
writeDivStart(w, attributes) writeDivStart(w, attributes, cfg.WrapperClass)
} }
options := cfg.toHTMLOptions() options := cfg.toHTMLOptions()
@ -303,8 +303,9 @@ func (s startEnd) End(code bool) string {
return s.end(code) return s.end(code)
} }
func writeDivStart(w hugio.FlexiWriter, attrs []attributes.Attribute) { func writeDivStart(w hugio.FlexiWriter, attrs []attributes.Attribute, wrapperClass string) {
w.WriteString(`<div class="highlight`) w.WriteString(`<div class="`)
w.WriteString(wrapperClass)
if attrs != nil { if attrs != nil {
for _, attr := range attrs { for _, attr := range attrs {
if attr.Name == "class" { if attr.Name == "class" {

View file

@ -103,3 +103,29 @@ xəx := 0
<span class="nx">xəx</span> <span class="nx">xəx</span>
`) `)
} }
func TestHighlightClass(t *testing.T) {
t.Parallel()
files := `
-- config.toml --
[markup.highlight]
noClasses = false
wrapperClass = "highlight no-prose"
-- content/_index.md --
---
title: home
---
§§§go
xəx := 0
§§§
-- layouts/index.html --
{{ .Content }}
`
b := hugolib.Test(t, files)
b.AssertFileContent("public/index.html", `
<div class="highlight no-prose"><pre
`)
}