equal
deleted
inserted
replaced
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 // import "github.com/shurcooL/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 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 } |
|