Skip to content

Commit

Permalink
feat: #8734, add slugify module, deprecate utils.slugify
Browse files Browse the repository at this point in the history
  • Loading branch information
barisusakli committed Oct 12, 2020
1 parent f16c826 commit bddfcb5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
37 changes: 37 additions & 0 deletions public/src/modules/slugify.js
@@ -0,0 +1,37 @@
'use strict';

(function (factory) {
if (typeof module === 'object' && module.exports) {
module.exports = factory(require('xregexp'));
} else {
define('slugify', ['xregexp'], factory);
}
}(function (XRegExp) {
var invalidUnicodeChars = XRegExp('[^\\p{L}\\s\\d\\-_]', 'g');
var invalidLatinChars = /[^\w\s\d\-_]/g;
var trimRegex = /^\s+|\s+$/g;
var collapseWhitespace = /\s+/g;
var collapseDash = /-+/g;
var trimTrailingDash = /-$/g;
var trimLeadingDash = /^-/g;
var isLatin = /^[\w\d\s.,\-@]+$/;

// http://dense13.com/blog/2009/05/03/converting-string-to-slug-javascript/
return function slugify(str, preserveCase) {
if (!str) {
return '';
}
str = String(str).replace(trimRegex, '');
if (isLatin.test(str)) {
str = str.replace(invalidLatinChars, '-');
} else {
str = XRegExp.replace(str, invalidUnicodeChars, '-');
}
str = !preserveCase ? str.toLocaleLowerCase() : str;
str = str.replace(collapseWhitespace, '-');
str = str.replace(collapseDash, '-');
str = str.replace(trimTrailingDash, '');
str = str.replace(trimLeadingDash, '');
return str;
};
}));
10 changes: 9 additions & 1 deletion public/src/utils.js
Expand Up @@ -330,6 +330,8 @@
return String(str).replace(new RegExp('<(\\/)?(' + (pattern || '[^\\s>]+') + ')(\\s+[^<>]*?)?\\s*(\\/)?>', 'gi'), '');
},


// TODO: remove XRegExp & all these in 1.16.0, they are moved to slugify module
invalidUnicodeChars: XRegExp('[^\\p{L}\\s\\d\\-_]', 'g'),
invalidLatinChars: /[^\w\s\d\-_]/g,
trimRegex: /^\s+|\s+$/g,
Expand All @@ -338,10 +340,15 @@
trimTrailingDash: /-$/g,
trimLeadingDash: /^-/g,
isLatin: /^[\w\d\s.,\-@]+$/,
languageKeyRegex: /\[\[[\w]+:.+\]\]/,

// http://dense13.com/blog/2009/05/03/converting-string-to-slug-javascript/
slugify: function (str, preserveCase) {
if (typeof module === 'object' && module.exports) {
console.warn('[deprecated] utils.slugify deprecated. Use `require("slugify")` instead');
} else {
console.warn('[deprecated] utils.slugify deprecated. Use `require(["slugify"], function (slugify) { ... })` instead');
}

if (!str) {
return '';
}
Expand Down Expand Up @@ -397,6 +404,7 @@
return !isNaN(parseFloat(n)) && isFinite(n);
},

languageKeyRegex: /\[\[[\w]+:.+\]\]/,
hasLanguageKey: function (input) {
return utils.languageKeyRegex.test(input);
},
Expand Down
1 change: 1 addition & 0 deletions src/meta/js.js
Expand Up @@ -103,6 +103,7 @@ JS.scripts = {
'compare-versions.js': 'node_modules/compare-versions/index.js',
'timeago/locales': 'node_modules/timeago/locales',
'jquery-form.js': 'node_modules/jquery-form/dist/jquery.form.min.js',
'xregexp.js': 'node_modules/xregexp/xregexp-all.js',
},
};

Expand Down
3 changes: 3 additions & 0 deletions src/slugify.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = require('../public/src/modules/slugify');

0 comments on commit bddfcb5

Please sign in to comment.