aboutsummaryrefslogtreecommitdiff
path: root/modules/markdown-config.el
blob: 4faa4474ec04c061d2a38e02ef2479d64b2c18c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
;;; markdown-config --- Settings for Editing Markdown -*- lexical-binding: t; coding: utf-8; -*-
;; author Craig Jennings <c@cjennings.net>

;;; Commentary:
;;
;; Layer: 3 (Domain Workflow).
;; Category: D/P.
;; Load shape: eager.
;; Eager reason: none; markdown editing, a mode-loaded deferral candidate.
;; Top-level side effects: package configuration via use-package; org-src lang
;;   mapping registered after-load.
;; Runtime requires: none (configures packages via use-package).
;; Direct test load: yes.
;;
;;; Code:

;;;; ------------------------- Markdown-Mode -------------------------

(use-package markdown-mode
  :mode (("README\\.md\\'" . gfm-mode)
		 ("\\.md\\'"       . markdown-mode)
         ("\\.markdown\\'" . markdown-mode))
  :bind (:map markdown-mode-map
			  ("<f2>" . markdown-preview)) ;; use same key as compile for consistency
  :init (setq markdown-command "multimarkdown"))

;; Register markdown as a known org-src-block language so `org-lint'
;; stops warning on `#+begin_src markdown ... #+end_src' and `C-c ''
;; inside such a block opens it in `markdown-mode' instead of falling
;; back to fundamental-mode.
(with-eval-after-load 'org
  (add-to-list 'org-src-lang-modes '("markdown" . markdown)))

;;;; ------------------------- Impatient-Mode ------------------------

;; allows for live previews of your html
;; see: https://github.com/skeeto/impatient-mode
(use-package impatient-mode
  :defer t
  :config
  (setq imp-set-user-filter 'markdown-html))

;;;; --------------------- WIP: Markdown-Preview ---------------------

(defun cj/markdown-preview-server-start ()
  "Start the simple-httpd listener that serves the live markdown preview.
Idempotent: re-running while the server is already up is a no-op."
  (interactive)
  (require 'simple-httpd)
  (httpd-start)
  (message "markdown preview server running on http://localhost:8080/imp"))

;; the filter to apply to markdown before impatient-mode pushes it to the server
(defun markdown-preview ()
  "Open the current buffer as a live HTML preview at http://localhost:8080/imp.
The simple-httpd listener must already be running -- see
`cj/markdown-preview-server-start'.  Starting a network listener as a
side effect of opening a preview is surprising, so the server start
lives in a separate command."
  (interactive)
  (unless (and (boundp 'httpd-process) httpd-process)
    (user-error "markdown preview server not running; run `M-x cj/markdown-preview-server-start' first"))
  (impatient-mode 1)
  (setq imp-user-filter #'cj/markdown-html)
  (cl-incf imp-last-state)
  (imp--notify-clients)
  ;; Use plain `browse-url' (not `browse-url-generic') so the user's
  ;; default protocol handler picks the browser, and use `http://' --
  ;; simple-httpd serves plaintext; the previous `https://' URL caused
  ;; a TLS handshake against a non-TLS listener and the preview never
  ;; rendered.
  (browse-url "http://localhost:8080/imp"))

(defun cj/markdown-html (buffer)
  (princ (with-current-buffer buffer
		   (format "<!DOCTYPE html><html><title>Impatient Markdown</title><xmp theme=\"united\" style=\"display:none;\"> %s  </xmp><script src=\"http://ndossougbe.github.io/strapdown/dist/strapdown.js\"></script></html>"
				   (buffer-substring-no-properties (point-min) (point-max))))
		 (current-buffer)))

(provide 'markdown-config)
;;; markdown-config.el ends here