feat: add markdown drag-and-drop for blog images
- Fix org DND flag: +download-dnd → +dragndrop (was silently no-op) - New qwan-markdown-dnd.el: handles file drops in markdown buffers copies images to configurable attachment dir (default: attachments/) inserts markdown image links at point per-repo config via .dir-locals.el (qwan-markdown-attachment-dir) - AGENTS.md: document the new module and DND setup
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
;;; qwan/qwan-markdown-dnd.el -*- lexical-binding: t; -*-
|
||||
|
||||
;;; Commentary:
|
||||
|
||||
;; Adds drag-and-drop support for markdown buffers.
|
||||
;; When you drag an image from Finder (or other file manager) into a markdown
|
||||
;; buffer, it is copied to `qwan-markdown-attachment-dir' and a markdown image
|
||||
;; link is inserted at point.
|
||||
;;
|
||||
;; Configure per-repo by setting `qwan-markdown-attachment-dir' in
|
||||
;; .dir-locals.el. Example:
|
||||
;;
|
||||
;; ((markdown-mode . ((qwan-markdown-attachment-dir . "assets/images"))))
|
||||
;;
|
||||
;; Requires Emacs' built-in dnd mechanism (no extra packages).
|
||||
|
||||
;;; Code:
|
||||
|
||||
(provide 'qwan-markdown-dnd)
|
||||
|
||||
(defvar-local qwan-markdown-attachment-dir "attachments"
|
||||
"Directory for dropped attachments, relative to the markdown file.
|
||||
Set this buffer-locally (e.g. via .dir-locals.el) to customise per project.")
|
||||
|
||||
|
||||
;;;###autoload
|
||||
(defun qwan-markdown-dnd-handler (uri action)
|
||||
"Handle dropping a file URI into a markdown buffer.
|
||||
|
||||
Copies the file referenced by URI into `qwan-markdown-attachment-dir'
|
||||
and inserts a markdown link at point. Images get an `![]()' link,
|
||||
other files a regular `[]()' link.
|
||||
|
||||
ACTION is ignored (always copies)."
|
||||
(ignore action)
|
||||
(let* ((filename (dnd-get-local-file-name uri 'must-exist))
|
||||
(mime-type (when (fboundp 'mailcap-file-name-to-mime-type)
|
||||
(mailcap-file-name-to-mime-type filename)))
|
||||
(is-image (and mime-type (string-prefix-p "image/" mime-type))))
|
||||
(unless filename
|
||||
(error "Cannot find local file for %s" uri))
|
||||
(let* ((buffer-dir (or (when (buffer-file-name)
|
||||
(file-name-directory (buffer-file-name)))
|
||||
default-directory))
|
||||
(dest-dir (expand-file-name qwan-markdown-attachment-dir buffer-dir))
|
||||
(dest-name (file-name-nondirectory filename))
|
||||
(dest-file (expand-file-name dest-name dest-dir)))
|
||||
;; Create destination directory if needed
|
||||
(unless (file-directory-p dest-dir)
|
||||
(make-directory dest-dir t))
|
||||
;; Handle name conflicts by appending a timestamp
|
||||
(when (file-exists-p dest-file)
|
||||
(setq dest-file
|
||||
(expand-file-name
|
||||
(concat (file-name-base dest-name)
|
||||
"_" (format-time-string "%Y%m%d_%H%M%S")
|
||||
"." (file-name-extension dest-name))
|
||||
dest-dir)))
|
||||
;; Copy the file
|
||||
(copy-file filename dest-file)
|
||||
;; Insert the appropriate markdown link
|
||||
(let ((relative-path (file-relative-name dest-file buffer-dir)))
|
||||
(if is-image
|
||||
(insert (format "" "" relative-path))
|
||||
(insert (format "[%s](%s)" dest-name relative-path))))
|
||||
;; Signal that we've handled the drop
|
||||
'private)))
|
||||
|
||||
|
||||
;;;###autoload
|
||||
(defun qwan-markdown-setup-dnd ()
|
||||
"Register DND handler for the current markdown buffer.
|
||||
Add this to `markdown-mode-hook'."
|
||||
(setq-local dnd-protocol-alist
|
||||
(append
|
||||
(list (cons "^\\(?:https?\\|ftp\\|file\\|nfs\\):"
|
||||
#'qwan-markdown-dnd-handler))
|
||||
dnd-protocol-alist)))
|
||||
|
||||
|
||||
;;;###autoload
|
||||
(add-hook 'markdown-mode-hook #'qwan-markdown-setup-dnd)
|
||||
(add-hook 'gfm-mode-hook #'qwan-markdown-setup-dnd)
|
||||
|
||||
;;; qwan-markdown-dnd.el ends here
|
||||
Reference in New Issue
Block a user