]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
markup/goldmark: Support passthrough extension
authorJeremy Kun <j2kun@users.noreply.github.com>
Sat, 6 Jan 2024 16:53:24 +0000 (08:53 -0800)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 26 Jan 2024 07:11:35 +0000 (08:11 +0100)
Fixes #10894

go.mod
go.sum
markup/goldmark/convert.go
markup/goldmark/goldmark_config/config.go
markup/goldmark/integration_test.go

diff --git a/go.mod b/go.mod
index 3dbd300da396507e26213e151d4807ef3ff50775..bc782e012793df6bc6bb3ae0f8ba7f3a12d6b13e 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -35,6 +35,7 @@ require (
        github.com/gobuffalo/flect v1.0.2
        github.com/gobwas/glob v0.2.3
        github.com/gohugoio/go-i18n/v2 v2.1.3-0.20230805085216-e63c13218d0e
+       github.com/gohugoio/hugo-goldmark-extensions/passthrough v0.1.0
        github.com/gohugoio/locales v0.14.0
        github.com/gohugoio/localescompressed v1.0.1
        github.com/gohugoio/testmodBuilder/mods v0.0.0-20190520184928-c56af20f2e95
diff --git a/go.sum b/go.sum
index 212ec6457a6db13002545ff650724fdddb99f2bb..ccad130bad03c2e66c262c9af485f2066c844684 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -166,6 +166,8 @@ github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
 github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
 github.com/gohugoio/go-i18n/v2 v2.1.3-0.20230805085216-e63c13218d0e h1:QArsSubW7eDh8APMXkByjQWvuljwPGAGQpJEFn0F0wY=
 github.com/gohugoio/go-i18n/v2 v2.1.3-0.20230805085216-e63c13218d0e/go.mod h1:3Ltoo9Banwq0gOtcOwxuHG6omk+AwsQPADyw2vQYOJQ=
+github.com/gohugoio/hugo-goldmark-extensions/passthrough v0.1.0 h1:oFQ3f1M3Ook6amHmbqVu/uBRrQ6yjMDFkIv4HQr0f1Y=
+github.com/gohugoio/hugo-goldmark-extensions/passthrough v0.1.0/go.mod h1:g9CCh+Ci2IMbPUrVJuXbBTrA+rIIx5+hDQ4EXYaQDoM=
 github.com/gohugoio/locales v0.14.0 h1:Q0gpsZwfv7ATHMbcTNepFd59H7GoykzWJIxi113XGDc=
 github.com/gohugoio/locales v0.14.0/go.mod h1:ip8cCAv/cnmVLzzXtiTpPwgJ4xhKZranqNqtoIu0b/4=
 github.com/gohugoio/localescompressed v1.0.1 h1:KTYMi8fCWYLswFyJAeOtuk/EkXR/KPTHHNN9OS+RTxo=
index 6ebcd8a7779f581ff236ec028bba7468ea8899d4..56cc56fcdd41154aa8a91c0e57bd20366b34f14f 100644 (file)
@@ -19,6 +19,7 @@ import (
 
        "github.com/gohugoio/hugo/identity"
 
+       "github.com/gohugoio/hugo-goldmark-extensions/passthrough"
        "github.com/gohugoio/hugo/markup/goldmark/codeblocks"
        "github.com/gohugoio/hugo/markup/goldmark/goldmark_config"
        "github.com/gohugoio/hugo/markup/goldmark/images"
@@ -154,6 +155,33 @@ func newMarkdown(pcfg converter.ProviderConfig) goldmark.Markdown {
                extensions = append(extensions, c)
        }
 
+       if cfg.Extensions.Passthrough.Enable {
+               configuredInlines := cfg.Extensions.Passthrough.Delimiters.Inline
+               configuredBlocks := cfg.Extensions.Passthrough.Delimiters.Block
+
+               inlineDelimiters := make([]passthrough.Delimiters, len(configuredInlines))
+               blockDelimiters := make([]passthrough.Delimiters, len(configuredBlocks))
+
+               for i, d := range configuredInlines {
+                       inlineDelimiters[i] = passthrough.Delimiters{
+                               Open:  d[0],
+                               Close: d[1],
+                       }
+               }
+
+               for i, d := range configuredBlocks {
+                       blockDelimiters[i] = passthrough.Delimiters{
+                               Open:  d[0],
+                               Close: d[1],
+                       }
+               }
+
+               extensions = append(extensions, passthrough.NewPassthroughWithDelimiters(
+                       inlineDelimiters,
+                       blockDelimiters,
+               ))
+       }
+
        if pcfg.Conf.EnableEmoji() {
                extensions = append(extensions, emoji.Emoji)
        }
index cdfb4e7cc00ecffdf590a50cc46097b865397e7a..ba1874a184b8569cde19a3718b757443352a1ee6 100644 (file)
@@ -49,6 +49,13 @@ var Default = Config{
                        EastAsianLineBreaksStyle: "simple",
                        EscapedSpace:             false,
                },
+               Passthrough: Passthrough{
+                       Enable: false,
+                       Delimiters: DelimitersConfig{
+                               Inline: [][]string{},
+                               Block:  [][]string{},
+                       },
+               },
        },
        Renderer: Renderer{
                Unsafe: false,
@@ -75,6 +82,7 @@ type Extensions struct {
        Typographer    Typographer
        Footnote       bool
        DefinitionList bool
+       Passthrough    Passthrough
 
        // GitHub flavored markdown
        Table           bool
@@ -112,6 +120,28 @@ type Typographer struct {
        Apostrophe string
 }
 
+// Passthrough hold passthrough configuration.
+// github.com/hugoio/hugo-goldmark-extensions/passthrough
+type Passthrough struct {
+       // Whether to enable the extension
+       Enable bool
+
+       // The delimiters to use for inline and block passthroughs.
+       Delimiters DelimitersConfig
+}
+
+type DelimitersConfig struct {
+       // The delimiters to use for inline passthroughs. Each entry in the list
+       // is a size-2 list of strings, where the first string is the opening delimiter
+       // and the second string is the closing delimiter, e.g.,
+       //
+       // [["$", "$"], ["\\(", "\\)"]]
+       Inline [][]string
+
+       // The delimiters to use for block passthroughs. Same format as Inline.
+       Block [][]string
+}
+
 type CJK struct {
        // Whether to enable CJK support.
        Enable bool
index a2bf1637489b2c78c5cbbf093fe0255f1df9aceb..c9c6ef3386423222dd0fb61ed142fdffd2470a88 100644 (file)
@@ -711,3 +711,103 @@ echo "hello";
        b := hugolib.Test(t, files)
        b.AssertFileContent("public/index.html", "<div class=foo><?php\necho \"hello\";\n?>\n</div>")
 }
+
+// Issue #10894
+func TestPassthroughInlineFences(t *testing.T) {
+       t.Parallel()
+
+       files := `
+-- config.toml --
+[markup.goldmark.extensions.passthrough]
+enable = true
+[markup.goldmark.extensions.passthrough.delimiters]
+inline = [['$', '$'], ['\(', '\)']]
+-- content/p1.md --
+---
+title: "p1"
+---
+## LaTeX test
+
+Inline equation that would be mangled by default parser: $a^*=x-b^*$
+
+-- layouts/_default/single.html --
+{{ .Content }}
+`
+
+       b := hugolib.Test(t, files)
+       b.AssertFileContent("public/p1/index.html", `
+               $a^*=x-b^*$
+       `)
+}
+
+func TestPassthroughBlockFences(t *testing.T) {
+       t.Parallel()
+
+       files := `
+-- config.toml --
+[markup.goldmark.extensions.passthrough]
+enable = true
+[markup.goldmark.extensions.passthrough.delimiters]
+block = [['$$', '$$']]
+-- content/p1.md --
+---
+title: "p1"
+---
+## LaTeX test
+
+Block equation that would be mangled by default parser:
+
+$$a^*=x-b^*$$
+
+-- layouts/_default/single.html --
+{{ .Content }}
+`
+
+       b := hugolib.Test(t, files)
+       b.AssertFileContent("public/p1/index.html", `
+               $$a^*=x-b^*$$
+       `)
+}
+
+func TestPassthroughWithAlternativeFences(t *testing.T) {
+       t.Parallel()
+
+       files := `
+-- config.toml --
+[markup.goldmark.extensions.passthrough]
+enable = true
+[markup.goldmark.extensions.passthrough.delimiters]
+inline = [['(((', ')))']]
+block = [['%!%', '%!%']]
+-- content/p1.md --
+---
+title: "p1"
+---
+## LaTeX test
+
+Inline equation that would be mangled by default parser: (((a^*=x-b^*)))
+Inline equation that should be mangled by default parser: $a^*=x-b^*$
+
+Block equation that would be mangled by default parser:
+
+%!%
+a^*=x-b^*
+%!%
+
+-- layouts/_default/single.html --
+{{ .Content }}
+`
+
+       b := hugolib.Test(t, files)
+       b.AssertFileContent("public/p1/index.html", `
+               (((a^*=x-b^*)))
+       `)
+       b.AssertFileContent("public/p1/index.html", `
+               $a^<em>=x-b^</em>$
+       `)
+       b.AssertFileContent("public/p1/index.html", `
+%!%
+a^*=x-b^*
+%!%
+       `)
+}