diff --git a/nip54/nip54.go b/nip54/nip54.go index f24d59d..1b48eea 100644 --- a/nip54/nip54.go +++ b/nip54/nip54.go @@ -11,20 +11,24 @@ import ( ) func NormalizeIdentifier(name string) string { - name = strings.TrimSpace(strings.ToLower(name)) res, _, _ := transform.Bytes(norm.NFKC, []byte(name)) - runes := []rune(string(res)) + runes := []rune(strings.ToLower(string(res))) - b := make([]rune, len(runes)) - for i, letter := range runes { + words := make([]string, 0, 3) + word := make([]rune, 0, 12) + for _, letter := range runes { if unicode.IsLetter(letter) || unicode.IsNumber(letter) { - b[i] = letter - } else { - b[i] = '-' + word = append(word, letter) + } else if len(word) > 0 { + words = append(words, string(word)) + word = make([]rune, 0, 12) } } + if len(word) > 0 { + words = append(words, string(word)) + } - return string(b) + return strings.Join(words, "-") } func ArticleAsHTML(content string) string { diff --git a/nip54/nip54_test.go b/nip54/nip54_test.go index af2dfa5..e91ccbf 100644 --- a/nip54/nip54_test.go +++ b/nip54/nip54_test.go @@ -13,7 +13,7 @@ func TestNormalization(t *testing.T) { }{ {" hello ", "hello"}, {"Goodbye", "goodbye"}, - {"the long and winding road / that leads to your door", "the-long-and-winding-road---that-leads-to-your-door"}, + {"the long and winding road / that leads to your door", "the-long-and-winding-road-that-leads-to-your-door"}, {"it's 平仮名", "it-s-平仮名"}, } { if norm := NormalizeIdentifier(vector.before); norm != vector.after {