Slugs: use shorter slug fallback (#93081)

pull/92970/head^2
Ryan McKinley 10 months ago committed by GitHub
parent f650a17030
commit aac66e9119
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 10
      pkg/infra/slugify/slugify.go
  2. 8
      pkg/infra/slugify/slugify_test.go

@ -32,11 +32,15 @@ package slugify
import (
"bytes"
"encoding/hex"
"fmt"
"strings"
"unicode/utf8"
"github.com/google/uuid"
// can ignore because we don't need a cryptographically secure hash function
// sha1 low chance of collisions and better performance than sha256
// nolint:gosec
"crypto/sha1"
)
var (
@ -52,7 +56,9 @@ var (
func Slugify(value string) string {
s := simpleSlugger.Slugify(strings.TrimSpace(value))
if len(s) > 50 || s == "" {
s = uuid.NewSHA1(uuid.NameSpaceOID, []byte(value)).String()
h := sha1.New()
h.Write([]byte(s))
s = hex.EncodeToString(h.Sum(nil))[:7]
}
return s

@ -7,12 +7,12 @@ import (
func TestSlugify(t *testing.T) {
results := make(map[string]string)
results["hello-playground"] = "Hello, playground"
results["00a4bc92-3695-5702-9ddf-6719fdf11567"] = "😢 😣 😤 😥 😦 😧 😨 😩 😪 😫 😬 Hello, it's paradise"
results["61db60b5-f1e7-5853-9b81-0f074fc268ea"] = "😢 😣 😤 😥 😦 😧 😨 😩 😪 😫 😬"
results["37e4fb9"] = "😢 😣 😤 😥 😦 😧 😨 😩 😪 😫 😬 Hello, it's paradise"
results["f6bcbac"] = "😢 😣 😤 😥 😦 😧 😨 😩 😪 😫 😬"
results["f09f98a2"] = "😢 -"
results["a"] = "?,a . \n "
results["0a68eb57-c88a-5f34-9e9d-27f85e68af4f"] = "" // empty input has a slug!
results["3cbb528a-0ebf-54ad-bed2-2a188cd1824e"] = "方向盤後面 hi this is a test خلف المقو"
results["da39a3e"] = "" // empty input has a slug!
results["f96f70a"] = "方向盤後面 hi this is a test خلف المقو"
results["cong-hoa-xa-hoi-chu-nghia-viet-nam"] = "Cộng hòa xã hội chủ nghĩa Việt Nam"
results["noi-nang-canh-canh-ben-long-bieng-khuay"] = "Nỗi nàng canh cánh bên lòng biếng khuây" // This line in a poem called Truyen Kieu
results["hello-playground"] = "Hello / playground"

Loading…
Cancel
Save