mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Slugs: use shorter slug fallback (#93081)
This commit is contained in:
parent
f650a17030
commit
aac66e9119
@ -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…
Reference in New Issue
Block a user