Skip to content

Commit da1c825

Browse files
vermiculusjrblevin
authored andcommitted
Add option to display images stored online
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.
1 parent 906e97d commit da1c825

File tree

2 files changed

+51
-18
lines changed

2 files changed

+51
-18
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
table insertion. ([GH-369][])
5858
- Add `markdown-kill-outline` and `markdown-kill-block`
5959
functions.
60+
- Added options for viewing remote images. ([GH-378][])
6061

6162
* Improvements:
6263

markdown-mode.el

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8449,6 +8449,33 @@ or \\[markdown-toggle-inline-images]."
84498449
(mapc #'delete-overlay markdown-inline-image-overlays)
84508450
(setq markdown-inline-image-overlays nil))
84518451

8452+
(defcustom markdown-display-remote-images nil
8453+
"If non-nil, download and display remote images.
8454+
See also `markdown-inline-image-overlays'.
8455+
8456+
Only image URLs specified with a protocol listed in
8457+
`markdown-remote-image-protocols' are displayed."
8458+
:group 'markdown
8459+
:type 'boolean)
8460+
8461+
(defcustom markdown-remote-image-protocols '("https")
8462+
"List of protocols to use to download remote images.
8463+
See also `markdown-display-remote-images'."
8464+
:group 'markdown
8465+
:type '(repeat string))
8466+
8467+
(defvar markdown--remote-image-cache
8468+
(make-hash-table :test 'equal)
8469+
"A map from URLs to image paths.")
8470+
8471+
(defun markdown--get-remote-image (url)
8472+
"Retrieve the image path for a given URL."
8473+
(or (gethash url markdown--remote-image-cache)
8474+
(let ((dl-path (make-temp-file "markdown-mode--image")))
8475+
(require 'url)
8476+
(url-copy-file url dl-path t)
8477+
(puthash url dl-path markdown--remote-image-cache))))
8478+
84528479
(defun markdown-display-inline-images ()
84538480
"Add inline image overlays to image links in the buffer.
84548481
This can be toggled with `markdown-toggle-inline-images'
@@ -8466,24 +8493,29 @@ or \\[markdown-toggle-inline-images]."
84668493
(end (match-end 0))
84678494
(file (match-string-no-properties 6)))
84688495
(when (and imagep
8469-
(not (zerop (length file)))
8470-
(file-exists-p file))
8471-
(let* ((abspath (if (file-name-absolute-p file)
8472-
file
8473-
(concat default-directory file)))
8474-
(image
8475-
(if (and markdown-max-image-size
8476-
(image-type-available-p 'imagemagick))
8477-
(create-image
8478-
abspath 'imagemagick nil
8479-
:max-width (car markdown-max-image-size)
8480-
:max-height (cdr markdown-max-image-size))
8481-
(create-image abspath))))
8482-
(when image
8483-
(let ((ov (make-overlay start end)))
8484-
(overlay-put ov 'display image)
8485-
(overlay-put ov 'face 'default)
8486-
(push ov markdown-inline-image-overlays))))))))))
8496+
(not (zerop (length file))))
8497+
(unless (file-exists-p file)
8498+
(when (and markdown-display-remote-images
8499+
(member (downcase (url-type (url-generic-parse-url file)))
8500+
markdown-remote-image-protocols))
8501+
(setq file (markdown--get-remote-image file))))
8502+
(when (file-exists-p file)
8503+
(let* ((abspath (if (file-name-absolute-p file)
8504+
file
8505+
(concat default-directory file)))
8506+
(image
8507+
(if (and markdown-max-image-size
8508+
(image-type-available-p 'imagemagick))
8509+
(create-image
8510+
abspath 'imagemagick nil
8511+
:max-width (car markdown-max-image-size)
8512+
:max-height (cdr markdown-max-image-size))
8513+
(create-image abspath))))
8514+
(when image
8515+
(let ((ov (make-overlay start end)))
8516+
(overlay-put ov 'display image)
8517+
(overlay-put ov 'face 'default)
8518+
(push ov markdown-inline-image-overlays)))))))))))
84878519

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

0 commit comments

Comments
 (0)