-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutil.js
More file actions
106 lines (93 loc) · 2.71 KB
/
util.js
File metadata and controls
106 lines (93 loc) · 2.71 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* Copyright (c) Source Solutions, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
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) => {
const ye = new Intl.DateTimeFormat("en", {
year: "numeric",
}).format(val);
const mo = new Intl.DateTimeFormat("en", {
month: "2-digit",
}).format(val);
const 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?$/, "");
};
// Helper function to get all active conditions from hierarchical structure
export const getAllConditions = (conditionsData) => {
const conditions = [];
if (Array.isArray(conditionsData?.categories)) {
for (const category of conditionsData.categories) {
if (category.conditions && Array.isArray(category.conditions)) {
for (const condition of category.conditions) {
if (condition.active !== false) {
// include active conditions and those without active field
conditions.push(condition.condition);
}
}
}
}
}
return conditions;
};
// Helper function to get condition details by name
export const getConditionDetails = (conditionsData, conditionName) => {
if (Array.isArray(conditionsData?.categories)) {
for (const category of conditionsData.categories) {
if (category.conditions && Array.isArray(category.conditions)) {
for (const condition of category.conditions) {
if (condition.condition === conditionName) {
return {
...condition,
category: category.name,
categoryDescription: category.description,
};
}
}
}
}
}
return null;
};