forked from docsifyjs/docsify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslugify.js
More file actions
37 lines (29 loc) 路 687 Bytes
/
Copy pathslugify.js
File metadata and controls
37 lines (29 loc) 路 687 Bytes
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
import { hasOwn } from '../util/core';
let cache = {};
const re = /[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~]/g;
function lower(string) {
return string.toLowerCase();
}
export function slugify(str) {
if (typeof str !== 'string') {
return '';
}
let slug = str
.trim()
.replace(/[A-Z]+/g, lower)
.replace(/<[^>\d]+>/g, '')
.replace(re, '')
.replace(/\s/g, '-')
.replace(/-+/g, '-')
.replace(/^(\d)/, '_$1');
let count = cache[slug];
count = hasOwn.call(cache, slug) ? count + 1 : 0;
cache[slug] = count;
if (count) {
slug = slug + '-' + count;
}
return slug;
}
slugify.clear = function() {
cache = {};
};