drag-and-dropでブラウザから画像をダウンロードしてリンクを入れる

request.elを使う.

(defun my/org-insert-uri-image (uri action)
  (if (not (eq major-mode 'org-mode))
      (dnd-open-file uri action)
    (require 'request)
    (request uri
      :encoding 'binary
      :complete (cl-function
                 (lambda (&key data response &allow-other-keys)
                   (let* ((ext (pcase (request-response-header response "content-type")
                                 ("image/jpeg" ".jpeg")
                                 ("image/png" ".png")
                                 (typ "")))
                          (fname (format "./%s%s"
                                         (format-time-string "%Y-%m-%d_%H-%M-%S"
                                                             (current-time))
                                         ext)))
                     (with-temp-buffer
                       (insert data)
                       (write-region nil nil fname))
                     (insert (format "[[%s]]" fname))))))))

(push '("^\\(https?\\)://" . my/org-insert-uri-image) dnd-protocol-alist)

TODO: ローカルの画像もmvして同じようにやりたい

TOP

Date: 2023-08-03 Thu 00:00

Last modified at: 2023-08-03 Thu 00:07