[Update] Improve slugify()'s set of invalid characters.

This commit is contained in:
Thomas M. Edwards 2020-02-24 15:23:00 -06:00
parent 59bf950d98
commit 0309c003e7

12
util.go
View file

@ -85,9 +85,17 @@ func normalizedFileExt(filename string) string {
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 {
// TODO: Maybe expand this to include non-ASCII alphas?
invalidRe := regexp.MustCompile(`[^[:word:]-]`)
// NOTE: The range of illegal characters consists of: C0 controls, space, exclamation,
// 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, "-"))
}