-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
54 lines (46 loc) · 1.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict';
/**
* Module dependencies
*/
var extend = require('extend-shallow');
var querystring = require('querystring');
var diacritics = require('diacritics-map');
var stripColor = require('strip-color');
/**
* Slugify the url part of a markdown link.
*
* @name options.slugify
* @param {String} `str` The string to slugify
* @return {String}
*/
module.exports = function(str, options) {
if (typeof str !== 'string') {
throw new TypeError('expected a string');
}
var opts = extend({}, options);
str = stripColor(str);
str = str.toLowerCase();
// `.split()` is sometimes faster than `.replace()`
str = str.split(' ').join('-');
str = str.split(/\t/).join('--');
if (opts.stripHeadingTags === true) {
str = str.split(/<\/?[^>]+>/).join('');
}
str = str.split(/[|$&`~=\\\/@+*!?({[\]})<>=.,;:'"^]/).join('');
str = str.split(/[。?!,、;:“”【】()〔〕[]﹃﹄“ ”‘’﹁﹂—…-~《》〈〉「」]/).join('');
str = replaceDiacritics(str);
if (opts.num) {
str += '-' + opts.num;
} else if (opts.cache) {
str = unique(str, opts.cache);
}
return querystring.escape(str);
};
function replaceDiacritics(str) {
return str.replace(/[À-ž]/g, function(ch) {
return diacritics[ch] || ch;
});
}
function unique(str, cache) {
return cache[str] ? (str + '-' + cache[str]++) : ((cache[str] = 1) && str);
}