github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/shurcooL/sanitized_anchor_name/main.go (about) 1 // Package sanitized_anchor_name provides a func to create sanitized anchor names. 2 // 3 // Its logic can be reused by multiple packages to create interoperable anchor names 4 // and links to those anchors. 5 // 6 // At this time, it does not try to ensure that generated anchor names 7 // are unique, that responsibility falls on the caller. 8 package sanitized_anchor_name 9 10 import "unicode" 11 12 // Create returns a sanitized anchor name for the given text. 13 func Create(text string) string { 14 var anchorName []rune 15 var futureDash = false 16 for _, r := range []rune(text) { 17 switch { 18 case unicode.IsLetter(r) || unicode.IsNumber(r): 19 if futureDash && len(anchorName) > 0 { 20 anchorName = append(anchorName, '-') 21 } 22 futureDash = false 23 anchorName = append(anchorName, unicode.ToLower(r)) 24 default: 25 futureDash = true 26 } 27 } 28 return string(anchorName) 29 }