markdown 插入剪切板图片

有没有在 markdown-mode 下快速将剪切板图片插入到当前buffer的, 类似于在org-mode下,使用org-download 插件的 org-download-clipboard 命令的效果

看起来 markdown-mode 是支持了 dnd 和 yank-media 的

不过看起来 handles 不太全,可以自行加上或者提个 PR 参考 org-setup-yank-dnd-handlers:

(defun org-setup-yank-dnd-handlers ()
  "Setup the `yank-media' and DND handlers for buffer."
  (let ((handler (if (>= emacs-major-version 30)
                     #'org--dnd-multi-local-file-handler
                   #'org--dnd-local-file-handler)))
    (setq-local dnd-protocol-alist
                (append
                 (list (cons "^file:///" handler)
                       (cons "^file:/[^/]" handler)
                       (cons "^file:[^/]" handler))
                 dnd-protocol-alist)))
  (when (fboundp 'yank-media-handler)
    (yank-media-handler "image/.*" #'org--image-yank-media-handler)
    ;; Looks like different DEs go for different handler names,
    ;; https://larsee.com/blog/2019/05/clipboard-files/.
    (yank-media-handler "x/special-\\(?:gnome\\|KDE\\|mate\\)-files"
                        #'org--copied-files-yank-media-handler))
  (when (boundp 'x-dnd-direct-save-function)
    (setq-local x-dnd-direct-save-function #'org--dnd-xds-function)))
1 个赞
 ;; 支持 Windows 从剪贴板复制图片
  (defun freedom/insert-image-from-clipboard ()
    "Insert an image from the clipboard into the current org buffer."
    (interactive)
    (let* ((current-dir (file-name-directory buffer-file-name))
           (file-name-base (file-name-base buffer-file-name))
           (attach-dir (concat current-dir "attach/" file-name-base "/"))
           ;; (attach-dir (concat current-dir "attach/"  (file-name-nondirectory buffer-file-name) "/"))
           (image-file (concat attach-dir (format-time-string "%Y%m%d_%H%M%S") ".png")))
      ;; Ensure attach directory exists
      (unless (file-exists-p attach-dir)
        (make-directory attach-dir t))
      ;; Save the clipboard image to the attach directory
      (if (eq system-type 'windows-nt)
          (progn 
            (shell-command (concat "powershell -command \"Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.Clipboard]::GetImage().Save('" image-file "', [System.Drawing.Imaging.ImageFormat]::Png)\""))
            ;; 转换格式, 从 c:/Users/Jack/Desktop/attact/test/clipboard.png 之类
            ;; 转换成 ./attach/test/clipboard.png
            (setq image-file (replace-regexp-in-string
                              "^[^:]+:/.*\\(/attach/.*\\)" ".\\1" image-file))
            (when (eq major-mode 'org-mode)
              ;; 输入文件所在位置文本
              (insert (concat "[[file:" image-file "]]"))
              ;; 显示图片
              (org-display-inline-images))
            (when (eq major-mode 'markdown-mode)
              (insert (concat "![图片]" "(" image-file ")"))
              (markdown-display-inline-images))
            )
        (error "Unsupported OS"))
      ))

仅支持 Windows 系统插入, 任何文件类型都可以, 只需修改 (insert (concat “[[file:” image-file “]]” )) 为合适的就可以