style(article): 教程页三栏重排版 — 左浮动目录/右浮动更多教程/正文间距加大

- layout 三栏: 左 sticky 目录(224px,滚动高亮+点击锚点) + 正文(940px) + 右 sticky 更多教程(256px)
- 底部「更多教程」区块移至右侧固定浮动
- 正文排版全面加大间距: h2 52px上边距+底部分隔线, 段落20px, 列表10px, blockquote/表格/代码块统一美化
- 锚点 scroll-margin-top 让开 60px fixed 导航
- <1280px 回退单栏: 目录隐藏, 更多教程回正文下方全宽
This commit is contained in:
3970260761 committed 2026-11-12 10:57:47 +08:00
1 parent 8689a350e3
commit 42e5b11c13
2 files changed
+435 -117

No files matched your search

+126 -21
View File
@@ -6,8 +6,11 @@
* - 正文:public/articles/<slug>.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<ArticleMeta | null>(null);
const [list, setList] = useState<ArticleMeta[]>([]);
const [md, setMd] = useState<string>('');
const [state, setState] = useState<'loading' | 'ok' | 'notfound' | 'badslug'>('loading');
const [toc, setToc] = useState<TocItem[]>([]);
const [active, setActive] = useState<string>('');
const articleRef = useRef<HTMLElement | null>(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<HTMLElement>('h2, h3'));
const seen: Record<string, number> = {};
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')}
/>
<div className={styles.wrap}>
{state === 'loading' && <p className={styles.tip}>加载中…</p>}
{state === 'badslug' && (
<div className={styles.tip}>
<p>这个地址不符合文章链接规则(只允许小写字母、数字和连字符)。</p>
<Link to="/">回到首页</Link>
</div>
<div className={styles.layout}>
{toc.length > 0 && (
<nav className={styles.toc} aria-label="文章目录">
<div className={styles.tocTitle}>目录</div>
<ul className={styles.tocList}>
{toc.map(t => (
<li key={t.id} className={t.level === 3 ? styles.lvl3 : undefined}>
<a
href={'#' + t.id}
className={active === t.id ? styles.active : undefined}
onClick={e => goTo(e, t.id)}
>
{t.text}
</a>
</li>
))}
</ul>
</nav>
)}
{state === 'notfound' && (
<div className={styles.tip}>
<p>没有找到这篇文章:{slug}</p>
<Link to="/">回到首页</Link>
</div>
)}
<div className={styles.main}>
{state === 'loading' && <p className={styles.tip}>加载中…</p>}
{(state === 'ok' || (state === 'loading' && md)) && (
<article className={styles.article}>
{meta?.date && <p className={styles.meta}>更新于 {meta.date}</p>}
<ReactMarkdown className={styles.md}>{md}</ReactMarkdown>
</article>
)}
{state === 'badslug' && (
<div className={styles.tip}>
<p>这个地址不符合文章链接规则(只允许小写字母、数字和连字符)。</p>
<Link to="/">回到首页</Link>
</div>
)}
{state === 'notfound' && (
<div className={styles.tip}>
<p>没有找到这篇文章:{slug}</p>
<Link to="/">回到首页</Link>
</div>
)}
{(state === 'ok' || (state === 'loading' && md)) && (
<article className={styles.article} ref={articleRef}>
{meta?.date && <p className={styles.meta}>更新于 {meta.date}</p>}
<ReactMarkdown className={styles.md}>{md}</ReactMarkdown>
</article>
)}
</div>
{others.length > 0 && (
<aside className={styles.related}>
+309 -96
View File
@@ -5,12 +5,23 @@
background: #f5f6fa;
}
.wrap {
// 三栏布局:左浮动目录 + 正文 + 右浮动「更多教程」
.layout {
flex: 1;
width: 100%;
max-width: 880px;
max-width: 1440px;
margin: 0 auto;
padding: 32px 20px 64px;
padding: 92px 28px 72px; // 顶部让开 fixed 导航(60px)+ 间距
display: flex;
align-items: flex-start;
justify-content: center;
gap: 28px;
}
.main {
flex: 1;
min-width: 0;
max-width: 940px;
}
.tip {
@@ -29,112 +40,92 @@
}
.article {
padding: 28px 32px;
padding: 40px 48px 48px;
background: #fff;
border: 1px solid #e4e6eb;
border-radius: 12px;
border: 1px solid #e8eaed;
border-radius: 14px;
box-shadow: 0 1px 3px rgba(16, 24, 40, 0.05);
}
.meta {
margin: 0 0 16px;
margin: 0 0 6px;
color: #9aa0a6;
font-size: 13px;
}
.md {
font-size: 15px;
line-height: 1.9;
color: #202124;
word-break: break-word;
// ---------- 左侧浮动目录 ----------
.toc {
position: sticky;
top: 76px; // fixed 导航 60px + 16px 间距
flex-shrink: 0;
width: 224px;
max-height: calc(100vh - 108px);
overflow-y: auto;
padding: 18px 16px 18px 0;
}
:global {
h1 {
font-size: 26px;
margin: 8px 0 18px;
}
.tocTitle {
margin: 0 0 10px 14px;
font-size: 12px;
font-weight: 700;
letter-spacing: 0.12em;
color: #9aa0a6;
}
h2 {
font-size: 20px;
margin: 28px 0 12px;
padding-left: 10px;
border-left: 4px solid #202124;
}
.tocList {
margin: 0;
padding: 0 0 0 2px;
list-style: none;
border-left: 2px solid #e8eaed;
h3 {
font-size: 17px;
margin: 22px 0 10px;
}
li a {
display: block;
padding: 7px 8px 7px 14px;
margin-left: -2px;
border-left: 2px solid transparent;
color: #5f6368;
font-size: 13.5px;
line-height: 1.55;
text-decoration: none;
border-radius: 0 8px 8px 0;
transition: color 0.15s, border-color 0.15s, background 0.15s;
p,
ul,
ol {
margin: 10px 0;
}
li {
margin: 4px 0;
}
code {
background: #f1f3f4;
border-radius: 4px;
padding: 1px 6px;
font-size: 13px;
}
pre {
background: #f8f9fb;
border: 1px solid #e4e6eb;
border-radius: 8px;
padding: 14px;
overflow: auto;
code {
background: none;
padding: 0;
}
}
blockquote {
margin: 14px 0;
padding: 8px 16px;
border-left: 4px solid #dadce0;
color: #5f6368;
}
table {
border-collapse: collapse;
margin: 14px 0;
th,
td {
border: 1px solid #e4e6eb;
padding: 8px 12px;
font-size: 14px;
}
}
img {
max-width: 100%;
border-radius: 8px;
}
a {
&:hover {
color: #1a73e8;
background: #f0f6ff;
}
}
li.lvl3 a {
padding-left: 30px;
font-size: 13px;
color: #80868b;
}
li a.active {
color: #1a73e8;
font-weight: 600;
border-left-color: #1a73e8;
background: #f0f6ff;
}
}
// ---------- 右侧浮动「更多教程」----------
.related {
margin-top: 24px;
padding: 20px 24px;
position: sticky;
top: 76px;
flex-shrink: 0;
width: 256px;
padding: 20px 20px 12px;
background: #fff;
border: 1px solid #e4e6eb;
border-radius: 12px;
border: 1px solid #e8eaed;
border-radius: 14px;
box-shadow: 0 1px 3px rgba(16, 24, 40, 0.05);
h3 {
margin: 0 0 12px;
font-size: 16px;
margin: 0 0 6px;
font-size: 15px;
font-weight: 700;
}
ul {
@@ -144,29 +135,251 @@
}
li {
display: flex;
align-items: baseline;
gap: 10px;
padding: 8px 0;
border-top: 1px dashed #e4e6eb;
display: block;
padding: 11px 10px;
margin: 0 -10px;
border-top: 1px solid #f1f3f4;
border-radius: 8px;
font-size: 14px;
&:first-child {
border-top: none;
}
&:hover {
background: #f7f9fc;
}
a {
display: block;
color: #202124;
font-weight: 600;
line-height: 1.5;
text-decoration: none;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
&:hover {
color: #1a73e8;
}
}
}
}
.desc {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
color: #9aa0a6;
font-size: 13px;
font-size: 12.5px;
line-height: 1.6;
margin-top: 3px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
// ---------- 正文排版 ----------
.md {
font-size: 16px;
line-height: 2;
color: #2b2d31;
word-break: break-word;
:global {
h1 {
font-size: 30px;
font-weight: 800;
line-height: 1.4;
letter-spacing: -0.01em;
margin: 0 0 14px;
color: #0f1419;
}
h2 {
font-size: 23px;
font-weight: 700;
line-height: 1.45;
margin: 52px 0 18px;
padding-bottom: 12px;
border-bottom: 1px solid #e8eaed;
color: #0f1419;
scroll-margin-top: 84px; // 锚点跳转让开 fixed 导航
}
h3 {
font-size: 18.5px;
font-weight: 700;
line-height: 1.5;
margin: 38px 0 14px;
color: #1f2328;
scroll-margin-top: 84px;
}
h4,
h5,
h6 {
margin: 30px 0 12px;
font-weight: 700;
}
p {
margin: 20px 0;
}
ul,
ol {
margin: 18px 0;
padding-left: 1.6em;
}
li {
margin: 10px 0;
line-height: 1.95;
&::marker {
color: #1a73e8;
}
}
li p {
margin: 6px 0;
}
strong {
font-weight: 700;
color: #0f1419;
}
code {
background: #f4f5f7;
border: 1px solid #eceef1;
border-radius: 6px;
padding: 2px 7px;
font-size: 14.5px;
color: #c7254e;
}
pre {
background: #f6f8fa;
border: 1px solid #e8eaed;
border-radius: 12px;
padding: 20px 22px;
overflow: auto;
margin: 24px 0;
line-height: 1.75;
code {
background: none;
border: none;
padding: 0;
color: #24292f;
font-size: 14px;
}
}
blockquote {
margin: 24px 0;
padding: 14px 22px;
background: #f7f9fc;
border-left: 4px solid #1a73e8;
border-radius: 0 10px 10px 0;
color: #5f6368;
font-size: 15.5px;
p {
margin: 8px 0;
}
}
table {
border-collapse: collapse;
width: 100%;
margin: 26px 0;
border: 1px solid #e8eaed;
border-radius: 10px;
overflow: hidden;
font-size: 15px;
th {
background: #f6f8fa;
font-weight: 700;
color: #0f1419;
padding: 11px 16px;
border: 1px solid #e8eaed;
text-align: left;
}
td {
padding: 11px 16px;
border: 1px solid #e8eaed;
line-height: 1.7;
}
tr:nth-child(even) td {
background: #fafbfc;
}
}
hr {
border: 0;
border-top: 1px solid #e8eaed;
margin: 48px 0;
}
img {
display: block;
max-width: 100%;
margin: 26px auto;
border-radius: 10px;
}
a {
color: #1a73e8;
&:hover {
text-decoration: underline;
}
}
}
}
// ---------- 窄屏:收起两侧浮动面板,恢复单栏 ----------
@media (max-width: 1280px) {
.layout {
flex-wrap: wrap;
padding: 84px 16px 56px;
}
.toc {
display: none;
}
.main {
max-width: 100%;
}
.related {
position: static;
width: 100%;
margin-top: 4px;
}
.article {
padding: 28px 22px 36px;
}
.md {
font-size: 15.5px;
:global {
h1 {
font-size: 25px;
}
h2 {
font-size: 21px;
margin-top: 40px;
}
}
}
}