-
-
Notifications
You must be signed in to change notification settings - Fork 715
/
Copy pathutils.js
86 lines (73 loc) · 1.97 KB
/
utils.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'use strict';
/**
* Module dependencies
*/
var diacritics = require('diacritics-map');
var utils = require('lazy-cache')(require);
var fn = require;
require = utils;
/**
* Lazily required module dependencies
*/
require('concat-stream', 'concat');
require('gray-matter', 'matter');
require('list-item', 'li');
require('markdown-link', 'mdlink');
require('minimist');
require('mixin-deep', 'merge');
require('object.pick', 'pick');
require('remarkable', 'Remarkable');
require('repeat-string', 'repeat');
require('strip-color');
require = fn;
/**
* Get the "title" from a markdown link
*/
utils.getTitle = function(str) {
if (/^\[[^\]]+\]\(/.test(str)) {
var m = /^\[([^\]]+)\]/.exec(str);
if (m) return m[1];
}
return str;
};
/**
* Slugify the url part of a markdown link.
*
* @name options.slugify
* @param {String} `str` The string to slugify
* @param {Object} `options` Pass a custom slugify function on `options.slugify`
* @return {String}
* @api public
*/
utils.slugify = function(str, options) {
options = options || {};
if (options.slugify === false) return str;
if (typeof options.slugify === 'function') {
return options.slugify(str, options);
}
str = utils.getTitle(str);
str = utils.stripColor(str);
str = str.toLowerCase();
// `.split()` is often (but not always) faster than `.replace()`
str = str.split(' ').join('-');
str = str.split(/\t/).join('--');
if (options.stripHeadingTags !== false) {
str = str.split(/<\/?[^>]+>/).join('');
}
str = str.split(/[|$&`~=\\\/@+*!?({[\]})<>=.,;:'"^]/).join('');
str = str.split(/[。?!,、;:“”【】()〔〕[]﹃﹄“ ”‘’﹁﹂—…-~《》〈〉「」]/).join('');
str = replaceDiacritics(str);
if (options.num) {
str += '-' + options.num;
}
return str;
};
function replaceDiacritics(str) {
return str.replace(/[À-ž]/g, function(ch) {
return diacritics[ch] || ch;
});
}
/**
* Expose `utils` modules
*/
module.exports = utils;