From a9f3d013a7f82814fce7e67a699dd00410f9a0e1 Mon Sep 17 00:00:00 2001 From: Firehose Bot Date: Fri, 26 Jun 2026 20:50:37 +0200 Subject: [PATCH] experimental file that shows images in markdown --- init.el | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 init.el diff --git a/init.el b/init.el new file mode 100644 index 0000000..1345e9f --- /dev/null +++ b/init.el @@ -0,0 +1,72 @@ +; This file is GPL licensed, same as Emacs + + +; callback to rewrite image pat so emacs can show images inside blog post drafts. +(defun firehose/markdown-translate-image-path (path) + "Translate /media/... image links to ../media/... relative to buffer." + (message (concat "markdown translate image path called on " path)) + + (if (string-prefix-p "/images" path) + (concat "../../../static" path))) + + +; test +(message (firehose/markdown-translate-image-path "/images/bla")) + +; monkey patch function to fix our filename +(defun markdown-display-inline-images () + "Add inline image overlays to image links in the buffer. +This can be toggled with `markdown-toggle-inline-images' +or \\[markdown-toggle-inline-images]." + (interactive) + (message "monkeypatched function called") + (unless (display-images-p) + (error "Cannot show images")) + (save-excursion + (save-restriction + (widen) + (goto-char (point-min)) + (while (re-search-forward markdown-regex-link-inline nil t) + (let* ((start (match-beginning 0)) + (imagep (match-beginning 1)) + (end (match-end 0)) + (file (funcall markdown-translate-filename-function (match-string-no-properties 6)))) + (when (and imagep + (not (zerop (length file)))) + (unless (file-exists-p file) + (let* ((download-file (funcall markdown-translate-filename-function file)) + (valid-url (ignore-errors + (member (downcase (url-type (url-generic-parse-url download-file))) + markdown-remote-image-protocols)))) + (if (and markdown-display-remote-images valid-url) + (setq file (markdown--get-remote-image download-file)) + (when (not valid-url) + ;; strip query parameter + (setq file (replace-regexp-in-string "?.+\\'" "" file)) + (unless (file-exists-p file) + (setq file (url-unhex-string file))))))) + (when (file-exists-p file) + (let* ((abspath (if (file-name-absolute-p file) + file + (concat default-directory file))) + (image + (cond ((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))) + (markdown-max-image-size + (create-image abspath nil nil + :max-width (car markdown-max-image-size) + :max-height (cdr markdown-max-image-size))) + (t (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))))))))))) + +; register hook +(setq markdown-translate-filename-function + #'firehose/markdown-translate-image-path)