Skip to content

Commit

Permalink
helpers: Fix panic with invalid defaultMarkdownHandler
Browse files Browse the repository at this point in the history
Fixes #9968
  • Loading branch information
bep committed Jun 4, 2022
1 parent c7d5f9f commit 311b800
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
26 changes: 26 additions & 0 deletions hugolib/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

}
15 changes: 12 additions & 3 deletions hugolib/integrationtest_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -338,6 +345,8 @@ func (s *IntegrationTestBuilder) initBuilder() {

}
})

return initErr
}

func (s *IntegrationTestBuilder) absFilename(filename string) string {
Expand Down
14 changes: 13 additions & 1 deletion markup/markup.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package markup

import (
"fmt"
"strings"

"github.com/gohugoio/hugo/markup/highlight"
Expand Down Expand Up @@ -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)
Expand All @@ -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...)
Expand All @@ -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,
Expand Down

0 comments on commit 311b800

Please sign in to comment.