summaryrefslogtreecommitdiffstats
path: root/markup/goldmark/autoid.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2020-01-05 11:52:00 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2020-01-05 11:56:05 +0100
commit16e7c1120346bd853cf6510ffac8e94824bf2c7f (patch)
treee1f1c38bdf978015d8e9fd81e3c5c51d18fff327 /markup/goldmark/autoid.go
parent8f071fc159ce9a0fc0ea14a73bde8f299bedd109 (diff)
markup/goldmark: Add an optional Blackfriday auto ID strategy
Fixes #6707
Diffstat (limited to 'markup/goldmark/autoid.go')
-rw-r--r--markup/goldmark/autoid.go57
1 files changed, 33 insertions, 24 deletions
diff --git a/markup/goldmark/autoid.go b/markup/goldmark/autoid.go
index aaf1852d1..950d4a577 100644
--- a/markup/goldmark/autoid.go
+++ b/markup/goldmark/autoid.go
@@ -19,6 +19,8 @@ import (
"unicode"
"unicode/utf8"
+ "github.com/gohugoio/hugo/markup/blackfriday"
+
"github.com/gohugoio/hugo/markup/goldmark/goldmark_config"
"github.com/gohugoio/hugo/common/text"
@@ -30,34 +32,41 @@ import (
bp "github.com/gohugoio/hugo/bufferpool"
)
-func sanitizeAnchorNameString(s string, asciiOnly bool) string {
- return string(sanitizeAnchorName([]byte(s), asciiOnly))
+func sanitizeAnchorNameString(s string, idType string) string {
+ return string(sanitizeAnchorName([]byte(s), idType))
}
-func sanitizeAnchorName(b []byte, asciiOnly bool) []byte {
- return sanitizeAnchorNameWithHook(b, asciiOnly, nil)
+func sanitizeAnchorName(b []byte, idType string) []byte {
+ return sanitizeAnchorNameWithHook(b, idType, nil)
}
-func sanitizeAnchorNameWithHook(b []byte, asciiOnly bool, hook func(buf *bytes.Buffer)) []byte {
+func sanitizeAnchorNameWithHook(b []byte, idType string, hook func(buf *bytes.Buffer)) []byte {
buf := bp.GetBuffer()
- if asciiOnly {
- // Normalize it to preserve accents if possible.
- b = text.RemoveAccents(b)
- }
+ if idType == goldmark_config.AutoHeadingIDTypeBlackfriday {
+ // TODO(bep) make it more efficient.
+ buf.WriteString(blackfriday.SanitizedAnchorName(string(b)))
+ } else {
+ asciiOnly := idType == goldmark_config.AutoHeadingIDTypeGitHubAscii
- for len(b) > 0 {
- r, size := utf8.DecodeRune(b)
- switch {
- case asciiOnly && size != 1:
- case r == '-' || isSpace(r):
- buf.WriteRune('-')
- case isAlphaNumeric(r):
- buf.WriteRune(unicode.ToLower(r))
- default:
+ if asciiOnly {
+ // Normalize it to preserve accents if possible.
+ b = text.RemoveAccents(b)
}
- b = b[size:]
+ for len(b) > 0 {
+ r, size := utf8.DecodeRune(b)
+ switch {
+ case asciiOnly && size != 1:
+ case r == '-' || isSpace(r):
+ buf.WriteRune('-')
+ case isAlphaNumeric(r):
+ buf.WriteRune(unicode.ToLower(r))
+ default:
+ }
+
+ b = b[size:]
+ }
}
if hook != nil {
@@ -83,19 +92,19 @@ func isSpace(r rune) bool {
var _ parser.IDs = (*idFactory)(nil)
type idFactory struct {
- asciiOnly bool
- vals map[string]struct{}
+ idType string
+ vals map[string]struct{}
}
func newIDFactory(idType string) *idFactory {
return &idFactory{
- vals: make(map[string]struct{}),
- asciiOnly: idType == goldmark_config.AutoHeadingIDTypeGitHubAscii,
+ vals: make(map[string]struct{}),
+ idType: idType,
}
}
func (ids *idFactory) Generate(value []byte, kind ast.NodeKind) []byte {
- return sanitizeAnchorNameWithHook(value, ids.asciiOnly, func(buf *bytes.Buffer) {
+ return sanitizeAnchorNameWithHook(value, ids.idType, func(buf *bytes.Buffer) {
if buf.Len() == 0 {
if kind == ast.KindHeading {
buf.WriteString("heading")