lang/markdown: auto-resolve build & open programs

Markdown-mode will now auto-detect a markdown compiler when you use
markdown-preview (SPC m b). It will try marked, pandoc and markdown, in
that order.

As for markdown-open, it will now use "open" on MacOS and "xdg-open" on
Linux, by default.
This commit is contained in:
Henrik Lissner 2019-05-18 00:53:39 -04:00
parent 2e6d8be6fc
commit 30319d1c3e
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 62 additions and 1 deletions

View file

@ -40,3 +40,46 @@ Return nil if on a link url, markup, html, or references."
for face in faces
if (memq face unsafe-faces)
return t)))))
;;;###autoload
(defun +markdown-compile (beg end output-buffer)
"Compile markdown into html.
Runs `+markdown-compile-functions' until the first function to return non-nil,
otherwise throws an error."
(or (run-hook-with-args-until-success '+markdown-compile-functions
beg end output-buffer)
(user-error "No markdown program could be found. Install marked, pandoc or markdown.")))
;;;###autoload
(defun +markdown-compile-marked (beg end output-buffer)
"Compiles markdown with the marked program, if available.
Returns its exit code."
(when (executable-find "marked")
(apply #'call-process-region
beg end
shell-file-name nil output-buffer nil
shell-command-switch
"marked"
(when (eq major-mode 'gfm-mode)
(list "--gfm" "--tables" "--breaks")))))
;;;###autoload
(defun +markdown-compile-pandoc (beg end output-buffer)
"Compiles markdown with the pandoc program, if available.
Returns its exit code."
(when (executable-find "pandoc")
(call-process-region beg end
shell-file-name nil output-buffer nil
shell-command-switch
"pandoc" "--smart" "-f" "markdown" "-t" "html")))
;;;###autoload
(defun +markdown-compile-markdown (beg end output-buffer)
"Compiles markdown using the markdown program, if available.
Returns its exit code."
(when (executable-find "markdown")
(call-process-region beg end
shell-file-name nil output-buffer nil
shell-command-switch
"markdown")))

View file

@ -1,5 +1,19 @@
;;; lang/markdown/config.el -*- lexical-binding: t; -*-
(defvar +markdown-compile-functions
'(+markdown-compile-marked
+markdown-compile-pandoc
+markdown-compile-markdown)
"A list of commands to try when attempting to build a markdown file with
`markdown-open' or `markdown-preview', stopping at the first one to return non-nil.
Each function takes three argument. The beginning position of the region to
capture, the end position, and the output buffer.")
;;
;;; Packages
(def-package! markdown-mode
:mode ("/README\\(?:\\.\\(?:markdown\\|md\\)\\)?\\'" . gfm-mode)
:init
@ -11,7 +25,11 @@
markdown-fontify-code-blocks-natively t
markdown-hide-urls nil ; trigger with `markdown-toggle-url-hiding'
markdown-enable-math t ; syntax highlighting for latex fragments
markdown-gfm-uppercase-checkbox t) ; for compat with org-mode
markdown-gfm-uppercase-checkbox t ; for compat with org-mode
markdown-command #'+markdown-compile
markdown-open-command
(cond (IS-MAC "open")
(IS-LINUX "xdg-open")))
:config
(set-flyspell-predicate! '(markdown-mode gfm-mode)