@alauda/doom
Version:
Doctor Doom making docs.
93 lines (92 loc) • 3.98 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useLang, useSite } from '@rspress/core/runtime';
import { Link, Layout as OriginalLayout } from '@rspress/core/theme-original';
import virtual from 'doom-@global-virtual';
import { useCallback, useMemo, useState } from 'react';
import { useLocation } from 'react-router';
import { useTranslation } from "../runtime/index.js";
import { BuildInfoContext } from "../shared/context.js";
import { BreadCrumb } from "./Breadcrumb/index.js";
import { ForceRenderContext } from "./VersionsNav/context.js";
import { VersionsNav } from "./VersionsNav/index.js";
import { X } from "./_X.js";
const cleanupUrlPath = (urlPath) => {
if (urlPath.endsWith('.html')) {
urlPath = urlPath.slice(0, -5);
}
if (urlPath.endsWith('/index')) {
urlPath = urlPath.slice(0, -5);
}
return urlPath;
};
const getClosestSidebar_ = (sidebarItems, pathname, exportItem, matched, depth = 0) => {
pathname = cleanupUrlPath(pathname);
for (const sidebar of sidebarItems) {
if ('_fileKey' in sidebar && sidebar._fileKey) {
if (depth === 0) {
matched = undefined;
}
if (exportItem.flattenScope.includes(sidebar._fileKey)) {
matched = {
sidebar,
exportItem,
depth,
};
}
const sidebarLink = cleanupUrlPath(sidebar.link || '');
if (sidebarLink === pathname) {
return matched;
}
}
if ('items' in sidebar) {
const found = getClosestSidebar_(sidebar.items, pathname, exportItem, matched, depth + 1);
if (found) {
return found;
}
}
}
};
const getClosestSidebar = (sidebarItems, pathname) => {
let found;
for (const item of virtual.export) {
const matched = getClosestSidebar_(sidebarItems, pathname, item);
if (matched) {
if (!found || matched.depth >= found.depth) {
found = matched;
}
}
}
return found;
};
export const Layout = () => {
const { site: { lang: siteLang, themeConfig }, } = useSite();
const lang = useLang();
const t = useTranslation();
const { pathname } = useLocation();
const found = useMemo(() => {
if (!virtual.download || !virtual.export?.length) {
return;
}
if (themeConfig.locales.length) {
let found;
for (const { lang, sidebar } of themeConfig.locales) {
const sidebarItems = sidebar[siteLang === lang ? '/' : `/${lang}`];
found ??= getClosestSidebar(sidebarItems, pathname);
if (found) {
return found;
}
}
}
else {
const sidebarItems = themeConfig.sidebar['/'];
return getClosestSidebar(sidebarItems, pathname);
}
}, [pathname, siteLang, themeConfig]);
const pdfLink = useMemo(() => found && `/${found.exportItem.name ?? found.sidebar.text}-${lang}.pdf`, [found, lang]);
const [render, setRender] = useState(false);
const forceRender = useCallback(() => {
setRender((v) => !v);
}, []);
const [buildInfoGroups, setBuildInfoGroups] = useState([]);
return (_jsxs(ForceRenderContext, { value: useMemo(() => ({ value: render, setValue: forceRender }), [forceRender, render]), children: [_jsx(VersionsNav, {}), _jsx(BuildInfoContext, { value: useMemo(() => ({ groups: buildInfoGroups, setGroups: setBuildInfoGroups }), [buildInfoGroups, setBuildInfoGroups]), children: _jsx(OriginalLayout, { afterNav: _jsx(BreadCrumb, {}), beforeOutline: pdfLink && (_jsx(X.p, { className: "rp-doc", style: { marginBottom: 16 }, children: _jsx(Link, { href: pdfLink, target: "_blank", rel: "noopener noreferrer", children: t('view_docs_as_pdf') }) })) }) })] }));
};