Use org-download to drag image to emacs

Now we know how to create our posts and insert image via org-mode's C-c i (org-insert-link), let's talk about how to use drag-and-drop method to add image to your post.


Drag-and-drop image ONLY support under GUI emacs.

How to start ?

To make drag-and-drop image to work on emacs, first you need to install org-download. Just use emacs's M-x with package-install to add this package to your emacs.

M-x package-install RET org-download RET

If you use use-package to install package in your emacs, you can use following code instead

(use-package org-download
  :ensure t
  ;; add support to dired
  (add-hook 'dired-mode-hook 'org-download-enable))

Add .dir-locals.el

Now we need to add .dir-locals.el to our hexo's root directory, create a file called .dir-locals.el with following contents


      ;; make drag-and-drop image save in the same name folder as org file
      ;; ex: `aa-bb-cc.org' then save image test.png to `aa-bb-cc/test.png'
      (defun my-org-download-method (link)
        (let ((filename
                (car (url-path-and-query
                      (url-generic-parse-url link)))))
              (dirname (file-name-sans-extension (buffer-name)) ))
          ;; if directory not exist, create it
          (unless (file-exists-p dirname)
            (make-directory dirname))
          ;; return the path to save the download files
          (expand-file-name filename dirname)))

      ;; only modify `org-download-method' in this project
      (setq-local org-download-method 'my-org-download-method)


This will help emacs use my-org-download-method when enter the hexo project.

Drag and Drop images

Now you can drag and drop image to your posts.

For example, you the posts you edit is called hi-this-is-a-post.org, the org-download will create a folder called hi-this-is-a-post and contains the image you drag into.

Happy coding :)