lang/org: add inline base64/http(s) image support

Adds inline image preview support for http/https links, as well as
base64 encode image support with the img protocol. e.g.

Examples:
[[http://domain.com/some/image.png]]
[[https://raw.githubusercontent.com/hlissner/doom-emacs/screenshots/company.png]]
[[img:iVBORw0KGgoAAAANSUhEUgAAACUAAAAuCAAAAACKMo8cAA...]]
This commit is contained in:
Henrik Lissner 2018-10-17 16:17:36 -04:00
parent fda4734164
commit 99b7b7f740
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
3 changed files with 28 additions and 3 deletions

View file

@ -63,6 +63,28 @@
(advice-add #'org-download--fullname (advice-add #'org-download--fullname
:filter-return #'+org-attach*download-fullname)) :filter-return #'+org-attach*download-fullname))
(def-package! org-yt
:after org
:config
(defun +org-inline-data-image (_protocol link _description)
"Interpret LINK as base64-encoded image data."
(base64-decode-string link))
(defun +org-image-link (protocol link _description)
"Interpret LINK as base64-encoded image data."
(when (image-type-from-file-name link)
(let ((buf (url-retrieve-synchronously (concat protocol ":" link))))
;; TODO Better error handling
(cl-assert buf nil "Download of image \"%s\" failed." link)
(with-current-buffer buf
(goto-char (point-min))
(re-search-forward "\r?\n\r?\n")
(buffer-substring-no-properties (point) (point-max))))))
(org-link-set-parameters "http" :image-data-fun #'+org-image-link)
(org-link-set-parameters "https" :image-data-fun #'+org-image-link)
(org-link-set-parameters "img" :image-data-fun #'+org-inline-data-image))
;; ;;
;; Bootstrap ;; Bootstrap

View file

@ -119,8 +119,10 @@ If on a:
(org-toggle-latex-fragment)) (org-toggle-latex-fragment))
(`link (`link
(let ((path (org-element-property :path (org-element-lineage context '(link) t)))) (let* ((lineage (org-element-lineage context '(link) t))
(if (and path (image-type-from-file-name path)) (path (org-element-property :path lineage)))
(if (or (equal (org-element-property :type lineage) "img")
(and path (image-type-from-file-name path)))
(+org/refresh-inline-images) (+org/refresh-inline-images)
(org-open-at-point)))) (org-open-at-point))))

View file

@ -15,7 +15,8 @@
(package! org-pdfview)) (package! org-pdfview))
(when (featurep! +attach) (when (featurep! +attach)
(package! org-download)) (package! org-download)
(package! org-yt :recipe (:fetcher github :repo "TobiasZawada/org-yt")))
(when (featurep! +babel) (when (featurep! +babel)
(package! ob-mongo) (package! ob-mongo)