Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download remote images and cache them #378

Merged
merged 1 commit into from
Dec 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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