feat(reader): PDF 改 pdf.js 渲染+斜体 bug 根治+评论假挂载修复+排版类

【T1 PDF 内置阅读重做】iframe 对 arXiv PDF 实测 ERR_ABORTED(浏览器 PDF
插件策略差异)→ pdfjs-dist 6.4.299 客户端 canvas 渲染:只在 /papers/
动态 import(worker ?url 打包,不进全站 bundle);文档一次取回 + IO
懒渲染(rootMargin 400px)+ 页占位 aspect-ratio 防跳动 + 12s 首屏兜底 +
微信 UA 直接兜底;工具条页码/缩放/翻页/下载/arXiv/授权声明保留。红线:
PDF 由访客浏览器直取 arXiv(CORS * 实测),仓内零副本。真页实测:
13 页 canvas 全绘制、首屏墨迹像素非空、零 console 错误。

【T2 斜体排版 bug 真凶】global.css 的 `.prose-* p > em:only-child
{ display:block; text-align:center }` 图注样式按"斜体独占段"自动命中,
把所有正文独占段斜体变成块级居中。改:图注用显式 .fig-cap 驱动 + 补
em/i { display:inline; font-style:italic } 基线;media-sample 迁移新写法,
指南注明旧写法失效原因。实测独占段与行内斜体均 display:inline/italic。

【T3 评论"假挂载"真 bug】原 initArtalk 一进来就置 mounted=true——脚本
404/被拦截时把空容器误标已挂载,5s 兜底被绕过,正是"评论区一直空着"的
来源之一。改为库就绪才算成功;另加三触发路径(IO 主 + 锚点点击 + 滚到
页底 2s 兜底)、5s 失败提示+重试钮、软导航重挂、成功打
data-artalk-mounted=1 供 E2E。

【T4 文章排版类 + 安全白名单】11 个受限类(ink-1/2/3/muted/accent、
font-serif/kai/sans/mono、drop-cap、quote-line、fig-cap)全走 tokens 禁
任意 hex;新增 rehype-class-whitelist.ts:className 白名单 + 剥离 style
与 on*;实测注入用例只剩 class="ink-2",style/onclick 全无。新坑入档:
markdown raw HTML 在 hast 是 type:'raw' 节点、白名单插件看不见 span,必须
先 rehype-raw 再过滤 → 插件链 [rehypeRaw, rehypeClassWhitelist,
rehypeLinkCards]。新依赖:pdfjs-dist、rehype-raw。

车道:op-artalk(T1 6/6、T2 2/2、T3 3/3、T4 6/6);收编复验:build 46 页、
E2E 37/37、四专项 playwright 实测(PDF 13 canvas/斜体 inline/白名单净化/
评论 mounted 标记)

验证: node v26.13.0 + astro 7.4.4 构建 46 页 exit 0;E2E 37/37 PASS

Spec: none (阅读体验四件,车道级交付)

Co-authored-by: GLM-5.3
This commit is contained in:
wait4xandGLM-5.3 committed 2026-11-02 15:59:28 +08:00
1 parent 328ab68f18
commit 280654f9e3
11 files changed
+621 -55

No files matched your search

+50
View File
@@ -0,0 +1,50 @@
/**
* rehype 排版类白名单:文章里允许用 `<span class="...">` / `<p class="fig-cap">` 做受限排版,
* 但**只放行白名单类名**,其余 class 一律剔除;同时剥离 style 与 on* 事件属性(纵深防御)。
*
* 为什么需要:Astro 默认不 sanitize markdown 里的 raw HTML——若直接放行 class,作者(或
* Sveltia 后台协作者)可写入任意类名/颜色破坏灰阶骨架,甚至借 style/on* 注入。
* 白名单与 global.css 的「受限排版类」段一一对应,加类时两处同改。
*/
const ALLOWED = new Set([
// 颜色(走 tokens,明暗双态可读)
'ink-1',
'ink-2',
'ink-3',
'ink-muted',
'ink-accent',
// 字族(与分轨排版共存的四个既有字族)
'font-serif',
'font-kai',
'font-sans',
'font-mono',
// 文学排版
'drop-cap',
'quote-line',
'fig-cap',
]);
export default function rehypeClassWhitelist() {
return (tree: any) => {
const walk = (node: any) => {
if (node?.type === 'element') {
const props = node.properties ?? (node.properties = {});
// 剥离内联样式与事件属性:排版能力只经白名单类提供
delete props.style;
for (const key of Object.keys(props)) {
if (/^on/i.test(key)) delete props[key];
}
const raw = props.className;
if (raw !== undefined) {
const list = Array.isArray(raw) ? raw : String(raw).split(/\s+/);
const kept = list.map((c: string) => String(c).trim()).filter((c: string) => ALLOWED.has(c));
if (kept.length > 0) props.className = kept;
else delete props.className;
}
}
for (const child of node?.children ?? []) walk(child);
};
walk(tree);
};
}