Files
md-preview/agent/docs/scan-2026-10-21.md

126 lines
5.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 代码扫描记录 — 2026-10-21
> 基线:`src/main.rs` 4229 行,`cargo test` 39 passed。
> 行号均基于该基线,改动后可能偏移。
## 0. 验证方式
工作区是 Medium,而 DSH 的 workspace-write 是 Low,直接 `cargo test` 会报:
```text
error: failed to open: D:\Scripts\md-preview\target\debug\.cargo-build-lock
拒绝访问。 (os error 5)
```
为了在不提权的情况下完成扫描,本次把编译输出临时放到 TEMP:
```powershell
$env:CARGO_TARGET_DIR = Join-Path $env:TEMP 'mdp-scan-target'
cargo test
```
结果:**39 passed / 0 failed**;唯一警告是 5 个 dead variants。
(TEMP 里编出来的 exe 是 Low,不能拿来当成品。)
## 1. 残留
| # | 位置 | 问题 | 影响 |
|---|---|---|---|
| 1 | `src/main.rs:3336` | `show_info_dialog(...)` 被调用,但全项目没有定义 | 在 `cfg(target_os = "macos")` 内,Windows 不编译所以没暴露;编 macOS 直接失败 |
| 2 | `src/main.rs:2917` | 菜单项 `mdPreviewQuit:` 没有在 ClassBuilder 注册(只注册 8 个 selector) | macOS Cmd+Q 无效 / unrecognized selector |
| 3 | `src/main.rs:43` | `CloseActiveTab / ToggleEdit / ShowFind / SetTheme / Quit` 在 Windows 永不构造 | 唯一编译警告 |
| 4 | main.rs 全文 | macOS/Linux 分支约 900 行 + Cargo 里 3 个 objc 依赖 | Windows 不编译,属于多平台包袱 |
| 5 | `src/main.rs:1947` | `body.classList.add('missing')` 没有对应 CSS | 死状态 |
| 6 | `src/main.rs:251` | `theme.txt` / `ThemeChoice` 在 Windows 没有入口 | macOS 菜单配套残留 |
| 7 | `HANDOFF.md` | 行数 4218 与实际 4229 不符 | 已顺手更新 |
## 2. Bug
### 2.1 `\\?\` 前缀污染(高可见度 / 低风险)
`session.rs` 的 `normalize_path` 使用 `fs::canonicalize`,Windows 返回 verbatim 路径:
```text
%LOCALAPPDATA%\md-preview\recent-files.txt
\\?\D:\Scripts\XFab\Agent\notes\slicer-frontload-migration-2.md
...
session.json "tabs": ["\\\\?\\D:\\Scripts\\..."]
```
影响:
- 空状态 Recent 列表的二级路径直接显示 `\\?\D:\...`;
- 丢失文件页的路径 `<code>` 也带前缀;
- session/recent 跨机器、跨用户失效。
### 2.2 Base href 畸形(由 2.1 引出)
`file_url_for_path_dir` 手工拼 `file://`,对 `\\?\D:\Scripts\md-preview` 得到:
```text
file:////%3F/D:/Scripts/md-preview/
```
正确应为 `file:///D:/Scripts/md-preview/`。目前本地图片都 base64 内嵌、链接由 JS 拦截,所以问题不明显;**未内嵌的相对图片**(扩展名不支持/读取失败)会加载失败。
建议直接用已有依赖 `url`:
```rust
url::Url::from_directory_path(dir).ok().map(|u| u.to_string())
```
### 2.3 右键被全局禁用(本轮保持现状)
`src/main.rs:1489`:预览模式 `preventDefault()`,编辑模式仅 textarea 放行。
本轮决定**继续禁用**右键菜单,缩放改走 Ctrl+滚轮。
### 2.4 非 md 本地链接静默失败
`local_document_path_from_url` 只接受 `md/markdown/mdown/mkd/txt`;点 `file:` 的 pdf/html 链接毫无反应,也没有提示。
建议:非文档类型交给 `open::that`(仍然离线)。
### 2.5 折叠状态会被自动保存冲掉(新功能约束)
`src/main.rs:4083`:每次保存都用新 HTML 重设 `#preview.innerHTML`,而自动保存是 700ms。
折叠状态如果只放 DOM,用户打字时每 0.7 秒展开一次。必须按 `tab + heading slug` 持久化。
### 2.6 其它低风险
| 问题 | 位置 | 说明 |
|---|---|---|
| 大文档 IPC 体积 | `src/main.rs:3745` | 启动页已避开 2MB 限制,但保存仍把整篇 md 走 `postMessage`,预览走 `evaluate_script`;3MB 文档没测过 |
| 每次保存全量重高亮 | `src/main.rs:1845` | 大文档高频保存会卡,可节流 |
| session.json 写入窗口 | `src/session.rs:113` | Windows 上先 `remove_file` 再 `rename`,中间崩溃会丢会话 |
| 主题背景覆盖 | `src/main.rs:1079` / `1271` | `#preview pre` 的底色会盖掉 hljs 主题背景,换 VS 配色时必须同步改 |
| 删按钮的连带点 | `src/main.rs:1350/1353/1333` | HTML、CSS、JS、Strings、测试共 6 处 |
## 3. 已确认干净
- 更新功能无残留:exe 中 `api.github`、`btn-update`、`Check for Updates`、`powershell`、`update-check-result`、`open-url` 计数均为 0。
- 扫描时 exe 与 `target\release\md-preview.exe` SHA256 一致:`1931EA5D...A3A6F`;重新构建后哈希可能变化(同源码/同大小,非 bit-for-bit 可复现),以 `build.ps1` 输出为准。
- 文件关联:`HKCU\Software\Classes\MDPreview.md\shell\open\command` 指向固定路径;`.md` 的 `UserChoiceLatest\ProgId = MDPreview.md`。
- 单实例、会话恢复、外部修改保护、自动保存、查找、缩放、打印逻辑均有测试或明确实现。
- `.gitignore` 基本正确(只需补 agent 临时目录)。
## 4. 修复建议(按优先级)
1. `\\?\` 去前缀 + base href 改用 `url::Url::from_directory_path`(约 1 h,含测试)。
2. 非 md 本地链接交给系统打开(约 0.5 h)。
3. session.json 写入加 `.bak` 兜底(约 0.5 h)。
4. macOS 两个残留错误:要么修掉,要么把 macOS 分支整体删除(0.5 h / 3–5 h)。
5. 补一条 3MB 文档“编辑-保存-切换”冒烟(约 1 h)。
## 5. 复现命令
```powershell
# 测试
$env:CARGO_TARGET_DIR = Join-Path $env:TEMP 'mdp-scan-target'
cargo test
# 产物一致性
Get-FileHash .\MD-Preview-windows-x64.exe, .\target\release\md-preview.exe -Algorithm SHA256
# 关联
reg query "HKCU\Software\Classes\MDPreview.md\shell\open\command"
```