Skip to content

Commit

Permalink
markup/highlight: Add wrapperClass option
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bep committed Dec 25, 2024
1 parent 845b888 commit ec0caae
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
5 changes: 5 additions & 0 deletions markup/highlight/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,18 @@ var DefaultConfig = Config{
NoClasses: true,
LineNumbersInTable: true,
TabWidth: 4,
WrapperClass: "highlight",
}

type Config struct {
Style string

// Enable syntax highlighting of fenced code blocks.
CodeFences bool

// The class or classes to use for the outermost element of the highlighted code.
WrapperClass string

// Use inline CSS styles.
NoClasses bool

Expand Down
7 changes: 4 additions & 3 deletions markup/highlight/highlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func highlight(fw hugio.FlexiWriter, code, lang string, attributes []attributes.
}

if !cfg.Hl_inline {
writeDivStart(w, attributes)
writeDivStart(w, attributes, cfg.WrapperClass)
}

options := cfg.toHTMLOptions()
Expand Down Expand Up @@ -303,8 +303,9 @@ func (s startEnd) End(code bool) string {
return s.end(code)
}

func writeDivStart(w hugio.FlexiWriter, attrs []attributes.Attribute) {
w.WriteString(`<div class="highlight`)
func writeDivStart(w hugio.FlexiWriter, attrs []attributes.Attribute, wrapperClass string) {
w.WriteString(`<div class="`)
w.WriteString(wrapperClass)
if attrs != nil {
for _, attr := range attrs {
if attr.Name == "class" {
Expand Down
26 changes: 26 additions & 0 deletions markup/highlight/highlight_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,29 @@ xəx := 0
<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
`)
}

0 comments on commit ec0caae

Please sign in to comment.