]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
markup/highlight: Add wrapperClass option
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 25 Dec 2024 17:20:16 +0000 (18:20 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 25 Dec 2024 18:31:47 +0000 (19:31 +0100)
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

markup/highlight/config.go
markup/highlight/highlight.go
markup/highlight/highlight_integration_test.go

index 62db6b237eebba866425be801c9d5134349e0417..338ea77e5028501718bd305f374749e66004d3df 100644 (file)
@@ -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
 
index a284b59813cc049032e786caeecfb41b4c917da5..30f225c0b2f1301edf52eb61e7a399f99be65897 100644 (file)
@@ -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()
@@ -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" {
index d36bc820a7b259f34bb99b655474d62da76cbc02..e6cc16d2eaad11f71b3843ca209edc745f3f4e5e 100644 (file)
@@ -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
+       `)
+}