feat: 接入 KaTeX 渲染,完成 Sätteri 迁移(Phase 3)
方案:用现成的 satteri-katex(MDAST 插件,40 行,是 rehype-katex 的移植), 而不是自己写。它跑在 MDAST 阶段,天然绕开下面这个坑。 为什么不能用 HAST 阶段做 Sätteri 把块级公式输出成 <pre><code class="language-math math-display">, 而 expressive-code 过滤所有 <pre>,会把它当成未知语言渲染成带行号的 plaintext 代码块 —— 实测 10 个块级公式全部被吃掉(math-display: 0)。 unified 时期没这问题,是因为 rehype-katex 排在 rehypePlugins 第一位, 先把 <pre><code> 换掉了。satteri-katex 的源码注释里记录的是同一个坑。 插件顺序 satteriKatex() 放在 mdastPlugins 最后:它把 math 节点换成 KaTeX 的 html 节点,若排在 reading-time / excerpt 之前,字数统计会把 KaTeX 标记也算进去。 顺带修掉一个之前漏掉的回归 reading-time / excerpt 原本用 ctx.textContent(),但它只拼接纯 text 节点, 会漏掉代码块、行内代码、公式、图片 alt。实测 LaTeX 那篇字数 830 → 488。 改用 mdast-util-to-string 的 toString()(与迁移前的实现一致),字数完全复原。 之前没发现是因为我的对比脚本只截取了 <article> 区域,而字数在它外面的 header 里。 现改为整页对比。 清理依赖(10 个,已确认无引用) remark-math / rehype-katex / remark-directive / remark-github-admonitions-to-directives / remark-sectionize / rehype-slug / rehype-components / rehype-autolink-headings / @astrojs/markdown-remark / hastscript / unist-util-visit 验证(与 unified() 基线整页对比,41 页) - 39 页语义完全一致;2 页只剩智能标点在中文/emoji 旁的引号方向差异 (Sätteri 的结果更正确:把开引号排成 “ 而不是 ”)。 - LaTeX 页:class="katex" 76、katex-display 10,与基线完全一致; 块级公式不再被 expressive-code 吞掉。 - 字数/阅读时长与基线逐篇一致(830/4、1042/5、416/2)。 - pnpm build 41 页 + pagefind 通过;astro check 仍是 1 error/0 warnings (原有 footer.astro 报错,与本次无关)。
This commit is contained in:
@@ -14,6 +14,7 @@ import { sectionize } from "./src/plugins/satteri/sectionize.mjs"
|
|||||||
import { shiftHeadings } from "./src/plugins/satteri/shift-headings.mjs"
|
import { shiftHeadings } from "./src/plugins/satteri/shift-headings.mjs"
|
||||||
import { autolinkHeadings } from "./src/plugins/satteri/autolink-headings.mjs"
|
import { autolinkHeadings } from "./src/plugins/satteri/autolink-headings.mjs"
|
||||||
import { imageLightbox } from "./src/plugins/satteri/image-lightbox.mjs"
|
import { imageLightbox } from "./src/plugins/satteri/image-lightbox.mjs"
|
||||||
|
import { satteriKatex } from "satteri-katex"
|
||||||
import { site } from "./src/data/site"
|
import { site } from "./src/data/site"
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
@@ -88,6 +89,9 @@ export default defineConfig({
|
|||||||
excerpt,
|
excerpt,
|
||||||
shiftHeadings(),
|
shiftHeadings(),
|
||||||
sectionize(),
|
sectionize(),
|
||||||
|
// KaTeX 必须排在 reading-time / excerpt 之后:它把 math 节点换成 KaTeX 的
|
||||||
|
// HTML,先换的话 ctx.textContent() 会把 KaTeX 标记也算进字数。
|
||||||
|
satteriKatex(),
|
||||||
],
|
],
|
||||||
hastPlugins: [
|
hastPlugins: [
|
||||||
satteriHeadingIdsPlugin(),
|
satteriHeadingIdsPlugin(),
|
||||||
|
|||||||
+1
-11
@@ -14,7 +14,6 @@
|
|||||||
"reading": "node scripts/fetch-reading.mjs"
|
"reading": "node scripts/fetch-reading.mjs"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@astrojs/markdown-remark": "^7.3.1",
|
|
||||||
"@astrojs/markdown-satteri": "^0.4.1",
|
"@astrojs/markdown-satteri": "^0.4.1",
|
||||||
"@astrojs/mdx": "^8.0.1",
|
"@astrojs/mdx": "^8.0.1",
|
||||||
"@astrojs/react": "^6.0.6",
|
"@astrojs/react": "^6.0.6",
|
||||||
@@ -64,7 +63,6 @@
|
|||||||
"date-fns": "4.1.0",
|
"date-fns": "4.1.0",
|
||||||
"embla-carousel-react": "8.5.1",
|
"embla-carousel-react": "8.5.1",
|
||||||
"gray-matter": "^4.0.3",
|
"gray-matter": "^4.0.3",
|
||||||
"hastscript": "^9.0.1",
|
|
||||||
"input-otp": "1.4.1",
|
"input-otp": "1.4.1",
|
||||||
"katex": "^0.16.22",
|
"katex": "^0.16.22",
|
||||||
"lucide-react": "^0.454.0",
|
"lucide-react": "^0.454.0",
|
||||||
@@ -78,20 +76,12 @@
|
|||||||
"react-resizable-panels": "^2.1.7",
|
"react-resizable-panels": "^2.1.7",
|
||||||
"reading-time": "^1.5.0",
|
"reading-time": "^1.5.0",
|
||||||
"recharts": "2.15.4",
|
"recharts": "2.15.4",
|
||||||
"rehype-autolink-headings": "^7.1.0",
|
|
||||||
"rehype-components": "^0.3.0",
|
|
||||||
"rehype-katex": "^7.0.1",
|
|
||||||
"rehype-slug": "^6.0.0",
|
|
||||||
"remark-directive": "^3.0.1",
|
|
||||||
"remark-github-admonitions-to-directives": "^1.0.5",
|
|
||||||
"remark-math": "^6.0.0",
|
|
||||||
"remark-sectionize": "^2.1.0",
|
|
||||||
"sanitize-html": "^2.17.0",
|
"sanitize-html": "^2.17.0",
|
||||||
"satteri": "^0.10.5",
|
"satteri": "^0.10.5",
|
||||||
|
"satteri-katex": "^0.1.1",
|
||||||
"sonner": "^1.7.4",
|
"sonner": "^1.7.4",
|
||||||
"tailwind-merge": "^2.5.5",
|
"tailwind-merge": "^2.5.5",
|
||||||
"tailwindcss-animate": "^1.0.7",
|
"tailwindcss-animate": "^1.0.7",
|
||||||
"unist-util-visit": "^5.0.0",
|
|
||||||
"vaul": "^1.1.2",
|
"vaul": "^1.1.2",
|
||||||
"zod": "3.25.76"
|
"zod": "3.25.76"
|
||||||
},
|
},
|
||||||
|
|||||||
Generated
+145
-288
File diff suppressed because it is too large
Load Diff
@@ -1,4 +1,5 @@
|
|||||||
import { defineMdastPlugin } from "satteri"
|
import { defineMdastPlugin } from "satteri"
|
||||||
|
import { toString } from "mdast-util-to-string"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 取正文第一个段落作为摘要,写进 frontmatter.excerpt。
|
* 取正文第一个段落作为摘要,写进 frontmatter.excerpt。
|
||||||
@@ -11,7 +12,8 @@ export const excerpt = defineMdastPlugin({
|
|||||||
let text = ""
|
let text = ""
|
||||||
for (const node of root.children) {
|
for (const node of root.children) {
|
||||||
if (node.type !== "paragraph") continue
|
if (node.type !== "paragraph") continue
|
||||||
text = ctx.textContent(node, { includeImageAlt: true, includeHtml: true })
|
// 同 reading-time:ctx.textContent 不含行内代码/公式,用 toString 保持一致
|
||||||
|
text = toString(node)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
ctx.data.astro.frontmatter.excerpt = text
|
ctx.data.astro.frontmatter.excerpt = text
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { defineMdastPlugin } from "satteri"
|
import { defineMdastPlugin } from "satteri"
|
||||||
|
import { toString } from "mdast-util-to-string"
|
||||||
import getReadingTime from "reading-time"
|
import getReadingTime from "reading-time"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -10,8 +11,10 @@ import getReadingTime from "reading-time"
|
|||||||
export const readingTime = defineMdastPlugin({
|
export const readingTime = defineMdastPlugin({
|
||||||
name: "reading-time",
|
name: "reading-time",
|
||||||
after(root, ctx) {
|
after(root, ctx) {
|
||||||
// 与原实现 mdast-util-to-string 的默认行为对齐
|
// 用 mdast-util-to-string 而不是 ctx.textContent:
|
||||||
const text = ctx.textContent(root, { includeImageAlt: true, includeHtml: true })
|
// 后者只拼接纯 text 节点,会漏掉代码块、行内代码、公式、图片 alt,
|
||||||
|
// 导致字数比迁移前少一截(实测 LaTeX 那篇 830 → 488 字)。
|
||||||
|
const text = toString(root)
|
||||||
const stats = getReadingTime(text)
|
const stats = getReadingTime(text)
|
||||||
const frontmatter = ctx.data.astro.frontmatter
|
const frontmatter = ctx.data.astro.frontmatter
|
||||||
frontmatter.minutes = Math.max(1, Math.round(stats.minutes))
|
frontmatter.minutes = Math.max(1, Math.round(stats.minutes))
|
||||||
|
|||||||
Reference in New Issue
Block a user