forked from dmrd/exponent-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.js
40 lines (32 loc) · 955 Bytes
/
utilities.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
import GithubSlugger from 'github-slugger';
const GithubSluggerInstance = GithubSlugger();
// TODO(jim): Not sure what is the point of this.
export const toString = node => {
if (typeof node === 'string') {
return node;
} else if (Array.isArray(node)) {
return node.map(toString).join('');
} else if (node.props.children) {
return toString(node.props.children);
} else {
return '';
}
};
export const generateSlug = (node, length = 7) => {
GithubSluggerInstance.reset();
const stringToSlug = toString(node)
.split(' ')
.splice(0, length)
.join('-');
// NOTE(jim): This will strip out commas from stringToSlug
const slug = GithubSluggerInstance.slug(stringToSlug);
return slug;
};
export const replaceVersionInUrl = (url, replaceWith) => {
let urlArr = url.split('/');
urlArr[2] = replaceWith;
return urlArr.join('/');
};
export const getVersionFromUrl = url => {
return url.split('/')[2];
};