-
Notifications
You must be signed in to change notification settings - Fork 71
/
util.js
58 lines (51 loc) · 1.3 KB
/
util.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
import title from "title";
export const slugify = (text) => {
return text
.toString()
.toLowerCase()
.trim()
.replace(/[^\w\s-]/g, "") // remove non-word [a-z0-9_], non-whitespace, non-hyphen characters
.replace(/[\s_-]+/g, "_") // swap any length of whitespace, underscore, hyphen characters with a single _
.replace(/^-+|-+$/g, ""); // remove leading, trailing -
};
export const docusaurusDate = (val) => {
let ye = new Intl.DateTimeFormat("en", {
year: "numeric",
}).format(val);
let mo = new Intl.DateTimeFormat("en", {
month: "2-digit",
}).format(val);
let da = new Intl.DateTimeFormat("en", {
day: "2-digit",
}).format(val);
return `${ye}-${mo}-${da}`;
};
export const titleFromSlug = (slug) => {
const titleString = slug
.split("/")
.slice(1)
.join(" – ")
.replace(/-/g, " ")
.replace(/\.[^/.]+$/, "");
return title(titleString);
};
export const getDocId = (doc) => {
return doc
.replace(/\.mdx?$/, "")
.split("/")
.slice(1)
.join("/");
};
export const getDocPath = (doc) => {
return doc.replace(/\.mdx?$/, "");
};
export const getPageRoute = (page) => {
return page
.replace(/\.mdx?$/, "")
.split("/")
.slice(2)
.join("/");
};
export const getPath = (page) => {
return page.replace(/\.mdx?$/, "");
};