最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Automatic line break in js SyntaxHighlighter - Stack Overflow

programmeradmin3浏览0评论

Im using the js SyntaxHighlighter 3.0.83 from /

I've been googling the entire world now it seem but cant really find how to enable line breaks. Instad i get a horizontal scrollbar, which is good sometimes but not in my scenario.

In example

Anyone out there who know the way around this?

Im using the js SyntaxHighlighter 3.0.83 from http://alexgorbatchev.com/SyntaxHighlighter/

I've been googling the entire world now it seem but cant really find how to enable line breaks. Instad i get a horizontal scrollbar, which is good sometimes but not in my scenario.

In example

Anyone out there who know the way around this?

Share Improve this question edited May 6, 2012 at 16:17 Abel 57.1k25 gold badges157 silver badges259 bronze badges asked Jun 9, 2011 at 0:13 Eric HerlitzEric Herlitz 26.3k28 gold badges116 silver badges163 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 10

I don't actually use SyntaxHighlight, but it seems to be possible to attach an white-space: pre-wrap CSS style to the <pre> or <script> tag that SyntaxHighlight uses.

HTML (using <pre>):

<pre class="brush: js" class="syntaxhighlight">
    /**
     * SyntaxHighlighter
     */
    function foo()
    {
        if (counter <= 10)
            return;
        // it works!
    }
</pre>

HTML (using <script>):

<script type="syntaxhighlighter" class="syntaxhighlight brush: js"><![CDATA[
    /**
     * SyntaxHighlighter
     */
    function foo()
    {
        if (counter <= 10)
            return;
        // it works!
    }
]]></script>

CSS:

.syntaxhighlight {
    white-space: pre-wrap;
}

The wrap is no longer an option but you can add the functionality easily.

Add this to the css to break the lines

body .syntaxhighlighter .line {
        white-space: pre-wrap !important; /* make code wrap */
}

To fix the line numbering use the script below.

function lineWrap(){
    var wrap = function () {
        var elems = document.getElementsByClassName('syntaxhighlighter');
        for (var j = 0; j < elems.length; ++j) {
            var sh = elems[j];
            var gLines = sh.getElementsByClassName('gutter')[0].getElementsByClassName('line');
            var cLines = sh.getElementsByClassName('code')[0].getElementsByClassName('line');
            var stand = 15;
            for (var i = 0; i < gLines.length; ++i) {
                var h = $(cLines[i]).height();
                if (h != stand) {
                    console.log(i);
                    gLines[i].setAttribute('style', 'height: ' + h + 'px !important;');
                }
            }
        }
     };
    var whenReady = function () {
        if ($('.syntaxhighlighter').length === 0) {
            setTimeout(whenReady, 800);
        } else {
            wrap();
        }
    };
    whenReady();
};
lineWrap();
$(window).resize(function(){lineWrap()});
发布评论

评论列表(0)

  1. 暂无评论