Last active
August 29, 2015 14:19
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 : [] | |
} ) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import md from 'markdown' | |
const API = md.markdown | |
export default input => API.toHTMLTree( input ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jsonml2m from './jsonml2mithril' | |
import md2jsonml from './markdown2jsonml' | |
export default markdown => jsonml2m( md2jsonml( markdown ) ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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