mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-30 22:39:58 +02:00
markup/goldmark: Add an optional Blackfriday auto ID strategy
Fixes #6707
This commit is contained in:
@@ -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")
|
||||
|
@@ -17,6 +17,8 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gohugoio/hugo/markup/goldmark/goldmark_config"
|
||||
|
||||
qt "github.com/frankban/quicktest"
|
||||
)
|
||||
|
||||
@@ -69,9 +71,9 @@ under_score
|
||||
expect := expectlines[i]
|
||||
c.Run(input, func(c *qt.C) {
|
||||
b := []byte(input)
|
||||
got := string(sanitizeAnchorName(b, false))
|
||||
got := string(sanitizeAnchorName(b, goldmark_config.AutoHeadingIDTypeGitHub))
|
||||
c.Assert(got, qt.Equals, expect)
|
||||
c.Assert(sanitizeAnchorNameString(input, false), qt.Equals, expect)
|
||||
c.Assert(sanitizeAnchorNameString(input, goldmark_config.AutoHeadingIDTypeGitHub), qt.Equals, expect)
|
||||
c.Assert(string(b), qt.Equals, input)
|
||||
})
|
||||
}
|
||||
@@ -80,16 +82,21 @@ under_score
|
||||
func TestSanitizeAnchorNameAsciiOnly(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
|
||||
c.Assert(sanitizeAnchorNameString("god is神真美好 good", true), qt.Equals, "god-is-good")
|
||||
c.Assert(sanitizeAnchorNameString("Resumé", true), qt.Equals, "resume")
|
||||
c.Assert(sanitizeAnchorNameString("god is神真美好 good", goldmark_config.AutoHeadingIDTypeGitHubAscii), qt.Equals, "god-is-good")
|
||||
c.Assert(sanitizeAnchorNameString("Resumé", goldmark_config.AutoHeadingIDTypeGitHubAscii), qt.Equals, "resume")
|
||||
|
||||
}
|
||||
|
||||
func TestSanitizeAnchorNameBlackfriday(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
c.Assert(sanitizeAnchorNameString("Let's try this, shall we?", goldmark_config.AutoHeadingIDTypeBlackfriday), qt.Equals, "let-s-try-this-shall-we")
|
||||
}
|
||||
|
||||
func BenchmarkSanitizeAnchorName(b *testing.B) {
|
||||
input := []byte("God is good: 神真美好")
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
result := sanitizeAnchorName(input, false)
|
||||
result := sanitizeAnchorName(input, goldmark_config.AutoHeadingIDTypeGitHub)
|
||||
if len(result) != 24 {
|
||||
b.Fatalf("got %d", len(result))
|
||||
|
||||
@@ -101,7 +108,7 @@ func BenchmarkSanitizeAnchorNameAsciiOnly(b *testing.B) {
|
||||
input := []byte("God is good: 神真美好")
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
result := sanitizeAnchorName(input, true)
|
||||
result := sanitizeAnchorName(input, goldmark_config.AutoHeadingIDTypeGitHubAscii)
|
||||
if len(result) != 12 {
|
||||
b.Fatalf("got %d", len(result))
|
||||
|
||||
@@ -109,11 +116,23 @@ func BenchmarkSanitizeAnchorNameAsciiOnly(b *testing.B) {
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSanitizeAnchorNameBlackfriday(b *testing.B) {
|
||||
input := []byte("God is good: 神真美好")
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
result := sanitizeAnchorName(input, goldmark_config.AutoHeadingIDTypeBlackfriday)
|
||||
if len(result) != 24 {
|
||||
b.Fatalf("got %d", len(result))
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSanitizeAnchorNameString(b *testing.B) {
|
||||
input := "God is good: 神真美好"
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
result := sanitizeAnchorNameString(input, false)
|
||||
result := sanitizeAnchorNameString(input, goldmark_config.AutoHeadingIDTypeGitHub)
|
||||
if len(result) != 24 {
|
||||
b.Fatalf("got %d", len(result))
|
||||
}
|
||||
|
@@ -29,7 +29,6 @@ import (
|
||||
|
||||
"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"
|
||||
@@ -57,7 +56,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.AutoHeadingIDType == goldmark_config.AutoHeadingIDTypeGitHub)
|
||||
return sanitizeAnchorNameString(s, cfg.MarkupConfig.Goldmark.Parser.AutoHeadingIDType)
|
||||
},
|
||||
}, nil
|
||||
}), nil
|
||||
|
@@ -178,6 +178,21 @@ func TestConvertAutoIDAsciiOnly(t *testing.T) {
|
||||
c.Assert(got, qt.Contains, "<h2 id=\"god-is-good-\">")
|
||||
}
|
||||
|
||||
func TestConvertAutoIDBlackfriday(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
|
||||
content := `
|
||||
## Let's try this, shall we?
|
||||
|
||||
`
|
||||
mconf := markup_config.Default
|
||||
mconf.Goldmark.Parser.AutoHeadingIDType = goldmark_config.AutoHeadingIDTypeBlackfriday
|
||||
b := convert(c, mconf, content)
|
||||
got := string(b.Bytes())
|
||||
|
||||
c.Assert(got, qt.Contains, "<h2 id=\"let-s-try-this-shall-we\">")
|
||||
}
|
||||
|
||||
func TestCodeFence(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
|
||||
|
@@ -17,6 +17,7 @@ package goldmark_config
|
||||
const (
|
||||
AutoHeadingIDTypeGitHub = "github"
|
||||
AutoHeadingIDTypeGitHubAscii = "github-ascii"
|
||||
AutoHeadingIDTypeBlackfriday = "blackfriday"
|
||||
)
|
||||
|
||||
// DefaultConfig holds the default Goldmark configuration.
|
||||
|
Reference in New Issue
Block a user