Skip to content

fix(site): 图渲染修复 + Claude 主题 + 暗色切换 + 高亮兜底#2

Merged
Albert-PZY merged 2 commits into
masterfrom
feat/site-claude-theme-fix
Jul 25, 2026
Merged

fix(site): 图渲染修复 + Claude 主题 + 暗色切换 + 高亮兜底#2
Albert-PZY merged 2 commits into
masterfrom
feat/site-claude-theme-fix

Conversation

@Albert-PZY

Copy link
Copy Markdown
Owner

问题(你线上看到的)

打开 https://albert-pzy.github.io/mcp-tutorial/ 后:

  • PlantUML 图既不渲染 SVG、源码也不显示出来。
  • 代码块没有语法高亮。

根因

1) PlantUML 整体被隐藏

plantuml.js 把渲染结果写进了内层 <pre class="plantuml">,而 CSS 里这个 <pre>
display:none(让它只作隐藏的源码容器)。所以无论成功还是失败,图和源码都被一同藏起来了。
日志(CI 里跑过一轮)其实提示“没有 SVG”,但人眼只在浏览器里看到一片空,就是这个原因。

2) 代码高亮在不同环境不稳定

jsdelivr CDN 在某些网络下不可达;而我的启动脚本在 DOMContentLoaded 直接 if(window.hljs)
判断,CDN 还没加载完就跳过了高亮,导致代码块裸露。

修复

PlantUML

  • data-source 从内层 <pre> 移到外层 <figure class="figure-wrap"><pre class="plantuml">
    退回为隐藏的源码占位容器(CSS 仍 display:none)。
  • plantuml.js 干脆把渲染结果写进 外层 <figure>
    • 成功:内嵌 <svg> + figcaption,加 figure-ready,整张图可见可点。
    • 失败(plantuml.com 不可达):在同一 <figure> 内放一个展开式 <details>
      显示原始 PlantUML 源码 + .figure-error-hint;图区域不再空白。
  • 点击放大监听挂在 <figure> 上,但忽略 <details>/<pre>/<code> 内的点击,避免误触。

代码高亮

  • 默认主题换成 GitHub Dark 作为基础;style.css 又在浅 / 暗两种主题下各自重写 token 颜色,
    保证视觉一致。
  • CDN 双源:jsdelivr 4 秒内没拿到 window.hljs 就自动加载 unpkg 兜底。
  • 启动脚本改为 轮询最多 8 秒hljs 就位后再 highlightElement;都失败时代码块以
    纯文本裸露,仍可阅读,不会破坏页面。

Claude 主题 + 暗亮色切换

  • 风格整体对齐 Claude:暖米白底色、Claude 珊瑚咖啡色作为强调色、衬线正文 + 等宽代码、
    紧凑侧边导航 + hero + 内容区。
  • <html data-theme="light|dark">;侧边栏底部「🌗 主题」按钮切换;偏好写入
    localStorage["mcp-theme"];首次访问跟随 prefers-color-scheme
  • 首屏前内联脚本提前设置 data-theme,避免刷新闪屏(FOUC)。
  • 缩放浮层、callout、表格、figure 等都为两套主题调整了对比度。

验证

  • 本地 python -m http.server,全部资源 200;新 HTML 标记数符合预期(5 figure、5 pre.plantuml、
    theme-toggle、unpkg fallback、github-dark css 都在)。
  • tests/dom-smoke.cjs(零依赖 node 脚本 + 最小 DOM mock + Node zlib 兜 CompressionStream):
    • 成功路径:5/5 figure-ready。
    • 离线路径:5/5 figure-error、不再整体隐藏。
  • CI 会按现有 tests/playwright/page.spec.ts 继续过测试;现在因为修了真 bug,离线回退用例
    也会真正断言 <svg>.figure-error-hint 至少有一个
    (之前因为它被隐藏在 display:none 的 pre
    里,该断言其实总是 0)。

合并后

合到 master 即触发 deploy-pages.yml 自动重发布到
https://albert-pzy.github.io/mcp-tutorial/ —— 线上就会直接看到修复后的效果。

…me + dark mode

Two real bugs found by user review on the deployed page:

1) PlantUML diagrams + source were both invisible on the live site.
   Root cause: plantuml.js wrote content into the inner <pre.plantuml>
   (CSS display:none) instead of the outer <figure>. So both the success
   SVG and the offline source listing were hidden.
   Fix:
   - Move data-source onto <figure>; keep <pre class="plantuml"> as a
     hidden source container only.
   - host = figure.figure-wrap; success writes <svg> + caption into it
     and toggles .figure-ready; failure writes a visible <details> with
     the raw .puml source + an error hint inline, toggles .figure-error.
   - bindZoom listens on the figure but ignores clicks inside the
     offline <details> block so the fallback source stays clickable.

2) Code blocks were not syntax-highlighted on deploy.
   Fix:
   - hljs CDN github-dark theme as base; style.css re-declares token
     colors per light/dark mode so the look stays consistent.
   - jsdelivr first, jsdelivr-fail -> unpkg fallback (auto loads after 4s).
   - boot script polls up to 8s for window.hljs before highlighting, so
     the page is still readable even if all CDNs are blocked.

Plus a full visual rework to clone the Claude.ai feel:
- Warm beige background + Claude coral/terracotta accent + serif body
  font + monospace code; light + dark via <html data-theme>.
- Theme toggle button in sidebar persists to localStorage and follows
  prefers-color-scheme on first visit; inline pre-paint bootstrap avoids FOUC.
- Tightened typography, hero, callouts, tables, and the zoom overlay
  for both themes.

Verified locally: http.server all assets 200; dom-smoke.cjs confirms
both success (5/5 figure-ready) and offline (5/5 figure-error) paths.
…fline paths

tests/dom-smoke.cjs builds a tiny DOM mock + Node zlib-backed
CompressionStream/Response shims and exercises McpSite.renderPlantUml()
twice (network on -> 5/5 figure-ready; network off -> 5/5 figure-error).
Catches regression of the host-target bug fixed in the previous commit.
@Albert-PZY
Albert-PZY merged commit 82759a0 into master Jul 25, 2026
1 check passed
@Albert-PZY
Albert-PZY deleted the feat/site-claude-theme-fix branch July 25, 2026 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant