Created
August 25, 2014 22:57
-
-
Save ricardoalcocer/1acb94c3b53cd8ffa4a5 to your computer and use it in GitHub Desktop.
A quick example on how to use showdown.js to add blocks of Markdown to any website
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>Markdown-to-HTML Sample</title> | |
<link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"/> | |
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/showdown/0.3.1/showdown.min.js"></script> | |
</head> | |
<body style="margin: 10px"> | |
<div data-markdown> | |
#Hello | |
sdf | |
sdf asdf asdf sdf | |
var x=1; | |
entry.forEach(function(item){ | |
x=x++; | |
}) | |
## sdfsdfdf | |
</div> | |
<hr/> | |
<h2>This is regular HTML</h2> | |
<hr/> | |
<div data-markdown> | |
#Hello | |
sdf | |
sdf asdf asdf sdf | |
var x=1; | |
entry.forEach(function(item){ | |
x=x++; | |
}) | |
## sdfsdfdf | |
</div> | |
<script type="text/javascript"> | |
window.onload=function(){ | |
// this function is the reverse version of escapeHTML found at | |
// https://github.com/evilstreak/markdown-js/blob/master/src/render_tree.js | |
function unescapeHTML( text ) { | |
return text.replace( /&/g, "&" ) | |
.replace( /</g, "<" ) | |
.replace( />/g, ">" ) | |
.replace( /"/g, "\"" ) | |
.replace( /'/g, "'" ); | |
} | |
// based on https://gist.github.com/paulirish/1343518 | |
(function(){ | |
[].forEach.call( document.querySelectorAll('[data-markdown]'), function fn(elem){ | |
elem.innerHTML = (new Showdown.converter()).makeHtml(unescapeHTML(elem.innerHTML)); | |
}); | |
}()); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment