mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-19 21:21:39 +02:00
committed by
Bjørn Erik Pedersen
parent
2dd608378d
commit
d0d2c6795e
@@ -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)
|
||||
}
|
||||
|
@@ -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
|
||||
|
@@ -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^*
|
||||
%!%
|
||||
`)
|
||||
}
|
||||
|
Reference in New Issue
Block a user