Function: markdown-ts--fontify-image

markdown-ts--fontify-image is a byte-compiled function defined in markdown-ts-mode.el.gz.

Signature

(markdown-ts--fontify-image NODE OVERRIDE START END &rest _)

Documentation

Show an inline image at NODE.

When markdown-ts-inline-images is non-nil, display the image. If the image link is the only content on its line, display the image below the link. If the image is inline within a paragraph, display it right after the link text. Remote images are controlled by markdown-ts-display-remote-inline-images.

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/markdown-ts-mode.el.gz
(defun markdown-ts--fontify-image (node _override _start _end &rest _)
  "Show an inline image at NODE.
When `markdown-ts-inline-images' is non-nil, display the image.
If the image link is the only content on its line, display the
image below the link.  If the image is inline within a paragraph,
display it right after the link text.
Remote images are controlled by
`markdown-ts-display-remote-inline-images'."
  (let* ((node-start (treesit-node-start node))
         (node-end (treesit-node-end node))
         (eol (save-excursion (goto-char node-end)
                              (line-end-position)))
         (search-end (min (1+ eol) (point-max))))
    ;; Always clear stale overlays for this node's range.
    (dolist (ov (overlays-in node-start search-end))
      (when (and (overlay-get ov 'markdown-ts-image)
                 (>= (overlay-start ov) node-start)
                 (<= (overlay-end ov) search-end))
        (delete-overlay ov)))
    (when (and markdown-ts-inline-images
               (display-images-p)
               ;; Don't create image overlays for nodes inside
               ;; folded (outline-invisible) headings, since the
               ;; images wouldn't be visible and could interfere
               ;; with the folded display.
               (not (markdown-ts--outline-invisible-p node-start)))
      (let* ((dest (treesit-search-subtree node "\\`link_destination\\'"))
             (url (and dest (treesit-node-text dest t)))
             (remotep (and url (string-match-p "\\`https?://" url)))
             (displayable
              (when url
                (if remotep
                    (when (eq markdown-ts-display-remote-inline-images
                              'download)
                      (ignore-errors
                        (require 'url-handlers)
                        (with-work-buffer
                          (set-buffer-multibyte nil)
                          (url-insert-file-contents url)
                          (buffer-string))))
                  (let ((file (expand-file-name url)))
                    (and (not (file-remote-p file))
                         (file-exists-p file)
                         (image-supported-file-p file)
                         file)))))
             (max-w (and displayable
                         (if (eq markdown-ts-image-max-width 'window)
                             (window-body-width nil t)
                           markdown-ts-image-max-width)))
             (img (and max-w
                       (create-image displayable nil remotep
                                     :max-width max-w
                                     :scale 1))))
        (when img
          (let* ((alone (markdown-ts--image-alone-on-line-p node))
                 (str (if alone
                          (concat "\n" (propertize " " 'display img))
                        (propertize " " 'display img)))
                 (ov (make-overlay (1- node-end) node-end nil t nil)))
            (overlay-put ov 'markdown-ts-image t)
            (overlay-put ov 'after-string str)
            (overlay-put ov 'evaporate t)))))))