diff --git a/src/pages/article/ArticleDetail.tsx b/src/pages/article/ArticleDetail.tsx index 36c2696..0322afb 100644 --- a/src/pages/article/ArticleDetail.tsx +++ b/src/pages/article/ArticleDetail.tsx @@ -6,8 +6,11 @@ * - 正文:public/articles/.md * - 索引:public/articles/index.json(id 必须等于 slug) * - slug:^[a-z0-9]+(?:-[a-z0-9]+)*$,≤64 字符 + * + * 版式:左侧浮动目录(锚点快速定位 + 滚动高亮)+ 中间正文 + 右侧浮动「更多教程」, + * 两侧面板 position: sticky 固定显示;窄屏(≤1280px)自动收起为单栏。 */ -import { useEffect, useMemo, useState } from 'react'; +import { useEffect, useMemo, useRef, useState } from 'react'; import { Link, useParams } from 'react-router-dom'; import ReactMarkdown from 'react-markdown'; import SubHeader from '@components/header/Header'; @@ -26,12 +29,34 @@ interface ArticleMeta { tags?: string[]; } +interface TocItem { + id: string; + text: string; + level: 2 | 3; +} + +/** 标题文本 → 锚点 id:小写、空白转连字符、仅保留字母数字(含中文)与连字符 */ +function slugify(s: string): string { + return ( + s + .trim() + .toLowerCase() + .replace(/\s+/g, '-') + .replace(/[^\p{L}\p{N}-]/gu, '') + .replace(/-{2,}/g, '-') + .replace(/^-+|-+$/g, '') || 'section' + ); +} + function ArticleDetail() { const { slug } = useParams<{ slug: string }>(); const [meta, setMeta] = useState(null); const [list, setList] = useState([]); const [md, setMd] = useState(''); const [state, setState] = useState<'loading' | 'ok' | 'notfound' | 'badslug'>('loading'); + const [toc, setToc] = useState([]); + const [active, setActive] = useState(''); + const articleRef = useRef(null); const valid = useMemo(() => !!slug && slug.length <= SLUG_MAX && SLUG_RE.test(slug), [slug]); @@ -69,6 +94,65 @@ function ArticleDetail() { }; }, [slug, valid]); + // 正文渲染完成后:给 h2/h3 挂锚点 id,并生成左侧目录 + useEffect(() => { + if (state !== 'ok' || !md) return; + const root = articleRef.current; + if (!root) return; + const heads = Array.from(root.querySelectorAll('h2, h3')); + const seen: Record = {}; + const items: TocItem[] = heads.map(el => { + const base = slugify(el.textContent || ''); + const n = (seen[base] = (seen[base] || 0) + 1); + const id = n > 1 ? `${base}-${n}` : base; + el.id = id; + return { id, text: (el.textContent || '').trim(), level: el.tagName === 'H3' ? 3 : 2 }; + }); + setToc(items); + setActive(items[0]?.id || ''); + // 带锚点进入(/article/xxx#heading)时直接定位 + if (location.hash) { + const target = document.getElementById(decodeURIComponent(location.hash.slice(1))); + if (target) requestAnimationFrame(() => target.scrollIntoView({ block: 'start' })); + } + }, [md, state]); + + // 滚动时高亮目录当前节(rAF 节流) + useEffect(() => { + if (!toc.length) return; + let raf = 0; + const onScroll = () => { + if (raf) return; + raf = requestAnimationFrame(() => { + raf = 0; + const y = window.scrollY + 140; + let cur = toc[0].id; + for (const t of toc) { + const el = document.getElementById(t.id); + if (!el) continue; + if (el.getBoundingClientRect().top + window.scrollY <= y) cur = t.id; + else break; + } + setActive(prev => (prev === cur ? prev : cur)); + }); + }; + onScroll(); + window.addEventListener('scroll', onScroll, { passive: true }); + return () => { + window.removeEventListener('scroll', onScroll); + if (raf) cancelAnimationFrame(raf); + }; + }, [toc]); + + const goTo = (e: React.MouseEvent, id: string) => { + e.preventDefault(); + const el = document.getElementById(id); + if (!el) return; + setActive(id); + el.scrollIntoView({ behavior: 'smooth', block: 'start' }); + history.replaceState(null, '', '#' + id); + }; + const others = list.filter(d => d.id !== slug).slice(0, 6); return ( @@ -77,29 +161,50 @@ function ArticleDetail() { title={meta?.title || (state === 'badslug' ? '链接无效' : '米剪 教程')} desc={meta?.description || language.val('common_company')} /> -
- {state === 'loading' &&

加载中…

} - - {state === 'badslug' && ( -
-

这个地址不符合文章链接规则(只允许小写字母、数字和连字符)。

- 回到首页 -
+
+ {toc.length > 0 && ( + )} - {state === 'notfound' && ( -
-

没有找到这篇文章:{slug}

- 回到首页 -
- )} +
+ {state === 'loading' &&

加载中…

} - {(state === 'ok' || (state === 'loading' && md)) && ( -
- {meta?.date &&

更新于 {meta.date}

} - {md} -
- )} + {state === 'badslug' && ( +
+

这个地址不符合文章链接规则(只允许小写字母、数字和连字符)。

+ 回到首页 +
+ )} + + {state === 'notfound' && ( +
+

没有找到这篇文章:{slug}

+ 回到首页 +
+ )} + + {(state === 'ok' || (state === 'loading' && md)) && ( +
+ {meta?.date &&

更新于 {meta.date}

} + {md} +
+ )} +
{others.length > 0 && (