From 99b7b7f74021a728b1892a2094371de8ad67d239 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Wed, 17 Oct 2018 16:17:36 -0400 Subject: [PATCH] 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...]] --- modules/lang/org/+attach.el | 22 ++++++++++++++++++++++ modules/lang/org/autoload/org.el | 6 ++++-- modules/lang/org/packages.el | 3 ++- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/modules/lang/org/+attach.el b/modules/lang/org/+attach.el index eee6c0223..1eeee2618 100644 --- a/modules/lang/org/+attach.el +++ b/modules/lang/org/+attach.el @@ -63,6 +63,28 @@ (advice-add #'org-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 diff --git a/modules/lang/org/autoload/org.el b/modules/lang/org/autoload/org.el index 4a8138c69..0931c6b26 100644 --- a/modules/lang/org/autoload/org.el +++ b/modules/lang/org/autoload/org.el @@ -119,8 +119,10 @@ If on a: (org-toggle-latex-fragment)) (`link - (let ((path (org-element-property :path (org-element-lineage context '(link) t)))) - (if (and path (image-type-from-file-name path)) + (let* ((lineage (org-element-lineage context '(link) t)) + (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-open-at-point)))) diff --git a/modules/lang/org/packages.el b/modules/lang/org/packages.el index c3d5abedd..bb784e0eb 100644 --- a/modules/lang/org/packages.el +++ b/modules/lang/org/packages.el @@ -15,7 +15,8 @@ (package! org-pdfview)) (when (featurep! +attach) - (package! org-download)) + (package! org-download) + (package! org-yt :recipe (:fetcher github :repo "TobiasZawada/org-yt"))) (when (featurep! +babel) (package! ob-mongo)