Skip to content

Instantly share code, notes, and snippets.

@tylerjl
Created October 29, 2023 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylerjl/a58d451a48ae822fc59890f5fb6eb2b6 to your computer and use it in GitHub Desktop.
Save tylerjl/a58d451a48ae822fc59890f5fb6eb2b6 to your computer and use it in GitHub Desktop.
impatient-mode example
(use-package impatient-mode
:general
(:states 'normal
:keymaps 'markdown-mode-map
",p" 'tjl/md-preview-mode)
:commands impatient-mode)
(define-minor-mode tjl/md-preview-mode
"Toggles live markdown preview using impatient-mode"
:init-value nil
:lighter " 󰽛"
(if tjl/md-preview-mode
(progn
(if (process-status "httpd")
(setq tjl/httpd-was-running t)
(progn (httpd-start)))
(impatient-mode)
(imp-set-user-filter 'tjl/markdown-filter)
(imp-visit-buffer)
(message "Live markdown preview enabled"))
(progn
(impatient-mode -1)
(unless (or (imp--buffer-list)
(and (fboundp 'tjl/httpd-was-running) tjl/httpd-was-running))
;; Tear httpd down
(progn (httpd-stop)
(setq tjl/httpd-was-running nil)))
(message "Live markdown preview disabled"))))
;; For use with impatient-mode filter
(defun tjl/markdown-filter (buffer)
(princ
(with-temp-buffer
(let ((tmp (buffer-name)))
(set-buffer buffer)
(set-buffer (markdown tmp))
(format "<!DOCTYPE html><html><title>Markdown preview</title><link rel=\"stylesheet\" href = \"https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/3.0.1/github-markdown.min.css\"/>
<body><article class=\"markdown-body\" style=\"box-sizing: border-box;min-width: 200px;max-width: 980px;margin: 0 auto;padding: 45px;\">%s</article></body></html>" (buffer-string))))
(current-buffer)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment