Skip to content

Instantly share code, notes, and snippets.

@cuth
Last active April 3, 2021 12:09
Show Gist options
  • Save cuth/9367966 to your computer and use it in GitHub Desktop.
Save cuth/9367966 to your computer and use it in GitHub Desktop.
This is a simple bookmarklet to wrap code in a textarea with the correct highlight.js elements.

highlight.js bookmarklet

This is a bookmarklet to wrap code in a textarea with the appropriate highlight.js elements. Simply select the code in a text field then activate the bookmarklet. There will be a prompt to enter the language. This will only work on sites that have highlight.js installed.

(function (d) {
    var a = d.activeElement,
        t = a.value,
        s = a.selectionStart,
        e = a.selectionEnd,
        l = prompt('Input language');
    a.value = t.slice(0, s) + '<pre><code class="' + l.toLowerCase().replace(/\s*/g,'').replace('#','s') + '">' + t.slice(s, e) + '</code></pre>' + t.slice(e);
}(document));

Save the "URL" below as bookmark.

javascript:(function(d){var a=d.activeElement,t=a.value,s=a.selectionStart,e=a.selectionEnd,l=prompt('Input language');a.value=t.slice(0,s)+'<pre><code class="'+l.toLowerCase().replace(/\s*/g,'').replace('#','s')+'">'+t.slice(s,e)+'</code></pre>'+t.slice(e);}(document));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment