Automatic line break in js SyntaxHighlighter

前端 未结 2 1624
失恋的感觉
失恋的感觉 2020-12-31 05:38

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

2条回答
  •  失恋的感觉
    2020-12-31 06:00

    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()});
    

提交回复
热议问题