Slugify: Replace gosimple/slug with a simple function (#59517)

This commit is contained in:
Ryan McKinley
2022-11-30 11:12:56 -05:00
committed by GitHub
parent 000de83eb4
commit 5b71a16acf
13 changed files with 443 additions and 56 deletions
+2 -20
View File
@@ -1,14 +1,11 @@
package models
import (
"encoding/base64"
"fmt"
"strings"
"time"
"github.com/gosimple/slug"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/slugify"
"github.com/grafana/grafana/pkg/setting"
)
@@ -147,22 +144,7 @@ func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
// UpdateSlug updates the slug
func (d *Dashboard) UpdateSlug() {
title := d.Data.Get("title").MustString()
d.Slug = SlugifyTitle(title)
}
func SlugifyTitle(title string) string {
s := slug.Make(strings.ToLower(title))
if s == "" {
// If the dashboard name is only characters outside of the
// sluggable characters, the slug creation will return an
// empty string which will mess up URLs. This failsafe picks
// that up and creates the slug as a base64 identifier instead.
s = base64.RawURLEncoding.EncodeToString([]byte(title))
if slug.MaxLength != 0 && len(s) > slug.MaxLength {
s = s[:slug.MaxLength]
}
}
return s
d.Slug = slugify.Slugify(title)
}
// GetUrl return the html url for a folder if it's folder, otherwise for a dashboard
+3 -2
View File
@@ -4,6 +4,7 @@ import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/slugify"
"github.com/grafana/grafana/pkg/setting"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -72,14 +73,14 @@ func TestSlugifyTitle(t *testing.T) {
testCases := map[string]string{
"Grafana Play Home": "grafana-play-home",
"snöräv-över-ån": "snorav-over-an",
"漢字": "han-zi", // Hanzi for hanzi
"漢字": "5ryi5a2X", // "han-zi", // Hanzi for hanzi
"🇦🇶": "8J-HpvCfh7Y", // flag of Antarctica-emoji, using fallback
"𒆠": "8JKGoA", // cuneiform Ki, using fallback
}
for input, expected := range testCases {
t.Run(input, func(t *testing.T) {
slug := SlugifyTitle(input)
slug := slugify.Slugify(input)
assert.Equal(t, expected, slug)
})
}