(发布)Copy as org-mode chrome: 就是 Firfox 上 Copy as org-mode 的 Chrome 版本

见:

欢迎反馈~

6 个赞

如果复制带有图片和文字,粘贴到emacs中图片就变成连接了。有办法可以把图片也一起粘贴吗?

我用org-capture-html插件,也想有这个功能。

目前折衷办法是:

快速在emacs里瞥一眼图片,如果决定下载到本地再快速下载 以下两个函数凑合着用

(defun preview-image-url-in-split-window ()
  "Preview an image URL in a split window using `eww`, without closing the current buffer."
  (interactive)
  (let ((url (thing-at-point 'url t)))
    (when (and url (string-match-p "\\(\\.png\\|\\.jpg\\|\\.jpeg\\|\\.webp\\)" url))
      ;; 创建一个新的窗口并在其中使用 eww 打开 URL
      (let ((new-window (split-window-right))) ;; 默认右侧拆分窗口
        (select-window new-window)
        (eww url)))))
(global-set-key (kbd "C-c z") 'preview-image-url-in-split-window)

(defun my-org-download-yank()
  "Prompt the user for a FILENAME and save the screenshot to it."
  (interactive)
  (org-copy-link-url)
  (evil-open-below 1)
  (org-download-yank)
  (dpsk/org-download-rename-last-file))

(defun dpsk/org-download-rename-last-file ()
  "Rename the last downloaded file saved in your computer."
  (interactive)
  (let* ((dir-path (org-download--dir))
         (basename (file-name-base org-download-path-last-file))
         (ext (file-name-extension org-download-path-last-file t))
         (newname (read-string "Rename last file to: " basename))
         ;; 去除新文件名末尾的点(当存在扩展名时)
         (newname (if (and ext (not (string-empty-p ext)))
                      (replace-regexp-in-string "\\.$" "" newname)
                    newname))
         (newpath (concat dir-path "/" newname ext)))
    (when org-download-path-last-file
      (rename-file org-download-path-last-file newpath 1)
      (org-download-replace-all
       (file-name-nondirectory org-download-path-last-file)
       (concat newname ext))
      (setq org-download-path-last-file newpath)
      (org-download--display-inline-images))))
(global-set-key (kbd "C-c y") 'my-org-download-yank)

1 个赞

用 org-download 的话,可以把图片下载下来。

也有考虑添加这个功能,等闲一点弄弄。

1 个赞

上午折腾了一下,发现要折腾的力度有点大,暂时麻烦你用一下 3 楼的解决办法

由于在 Chrome 插件中实现下载图片的特性有些麻烦,所以改造一下 @hongfei6 的代码,添加了一个批量下载图片的小功能,基于 org-download

(defun my/preview-org-image ()
  "Preview org link image in a split window on the right."
  (interactive)
  (let* ((element (org-element-context))
         (type (org-element-type element))
         (link (org-element-property :raw-link element)))
    (when (and (eq type 'link) link)
      (let ((right-window (or (window-in-direction 'right)
                             (split-window-right))))
        (select-window right-window)
        (eww link)))))
(define-key org-mode-map (kbd "C-c z") 'my/preview-org-image)

(defun my/org-download-no-comment (_link)
  "Annotate without the DOWNLOADED comment."
  "")

(setq org-download-annotate-function #'my/org-download-no-comment)

(defun my/org-download-smart ()
  "Smart download function that decides action based on cursor position."
  (interactive)
  (let* ((element (org-element-context))
         (type (org-element-type element)))
    (cond
     ((eq type 'link)
      (message "Cursor is on a link, downloading single image...")
      (let* ((link (org-element-property :raw-link element))
             (begin (org-element-property :begin element))
             (end (org-element-property :end element)))
        (save-excursion
          (goto-char begin)
          (delete-region begin end)
          (org-download-image link))))
     (t
      (message "Cursor not on link, checking all images...")
      (let* ((tree (org-element-parse-buffer))
             (links (org-element-map tree 'link
                     (lambda (link)
                       (when (string-match-p "\\(\\.png\\|\\.jpg\\|\\.jpeg\\|\\.webp\\|wx_fmt=png\\)"
                                          (org-element-property :raw-link link))
                         ;; 返回链接及其位置信息
                         (list (org-element-property :raw-link link)
                               (org-element-property :begin link)
                               (org-element-property :end link))))))
             (total (length links)))
        (if (= total 0)
            (message "No image links found")
          (when (y-or-n-p (format "Found %d image links. Download them? " total))
            ;; 从后往前处理,这样位置信息不会失效
            (dolist (link-info (reverse links))
              (let ((link (nth 0 link-info))
                    (begin (nth 1 link-info))
                    (end (nth 2 link-info)))
                (save-excursion
                  (goto-char begin)
                  (delete-region begin end)
                  (org-download-image link)))))))))))

(define-key org-mode-map (kbd "C-c y") 'my/org-download-smart)

@improve100 你可以把以上代码添加到配置文件中,我已经在用了,效果还可以。

1 个赞