From 0309c003e79443c4a7f698800408324da7a63787 Mon Sep 17 00:00:00 2001 From: "Thomas M. Edwards" Date: Mon, 24 Feb 2020 15:23:00 -0600 Subject: [PATCH] [Update] Improve `slugify()`'s set of invalid characters. --- util.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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, "-")) }