From aac66e91198004bc044754105e18bfff8fbfd383 Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Tue, 10 Sep 2024 09:04:23 +0300 Subject: [PATCH] Slugs: use shorter slug fallback (#93081) --- pkg/infra/slugify/slugify.go | 10 ++++++++-- pkg/infra/slugify/slugify_test.go | 8 ++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/pkg/infra/slugify/slugify.go b/pkg/infra/slugify/slugify.go index ef4d716d81b..3fc1a1f919c 100644 --- a/pkg/infra/slugify/slugify.go +++ b/pkg/infra/slugify/slugify.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 diff --git a/pkg/infra/slugify/slugify_test.go b/pkg/infra/slugify/slugify_test.go index d8509d5e381..84c30140d28 100644 --- a/pkg/infra/slugify/slugify_test.go +++ b/pkg/infra/slugify/slugify_test.go @@ -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"