markup/goldmark: Make the autoID type config a string

To potentially make room for one more.

See #6707
This commit is contained in:
Bjørn Erik Pedersen 2020-01-05 11:29:22 +01:00
parent 469351d5b6
commit 8f071fc159
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
4 changed files with 22 additions and 11 deletions

View file

@ -19,6 +19,8 @@ import (
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
"github.com/gohugoio/hugo/markup/goldmark/goldmark_config"
"github.com/gohugoio/hugo/common/text" "github.com/gohugoio/hugo/common/text"
"github.com/yuin/goldmark/ast" "github.com/yuin/goldmark/ast"
@ -85,10 +87,10 @@ type idFactory struct {
vals map[string]struct{} vals map[string]struct{}
} }
func newIDFactory(asciiOnly bool) *idFactory { func newIDFactory(idType string) *idFactory {
return &idFactory{ return &idFactory{
vals: make(map[string]struct{}), vals: make(map[string]struct{}),
asciiOnly: asciiOnly, asciiOnly: idType == goldmark_config.AutoHeadingIDTypeGitHubAscii,
} }
} }

View file

@ -28,8 +28,8 @@ import (
"github.com/spf13/afero" "github.com/spf13/afero"
"github.com/gohugoio/hugo/hugofs" "github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/markup/converter" "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/highlight"
"github.com/gohugoio/hugo/markup/tableofcontents" "github.com/gohugoio/hugo/markup/tableofcontents"
"github.com/yuin/goldmark" "github.com/yuin/goldmark"
@ -57,7 +57,7 @@ func (p provide) New(cfg converter.ProviderConfig) (converter.Provider, error) {
cfg: cfg, cfg: cfg,
md: md, md: md,
sanitizeAnchorName: func(s string) string { 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
}), nil }), nil
@ -280,7 +280,7 @@ func (c *goldmarkConverter) Supports(feature identity.Identity) bool {
} }
func (c *goldmarkConverter) newParserContext(rctx converter.RenderContext) *parserContext { 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) ctx.Set(tocEnableKey, rctx.RenderTOC)
return &parserContext{ return &parserContext{
Context: ctx, Context: ctx,

View file

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

View file

@ -14,6 +14,11 @@
// Package goldmark_config holds Goldmark related configuration. // Package goldmark_config holds Goldmark related configuration.
package goldmark_config package goldmark_config
const (
AutoHeadingIDTypeGitHub = "github"
AutoHeadingIDTypeGitHubAscii = "github-ascii"
)
// DefaultConfig holds the default Goldmark configuration. // DefaultConfig holds the default Goldmark configuration.
var Default = Config{ var Default = Config{
Extensions: Extensions{ Extensions: Extensions{
@ -29,8 +34,9 @@ var Default = Config{
Unsafe: false, Unsafe: false,
}, },
Parser: Parser{ Parser: Parser{
AutoHeadingID: true, AutoHeadingID: true,
Attribute: true, AutoHeadingIDType: AutoHeadingIDTypeGitHub,
Attribute: true,
}, },
} }
@ -69,9 +75,10 @@ type Parser struct {
// auto generated heading ids. // auto generated heading ids.
AutoHeadingID bool AutoHeadingID bool
// When AutoHeadingID is enabled this will generate IDs with Ascii // The strategy to use when generating heading IDs.
// characters only. // Available options are "github", "github-ascii".
AutoHeadingIDAsciiOnly bool // Default is "github", which will create GitHub-compatible anchor names.
AutoHeadingIDType string
// Enables custom attributes. // Enables custom attributes.
Attribute bool Attribute bool