diff --git a/util.go b/util.go index fac2b1a..310ec2e 100644 --- a/util.go +++ b/util.go @@ -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, "-")) }