Last active
August 29, 2015 14:19
-
-
Save CodeVachon/c7e93683257e76711021 to your computer and use it in GitHub Desktop.
Showdown Gist
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 Angular Application | |
// | |
angular.module('MyApp', ['markdown']) | |
.config(function(markdownProvider) { | |
markdownProvider.config({ | |
extensions: ['table','gist'] | |
}); | |
}); |
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
// | |
// 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; | |
} | |
}()); |
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
// | |
// 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); | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works as both an Express Module, and as an Angular Filter