diff --git a/modules/markup/common/footnote.go b/modules/markup/common/footnote.go index ef89a7046a3..4f1c0ed9363 100644 --- a/modules/markup/common/footnote.go +++ b/modules/markup/common/footnote.go @@ -26,18 +26,15 @@ import ( // and more closely matches the output from the shurcooL sanitizer // In particular Unicode letters and numbers are a lot more than a-zA-Z0-9... func CleanValue(value []byte) []byte { - value = bytes.TrimSpace(value) - rs := bytes.Runes(value) - result := make([]rune, 0, len(rs)) - for _, r := range rs { - if unicode.IsLetter(r) || unicode.IsNumber(r) || r == '_' || r == '-' { - result = append(result, unicode.ToLower(r)) + return bytes.Map(func(r rune) rune { + switch { + case r == ' ': + return '-' + case r == '-' || unicode.In(r, unicode.L, unicode.M, unicode.Nd, unicode.Nl, unicode.Pc, unicode.Other_Alphabetic): + return unicode.ToLower(r) } - if unicode.IsSpace(r) { - result = append(result, '-') - } - } - return []byte(string(result)) + return -1 + }, bytes.TrimSpace(value)) } // Most of what follows is a subtly changed version of github.com/yuin/goldmark/extension/footnote.go diff --git a/modules/markup/common/footnote_test.go b/modules/markup/common/footnote_test.go index 2327a7b14b2..b8e8e7a4f10 100644 --- a/modules/markup/common/footnote_test.go +++ b/modules/markup/common/footnote_test.go @@ -50,9 +50,12 @@ func TestCleanValue(t *testing.T) { {"tes\\a", "tesa"}, {"tes/a", "tesa"}, {"a啊啊b", "a啊啊b"}, - {"c🤔️🤔️d", "cd"}, + {"c🤔️🤔️d", "c\ufe0f\ufe0fd"}, {"a⚡a", "aa"}, {"e.~f", "ef"}, + {"a \u2003 b", "a--b"}, + {"हिन्दी", "हिन्दी"}, + {"m²", "m"}, } for _, test := range tests { assert.Equal(t, []byte(test.expect), CleanValue([]byte(test.param)), test.param)