mirror of
https://github.com/TECHNOFAB11/docsonnet.git
synced 2025-12-13 15:00:20 +01:00
feat: rewrite render
This commit is contained in:
parent
5fbee517fe
commit
7308eecaf3
10 changed files with 352 additions and 198 deletions
35
pkg/slug/slug.go
Normal file
35
pkg/slug/slug.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package slug
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Slugger struct {
|
||||
occurences map[string]int
|
||||
}
|
||||
|
||||
var (
|
||||
expWhitespace = regexp.MustCompile(`\s`)
|
||||
expSpecials = regexp.MustCompile("[\u2000-\u206F\u2E00-\u2E7F\\'!\"#$%&()*+,./:;<=>?@[\\]^`{|}~’]")
|
||||
)
|
||||
|
||||
func New() *Slugger {
|
||||
return &Slugger{
|
||||
occurences: make(map[string]int),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Slugger) Slug(str string) string {
|
||||
str = expWhitespace.ReplaceAllString(str, "-")
|
||||
str = expSpecials.ReplaceAllString(str, "")
|
||||
|
||||
old := str
|
||||
if o := s.occurences[str]; o > 0 {
|
||||
str += "-" + strconv.Itoa(o)
|
||||
}
|
||||
s.occurences[old] = s.occurences[old] + 1
|
||||
|
||||
return strings.ToLower(str)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue