/ util.js
util.js
 1  import title from "title";
 2  
 3  export const slugify = (text) => {
 4    return text
 5      .toString()
 6      .toLowerCase()
 7      .trim()
 8      .replace(/[^\w\s-]/g, "") // remove non-word [a-z0-9_], non-whitespace, non-hyphen characters
 9      .replace(/[\s_-]+/g, "_") // swap any length of whitespace, underscore, hyphen characters with a single _
10      .replace(/^-+|-+$/g, ""); // remove leading, trailing -
11  };
12  
13  export const docusaurusDate = (val) => {
14    let ye = new Intl.DateTimeFormat("en", {
15      year: "numeric",
16    }).format(val);
17    let mo = new Intl.DateTimeFormat("en", {
18      month: "2-digit",
19    }).format(val);
20    let da = new Intl.DateTimeFormat("en", {
21      day: "2-digit",
22    }).format(val);
23    return `${ye}-${mo}-${da}`;
24  };
25  
26  export const titleFromSlug = (slug) => {
27    const titleString = slug
28      .split("/")
29      .slice(1)
30      .join(" – ")
31      .replace(/-/g, " ")
32      .replace(/\.[^/.]+$/, "");
33    return title(titleString);
34  };
35  
36  export const getDocId = (doc) => {
37    return doc
38      .replace(/\.mdx?$/, "")
39      .split("/")
40      .slice(1)
41      .join("/");
42  };
43  
44  export const getDocPath = (doc) => {
45    return doc.replace(/\.mdx?$/, "");
46  };
47  
48  export const getPageRoute = (page) => {
49    return page
50      .replace(/\.mdx?$/, "")
51      .split("/")
52      .slice(2)
53      .join("/");
54  };
55  
56  export const getPath = (page) => {
57    return page.replace(/\.mdx?$/, "");
58  };