helpers: Fix panic with invalid defaultMarkdownHandler

Fixes #9968
This commit is contained in:
Bjørn Erik Pedersen 2022-06-04 17:39:34 +02:00
parent c7d5f9f067
commit 311b8008bf
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
3 changed files with 51 additions and 4 deletions

View file

@ -756,3 +756,29 @@ theme_param="themevalue2"
})
}
func TestInvalidDefaultMarkdownHandler(t *testing.T) {
t.Parallel()
files := `
-- config.toml --
[markup]
defaultMarkdownHandler = 'blackfriday'
-- content/_index.md --
## Foo
-- layouts/index.html --
{{ .Content }}
`
b, err := NewIntegrationTestBuilder(
IntegrationTestConfig{
T: t,
TxtarString: files,
},
).BuildE()
b.Assert(err, qt.IsNotNil)
b.Assert(err.Error(), qt.Contains, "Configured defaultMarkdownHandler \"blackfriday\" not found. Did you mean to use goldmark? Blackfriday was removed in Hugo v0.100.0.")
}

View file

@ -197,7 +197,10 @@ func (s *IntegrationTestBuilder) Build() *IntegrationTestBuilder {
func (s *IntegrationTestBuilder) BuildE() (*IntegrationTestBuilder, error) {
s.Helper()
s.initBuilder()
if err := s.initBuilder(); err != nil {
return s, err
}
err := s.build(BuildCfg{})
return s, err
}
@ -267,7 +270,8 @@ func (s *IntegrationTestBuilder) FileContent(filename string) string {
return s.readWorkingDir(s, s.fs, filepath.FromSlash(filename))
}
func (s *IntegrationTestBuilder) initBuilder() {
func (s *IntegrationTestBuilder) initBuilder() error {
var initErr error
s.builderInit.Do(func() {
var afs afero.Fs
if s.Cfg.NeedsOsFS {
@ -320,7 +324,10 @@ func (s *IntegrationTestBuilder) initBuilder() {
depsCfg := deps.DepsCfg{Cfg: cfg, Fs: fs, Running: s.Cfg.Running, Logger: logger}
sites, err := NewHugoSites(depsCfg)
s.Assert(err, qt.IsNil)
if err != nil {
initErr = err
return
}
s.H = sites
s.fs = fs
@ -338,6 +345,8 @@ func (s *IntegrationTestBuilder) initBuilder() {
}
})
return initErr
}
func (s *IntegrationTestBuilder) absFilename(filename string) string {

View file

@ -14,6 +14,7 @@
package markup
import (
"fmt"
"strings"
"github.com/gohugoio/hugo/markup/highlight"
@ -43,6 +44,8 @@ func NewConverterProvider(cfg converter.ProviderConfig) (ConverterProvider, erro
}
cfg.MarkupConfig = markupConfig
defaultHandler := cfg.MarkupConfig.DefaultMarkdownHandler
var defaultFound bool
add := func(p converter.ProviderProvider, aliases ...string) error {
c, err := p.New(cfg)
@ -54,8 +57,9 @@ func NewConverterProvider(cfg converter.ProviderConfig) (ConverterProvider, erro
aliases = append(aliases, name)
if strings.EqualFold(name, cfg.MarkupConfig.DefaultMarkdownHandler) {
if strings.EqualFold(name, defaultHandler) {
aliases = append(aliases, "markdown")
defaultFound = true
}
addConverter(converters, c, aliases...)
@ -78,6 +82,14 @@ func NewConverterProvider(cfg converter.ProviderConfig) (ConverterProvider, erro
return nil, err
}
if !defaultFound {
msg := "markup: Configured defaultMarkdownHandler %q not found."
if defaultHandler == "blackfriday" {
msg += " Did you mean to use goldmark? Blackfriday was removed in Hugo v0.100.0."
}
return nil, fmt.Errorf(msg, defaultHandler)
}
return &converterRegistry{
config: cfg,
converters: converters,