mirror of
https://github.com/tmedwards/tweego.git
synced 2025-07-05 14:10:27 -04:00
[Update] Improve slugify()
's set of invalid characters.
This commit is contained in:
parent
59bf950d98
commit
0309c003e7
1 changed files with 10 additions and 2 deletions
12
util.go
12
util.go
|
@ -85,9 +85,17 @@ func normalizedFileExt(filename string) string {
|
||||||
return strings.ToLower(ext[1:])
|
return strings.ToLower(ext[1:])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns a trimmed and encoded slug of the passed string that should be safe
|
||||||
|
// for use as a DOM ID or class name.
|
||||||
func slugify(original string) string {
|
func slugify(original string) string {
|
||||||
// TODO: Maybe expand this to include non-ASCII alphas?
|
// NOTE: The range of illegal characters consists of: C0 controls, space, exclamation,
|
||||||
invalidRe := regexp.MustCompile(`[^[:word:]-]`)
|
// double quote, number, dollar, percent, ampersand, single quote, left paren, right
|
||||||
|
// paren, asterisk, plus, comma, hyphen, period, forward slash, colon, semi-colon,
|
||||||
|
// less-than, equals, greater-than, question, at, left bracket, backslash, right
|
||||||
|
// bracket, caret, backquote/grave, left brace, pipe/vertical-bar, right brace, tilde,
|
||||||
|
// delete, C1 controls.
|
||||||
|
invalidRe := regexp.MustCompile(`[\x00-\x20!"#$%&'()*+,\-./:;<=>?@[\\\]^\x60{|}~\x7f-\x9f]+`)
|
||||||
|
|
||||||
return strings.ToLower(invalidRe.ReplaceAllLiteralString(original, "-"))
|
return strings.ToLower(invalidRe.ReplaceAllLiteralString(original, "-"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue