fix: restore the page content call and stop writing the registry

- render_active_document passed the tab id as the first __setContent argument,
  but the page signature starts with previewHtml, so #preview rendered the tab
  id ("1", "2", "3" as tabs were opened) and the window looked blank. The call
  now goes through set_content_script(), and a test pins the exact script plus
  the page signature so the two cannot drift silently.
- remove register_as_default() and the winreg dependency: the app no longer
  writes HKCU on launch. publish.ps1 still refreshes the .md association.
This commit is contained in:
2026-11-04 15:25:13 +08:00
parent 0b97bcb6dc
commit 590a9a016c
6 changed files with 74 additions and 161 deletions

77
Cargo.lock generated
View File

@@ -1683,7 +1683,6 @@ dependencies = [
"serde_json",
"tao",
"url",
"winreg",
"winresource",
"wry",
]
@@ -3722,15 +3721,6 @@ dependencies = [
"windows-targets 0.42.2",
]
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets 0.48.5",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
@@ -3773,21 +3763,6 @@ dependencies = [
"windows_x86_64_msvc 0.42.2",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm 0.48.5",
"windows_aarch64_msvc 0.48.5",
"windows_i686_gnu 0.48.5",
"windows_i686_msvc 0.48.5",
"windows_x86_64_gnu 0.48.5",
"windows_x86_64_gnullvm 0.48.5",
"windows_x86_64_msvc 0.48.5",
]
[[package]]
name = "windows-targets"
version = "0.52.6"
@@ -3828,12 +3803,6 @@ version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
@@ -3846,12 +3815,6 @@ version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
@@ -3864,12 +3827,6 @@ version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
@@ -3888,12 +3845,6 @@ version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
@@ -3906,12 +3857,6 @@ version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
@@ -3924,12 +3869,6 @@ version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
@@ -3942,12 +3881,6 @@ version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
@@ -3981,16 +3914,6 @@ dependencies = [
"memchr",
]
[[package]]
name = "winreg"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a277a57398d4bfa075df44f501a17cfdf8542d224f0d36095a2adc7aee4ef0a5"
dependencies = [
"cfg-if",
"windows-sys 0.48.0",
]
[[package]]
name = "winresource"
version = "0.1.31"

View File

@@ -16,9 +16,6 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"
url = "2"
[target.'cfg(windows)'.dependencies]
winreg = "0.52"
[target.'cfg(windows)'.build-dependencies]
winresource = "0.1"

View File

