Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Last active August 29, 2015 14:19
Show Gist options
  • Save barneycarroll/502955e9955868eae756 to your computer and use it in GitHub Desktop.
Save barneycarroll/502955e9955868eae756 to your computer and use it in GitHub Desktop.
Convert Markdown strings to Mithril-compatible virtual DOM. Uses @evilstreak's Markdown parser, which renders JSONML. https://github.com/evilstreak/markdown-js and type-of-is for logic: https://github.com/stephenhandley/type-of-is
import type from 'type-of-is'
export default function parse( input ){
if( type( input, String ) ){
return input
}
else if( type( input[ 0 ], Array ) ){
return input.map( parse )
}
return input.reduce( ( node, item, index ) => {
switch ( type( item ) ){
case String :
if( index === 0 ){
node.tag = item
}
else {
node.children.push( item )
}
break
case Array :
node.children.push( parse( item ) )
break
case Object :
node.attrs = item
}
return node
}, {
attrs : {},
children : []
} )
}
import md from 'markdown'
const API = md.markdown
export default input => API.toHTMLTree( input )
import jsonml2m from './jsonml2mithril'
import md2jsonml from './markdown2jsonml'
export default markdown => jsonml2m( md2jsonml( markdown ) )
{
"main": "markdown2mithril.js",
"scripts": {
"build": "browserify markdown2mithril.js -t babelify"
},
"devDependencies": {
"babelify": "^6.0.1",
"browserify": "^9.0.7"
},
"dependencies" : {
"markdown": "^0.5.0",
"mithril": "^0.1.34",
"type-of-is": "^3.4.0"
},
"browserify": {
"transform": [
"babelify"
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment