Skip to content

Instantly share code, notes, and snippets.

@nielsnuebel
Last active August 29, 2017 14:26
Show Gist options
  • Save nielsnuebel/fecc7a7a9bf556f79d801c502a3a3932 to your computer and use it in GitHub Desktop.
Save nielsnuebel/fecc7a7a9bf556f79d801c502a3a3932 to your computer and use it in GitHub Desktop.
with this script, it's possible to use fragment in highlight.js
// Custom reveal.js integration
(function(){
if( typeof window.addEventListener === 'function' ) {
var hljs_nodes = document.querySelectorAll( 'pre code' );
for( var i = 0, len = hljs_nodes.length; i < len; i++ ) {
var element = hljs_nodes[i];
// Now escape html unless prevented by author
if( ! element.hasAttribute( 'data-fragment' )) {
var regexp = /&lt;mark class="fragment/g;
while ((match = regexp.exec(element.innerHTML)) != null) {
var replace = '<span' + element.innerHTML.substring(match.index+8, match.index + element.innerHTML.substring(match.index).indexOf('&gt;')) + '>';
replace = replace + element.innerHTML.substring(match.index + element.innerHTML.substring(match.index).indexOf('&gt;')+4, match.index + element.innerHTML.substring(match.index).indexOf('&lt;/mark&gt;')) + '</span>';
var search = element.innerHTML.substring(match.index, match.index + element.innerHTML.substring(match.index).indexOf('&lt;/mark&gt;')+13);
element.innerHTML = element.innerHTML.replace(search, replace);
}
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment