forked from anza-xyz/agave
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCardLayout.js
92 lines (82 loc) · 2.58 KB
/
CardLayout.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
87
88
89
90
91
92
import React from "react";
import Layout from "@theme/Layout";
import DocSidebar from "@theme/DocSidebar";
import SidebarStyles from "@docusaurus/theme-classic/lib/theme/DocPage/Layout/Sidebar/styles.module.css";
import DocPageStyles from "@docusaurus/theme-classic/lib/theme/DocPage/Layout/styles.module.css";
import sidebar from "../sidebars";
function CardLayout({
children,
sidebarKey = false,
title = "",
description = "",
path = "",
}) {
// load the sidebar item from the master `sidebars.js` file
let sidebarItems = (sidebarKey && sidebar?.[sidebarKey]) || [];
// process each of the loaded sidebar items for formatting
if (sidebarItems?.length) sidebarItems = parseSidebar(sidebarItems);
// return the page layout, ready to go
return (
<Layout title={title} description={description}>
<div className={DocPageStyles.docPage}>
{sidebarItems?.length > 0 && (
<aside className={SidebarStyles.docSidebarContainer}>
<DocSidebar sidebar={sidebarItems} path={path}></DocSidebar>
</aside>
)}
<main className={DocPageStyles.docPage}>{children}</main>
</div>
</Layout>
);
}
export default CardLayout;
/*
Create a simple label based on the string of a doc file path
*/
const computeLabel = (label) => {
label = label.split("/");
label = label[label?.length - 1]?.replace("-", " ");
label = label.charAt(0).toUpperCase() + label.slice(1);
return label && label;
};
/*
Recursively parse the sidebar
*/
const parseSidebar = (sidebarItems) => {
Object.keys(sidebarItems).forEach((key) => {
if (sidebarItems[key]?.type?.toLowerCase() === "category") {
sidebarItems[key].items = parseSidebar(sidebarItems[key].items);
} else sidebarItems[key] = formatter(sidebarItems[key]);
});
return sidebarItems;
};
/*
Parser to format a sidebar item to be compatible with the `DocSidebar` component
*/
const formatter = (item) => {
// handle string only document ids
if (typeof item === "string") {
item = {
type: "link",
href: item,
label: computeLabel(item) || item || "[unknown label]",
};
}
// handle object style docs
else if (item?.type?.toLowerCase() === "doc") {
item.type = "link";
item.href = item.id;
item.label = item.label || computeLabel(item.href) || "[unknown label]";
delete item.id;
}
// fix for local routing that does not specify starting at the site root
if (
!(
item?.href?.startsWith("/") ||
item?.href?.startsWith("http:") ||
item?.href?.startsWith("https")
)
)
item.href = `/${item?.href}`;
return item;
};