Skip to content

Commit

Permalink
Add option to display images stored online
Browse files Browse the repository at this point in the history
If non-nil, this option will download images from a remote server (if
the URL is given with a white-listed protocol; by default only
allowing HTTPS) and display them during the execution of
`markdown-display-inline-images'.

Absolute process-specific caching is employed to avoid
double-downloading images.  No `nice' means is yet given for the
invalidation of this cache.
  • Loading branch information
vermiculus authored and jrblevin committed Dec 29, 2018
1 parent 906e97d commit da1c825
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
table insertion. ([GH-369][])
- Add `markdown-kill-outline` and `markdown-kill-block`
functions.
- Added options for viewing remote images. ([GH-378][])

* Improvements:

Expand Down
68 changes: 50 additions & 18 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -8449,6 +8449,33 @@ or \\[markdown-toggle-inline-images]."
(mapc #'delete-overlay markdown-inline-image-overlays)
(setq markdown-inline-image-overlays nil))

(defcustom markdown-display-remote-images nil
"If non-nil, download and display remote images.
See also `markdown-inline-image-overlays'.

Only image URLs specified with a protocol listed in
`markdown-remote-image-protocols' are displayed."
:group 'markdown
:type 'boolean)

(defcustom markdown-remote-image-protocols '("https")
"List of protocols to use to download remote images.
See also `markdown-display-remote-images'."
:group 'markdown
:type '(repeat string))

(defvar markdown--remote-image-cache
(make-hash-table :test 'equal)
"A map from URLs to image paths.")

(defun markdown--get-remote-image (url)
"Retrieve the image path for a given URL."
(or (gethash url markdown--remote-image-cache)
(let ((dl-path (make-temp-file "markdown-mode--image")))
(require 'url)
(url-copy-file url dl-path t)
(puthash url dl-path markdown--remote-image-cache))))

(defun markdown-display-inline-images ()
"Add inline image overlays to image links in the buffer.
This can be toggled with `markdown-toggle-inline-images'
Expand All @@ -8466,24 +8493,29 @@ or \\[markdown-toggle-inline-images]."
(end (match-end 0))
(file (match-string-no-properties 6)))
(when (and imagep
(not (zerop (length file)))
(file-exists-p file))
(let* ((abspath (if (file-name-absolute-p file)
file
(concat default-directory file)))
(image
(if (and markdown-max-image-size
(image-type-available-p 'imagemagick))
(create-image
abspath 'imagemagick nil
:max-width (car markdown-max-image-size)
:max-height (cdr markdown-max-image-size))
(create-image abspath))))
(when image
(let ((ov (make-overlay start end)))
(overlay-put ov 'display image)
(overlay-put ov 'face 'default)
(push ov markdown-inline-image-overlays))))))))))
(not (zerop (length file))))
(unless (file-exists-p file)
(when (and markdown-display-remote-images
(member (downcase (url-type (url-generic-parse-url file)))
markdown-remote-image-protocols))
(setq file (markdown--get-remote-image file))))
(when (file-exists-p file)
(let* ((abspath (if (file-name-absolute-p file)
file
(concat default-directory file)))
(image
(if (and markdown-max-image-size
(image-type-available-p 'imagemagick))
(create-image
abspath 'imagemagick nil
:max-width (car markdown-max-image-size)
:max-height (cdr markdown-max-image-size))
(create-image abspath))))
(when image
(let ((ov (make-overlay start end)))
(overlay-put ov 'display image)
(overlay-put ov 'face 'default)
(push ov markdown-inline-image-overlays)))))))))))

(defun markdown-toggle-inline-images ()
"Toggle inline image overlays in the buffer."
Expand Down

0 comments on commit da1c825

Please sign in to comment.