Skip to content

Commit ec0caae

Browse files
committed
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 tailwindlabs/tailwindcss-typography#363
1 parent 845b888 commit ec0caae

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

markup/highlight/config.go

+5
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,18 @@ var DefaultConfig = Config{
4545
NoClasses: true,
4646
LineNumbersInTable: true,
4747
TabWidth: 4,
48+
WrapperClass: "highlight",
4849
}
4950

5051
type Config struct {
5152
Style string
5253

54+
// Enable syntax highlighting of fenced code blocks.
5355
CodeFences bool
5456

57+
// The class or classes to use for the outermost element of the highlighted code.
58+
WrapperClass string
59+
5560
// Use inline CSS styles.
5661
NoClasses bool
5762

markup/highlight/highlight.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func highlight(fw hugio.FlexiWriter, code, lang string, attributes []attributes.
202202
}
203203

204204
if !cfg.Hl_inline {
205-
writeDivStart(w, attributes)
205+
writeDivStart(w, attributes, cfg.WrapperClass)
206206
}
207207

208208
options := cfg.toHTMLOptions()
@@ -303,8 +303,9 @@ func (s startEnd) End(code bool) string {
303303
return s.end(code)
304304
}
305305

306-
func writeDivStart(w hugio.FlexiWriter, attrs []attributes.Attribute) {
307-
w.WriteString(`<div class="highlight`)
306+
func writeDivStart(w hugio.FlexiWriter, attrs []attributes.Attribute, wrapperClass string) {
307+
w.WriteString(`<div class="`)
308+
w.WriteString(wrapperClass)
308309
if attrs != nil {
309310
for _, attr := range attrs {
310311
if attr.Name == "class" {

markup/highlight/highlight_integration_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,29 @@ xəx := 0
103103
<span class="nx">xəx</span>
104104
`)
105105
}
106+
107+
func TestHighlightClass(t *testing.T) {
108+
t.Parallel()
109+
110+
files := `
111+
-- config.toml --
112+
[markup.highlight]
113+
noClasses = false
114+
wrapperClass = "highlight no-prose"
115+
-- content/_index.md --
116+
---
117+
title: home
118+
---
119+
§§§go
120+
xəx := 0
121+
§§§
122+
-- layouts/index.html --
123+
{{ .Content }}
124+
`
125+
126+
b := hugolib.Test(t, files)
127+
128+
b.AssertFileContent("public/index.html", `
129+
<div class="highlight no-prose"><pre
130+
`)
131+
}

0 commit comments

Comments
 (0)