From 42e5b11c13d3fb917dd07b63da929add37d47ca1 Mon Sep 17 00:00:00 2001 From: 3970260761 <3970260761@qq.com> Date: Thu, 12 Nov 2026 10:57:47 +0800 Subject: [PATCH] =?UTF-8?q?style(article):=20=E6=95=99=E7=A8=8B=E9=A1=B5?= =?UTF-8?q?=E4=B8=89=E6=A0=8F=E9=87=8D=E6=8E=92=E7=89=88=20=E2=80=94=20?= =?UTF-8?q?=E5=B7=A6=E6=B5=AE=E5=8A=A8=E7=9B=AE=E5=BD=95/=E5=8F=B3?= =?UTF-8?q?=E6=B5=AE=E5=8A=A8=E6=9B=B4=E5=A4=9A=E6=95=99=E7=A8=8B/?= =?UTF-8?q?=E6=AD=A3=E6=96=87=E9=97=B4=E8=B7=9D=E5=8A=A0=E5=A4=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - layout 三栏: 左 sticky 目录(224px,滚动高亮+点击锚点) + 正文(940px) + 右 sticky 更多教程(256px) - 底部「更多教程」区块移至右侧固定浮动 - 正文排版全面加大间距: h2 52px上边距+底部分隔线, 段落20px, 列表10px, blockquote/表格/代码块统一美化 - 锚点 scroll-margin-top 让开 60px fixed 导航 - <1280px 回退单栏: 目录隐藏, 更多教程回正文下方全宽 --- src/pages/article/ArticleDetail.tsx | 147 ++++++++-- src/pages/article/article.module.less | 405 ++++++++++++++++++++------ 2 files changed, 435 insertions(+), 117 deletions(-) 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 && (