Skip to content

feat: add local image insertion with file picker and hardened safety checks - #291

Merged
AmethystLiang merged 1 commit into
mainfrom
improve-markdown-editor
Apr 4, 2026
Merged

AmethystLiang merged 1 commit into
mainfrom
improve-markdown-editor

Conversation

@AmethystLiang

Copy link
Copy Markdown
Contributor

Problem

Users cannot easily insert images from their local computer into markdown notes. Previously, the image insertion feature relied on window.prompt() which is unreliable in Electron, and required users to provide image URLs instead of selecting from the filesystem.

Additionally, image handling lacked safety guards: there was no protection against silent file overwrites, no limit on filename deconfliction attempts, and relative image paths in the rich editor weren't resolved consistently with preview mode.

Solution

  • Local image picker: Added a native OS file dialog (via Electron's dialog.showOpenDialog) to let users select image files from their computer
  • Toolbar button: Added an "Image" button to the rich editor toolbar for quick image insertion
  • Hardened file operations:
    • Use COPYFILE_EXCL flag to prevent silent overwrites
    • Add 1000-iteration limit to deconfliction loop to prevent hangs
    • Validate paths are absolute before copying
  • Consistent image URL resolution: Custom Image extension resolves relative image src to file:// URLs using the same resolver as preview mode, and re-renders when filePath changes
  • Extracted toolbar component: Moved ToolbarButton into reusable RichMarkdownToolbarButton with tooltip support
  • Fixed slash commands: Use setHeading (not toggleHeading) for idempotent /h1, /h2, /h3 behavior
  • Path utilities: Added dirname and joinPath helpers with comprehensive tests for cross-platform path handling

Changes

  • src/main/ipc/shell.ts: Added pickImage and copyFile IPC handlers with safety checks
  • src/preload/index.ts / index.d.ts: Exposed new IPC methods
  • src/renderer/src/components/editor/RichMarkdownEditor.tsx: Integrated image picker, fixed closure issues with editorRef, added image toolbar button
  • src/renderer/src/components/editor/RichMarkdownToolbarButton.tsx (new): Extracted toolbar button component with tooltips
  • src/renderer/src/components/editor/rich-markdown-image-utils.ts (new): Image copy destination logic with deconfliction
  • src/renderer/src/components/editor/rich-markdown-extensions.ts: Custom Image extension for consistent URL resolution
  • src/renderer/src/components/editor/rich-markdown-commands.tsx: Updated image slash command to use new picker
  • src/renderer/src/lib/path.ts: Enhanced dirname for Windows drive roots
  • src/renderer/src/lib/path.test.ts (new): Tests for path utilities
  • src/renderer/src/assets/main.css: Task list styling improvements

Testing

  • Image insertion via toolbar button and /image slash command
  • Filename deconfliction when picking an image that collides with existing file
  • Relative paths in inserted markdown markdown
  • Cross-platform path handling (Windows drive roots, POSIX paths)

… hardened safety checks

- Add local image insertion support with file picker and toolbar button
- Harden image copy with COPYFILE_EXCL flag to prevent overwrites
- Add deconfliction loop limit (1000 iterations) to prevent hangs
- Force image re-render on filePath change for correct URL resolution
- Extract toolbar button into reusable RichMarkdownToolbarButton component
- Use setHeading (not toggleHeading) for idempotent slash commands
- Add path utilities (dirname, joinPath) with comprehensive tests
- Add image utility functions for safe copy destination handling
@AmethystLiang
AmethystLiang merged commit e053b4f into main Apr 4, 2026
1 check passed
@AmethystLiang
AmethystLiang deleted the improve-markdown-editor branch April 4, 2026 07:31
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
… hardened safety checks (stablyai#291)

- Add local image insertion support with file picker and toolbar button
- Harden image copy with COPYFILE_EXCL flag to prevent overwrites
- Add deconfliction loop limit (1000 iterations) to prevent hangs
- Force image re-render on filePath change for correct URL resolution
- Extract toolbar button into reusable RichMarkdownToolbarButton component
- Use setHeading (not toggleHeading) for idempotent slash commands
- Add path utilities (dirname, joinPath) with comprehensive tests
- Add image utility functions for safe copy destination handling
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