|
| 1 | +/** |
| 2 | + * CrowdSec Made Swizzled override of @docusaurus/theme-classic's DocRoot/Layout component. |
| 3 | + * |
| 4 | + * The original only renders the sidebar + main content area. |
| 5 | + * This override injects a custom SecondaryNavbar above that content, which adds: |
| 6 | + * - A breadcrumb trail (Home > Section > [current page path]) |
| 7 | + * - A version dropdown (when multiple doc versions exist) |
| 8 | + * |
| 9 | + * Docusaurus picks this file automatically because it lives at |
| 10 | + * src/theme/DocRoot/Layout/index.tsx, shadowing the original in node_modules. |
| 11 | + */ |
| 12 | + |
| 13 | +import Link from "@docusaurus/Link"; |
| 14 | +import type { PropSidebarBreadcrumbsItem } from "@docusaurus/plugin-content-docs"; |
| 15 | +import { |
| 16 | + useActiveDocContext, |
| 17 | + useActivePlugin, |
| 18 | + useDocsPreferredVersion, |
| 19 | + useDocsSidebar, |
| 20 | + useSidebarBreadcrumbs, |
| 21 | + useVersions, |
| 22 | +} from "@docusaurus/plugin-content-docs/client"; |
| 23 | +import { useHistorySelector } from "@docusaurus/theme-common"; |
| 24 | +import { SECTION_MAP } from "@site/src/sectionMap"; |
| 25 | +import BackToTopButton from "@theme/BackToTopButton"; |
| 26 | +import type { Props } from "@theme/DocRoot/Layout"; |
| 27 | +import DocRootLayoutMain from "@theme/DocRoot/Layout/Main"; |
| 28 | +import DocRootLayoutSidebar from "@theme/DocRoot/Layout/Sidebar"; |
| 29 | +import clsx from "clsx"; |
| 30 | +import { ChevronRight, House } from "lucide-react"; |
| 31 | +import React, { type ReactNode, useState } from "react"; |
| 32 | + |
| 33 | +import styles from "./styles.module.css"; |
| 34 | + |
| 35 | +function VersionDropdown({ pluginId }: { pluginId: string }): ReactNode { |
| 36 | + const versions = useVersions(pluginId); |
| 37 | + const activeDocContext = useActiveDocContext(pluginId); |
| 38 | + const { savePreferredVersionName } = useDocsPreferredVersion(pluginId); |
| 39 | + const search = useHistorySelector((h) => h.location.search); |
| 40 | + const hash = useHistorySelector((h) => h.location.hash); |
| 41 | + |
| 42 | + const activeVersion = activeDocContext.activeVersion; |
| 43 | + if (!activeVersion || versions.length <= 1) return null; |
| 44 | + |
| 45 | + return ( |
| 46 | + <div className={styles.versionDropdown}> |
| 47 | + <span className={styles.versionLabel}>Security Engine version:</span> |
| 48 | + <select |
| 49 | + value={activeVersion.name} |
| 50 | + onChange={(e) => { |
| 51 | + const selected = versions.find((v) => v.name === e.target.value); |
| 52 | + if (!selected) return; |
| 53 | + savePreferredVersionName(selected.name); |
| 54 | + const alternateDoc = activeDocContext.alternateDocVersions[selected.name]; |
| 55 | + const target = alternateDoc?.path ?? selected.path; |
| 56 | + window.location.href = `${target}${search}${hash}`; |
| 57 | + }} |
| 58 | + aria-label="Select documentation version" |
| 59 | + > |
| 60 | + {versions.map((v) => ( |
| 61 | + <option key={v.name} value={v.name}> |
| 62 | + {v.label} |
| 63 | + </option> |
| 64 | + ))} |
| 65 | + </select> |
| 66 | + </div> |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +function SecondaryNavbar(): ReactNode { |
| 71 | + const sidebar = useDocsSidebar(); |
| 72 | + const activePlugin = useActivePlugin(); |
| 73 | + const breadcrumbs = useSidebarBreadcrumbs(); |
| 74 | + |
| 75 | + const pluginId = activePlugin?.pluginId ?? "default"; |
| 76 | + const sidebarName = sidebar?.name ?? ""; |
| 77 | + const section = SECTION_MAP[sidebarName]; |
| 78 | + |
| 79 | + return ( |
| 80 | + <div className={styles.secondaryNavbar}> |
| 81 | + <div className={styles.secondaryNavbarInner}> |
| 82 | + <nav className={styles.breadcrumb} aria-label="Breadcrumb"> |
| 83 | + {/* Home icon */} |
| 84 | + <Link to="/" className={styles.breadcrumbHome} aria-label="Home"> |
| 85 | + <House size={14} aria-hidden /> |
| 86 | + </Link> |
| 87 | + |
| 88 | + {/* Optional parent section (e.g. "Security Engine" for AppSec) */} |
| 89 | + {section?.parent && ( |
| 90 | + <> |
| 91 | + <ChevronRight size={13} className={styles.breadcrumbSeparator} aria-hidden /> |
| 92 | + <Link to={section.parent.introPath} className={styles.breadcrumbItem}> |
| 93 | + {section.parent.label} |
| 94 | + </Link> |
| 95 | + </> |
| 96 | + )} |
| 97 | + |
| 98 | + {/* Section label (e.g. "Remediation Components") */} |
| 99 | + {section && ( |
| 100 | + <> |
| 101 | + <ChevronRight size={13} className={styles.breadcrumbSeparator} aria-hidden /> |
| 102 | + <Link to={section.introPath} className={styles.breadcrumbItem}> |
| 103 | + {section.label} |
| 104 | + </Link> |
| 105 | + </> |
| 106 | + )} |
| 107 | + |
| 108 | + {/* Sidebar breadcrumb trail */} |
| 109 | + {breadcrumbs?.map((item: PropSidebarBreadcrumbsItem, idx: number) => { |
| 110 | + const isLast = idx === breadcrumbs.length - 1; |
| 111 | + const href = item.type === "category" && (item as { linkUnlisted?: boolean }).linkUnlisted ? undefined : item.href; |
| 112 | + |
| 113 | + return ( |
| 114 | + <React.Fragment key={`${item.label}-${idx}`}> |
| 115 | + <ChevronRight size={13} className={styles.breadcrumbSeparator} aria-hidden /> |
| 116 | + {!isLast && href ? ( |
| 117 | + <Link to={href} className={styles.breadcrumbItem}> |
| 118 | + {item.label} |
| 119 | + </Link> |
| 120 | + ) : ( |
| 121 | + <span className={clsx(styles.breadcrumbItem, isLast && styles.breadcrumbItemActive)}>{item.label}</span> |
| 122 | + )} |
| 123 | + </React.Fragment> |
| 124 | + ); |
| 125 | + })} |
| 126 | + </nav> |
| 127 | + |
| 128 | + <VersionDropdown pluginId={pluginId} /> |
| 129 | + </div> |
| 130 | + </div> |
| 131 | + ); |
| 132 | +} |
| 133 | + |
| 134 | +export default function DocRootLayout({ children }: Props): ReactNode { |
| 135 | + const sidebar = useDocsSidebar(); |
| 136 | + const [hiddenSidebarContainer, setHiddenSidebarContainer] = useState(false); |
| 137 | + |
| 138 | + return ( |
| 139 | + <div className={styles.docsWrapper}> |
| 140 | + <BackToTopButton /> |
| 141 | + <SecondaryNavbar /> |
| 142 | + <div className={styles.docRoot}> |
| 143 | + {sidebar && ( |
| 144 | + <DocRootLayoutSidebar |
| 145 | + sidebar={sidebar.items} |
| 146 | + hiddenSidebarContainer={hiddenSidebarContainer} |
| 147 | + setHiddenSidebarContainer={setHiddenSidebarContainer} |
| 148 | + /> |
| 149 | + )} |
| 150 | + <DocRootLayoutMain hiddenSidebarContainer={hiddenSidebarContainer}>{children}</DocRootLayoutMain> |
| 151 | + </div> |
| 152 | + </div> |
| 153 | + ); |
| 154 | +} |
0 commit comments