/flexmark-clj

A (very) thin wrapper around flexmark-java, in Clojure.

Primary LanguageHTMLThe UnlicenseUnlicense

flexmark-clj

Turn # Hello, world! into a Hiccup-esque Clojure data structure [:document {} [:heading {:level 1} "Hello, world!"]] or into HTML. Uses flexmark-java under the hood to parse and render.

Usage

Warning
Pre-alpha Library

The public facing API can (and will) change. Do not use for serious projects.

There are no releases yet. Depend on the git coordinates directly.

deps.edn
{:deps
 {com.github.hindol/flexmark-clj
  {:git/url "https://github.com/hindol/flexmark-clj"
   :sha "79d00514254a086bcd6d174b41c6d5b49790579c"}}} ;; (1)
  1. The latest commit hash from master branch.

Quickstart

The API consists of three functions.

(defn markdown->tree
  "Parses a Markdown string into a tree of Flexmark nodes."
  [markdown]
  ...)
(defn tree->marccup
  "Walks the tree of Flexmark nodes, returns a Hiccup-esque Clojure array."
  [parsed]
  ...)
(defn tree->html
  "Takes in a tree of Flexmark nodes, returns the rendered HTML in a string."
  [parsed]
  ...)
main.clj
(with-open [reader (io/reader "./resources/API-Methods.md")   ;; (1)
            writer (io/writer "./resources/API-Methods.html") ;; (2)
            edn    (io/writer "./resources/API-Methods.edn")] ;; (3)
  (let [markdown (->> reader line-seq (str/join "\n"))
        tree     (markdown->tree markdown)
        marccup  (tree->marccup tree)                         ;; (4)
        html     (tree->html tree)]
    (pp/pprint marccup edn)
    (.write writer html)))
  1. See: API-Methods.md

  2. See: API-Methods.html

  3. See: API-Methods.edn

  4. Marccup = Hiccup-esque Markdown

Anatomy of a Marccup Node

A node is a vector of three things,

[
    :tag                                        ;; (1)
    {:attribute-1 value :attribute-2 value ...} ;; (2)
    [child-1 child-2 ...]                       ;; (3)
]
  1. A tag representing the type of node,

  2. A map of its attributes, and,

  3. A vector of child nodes.