Skip to content

Instantly share code, notes, and snippets.

@mrmcc3
Last active December 21, 2020 04:58
Show Gist options
  • Save mrmcc3/9beda43069a188bd9a7646aa2fa1005e to your computer and use it in GitHub Desktop.
Save mrmcc3/9beda43069a188bd9a7646aa2fa1005e to your computer and use it in GitHub Desktop.
CommonMark from Clojure + EDN front matter https://mrmcc3.github.io/blog/posts/commonmark-in-clojure/
(ns demo
(:import
(org.commonmark.parser Parser)
(org.commonmark.renderer.html HtmlRenderer)))
(def parser (.build (Parser/builder)))
(def renderer (.build (HtmlRenderer/builder)))
(defn -main [input]
(->> (slurp input)
(.parse parser)
(.render renderer)
(println)))
(ns demo2
(:require
[clojure.edn :as edn]
[clojure.pprint :refer [pprint]])
(:import
(org.commonmark.parser Parser)
(org.commonmark.node FencedCodeBlock)
(org.commonmark.renderer.html HtmlRenderer)))
(defn extract-meta! [doc]
(let [node (.getFirstChild doc)]
(when (and (instance? FencedCodeBlock node)
(= (.getInfo node) "clojure"))
(.unlink node)
(edn/read-string (.getLiteral node)))))
(def parser (.build (Parser/builder)))
(def renderer (.build (HtmlRenderer/builder)))
(defn -main [input]
(let [doc (.parse parser (slurp input))
meta (extract-meta! doc)
html (.render renderer doc)]
(pprint (assoc meta :html html))))
{:paths ["."]
:deps {org.clojure/clojure {:mvn/version "1.9.0"}
com.atlassian.commonmark/commonmark {:mvn/version "0.11.0"}}}
{:title "EDN Frontmatter Example"
 :published #inst "2018-03-08"}

This is the edn frontmatter example

@mrmcc3
Copy link
Author

mrmcc3 commented Mar 10, 2018

Runnable examples from https://mrmcc3.github.io/blog/posts/commonmark-in-clojure/

Basic markdown processing

clj -Sdeps '{:deps {mrmcc3/gist-9beda43069a188bd9a7646aa2fa1005e {:git/url "https://gist.github.com/mrmcc3/9beda43069a188bd9a7646aa2fa1005e" :sha "c7b61caa2bbbb551865e113f499347b373cbbdbd"}}}' -m demo https://gist.githubusercontent.com/mrmcc3/9beda43069a188bd9a7646aa2fa1005e/raw/c7b61caa2bbbb551865e113f499347b373cbbdbd/edn.md

EDN front matter trick

clj -Sdeps '{:deps {mrmcc3/gist-9beda43069a188bd9a7646aa2fa1005e {:git/url "https://gist.github.com/mrmcc3/9beda43069a188bd9a7646aa2fa1005e" :sha "c7b61caa2bbbb551865e113f499347b373cbbdbd"}}}' -m demo2 https://gist.githubusercontent.com/mrmcc3/9beda43069a188bd9a7646aa2fa1005e/raw/c7b61caa2bbbb551865e113f499347b373cbbdbd/edn.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment