import React, { useEffect, useMemo, useState } from 'react'; import { Layout, Menu } from 'antd'; import { useLocation, useNavigate, useParams } from 'react-router-dom'; import { helpMenu, HelpMenuItem } from '../menu'; import MarkdownViewer, { TocItem } from './MarkdownViewer'; const { Sider, Content } = Layout; // 动态加载 md 文件(Vite 6 写法) const files = import.meta.glob('/src/help/docs/**/*.md', { query: '?raw', import: 'default' }); function findMenuByPath(pathname: string): HelpMenuItem | undefined { const flat: HelpMenuItem[] = []; const dfs = (arr: HelpMenuItem[]) => arr.forEach((i) => { flat.push(i); i.children && dfs(i.children); }); dfs(helpMenu); return flat.find((i) => i.path.split('#')[0] === pathname); } const HelpLayout: React.FC = () => { const params = useParams(); const navigate = useNavigate(); const location = useLocation(); const pathname = location.pathname; const [md, setMd] = useState(''); const [toc, setToc] = useState([]); const current = useMemo(() => findMenuByPath(pathname), [pathname]); useEffect(() => { // 根据菜单项的 doc 加载 md const doc = current?.doc; if (!doc) { setMd('# 欢迎使用帮助中心'); return; } const key = `/src/help/docs/${doc}`; const loader = (files as any)[key]; if (loader) { (loader as () => Promise )().then((raw) => setMd(raw)); } else { setMd('> 文档未找到: ' + doc); } }, [current?.doc]); useEffect(() => { // 定位到 hash 标题 if (!location.hash) return; const id = decodeURIComponent(location.hash.slice(1)); const el = document.getElementById(id); if (el) { el.scrollIntoView({ behavior: 'smooth', block: 'start' }); } }, [md, location.hash]); // build antd menu items const menuItems = useMemo(() => { const toAntd = (items: HelpMenuItem[]): any[] => items.map((it) => ({ key: it.path, label: it.title, children: it.children ? toAntd(it.children) : undefined, })); return toAntd(helpMenu); }, []); const defaultOpenKeys = useMemo(() => { const keys: string[] = []; const dfs = (items: HelpMenuItem[]) => { items.forEach((it) => { if (it.children && it.children.length) { keys.push(it.path); dfs(it.children); } }); }; dfs(helpMenu); return keys; }, []); return (
帮助文档
navigate(e.key)} />
On this page
{toc.map((t) => ( ))}
); }; export default HelpLayout;