Skip to content

Instantly share code, notes, and snippets.

@perrupa
Last active August 29, 2015 14:23
Show Gist options
  • Save perrupa/d50d11551acfffa52b3c to your computer and use it in GitHub Desktop.
Save perrupa/d50d11551acfffa52b3c to your computer and use it in GitHub Desktop.
Automatically generate code blocks
/*
Find all code wrapped in .exampleCode classes and generate
a highlighted code block to insert in the DOM after it
*/
$(".exampleCode").each(function(){
var $example = $(this),
code = $example.html(),
$preBlock = $("<pre></pre>").append( $("<code></code>").text(code) );
// Insert code block *after* the example div
$example.after( $preBlock );
// Find inserted <pre/> in the DOM..
var codeBlock = $example.next().get(0);
// ..Highlight the code block with hljs (https://highlightjs.org/)
hljs.highlightBlock( codeBlock, null );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment