fix: preserve Windows drive image paths in export (#3323) by Jocs · Pull Request #5732 · marktext/marktext · GitHub
Skip to content

fix: preserve Windows drive image paths in export (#3323) - #5732

Merged
Jocs merged 5 commits into
developfrom
fix/3323-windows-image-export
Oct 15, 2026
Merged

Jocs merged 5 commits into
developfrom
fix/3323-windows-image-export

Conversation

@Jocs

@Jocs Jocs commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

Closes #3323

Summary

Exporting Markdown such as ![photo](<C:\Users\Alice\Pictures\photo.png>) loses the image source: the export sanitizer interprets the drive letter as an unsupported URL scheme and removes src before the local-image resolver runs.

Extend the Muya and desktop export sanitizers' existing URI allowlists to retain absolute Windows drive paths (C:\..., C:/..., and C:%5C...). The existing resolveLocalImageSrc then converts them to file:///C:/... URLs in the shared HTML/PDF/print pipeline. Markdown rendering keeps its existing behavior, avoiding duplicate path conversion there. Source Markdown is unchanged.

PR #5716 already repairs encoded separators downstream. This change ensures the absolute image source survives sanitization and reaches that resolver.

Type of change

  • Bug fix (non-breaking, fixes an issue)
  • New feature (non-breaking, adds functionality)
  • Breaking change (causes existing functionality to change)
  • Documentation update

Test plan

  • Four regression tests exercise Windows drive paths through the actual desktop export wrapper and Muya renderer: backslashes, forward slashes, URL-encoded backslashes, and paths with spaces. All four fail on the original implementation and pass with the fix.
  • Both export sanitizer configurations are tested for accepted absolute drive paths and rejected JavaScript, VBScript, unknown schemes, and drive-relative paths (c:photo.png). A complete export-pipeline test verifies a JavaScript image source is removed.
  • Desktop export and print image suites: 48 tests passed.
  • Muya full unit suite: 287 files / 2,478 tests passed.
  • Root lint and typecheck passed; lint reports existing warnings, no errors.
  • Muya type checking and full lint passed; existing warnings remain.
  • Manually tested on Windows: no Windows runtime was available. Automated verification ran on macOS ARM64 and did not exercise a native file drag or inspect a generated PDF visually.

Notes for reviewers

The URI exception requires a single drive letter followed by a colon and a path separator; arbitrary unknown protocols remain blocked. As with the existing ALLOWED_URI_REGEXP, the exception applies to all URI attributes inspected by the export sanitizer, including href and src. It does not affect the preview sanitizer or enable ALLOW_UNKNOWN_PROTOCOLS.


By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

@Jocs
Jocs requested a review from Tkaixiang October 14, 2026 05:10

@Tkaixiang Tkaixiang left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, tested before and after results, after works well

…on (#3323)

Replace the export URI allowlist change with the approach MarkText 0.17
used: rewrite Windows drive image paths to file:/// URLs before DOMPurify
runs, so the sanitizer validates the final src instead of being widened to
accept bare `C:` values.

- add a `correctImageTokenSrc` marked walkTokens hook that runs image
  tokens through the existing `correctImageSrc` (#1997) helper
- register it in `getHighlightHtml` (HTML / PDF / print export,
  `renderToStaticHTML`) and `getClipboardHtml` (copy as HTML); the shared
  `walkTokens` is left alone because the editor's lexer also uses it
- revert both `ALLOWED_URI_REGEXP` edits and the allowlist specs
- new `windowsDriveImageExport.spec.ts` drives all three renderers with
  `isWin` mocked

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@Tkaixiang Tkaixiang left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to restore the original export behaviour present in 0.17

Previously, Windows drive letters C:\... were converted to file:/// before being passed into DOMPurify, so it was correctly parsed into src tags in images.

However, 0.20's Muya re-write caused this regression efcaf0c

Tkaixiang and others added 2 commits October 15, 2026 13:39
…hem (#3323)

Follow-up to the previous commit. Instead of rewriting only Markdown image
tokens, resolve every exported <img> src while sanitizing, so the allowlist
checks the final file:// URL and the desktop no longer patches srcs into
already-sanitized HTML.

- add `resolveExportImageSrc` (ported from the desktop `resolveLocalImageSrc`)
  to a new `utils/fileUrl.ts`, alongside the path helpers moved out of
  `utils/image.ts`; it leaves anything with a URL scheme to the sanitizer
- add `sanitizeExportHtml`, which attaches an `uponSanitizeAttribute` hook to
  the shared purifier for the call only; use it in `MarkdownToHtml`,
  `renderToStaticHTML` and `getSanitizeClipboardHtml`. This also covers raw
  <img> tags, which the token hook missed (resized / aligned images)
- drop the `correctImageTokenSrc` token hook
- desktop: remove `rewriteImageSrcs`, the printService image loop and
  `resolveImageSrc.ts`; `renderMarkdown` loses its `renderStatic` flag
- specs: `exportImageSrc.spec.ts` covers all three renderers and the ported
  resolver cases; desktop export specs add drive-path and raw <img> cases

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copy as HTML puts the sanitized markup on the clipboard as text for pasting
into other sites, where a resolved `file://` path is useless and exposes the
user's folder layout. Revert `getSanitizeClipboardHtml` to the plain export
sanitizer so only HTML / PDF / print export and `renderToStaticHTML` resolve
image srcs, and pin the clipboard behaviour in `exportImageSrc.spec.ts`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Jocs
Jocs merged commit d0428e9 into develop Oct 15, 2026
18 checks passed
@Jocs
Jocs deleted the fix/3323-windows-image-export branch October 15, 2026 07:01
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.

Image Error while import/export

2 participants