github.com/elliott5/community@v0.14.1-0.20160709191136-823126fb026a/wordsmith/utility/slug.go (about) 1 // Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved. 2 // 3 // This software (Documize Community Edition) is licensed under 4 // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html 5 // 6 // You can operate outside the AGPL restrictions by purchasing 7 // Documize Enterprise Edition and obtaining a commercial license 8 // by contacting <sales@documize.com>. 9 // 10 // https://documize.com 11 12 package utility 13 14 import ( 15 "strings" 16 "unicode" 17 ) 18 19 // MakeSlug creates a slug, suitable for use in a URL, from a string 20 func MakeSlug(str string) string { 21 slg := strings.Map( 22 func(r rune) rune { // individual mapping of runes into a format suitable for use in a URL 23 r = unicode.ToLower(r) 24 if unicode.IsLower(r) || unicode.IsDigit(r) { 25 return r 26 } 27 return '-' 28 }, str) 29 slg = strings.NewReplacer("---", "-", "--", "-").Replace(slg) 30 for strings.HasSuffix(slg, "-") { 31 slg = strings.TrimSuffix(slg, "-") 32 } 33 for strings.HasPrefix(slg, "-") { 34 slg = strings.TrimPrefix(slg, "-") 35 } 36 return slg 37 }