mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
feat: #8734, add slugify module, deprecate utils.slugify
This commit is contained in:
37
public/src/modules/slugify.js
Normal file
37
public/src/modules/slugify.js
Normal file
@@ -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;
|
||||||
|
};
|
||||||
|
}));
|
||||||
@@ -330,6 +330,8 @@
|
|||||||
return String(str).replace(new RegExp('<(\\/)?(' + (pattern || '[^\\s>]+') + ')(\\s+[^<>]*?)?\\s*(\\/)?>', 'gi'), '');
|
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'),
|
invalidUnicodeChars: XRegExp('[^\\p{L}\\s\\d\\-_]', 'g'),
|
||||||
invalidLatinChars: /[^\w\s\d\-_]/g,
|
invalidLatinChars: /[^\w\s\d\-_]/g,
|
||||||
trimRegex: /^\s+|\s+$/g,
|
trimRegex: /^\s+|\s+$/g,
|
||||||
@@ -338,10 +340,15 @@
|
|||||||
trimTrailingDash: /-$/g,
|
trimTrailingDash: /-$/g,
|
||||||
trimLeadingDash: /^-/g,
|
trimLeadingDash: /^-/g,
|
||||||
isLatin: /^[\w\d\s.,\-@]+$/,
|
isLatin: /^[\w\d\s.,\-@]+$/,
|
||||||
languageKeyRegex: /\[\[[\w]+:.+\]\]/,
|
|
||||||
|
|
||||||
// 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, preserveCase) {
|
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) {
|
if (!str) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
@@ -397,6 +404,7 @@
|
|||||||
return !isNaN(parseFloat(n)) && isFinite(n);
|
return !isNaN(parseFloat(n)) && isFinite(n);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
languageKeyRegex: /\[\[[\w]+:.+\]\]/,
|
||||||
hasLanguageKey: function (input) {
|
hasLanguageKey: function (input) {
|
||||||
return utils.languageKeyRegex.test(input);
|
return utils.languageKeyRegex.test(input);
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -103,6 +103,7 @@ JS.scripts = {
|
|||||||
'compare-versions.js': 'node_modules/compare-versions/index.js',
|
'compare-versions.js': 'node_modules/compare-versions/index.js',
|
||||||
'timeago/locales': 'node_modules/timeago/locales',
|
'timeago/locales': 'node_modules/timeago/locales',
|
||||||
'jquery-form.js': 'node_modules/jquery-form/dist/jquery.form.min.js',
|
'jquery-form.js': 'node_modules/jquery-form/dist/jquery.form.min.js',
|
||||||
|
'xregexp.js': 'node_modules/xregexp/xregexp-all.js',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
3
src/slugify.js
Normal file
3
src/slugify.js
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = require('../public/src/modules/slugify');
|
||||||
Reference in New Issue
Block a user