Giter VIP home page Giter VIP logo

commonmark-hiccup's Introduction

commonmark-hiccup

Build Status

A small Clojure library for converting CommonMark markdown to HTML. It is designed to make the HTML output as configurable as possible, relying on Hiccup as an intermediary representation.

Installation

Add the following dependency to project.clj:

[commonmark-hiccup "0.1.0"]

Documentation

commonmark-hiccup uses the commonmark-java parser and implements its own renderer, which transforms the CommonMark AST to Hiccup-compatible Clojure data structures. It then uses Hiccup to render them to their final HTML representation.

commonmark-hiccup is built for configurability, not performance. I use it to render static content for different web sites, each with different requirements for how paragraphs, code blocks etc. should look like.

Usage

You can convert a markdown string to HTML using markdown->html:

user=> (require '[commonmark-hiccup.core :refer [markdown->html]])
nil
user=> (markdown->html "This is a *test*.")
"<p>This is a <em>test</em>.</p>"

You can pass a configuration to the converter to tweak the output. This example renders paragraphs without the surrounding <p></p> tags:

user=> (let [config (update-in commonmark-hiccup.core/default-config
                               [:renderer :nodes org.commonmark.node.Paragraph]
                               (constantly :content))]
         (markdown->html config "This is a *test*."))
"This is a <em>test</em>."

Configuration

The default configuration defines the Hiccup snippets to which the different CommonMark AST nodes are rendered:

(def default-config
  {:renderer {:nodes {org.commonmark.node.Document          :content
                      org.commonmark.node.Heading           ['(:h :node-level) :content]
                      org.commonmark.node.Paragraph         [:p :content]
                      org.commonmark.node.Text              :node-literal
                      org.commonmark.node.BulletList        [:ul :content]
                      org.commonmark.node.OrderedList       [:ol {:start :node-startNumber} :content]
                      org.commonmark.node.ListItem          [:li :content]
                      org.commonmark.node.BlockQuote        [:blockquote :content]
                      org.commonmark.node.HtmlBlock         :node-literal
                      org.commonmark.node.HtmlInline        :node-literal
                      org.commonmark.node.FencedCodeBlock   [:pre [:code {:class :node-info} :node-literal]]
                      org.commonmark.node.IndentedCodeBlock [:pre [:code :node-literal]]
                      org.commonmark.node.Code              [:code :node-literal]
                      org.commonmark.node.Link              [:a {:href :node-destination} :content]
                      org.commonmark.node.Image             [:img {:src   :node-destination
                                                                   :alt   :text-content
                                                                   :title :node-title}]
                      org.commonmark.node.Emphasis          [:em :content]
                      org.commonmark.node.StrongEmphasis    [:strong :content]
                      org.commonmark.node.ThematicBreak     [:hr]
                      org.commonmark.node.SoftLineBreak     " "
                      org.commonmark.node.HardLineBreak     [:br]}}})

The :nodes map uses commonmark-java node classes as keys. The values are just Clojure data structures. Some keywords and lists are replaced during rendering:

  • All keywords prefixed with :node- are replaced with the respective property of the rendered node (e.g. :node-literal for org.commonmark.node.HtmlBlock is replaced with the value returned by HtmlBlock::getLiteral).
  • Some keywords are special: :content is replaced with the rendered content of the current node's children; :text-content is replaced with the concatenated content of all org.commonmark.node.Text child nodes.
  • List elements are joined to strings. This is useful for rendering node properties as part of a longer string. ['(:h :node-level) :content] uses the level property of the Heading node to render the appropriate HTML tag (e.g. <h1></h1> for level 1 headings).

For the available properties for each node type, refer to the commonmark-java sources.

License

Copyright © 2017 Axel Schüssler

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

commonmark-hiccup's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.