├── go.mod ├── README.md ├── main.go ├── main_test.go └── LICENSE /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/shurcooL/sanitized_anchor_name 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | sanitized_anchor_name 2 | ===================== 3 | 4 | [![Go Reference](https://pkg.go.dev/badge/github.com/shurcooL/sanitized_anchor_name.svg)](https://pkg.go.dev/github.com/shurcooL/sanitized_anchor_name) 5 | 6 | Package sanitized_anchor_name provides a func to create sanitized anchor names. 7 | 8 | Its logic can be reused by multiple packages to create interoperable anchor names 9 | and links to those anchors. 10 | 11 | At this time, it does not try to ensure that generated anchor names 12 | are unique, that responsibility falls on the caller. 13 | 14 | Installation 15 | ------------ 16 | 17 | ```sh 18 | go get github.com/shurcooL/sanitized_anchor_name 19 | ``` 20 | 21 | Example 22 | ------- 23 | 24 | ```Go 25 | anchorName := sanitized_anchor_name.Create("This is a header") 26 | 27 | fmt.Println(anchorName) 28 | 29 | // Output: 30 | // this-is-a-header 31 | ``` 32 | 33 | License 34 | ------- 35 | 36 | - [MIT License](LICENSE) 37 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 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 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 | } 30 | -------------------------------------------------------------------------------- /main_test.go: -------------------------------------------------------------------------------- 1 | package sanitized_anchor_name_test 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/shurcooL/sanitized_anchor_name" 7 | ) 8 | 9 | func ExampleCreate() { 10 | anchorName := sanitized_anchor_name.Create("This is a header") 11 | 12 | fmt.Println(anchorName) 13 | 14 | // Output: 15 | // this-is-a-header 16 | } 17 | 18 | func ExampleCreate_two() { 19 | fmt.Println(sanitized_anchor_name.Create("This is a header")) 20 | fmt.Println(sanitized_anchor_name.Create("This is also a header")) 21 | fmt.Println(sanitized_anchor_name.Create("main.go")) 22 | fmt.Println(sanitized_anchor_name.Create("Article 123")) 23 | fmt.Println(sanitized_anchor_name.Create("<- Let's try this, shall we?")) 24 | fmt.Printf("%q\n", sanitized_anchor_name.Create(" ")) 25 | fmt.Println(sanitized_anchor_name.Create("Hello, 世界")) 26 | 27 | // Output: 28 | // this-is-a-header 29 | // this-is-also-a-header 30 | // main-go 31 | // article-123 32 | // let-s-try-this-shall-we 33 | // "" 34 | // hello-世界 35 | } 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2014 Dmitri Shuralyov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | --------------------------------------------------------------------------------