diff --git a/modules/lang/markdown/autoload.el b/modules/lang/markdown/autoload.el index f1c2c3904..7e1d2b23a 100644 --- a/modules/lang/markdown/autoload.el +++ b/modules/lang/markdown/autoload.el @@ -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"))) diff --git a/modules/lang/markdown/config.el b/modules/lang/markdown/config.el index 09148e0d3..2cf556b6c 100644 --- a/modules/lang/markdown/config.el +++ b/modules/lang/markdown/config.el @@ -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)