Skip to content

Instantly share code, notes, and snippets.

@aaronsnoswell
Last active April 8, 2018 03:45
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 aaronsnoswell/33c474ae70811fdbc4adb63a4db1b1e8 to your computer and use it in GitHub Desktop.
Save aaronsnoswell/33c474ae70811fdbc4adb63a4db1b1e8 to your computer and use it in GitHub Desktop.
Print GitHub Markdown Document
/* The below code is designed to be injected into any GitHub page with a
* markdown document, e.g. from a bookmarklet
*/
// Build list of parent and child elements (don't hide these)
var mdel = document.querySelector("article.markdown-body");
var el = mdel;
var els = [];
while (el) {
els.unshift(el);
el = el.parentNode;
}
function allDescendants (node) {
for (var i = 0; i < node.childNodes.length; i++) {
var child = node.childNodes[i];
allDescendants(child);
els.unshift(child);
}
}
allDescendants(mdel);
// Hide all elements except ones in the keep list
all = document.querySelectorAll("body *");
for (var i=0; i < all.length; i++) {
if(els.indexOf(all[i]) == -1) {
all[i].setAttribute("style", "display:none;");
}
}
// Touch up the markdown element a little
mdel.setAttribute("style", "border:none; padding:0px;");
// Load print dialog
window.print();
/**
* Minified for convenience:
var mdel=document.querySelector("article.markdown-body");var el=mdel;var els=[];while(el){els.unshift(el);el=el.parentNode}function allDescendants(node){for(var i=0;i<node.childNodes.length;i+=1){var child=node.childNodes[i];allDescendants(child);els.unshift(child)}}allDescendants(mdel);all=document.querySelectorAll("body *");for(var i=0;i<all.length;i+=1){if(els.indexOf(all[i])==-1){all[i].setAttribute("style","display:none;")}}mdel.setAttribute("style","border:none; padding:0px;");window.print();
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment