Skip to content

fix(notes): 修复打开 Markdown 文件时相对路径图片无法渲染 - #448

Merged
feigeCode merged 1 commit into
mainfrom
fix/markdown-image-relative-path
Oct 22, 2026
Merged

feigeCode merged 1 commit into
mainfrom
fix/markdown-image-relative-path

Conversation

@feigeCode

Copy link
Copy Markdown
Owner

问题

打开本地 Markdown 文件时,正文里相对路径的图片(如 ![logo](./assets/logo.png))无法渲染。

根因

NotesView 打开外部 Markdown 文件时,编辑器经 MarkdownEditor::from_markdown_embedded_with_host 创建,其 file_path 恒为 None:

  • Editor::image_base_dir()(crates/markdown-editor/src/editor/runtime_context.rs:88)在 file_path 为 None 时回退到进程 CWD(GUI 应用通常是 /)
  • 相对路径图片由此解析到错误位置,渲染失败
  • 同根因还影响图片粘贴目录:<stem>.assets 退化为 untitle.assets

修复

文件 改动
crates/markdown-editor/src/editor/host.rs Editor 新增公开 file_path() / set_file_path();设置新路径后重建图片运行时(rebuild_image_runtimes)并通知
crates/notes/src/markdown_view.rs open_markdown_document 创建编辑器后立即绑定 descriptor.absolute_path;remap_markdown_sessions(笔记本内重命名)同步更新编辑器 file_path
crates/notes/src/notes_actions.rs apply_rename 传入 cx

验证

  • ✅ 回归测试 standalone_markdown_binds_editor_to_the_opened_file:打开含相对路径图片的 md 文件,断言 editor.file_path() 为文件绝对路径(dev 基线隔离 worktree 中 1 passed)
  • ✅ cargo check -p markdown-editor 0 error

NotesView 打开外部 Markdown 文件时,编辑器经 from_markdown_embedded_with_host
创建,file_path 恒为 None,image_base_dir 回退到进程 CWD,导致相对路径图片
解析失败。现在:

- Editor 新增公开的 file_path()/set_file_path(),设置后重建图片运行时并通知
- open_markdown_document 创建编辑器后立即绑定 descriptor.absolute_path
- 笔记本内重命名文档时(remap_markdown_sessions)同步更新编辑器 file_path

回归测试:standalone_markdown_binds_editor_to_the_opened_file
@feigeCode
feigeCode merged commit b66dec8 into main Oct 22, 2026
8 checks passed
@feigeCode feigeCode mentioned this pull request Oct 22, 2026
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