# 06 · 详情页 MDX 系统(目录 · 锚点 · 代码块 · 页脚 · 内容管线) 详情页 = `app/work/[slug]/page.tsx`,复刻原站四个 MDX 组件 + 内容提取管线。URL 与原站一致:`/reunimos` 等根路径 slug 由 `next.config.ts` rewrites 指到 `/work/[slug]`。 ```ts const WORK_SLUGS = ["reunimos","inspire_mono","wasm_design_utils","adrive","shore_icon","teambition"]; rewrites: { beforeFiles: WORK_SLUGS.map(s => ({ source: `/${s}`, destination: `/work/${s}` })) } ``` ## 1. 内容管线(content/workArticles.ts) 生成流程(一次性,脚本见 01 §4): 1. `curl https://haoqi.design/` 存 SSR HTML 2. bs4 解析:按**文档序扁平收集** `article[data-mdx-article]` 下的 p/h2/h3/h4/pre/ul/ol/blockquote/img/hr(排除 header/footer/pre 内部;li 内 p 去重)——不能假设 prose 容器嵌套层级,各文章不一样 3. 类型: ```ts type InlineSeg = { t:"text"|"link"|"code"|"strong"|"em"; v; href? }; type ArticleBlock = | { type:"p"; segs: InlineSeg[] } | { type:"h2"|"h3"|"h4"; text; id? } | { type:"code"; language; label; html; text } // html=原站 prism token HTML | { type:"list"; ordered; items: InlineSeg[][] } | { type:"quote"; segs } | { type:"img"; src } | { type:"hr" }; type WorkArticle = { title; date; year; iframe: {src,title}|null; blocks: ArticleBlock[] }; ``` 4. **代码块**取 `code.decode_contents()`(含行号行 `div.gap-4.grid.grid-cols-[auto_1fr]` 与 `span.token`)+ `code.get_text()`(COPY 用纯文本)+ language class → label(Bash/JavaScript…) 5. ⚠️ 标题文本要剥掉锚点 `#` span(`lstrip('#')`),否则 TOC 标签和 aria-label 会带 "#" 数据是纯 TS 常量,无需运行时解析。 ## 2. MdxHeading(原模块 31250) ```tsx // 类 map(1:1) const HEADING_CLASSES = { 1: "mt-12 text-2xl leading-tight lg:mt-14 lg:text-[28px]", 2: "mt-10 text-xl leading-tight lg:mt-12 lg:text-2xl", 3: "mt-8 text-lg leading-snug lg:mt-10 lg:text-xl", 4: "mt-7 text-base leading-snug lg:mt-8 lg:text-lg", }; // 基类 "group relative scroll-mt-24 font-semibold tracking-tight text-l1 [&:first-child]:mt-0" ``` - id 优先用原站 SSR 的 id,缺省时 slugify:`trim().toLowerCase().replace(/[\s\u3000]+/g,"-").replace(/[^\w\u4e00-\u9fff-]+/g,"").replace(/-{2,}/g,"-").replace(/^-+|-+$/g,"")`(保留中文!) - 锚点 ``,内含隐藏 `#`(`absolute top-0 right-full mr-2 hidden … lg:group-hover:opacity-100`) - 点击:preventDefault → scrollToMdxAnchorTarget(-96)→ `history.replaceState(null,"",#id)` → 复制完整 URL 到剪贴板 ## 3. MdxTableOfContents(原模块 78825) ``` nav[aria-label="本页目录"] class="pointer-events-auto fixed inset-y-0 left-0 z-20 hidden min-h-0 w-[320px] flex-col overflow-hidden 2xl:flex pl-14 pr-2" └ flex flex-col flex-1 min-h-0 overflow-hidden └ flex-1 min-h-0 overflow-y-auto overscroll-y-contain no-scrollbar └ box-border flex min-h-full flex-col py-7 └ flex min-h-0 flex-1 flex-col justify-center gap-1 p-2 text-sm leading-snug text-l2 ``` - **只在 2xl(≥1536px)显示**;收集 `article[data-mdx-article]` 内**有 id 的 h1/h2/h3**(label = textContent 剥掉 aria-hidden span) - 无可收集标题 → 渲染 null - 链接:`block min-w-0 shrink-0 cursor-pointer truncate rounded-md py-0.5 transition-colors hover:text-l1 ${active?"text-l1":"text-l2"}`,`paddingLeft = [0,8,16][level-1]` - active:滚动/resize/ResizeObserver 监听,**最后一个 `top <= 96` 的标题**;点击即滚(scrollToMdxAnchorTarget)+ replaceState - 收集时机:effect 内立即 + 0ms + 150ms 各一次(水合后 DOM 才有 id) ## 4. CodeBlock(原模块 93325) ``` div.group.my-6.overflow-hidden.rounded-xl.bg-[rgba(var(--label-d),0.03)] ├ header: flex items-center justify-between px-3 py-2 shadow-[inset_0_-1px_0_0_var(--line)] lg:px-4 │ ├ span: px-1 font-mono text-xs tracking-wide text-(--label-3) ← 语言标签 │ └ button: COPY/COPIED,dotted 边框 hover(before:border-dotted 体系), │ aria-label Copy code/Copied code,复制成功 1200ms 后还原 └ pre: overflow-x-auto px-4 py-2 font-mono text-xs leading-relaxed text-(--label-1) lg:px-5 lg:py-2.5 lg:text-sm └ code.language-{lang} ← dangerouslySetInnerHTML(原站 token HTML,行号行自带) ``` 行号行结构(提取来的 HTML 里已有):`div.gap-4.grid.grid-cols-[auto_1fr] > span[aria-hidden].select-none.text-right.tabular-nums.text-(--label-4)` + 内容 span。token 配色靠 `--code-*` 变量(02 §2)。 ## 5. ArticleFooter(原模块 54450) ``` footer[data-mdx-footer]: bg-[rgba(var(--label-d),0.03)] mt-16 p-4 lg:p-6 rounded-xl ├ "Metadata" 标题 ├ dl grid-cols-2 lg:grid-cols-3:Last Updated(文章日期)/ Dimensions(实时 `${innerWidth}×${innerHeight}`,resize 监听) │ / Characters(article.textContent 去 footer 后的字符数,toLocaleString("en-US")) ├ 虚线分隔 my-6 border-line border-t border-dashed └ Links grid-cols-2 lg:grid-cols-3(首列 lg 可见标题): Home → ;Work/Contact → sessionStorage pendingAnchor + startNavigation("/") 社交列(# 占位) ``` - 值全部经 ScrambleReveal(`font-mono tabular-nums text-l2 text-sm`,startDelay 300/360/420,`scrambleColors=false`) - 字符数统计:`footerRef` 回调里 clone article、`[data-mdx-footer]` 移除后 `textContent.length`(用 `[...str].length` 数 code point) - ⚠️ ScrambleReveal 初始不显示,所以 SSR HTML 里 dd 是空的、按钮文本为空——不是 bug ## 6. 页面骨架 ```
h1(font-bold text-3xl! lg:text-5xl! + wdth 120) p(mt-1 tabular-nums text-l2! text-sm! lg:text-base!)=日期 div(mt-2 border-line border-b) p(pt-3 …)=年份
← 原站 prose 类全串(58 个类,逐字从 SSR 提取,含 [&_p]/[&_ul]/[&_blockquote_*]/[&_:not(pre)>code]/[&_u] 点状下划线 等) blocks… + iframe(reunimos 有 840×600 live 预览)
← 放 article 之外 ``` ## 7. 提示词(让 AI 复刻详情页) > 克隆 haoqi.design 的文章详情页(Next.js App Router,/work/[slug],根路径 slug 用 beforeFiles rewrites)。内容来自 content/workArticles.ts(我描述结构:每篇文章有 title/date/year/iframe/blocks;blocks 是 p|h2/h3|code|list|img|hr|quote 的可辨析联合类型;code 块带 language/label/html/text 四字段,html 是原站 prism 高亮后的 span.token + 行号行 HTML)。 > 1. 文章骨架:article[data-mdx-article],容器 mx-auto px-6 py-18 lg:py-24 max-w-[880px] text-l1;header.pt-[10vh] 放 h1(font-bold text-3xl! lg:text-5xl! + fontVariationSettings wdth 120)、日期、border-line 分隔线、年份。prose 容器类我提供逐字全串(含 [&_p]:text-sm 等任意变体),不要简化。 > 2. 标题组件:h2/h3/h4 用原站类 map(mt-10 text-xl…),id 用数据里的(中文 id 保留);标题内套锚点 a[aria-label="Copy link to …"],hidden 的 # 在 lg:group-hover 显示;点击 preventDefault + 滚动到 top-96(lerp .1,走 scrollEnv)+ history.replaceState + 复制完整 URL。 > 3. 左侧目录:fixed left-0 w-[320px],仅 2xl:flex,pl-14 pr-2,内层 flex 竖排 min-h-0 overflow-y-auto no-scrollbar,垂直居中;收集有 id 的 h1-h3,缩进 0/8/16px,active = 最后一个 getBoundingClientRect().top<=96 的标题(scroll/resize/ResizeObserver 监听);点击滚动 + replaceState;无标题渲染 null。 > 4. 代码块:rounded-xl 组容器 + header(语言标签 font-mono text-xs text-(--label-3),COPY 按钮 dotted before 边框 hover,复制后 COPIED 1200ms 还原,aria-label Copy code/Copied code)+ pre(overflow-x-auto px-4 py-2 font-mono text-xs leading-relaxed)+ code.language-X 用 dangerouslySetInnerHTML 注入预高亮 HTML(行号和配色都来自原站 HTML 与 --code-* 变量,不要自己引入 prism)。 > 5. 页脚:bg-[rgba(var(--label-d),0.03)] mt-16 p-4 lg:p-6 rounded-xl;Metadata dl 三列 = 日期 / 实时视口尺寸(resize 监听)/ 正文字符数(clone article 去 footer 后 textContent,toLocaleString en-US);值用 ScrambleReveal(scrambleColors=false,错峰 300/360/420)。Links 区:Home 是 Link,Work/Contact 是 sessionStorage("hq:pendingScrollAnchor") + 全局过渡导航回首页。 > 验证(CDP):/wasm_design_utils 有 4 个 pre、TOC 7 项、h2 id=安装、COPY 按钮存在、footer dd 渲染后出现 "宽×高" 和千分位字符数;TOC 点击后目标标题 top≈96。