mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-06 14:05:46 +01:00
full unicode support in slugify method, thanks to XRegExp
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
(function(module) {
|
||||
'use strict';
|
||||
|
||||
var utils, fs;
|
||||
var utils, fs, XRegExp;
|
||||
|
||||
if ('undefined' === typeof window) {
|
||||
fs = require('fs');
|
||||
XRegExp = require('xregexp').XRegExp;
|
||||
}
|
||||
|
||||
|
||||
@@ -92,19 +93,13 @@
|
||||
|
||||
//http://dense13.com/blog/2009/05/03/converting-string-to-slug-javascript/
|
||||
slugify: function(str) {
|
||||
var invalidChars = XRegExp('[^\\p{L} -]', 'g');
|
||||
|
||||
str = str.replace(/^\s+|\s+$/g, ''); // trim
|
||||
str = str.toLowerCase();
|
||||
|
||||
// remove accents, swap ñ for n, etc
|
||||
var from = "àáäâèéëêìíïîıòóöôùúüûñçşğ·/_,:;";
|
||||
var to = "aaaaeeeeiiiiioooouuuuncsg------";
|
||||
for (var i = 0, l = from.length; i < l; i++) {
|
||||
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
|
||||
}
|
||||
|
||||
str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
|
||||
.replace(/\s+/g, '-') // collapse whitespace and replace by -
|
||||
.replace(/-+/g, '-'); // collapse dashes
|
||||
str = XRegExp.replace(str, invalidChars, '');
|
||||
str = str.replace(/\s+/g, '-') // collapse whitespace and replace by -
|
||||
str = str.replace(/-+/g, '-'); // collapse dashes
|
||||
|
||||
return str;
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user