mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-06 14:05:46 +01:00
slug fix
This commit is contained in:
@@ -102,18 +102,23 @@
|
|||||||
invalidUnicodeChars : XRegExp('[^\\p{L}\\s\\d\\-_]', 'g'),
|
invalidUnicodeChars : XRegExp('[^\\p{L}\\s\\d\\-_]', 'g'),
|
||||||
invalidLatinChars : /[^\w\s\d\-_]/g,
|
invalidLatinChars : /[^\w\s\d\-_]/g,
|
||||||
|
|
||||||
|
trimRegex : /^\s+|\s+$/g,
|
||||||
|
collapseWhitespace : /\s+/g,
|
||||||
|
collapseDash : /-+/g,
|
||||||
|
trimTrailingDash : /-$/g,
|
||||||
|
|
||||||
//http://dense13.com/blog/2009/05/03/converting-string-to-slug-javascript/
|
//http://dense13.com/blog/2009/05/03/converting-string-to-slug-javascript/
|
||||||
slugify: function(str) {
|
slugify: function(str) {
|
||||||
str = str.replace(/^\s+|\s+$/g, ''); // trim
|
str = str.replace(utils.trimRegex, '');
|
||||||
str = str.toLowerCase();
|
str = str.toLowerCase();
|
||||||
if(/^[\w]+$/.test(str)) {
|
if(/^[\w]+$/.test(str)) {
|
||||||
str = str.replace(utils.invalidLatinChars, '-');
|
str = str.replace(utils.invalidLatinChars, '-');
|
||||||
} else {
|
} else {
|
||||||
str = XRegExp.replace(str, utils.invalidUnicodeChars, '-');
|
str = XRegExp.replace(str, utils.invalidUnicodeChars, '-');
|
||||||
}
|
}
|
||||||
str = str.replace(/\s+/g, '-') // collapse whitespace and replace by -
|
str = str.replace(utils.collapseWhitespace, '-') // collapse whitespace and replace by -
|
||||||
str = str.replace(/-+/g, '-'); // collapse dashes
|
str = str.replace(utils.collapseDash, '-'); // collapse dashes
|
||||||
str = str.replace(/-$/g, '');
|
str = str.replace(utils.trimTrailingDash, '');
|
||||||
return str;
|
return str;
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -206,7 +211,7 @@
|
|||||||
|
|
||||||
if (!String.prototype.trim) {
|
if (!String.prototype.trim) {
|
||||||
String.prototype.trim = function() {
|
String.prototype.trim = function() {
|
||||||
return this.replace(/^\s+|\s+$/g, '');
|
return this.replace(utils.trimRegex, '');
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user