Search code examples
javascriptsyntaxhighlight

highlight syntax in pre tags with highlight.js


there is a light powerful javascript code for syntax highlight here:

http://softwaremaniacs.org/soft/highlight/en/

but this code just work with <pre><code></code></pre> blocks, which is my problem, i like to use just <pre></pre> because Chrome and Safari don't include line breaks for <pre><code></code></pre> when copy and past the code, but for <pre></pre> it's work.

there's a user guide too, but honestly i can't understand what must i do to highlight just pre tags. user guide is here:

http://softwaremaniacs.org/soft/highlight/en/description/


Solution

  • The current version of chrome has no problems with line breaks in <code> tags.
    E.g. try this example in chrome.

    Here a version without jQuery.

    Here an example without the <code> tag.

    window.onload = function() {
        var aCodes = document.getElementsByTagName('pre');
        for (var i=0; i < aCodes.length; i++) {
            hljs.highlightBlock(aCodes[i]);
        }
    };