Skip to content

Simple library for converting markdown into hiccup data structures for Clojure/Script

Notifications You must be signed in to change notification settings

mpcarolin/markdown-to-hiccup

Repository files navigation

Clojars Project PRs Welcome

markdown-to-hiccup

Simple library for converting markdown strings or files into Clojure/Script compatible hiccup data structures

Installation

Leiningen/Boot

[markdown-to-hiccup "0.6.2"]

Clojure CLI/deps.edn

markdown-to-hiccup {:mvn/version "0.6.2"}

Gradle

compile 'markdown-to-hiccup:markdown-to-hiccup:0.6.2'

Usage

Clojure and ClojureScript

Use md->hiccup to convert a markdown string to hiccup:

(ns example.core
	(:require [markdown-to-hiccup.core :as m]))

(m/md->hiccup "#Title")

=> [:html {} [:head {}] [:body {} [:h1 {} "Title"]]]

If you just want the hiccup without the html, head, and body tags, pass it through the component function:

(->> "#Title\n* Bullet"
     (m/md->hiccup)
     (m/component))

=> [:div {} [:h1 {} "Title"] [:ul {} [:li {} "Bullet"]]]

If you want a specific nested hiccup vector, use hiccup-in. It lets you specify the keywords (and indices for sibling hiccup elements) to extract nested hiccup:

(let [hiccup (m/md->hiccup "#Title\n#SecondTitle")]
	(hiccup-in hiccup :html :body :h1 0)) ;; note the integer paired with :h1
=> [:h1 {} "Title"]

vs.

(let [hiccup (m/md->hiccup "#Title\n#SecondTitle")]
	(m/hiccup-in hiccup :html :body :h1 1)) ;; note the integer paired with :h1
=> [:h1 {} "SecondTitle"]

If you need to escape html characters (& " > <), pass a map to md->hiccup with the :encode? keyword:

(-> "Ben & Jerry's"
     (m/md->hiccup {:encode? true})
     (m/component))

=> [:div {} [:p {} "Ben &amp; Jerry's"]]

Clojure-only

Finally, for just Clojure, there is also a function for automatically reading in a markdown file from disk and outputting hiccup:

(m/file->hiccup file-path-to-markdown-file)

=> [:html {} [:head {}] [:body {} [:h1 {} "Title"]]]

License

Copyright © 2019 Michael Carolin

Distributed under the MIT License

About

Simple library for converting markdown into hiccup data structures for Clojure/Script

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published