@@ -28,7 +28,7 @@
1. 在 [Releases](https://git.wucl12.cn/wucl12/md-preview/releases) 页下载 `MD-Preview-windows-x64.exe`(当前发布版本 `v1.4.2-local.5`,2026-10-30);
2. 放到一个固定目录(例如 `C:\Tools\md-preview\`),不要随意改名/移动;
3. 双击运行,或在任意 `.md` 上右键 → 打开方式 → 选择该 exe 并设为默认;
3. 双击运行,或在任意 `.md` 上右键 → 打开方式 → 选择该 exe 并设为默认(程序本身不再写注册表;首次需要在“打开方式 → 选择其他应用”里选一次);
4. Windows 10/11 一般自带 WebView2 Runtime;若提示缺失,安装微软的 Evergreen Runtime 即可。
> exe 未做代码签名,SmartScreen 可能提示“未知发布者”,选择“更多信息 → 仍要运行”即可。

View File

@@ -214,7 +214,8 @@ cargo test
- 代码整理:`cargo fmt` 通过;clippy 7 条清零;保存写盘合并为 `save_active_file()`;`build.ps1` 的禁止字符串不再包含泛化的 `powershell`。
- 架构拆分阶段 1(功能不变,`1115052`):`assets/page/{page.html,page.css,page.js}` 存页面;新增 `src/markdown.rs`(md→html 管线)、`src/page.rs`(页面组装,只填 `__MDP_*__` 占位符)、`src/escape.rs`(JS/HTML 转义)。`main.rs` 5535 → 3709 行;页面输出用 home/doc/startup 三组快照做 sha256 对比,逐字节一致。
- 架构拆分阶段 2(功能不变,2026-11-03 晚):新增 `src/config.rs`(路径/zoom/窗口几何/对话框);删除 macOS/Linux/Finder 扩展代码、非 Windows 分支和 `objc2` 依赖(仓库现在只能在 Windows 编译);测试模块移到文件末尾;`main.rs` 3709 → 2822 行。59 tests(删除死代码时移除 3 个平台专用测试)。
- 已决定、未实施:移除应用启动时的 HKCU 关联写入;`publish.ps1` 维持现状(继续写关联)。
- HKCU 关联:应用启动时不再写注册表(`register_as_default` 及 `winreg` 依赖已删除);`publish.ps1` 维持现状(继续写 ProgID/关联)。首次使用需在 Windows“打开方式 → 选择其他应用”里选一次。
- 白屏 bug(2026-11-04 修复):`render_active_document` 给 `window.__setContent` 传了 8 个参数,页面签名只有 7 个(多传了 `active.id`),标签 id 被当成 `previewHtml` 渲染——窗口白屏、打开标签依次显示 1/2/3/4。修复:抽出 `set_content_script()`(单一参数源),新增精确输出 + 页面签名对照测试 `set_content_script_matches_the_page_signature`(60 tests)。教训:Rust→JS 这条边界没有编译器检查。
- 发布前 UI 冒烟:`agent/tests/ui-smoke.ps1`(普通桌面 PowerShell;启动 CDP、断言页面渲染、截图、写 `target/ui-test/ui-smoke.json`)。DSH 沙箱禁止命名管道,WebView2/Chromium 在沙箱内无法启动(窗口白屏、`msedgewebview2.exe` 立即退出),所以这一项只能由用户手动跑。`ui-smoke.mjs` 已用 mock CDP 服务验证通过。
- 规则调整(`agent/AGENTS.md`):仓库内新增文件/拆模块不需要事先询问;bug 修复、重构、测试、文档可直接做(每项独立提交);只有仓库外修改、发布、新第三方库(>200 KB vendored 资源)和不变量变化需要先问。
- 下一步(讨论中,未动手):编辑区样式化——加粗、标题加粗加大、行内代码换字体。

View File

@@ -28,12 +28,12 @@
## 4. 待做功能(都需要先讨论/确认)
### 4.1 移除应用自身的 HKCU 写入(已定方向,未实施)
### 4.1 移除应用自身的 HKCU 写入(2026-11-04 已完成)
- 删除 `main()` 里的 `register_as_default()` 调用和该函数(`src/main.rs`);应用不再写注册表。
- 已删除 `main()` 里的 `register_as_default()` 调用、该函数以及 `winreg` 依赖;应用不再写注册表。
- **`publish.ps1` 维持现状**(继续写 ProgID/关联),这是用户明确决定。
- 影响:新机器上应用不再自动出现在“打开方式”;用户通过 Windows 设置选择一次默认应用;exe 移动后由发布脚本刷新关联。
- 需要同步:README“下载与运行”里的关联说明、STATUS 的注册表相关条目。
- 影响:新机器上应用不再自动出现在“打开方式”;首次在 Windows“打开方式 → 选择其他应用”里选一次;exe 移动后由发布脚本刷新关联。
- README 第 3 步、STATUS §11 已同步。
### 4.2 编辑区样式化(需求已明确,实现未选型)
@@ -85,11 +85,12 @@ cd D:\Scripts\md-preview\AgentLowWorking
- mjs 通过 CDP 读取页面快照、断言(页面加载、`#preview` 有内容、`window.ipc` 桥、页面 API、标题/折叠按钮/代码高亮),再截图,写 `target/ui-test/ui-smoke.json` 与 `ui-smoke.png`。
- Agent 可以直接读这两个文件定位问题;失败时把 report 里的 checks 贴出来即可。
- 已知边界:只验证“页面真的渲染了 + 基础 API 在”,不覆盖标签切换、保存、外部修改等 Rust 侧流程(那些靠单元测试和人工点)。
- 注意:如果用 `-Exe` 指向 Nutstore 的发布版(Medium),启动会写 HKCU 关联(应用原有行为,指向它自己);默认的开发构建是 Low,写不了注册表。
- 2026-11-04 起应用启动不再写注册表,所以用发布版做冒烟也不会改 .md 关联;默认的开发构建仍是 Low,配置目录隔离在 `target/ui-test`。
## 6. 注意
- 本仓库现在**只能在 Windows 上编译**;`cargo test` 里仍有 `cfg(target_os = "windows")` 的 Windows 专用测试。
- `main.rs` 顶部的 `windows_subsystem` 只对 release 生效;debug 构建带控制台,属正常。
- 页面 JS 改动后建议做一次语法检查(临时测试把页面写到文件,再 `node --check`)。
- **Rust → JS 调用没有编译器检查**:改 `__setContent` / `__setPreview` 等参数列表时,必须同步改页面的函数签名,并用 `set_content_script_matches_the_page_signature` 这类“精确输出 + 页面签名”测试锁住。2026-11-04 的白屏就是这么来的(多传了一个 `active.id`,标签 id 被当 HTML 渲染)。
- 构建/测试临时文件放 `target\tmp`;不要写 `%TEMP%`。

View File

@@ -534,70 +534,6 @@ fn load_window_icon() -> Option<tao::window::Icon> {
tao::window::Icon::from_rgba(rgba.into_raw(), w, h).ok()
}
/// Register the ProgID in HKCU and advertise it once for Markdown extensions
/// so MD Preview shows up in the "Open with" list. Win8+ blocks silent
/// default-handler changes — only the Settings app can confirm those.
fn register_as_default() {
use winreg::enums::HKEY_CURRENT_USER;
use winreg::RegKey;
let Ok(exe) = std::env::current_exe() else {
return;
};
let exe_str = exe.to_string_lossy().to_string();
let progid = "MDPreview.md";
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
// Refresh the ProgID command on every launch: the exe is portable, so a
// moved or renamed binary must not leave "Open with" pointing at a stale path.
let progid_root = format!(r"Software\Classes\{progid}");
if let Ok((k, _)) = hkcu.create_subkey(&progid_root) {
let _ = k.set_value("", &"Markdown Document".to_string());
let _ = k.set_value("FriendlyTypeName", &"Markdown Document".to_string());
}
if let Ok((k, _)) = hkcu.create_subkey(format!(r"{progid_root}\DefaultIcon")) {
let _ = k.set_value("", &format!("\"{exe_str}\",0"));
}
if let Ok((k, _)) = hkcu.create_subkey(format!(r"{progid_root}\shell\open\command")) {
let _ = k.set_value("", &format!("\"{exe_str}\" \"%1\""));
}
// Applications\<exe-name> entry gives us a friendly label in the "Open with" menu.
if let Some(exe_name) = exe.file_name().map(|n| n.to_string_lossy().to_string()) {
let app_root = format!(r"Software\Classes\Applications\{exe_name}");
if let Ok((k, _)) = hkcu.create_subkey(&app_root) {
let _ = k.set_value("FriendlyAppName", &"MD Preview".to_string());
}
if let Ok((k, _)) = hkcu.create_subkey(format!(r"{app_root}\shell\open\command")) {
let _ = k.set_value("", &format!("\"{exe_str}\" \"%1\""));
}
if let Ok((k, _)) = hkcu.create_subkey(format!(r"{app_root}\SupportedTypes")) {
for ext in [".md", ".markdown", ".mdown", ".mkd"] {
let _ = k.set_value::<String, _>(ext, &String::new());
}
}
}
// Advertise the ProgID once; re-running this is unnecessary.
let marker_dir = config_dir();
let marker = marker_dir.join(".md-preview-registered");
if marker.exists() {
return;
}
for ext in [".md", ".markdown", ".mdown", ".mkd"] {
let path = format!(r"Software\Classes\{ext}\OpenWithProgids");
if let Ok((key, _)) = hkcu.create_subkey(&path) {
let _ = key.set_value::<String, _>(progid, &String::new());
}
}
let _ = fs::create_dir_all(&marker_dir);
let _ = fs::write(&marker, "");
// Intentionally no dialog: users can pick MD Preview via "Open with"
// whenever they want, and Win10+ blocks silent default-handler changes
// anyway — asking them to click through Settings on first launch is noise.
}
fn is_supported_document(path: &Path) -> bool {
path.extension()
.map(|extension| {
@@ -868,6 +804,32 @@ fn render_home(
update_window_title(window, session);
}
/// Build the `__setContent` call.
///
/// The argument list must match the `window.__setContent` signature in
/// `assets/page/page.js`. Nothing type-checks this boundary: when the two
/// drift apart the page reads a wrong value as HTML (it once rendered the tab
/// id, so a white window showed "1", "2", "3" as tabs were opened).
fn set_content_script(
html: &str,
raw: &str,
base_href: &str,
flags: EnhanceFlags,
editing: bool,
scroll_ratio: f32,
) -> String {
format!(
"if(window.__setContent)window.__setContent('{}', '{}', '{}', {}, {}, {}, {});",
escape_js(html),
escape_js(raw),
escape_js(base_href),
flags.math,
flags.mermaid,
editing,
scroll_ratio
)
}
fn render_active_document(
webview: &WebView,
window: &Window,
@@ -897,16 +859,13 @@ fn render_active_document(
let base_href = base_href_for_file(&path).unwrap_or_default();
let flags = enhance_flags_for(&raw);
*enhance_flags.lock().unwrap() = flags;
let _ = webview.evaluate_script(&format!(
"if(window.__setContent)window.__setContent({}, '{}', '{}', '{}', {}, {}, {}, {});",
active.id,
escape_js(&html),
escape_js(&raw),
escape_js(&base_href),
flags.math,
flags.mermaid,
let _ = webview.evaluate_script(&set_content_script(
&html,
&raw,
&base_href,
flags,
active.editing,
active.scroll_ratio
active.scroll_ratio,
));
for script in build_enhancer_bootstrap(flags, *loaded_enhancers) {
let _ = webview.evaluate_script(&script);
@@ -1374,7 +1333,6 @@ fn main() {
};
let strings = Strings::english();
register_as_default();
bench_log("after_register");
let mut initial_session = DocumentSession::default();
@@ -2627,6 +2585,39 @@ mod tests {
assert!(page.contains("sectionizeFolds"));
}
#[test]
fn set_content_script_matches_the_page_signature() {
let script = set_content_script(
"<p>x</p>",
"# x",
"file:///D:/tmp/",
EnhanceFlags {
math: true,
mermaid: false,
},
true,
0.25,
);
assert_eq!(
script,
"if(window.__setContent)window.__setContent('<p>x</p>', '# x', 'file:///D:/tmp/', true, false, true, 0.25);"
);
// The page must declare the same argument order; a mismatch renders a
// wrong value as HTML and shows up as a blank window.
let page = build_page(
"",
"",
None,
EnhanceFlags::default(),
&Strings::english(),
true,
);
assert!(page.contains(
"window.__setContent = function(previewHtml, rawMd, baseHref, needsMath, needsMermaid, editing, scrollRatio)"
));
}
#[test]
fn closing_the_last_editing_tab_restores_the_empty_preview() {
let strings = Strings::english();