Skip to content

Instantly share code, notes, and snippets.

@CodeVachon
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CodeVachon/c7e93683257e76711021 to your computer and use it in GitHub Desktop.
Save CodeVachon/c7e93683257e76711021 to your computer and use it in GitHub Desktop.
Showdown Gist
//
// Main Angular Application
//
angular.module('MyApp', ['markdown'])
.config(function(markdownProvider) {
markdownProvider.config({
extensions: ['table','gist']
});
});
//
// Gist Extension (WIP)
// ^^gist:[id] -> <script src="https://gist.github.com/[id].js"></script>
//
(function(){
var gist = function(converter) {
return [
{
type : 'lang',
regex : '\\B(\\\\)?\\^{2}gist\\:([a-zA-Z0-9\\/]+)\\b',
replace : function (a,b,c) {
if (typeof module !== 'undefined') {
//
// If module exists, then assume this is node, and render just the regular embed
//
return b === "\\" ? a : '<scri'+'pt src="https://gist.github.com/'+c+'.js"></scr'+'ipt>';
} else {
//
// This is client side, to avoid the `document.write` problem, render an iframe with the
// gist content inside.
//
if (b === "\\") {
return a;
} else {
var gistId = c;
var _iframeId = gistId.replace(/\W/g,"");
var _script = "";
_script += "var iframe = document.getElementById('"+_iframeId+"');\n";
_script += "var iframeHtml = '<html><head><base target=\"_parent\"><style>body {margin: 0; padding: 0;}</style></head><body onload=\"parent.document.getElementById(\\\'" + _iframeId + "\\\').style.height=document.body.scrollHeight + \\'px\\'\"><scr' + 'ipt type=\"text/javascript\" src=\"https://gist.github.com/" + gistId + ".js\"></sc'+'ript></body></html>';\n";
_script += "var doc = iframe.document;";
_script += "if (iframe.contentDocument) doc = iframe.contentDocument;\n";
_script += "else if (iframe.contentWindow) doc = iframe.contentWindow.document;\n";
_script += "doc.open();doc.writeln(iframeHtml);doc.close();";
return "<script>"+_script+"</script><iframe id='"+_iframeId+"' frameborder='0' width='100%'></iframe>";
}
}
}
}
];
};
// Client-side export
if (typeof window !== 'undefined' && window.Showdown && window.Showdown.extensions) {
window.Showdown.extensions.gist = gist;
}
// Server-side export
if (typeof module !== 'undefined') {
module.exports = gist;
}
}());
//
// Angular Markdown filter to work with Showdown
//
angular.module('markdown', [])
.provider('markdown', function () {
var opts = {};
return {
config: function (newOpts) {
opts = newOpts;
},
$get: function () {
return new Showdown.converter(opts);
}
};
})
.filter('markdown', ['$sce', 'markdown', function ($sce, markdown) {
return function (text) {
if(text == null) text = '';
var html = markdown.makeHtml(text);
return $sce.trustAsHtml(html);
};
}]);
@CodeVachon
Copy link
Author

Works as both an Express Module, and as an Angular Filter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment