UNPKG

@alauda/doom

Version:

Doctor Doom making docs.

107 lines (106 loc) 4.12 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { useLang, useSite, withBase } from '@rspress/core/runtime'; import { Search as OriginalSearch, Layout as OriginalLayout, getCustomMDXComponent, } from '@rspress/core/theme'; import { Search as AlgoliaSearch, ZH_LOCALES, } from '@rspress/plugin-algolia/runtime'; import virtual from 'doom-@global-virtual'; import { useMemo } from 'react'; import { useLocation } from 'react-router'; import classes from '../styles/link.module.scss'; import { useTranslation } from './runtime/index.js'; // eslint-disable-next-line import-x/export export * from '@rspress/core/theme'; const X = getCustomMDXComponent(); 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; }; // eslint-disable-next-line import-x/export 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 && withBase(`${found.exportItem.name ?? found.sidebar.text}-${lang}.pdf`), [found, lang]); return (_jsx(OriginalLayout, { beforeOutline: pdfLink && (_jsx(X.p, { children: _jsx("a", { className: classes.link, href: pdfLink, target: "_blank", rel: "noopener noreferrer", children: t('view_docs_as_pdf') }) })) })); }; // eslint-disable-next-line import-x/export export const Search = process.env.ALGOLIA_APP_ID && process.env.ALGOLIA_API_KEY && process.env.ALGOLIA_INDEX_NAME ? () => { const lang = useLang(); const docSearchProps = useMemo(() => ({ appId: process.env.ALGOLIA_APP_ID, apiKey: process.env.ALGOLIA_API_KEY, indexName: process.env.ALGOLIA_INDEX_NAME, searchParameters: { facetFilters: [`lang:${lang}`], }, }), [lang]); return (_jsx(AlgoliaSearch, { docSearchProps: docSearchProps, locales: ZH_LOCALES })); } : OriginalSearch;