Skip to content

Commit

Permalink
markup/goldmark: Make the autoID type config a string
Browse files Browse the repository at this point in the history
To potentially make room for one more.

See #6707
  • Loading branch information
bep committed Jan 5, 2020
1 parent 469351d commit 8f071fc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
6 changes: 4 additions & 2 deletions markup/goldmark/autoid.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"unicode"
"unicode/utf8"

"github.com/gohugoio/hugo/markup/goldmark/goldmark_config"

"github.com/gohugoio/hugo/common/text"

"github.com/yuin/goldmark/ast"
Expand Down Expand Up @@ -85,10 +87,10 @@ type idFactory struct {
vals map[string]struct{}
}

func newIDFactory(asciiOnly bool) *idFactory {
func newIDFactory(idType string) *idFactory {
return &idFactory{
vals: make(map[string]struct{}),
asciiOnly: asciiOnly,
asciiOnly: idType == goldmark_config.AutoHeadingIDTypeGitHubAscii,
}
}

Expand Down
6 changes: 3 additions & 3 deletions markup/goldmark/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
"github.com/spf13/afero"

"github.com/gohugoio/hugo/hugofs"

"github.com/gohugoio/hugo/markup/converter"
"github.com/gohugoio/hugo/markup/goldmark/goldmark_config"
"github.com/gohugoio/hugo/markup/highlight"
"github.com/gohugoio/hugo/markup/tableofcontents"
"github.com/yuin/goldmark"
Expand Down Expand Up @@ -57,7 +57,7 @@ func (p provide) New(cfg converter.ProviderConfig) (converter.Provider, error) {
cfg: cfg,
md: md,
sanitizeAnchorName: func(s string) string {
return sanitizeAnchorNameString(s, cfg.MarkupConfig.Goldmark.Parser.AutoHeadingIDAsciiOnly)
return sanitizeAnchorNameString(s, cfg.MarkupConfig.Goldmark.Parser.AutoHeadingIDType == goldmark_config.AutoHeadingIDTypeGitHub)
},
}, nil
}), nil
Expand Down Expand Up @@ -280,7 +280,7 @@ func (c *goldmarkConverter) Supports(feature identity.Identity) bool {
}

func (c *goldmarkConverter) newParserContext(rctx converter.RenderContext) *parserContext {
ctx := parser.NewContext(parser.WithIDs(newIDFactory(c.cfg.MarkupConfig.Goldmark.Parser.AutoHeadingIDAsciiOnly)))
ctx := parser.NewContext(parser.WithIDs(newIDFactory(c.cfg.MarkupConfig.Goldmark.Parser.AutoHeadingIDType)))
ctx.Set(tocEnableKey, rctx.RenderTOC)
return &parserContext{
Context: ctx,
Expand Down
4 changes: 3 additions & 1 deletion markup/goldmark/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"strings"
"testing"

"github.com/gohugoio/hugo/markup/goldmark/goldmark_config"

"github.com/gohugoio/hugo/markup/highlight"

"github.com/gohugoio/hugo/markup/markup_config"
Expand Down Expand Up @@ -169,7 +171,7 @@ func TestConvertAutoIDAsciiOnly(t *testing.T) {
## God is Good: 神真美好
`
mconf := markup_config.Default
mconf.Goldmark.Parser.AutoHeadingIDAsciiOnly = true
mconf.Goldmark.Parser.AutoHeadingIDType = goldmark_config.AutoHeadingIDTypeGitHubAscii
b := convert(c, mconf, content)
got := string(b.Bytes())

Expand Down
17 changes: 12 additions & 5 deletions markup/goldmark/goldmark_config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
// Package goldmark_config holds Goldmark related configuration.
package goldmark_config

const (
AutoHeadingIDTypeGitHub = "github"
AutoHeadingIDTypeGitHubAscii = "github-ascii"
)

// DefaultConfig holds the default Goldmark configuration.
var Default = Config{
Extensions: Extensions{
Expand All @@ -29,8 +34,9 @@ var Default = Config{
Unsafe: false,
},
Parser: Parser{
AutoHeadingID: true,
Attribute: true,
AutoHeadingID: true,
AutoHeadingIDType: AutoHeadingIDTypeGitHub,
Attribute: true,
},
}

Expand Down Expand Up @@ -69,9 +75,10 @@ type Parser struct {
// auto generated heading ids.
AutoHeadingID bool

// When AutoHeadingID is enabled this will generate IDs with Ascii
// characters only.
AutoHeadingIDAsciiOnly bool
// The strategy to use when generating heading IDs.
// Available options are "github", "github-ascii".
// Default is "github", which will create GitHub-compatible anchor names.
AutoHeadingIDType string

// Enables custom attributes.
Attribute bool
Expand Down

0 comments on commit 8f071fc

Please sign in to comment.