|
2 | 2 |
|
3 | 3 | // precompile for speed |
4 | 4 | var HTML_TAGS_REGEX = /<[^>]*>/g; // anything contained in < > tags |
5 | | -var FORBIDDEN_CHARS_REGEX = /[\\/:*?"<>|$`'(){}[\],]/g; // Characters in the set: \/:*?"<>|$`'(){}[], |
| 5 | +var FORBIDDEN_CHARS_REGEX = /[\\/:*?"<>|$%&!@#~.^`'(){}[\],]/g; // Characters in the set: \/:*?"<>|$%&!@#~.^`'(){}[], |
| 6 | +var CONTROL_CHARS_REGEX = /\p{Cc}/gu; // Unicode control characters |
| 7 | + |
6 | 8 | var UNICODE_REPLACEMENT_CHAR_REGEX = /�/g; // U+FFFD, the Unicode replacement character |
7 | 9 | var WHITESPACE_REGEX = /\s+/g; |
8 | 10 |
|
@@ -37,14 +39,15 @@ function toWellFormed(str) { |
37 | 39 | * @return {string} |
38 | 40 | */ |
39 | 41 | module.exports = function slugify(str, maxLen = DEFAULT_MAX_LEN) { |
40 | | - var slug = toWellFormed(str ?? '') // Guarantee well-formed Unicode text |
41 | | - .replace(UNICODE_REPLACEMENT_CHAR_REGEX, '') // Drop Unicode replacement characters left by previous step |
42 | | - .replace(HTML_TAGS_REGEX, ' ') // Remove anything contained in < > tags, such as <br> (replace with a space) |
43 | | - .replace(FORBIDDEN_CHARS_REGEX, '') // Remove forbidden filename characters |
44 | | - .toLowerCase() // Lowercase everything |
45 | | - .trim() // Strip leading/trailing whitespace |
46 | | - .replace(WHITESPACE_REGEX, WORD_SEP_CHAR) // Replace any remaining whitespace with the word separator char |
47 | | - .replace(WORD_SEP_CHARS_REGEX, WORD_SEP_CHAR); // Replace multiple consecutive word separator chars with a single one |
| 42 | + var slug = toWellFormed(str ?? '') // Guarantee well-formed Unicode text |
| 43 | + .replace(UNICODE_REPLACEMENT_CHAR_REGEX, '') // Drop Unicode replacement chars left by previous step |
| 44 | + .replace(HTML_TAGS_REGEX, ' ') // Remove < > tags, such as <br> (replace with space) |
| 45 | + .replace(FORBIDDEN_CHARS_REGEX, '') // Remove forbidden filename characters |
| 46 | + .toLowerCase() // Lowercase everything |
| 47 | + .trim() // Strip leading/trailing whitespace |
| 48 | + .replace(WHITESPACE_REGEX, WORD_SEP_CHAR) // Replace any remaining whitespace with the word sep char |
| 49 | + .replace(CONTROL_CHARS_REGEX, '') // Remove control characters (after whitespace) |
| 50 | + .replace(WORD_SEP_CHARS_REGEX, WORD_SEP_CHAR); // Replace multiple word sep chars with a single one |
48 | 51 |
|
49 | 52 | if (slug.length <= maxLen) return slug; |
50 | 53 | // Apply maxLen to the resulting string. Use Array.from().slice() instead of String.prototype.split() |
|
0 commit comments