From 9950607cd4b3a5947030e367220164f097ff3a0c Mon Sep 17 00:00:00 2001 From: "mula.liu" Date: Mon, 26 Oct 2026 15:43:59 +0800 Subject: [PATCH] fix(markdown): keep images when a document with relative figures is converted MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Same class of bug as the DOCX one, different cause. A Markdown image is stored as a path, so Pandoc has to open the file to embed it - and it resolves that path against its own working directory, which for a running Shift is nowhere near the document. 智飞空地一体化低空应用平台技术方案.md references images/system-architecture.png and images/production-deployment-topology.png; both came back as "Could not fetch resource", and Pandoc replaced each figure with its alt text. The DOCX was 22 KB with no word/media entry at all. Passing --resource-path of the source's own folder fixes DOCX, RTF and HTML in one line. It is additive rather than a replacement: Pandoc still searches the working directory too, so a figure that resolved before still resolves - verified with two images of the same relative name in two different folders, both of which landed in the output. md -> pdf already worked, because Pandoc leaves the path untouched there and hands the HTML to the renderer with a base URL pointing at the document's folder; only the routes where Pandoc must read the bytes itself were affected. md -> html was the quieter case: --self-contained promises one portable file, and without the figures inlined the single HTML opened as a broken image icon. keeps_markdown_images_when_converting_to_word pins docx and rtf. It fails against the previous build with "docx ... lost the picture", and the RTF check reads the pngblip marker rather than assuming a zip. --- src-tauri/src/files.rs | 47 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src-tauri/src/files.rs b/src-tauri/src/files.rs index c096a79..8f25112 100644 --- a/src-tauri/src/files.rs +++ b/src-tauri/src/files.rs @@ -1220,6 +1220,13 @@ fn convert_document( let output = unique_output_in(input, output_dir, "", extension)?; let mut command = background_command(&pandoc); command.arg(input); + // Pandoc resolves an image path relative to its own working directory, + // which for a running Shift is nowhere near the document. Without this a + // Markdown image is reported as unfetchable and replaced by its alt text, + // so the DOCX/RTF/HTML output silently loses every figure. + if let Some(directory) = input.parent().filter(|value| !value.as_os_str().is_empty()) { + command.arg("--resource-path").arg(directory); + } if let Some(writer) = writer { command.args(["-t", writer]); } @@ -2605,6 +2612,46 @@ mod tests { fs::remove_dir_all(dir).unwrap(); } + /// A Markdown image is stored as a path, so Pandoc has to fetch the file to + /// embed it. It resolves that path against its own working directory, which + /// for a running Shift is nowhere near the document, and a figure it cannot + /// read is quietly replaced by its alt text. + #[test] + fn keeps_markdown_images_when_converting_to_word() { + if which::which("pandoc").is_err() { + return; + } + let dir = scratch_dir(); + let nested = dir.join("images"); + fs::create_dir_all(&nested).unwrap(); + let picture = nested.join("architecture.png"); + sample_image(&picture); + let input = dir.join("report.md"); + fs::write( + &input, + "# Report\n\n![Architecture](images/architecture.png)\n", + ) + .unwrap(); + + for target in ["docx", "rtf"] { + let output = convert_document(&input, target, None, None).unwrap(); + let bytes = fs::read(&output).unwrap(); + let embedded = if target == "docx" { + let mut archive = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); + let names: Vec = archive.file_names().map(str::to_string).collect(); + names.iter().any(|name| name.starts_with("word/media/")) + } else { + bytes.windows(7).any(|window| window == b"pngblip") + }; + assert!( + embedded, + "{target} written from {} lost the picture", + input.display() + ); + } + fs::remove_dir_all(dir).unwrap(); + } + #[cfg(target_os = "macos")] #[test] fn renders_presentations_to_pdf() {