Skip to content

Commit 55e6e6c

Browse files
torfjeldennicandro
authored andcommitted
added jupyter-org--add-image-ext-maybe, allowing resolving file
extension from MIME even if custom filename has been provided
1 parent 5657a73 commit 55e6e6c

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

jupyter-org-client.el

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,18 @@ EXT is used as the extension."
862862

863863
(defvar org-image-actual-width)
864864

865+
(defun jupyter-org--add-image-ext-maybe (file &optional ext)
866+
"Return a file name for FILE with EXT.
867+
If FILE is an image file with a specified extension, return FILE.
868+
If FILE is an image file without a specified extension and EXT is
869+
non-nil, return FILE with EXT as the extension. Otherwise, return
870+
FILE."
871+
(cond
872+
((null file) file)
873+
((image-type-from-file-name file) file)
874+
((not (null ext)) (concat file "." ext))
875+
(t file)))
876+
865877
(defun jupyter-org--image-result (mime content params &optional b64-encoded)
866878
"Return an org-element suitable for inserting an image.
867879
MIME is the image mimetype, CONTENT is a property list
@@ -886,14 +898,15 @@ org-element will have an ATTR_ORG affiliated keyword containing
886898
the width or height of the image. When there is no :width or
887899
:height, an ATTR_ORG keyword containing the true size of the
888900
image may still be added, see `jupyter-org-adjust-image-size'."
889-
(let* ((overwrite (not (null (alist-get :file params))))
890-
(file (or (alist-get :file params)
901+
(let* ((file-ext (cl-case mime
902+
(:image/png "png")
903+
(:image/jpeg "jpg")
904+
(:image/svg+xml "svg")))
905+
(overwrite (not (null (alist-get :file params))))
906+
(file (or (jupyter-org--add-image-ext-maybe (alist-get :file params) file-ext)
891907
(jupyter-org-image-file-name
892908
(plist-get content :data)
893-
(cl-case mime
894-
(:image/png "png")
895-
(:image/jpeg "jpg")
896-
(:image/svg+xml "svg"))))))
909+
file-ext))))
897910
(when (or overwrite (not (file-exists-p file)))
898911
(let ((buffer-file-coding-system
899912
(if b64-encoded 'binary

0 commit comments

Comments
 (0)