How can I ignore leading and trailing linebreaks within an html “code” block using css, javascript or jquery?

前端 未结 6 928
伪装坚强ぢ
伪装坚强ぢ 2021-02-19 18:59

In my HTML source code, I have a code block like the following (I use showdown and highlight.js on this page):


double myNum         


        
6条回答
  •  我寻月下人不归
    2021-02-19 19:56

    Demo: http://jsfiddle.net/WjVVs/4/.

    Tested with Chrome and FF on the PC. It does not work in IE 9 when the plugin is applied to a code element (it appears to work fine when applied to a pre element). I can't find a suitable workaround, but feel free to comment/update.

    This is a modified version from another answer. This plugin attempts to remove extra indentation caused by the natural flow of the document. I've modified it to be smarter about leading whitespace.

    If it works correctly, you should see something like:

    enter image description here

    Usage

    $("code").prettyPre(); // any selector here

    HTML with leading whitespace and extra indentation

    
               double myNumber = (double)4;
    
               // another line
    
               // another line
    
                    // this is purposely indented further
                    for( var i = 0; i < 100; i++ ){
    
                    }            
    
            

    Plugin

    (function( $ ) {
        $.fn.prettyPre = function( method ) {
    
            var defaults = {
                ignoreExpression: /\s/ // what should be ignored?
            };
    
            var methods = {
                init: function( options ) {
                    this.each( function() {
                        var context = $.extend( {}, defaults, options );
                        var $obj = $( this );
                        var usingInnerText = true;
                        var text = $obj.get( 0 ).innerText;
    
                        // some browsers support innerText...some don't...some ONLY work with innerText.
                        if ( typeof text == "undefined" ) {
                            text = $obj.html();
                            usingInnerText = false;
                        }
    
                        // use the first line as a baseline for how many unwanted leading whitespace characters are present
                        var superfluousSpaceCount = 0;
                        var pos = 0;
                        var currentChar = text.substring( 0, 1 );
    
                        while ( context.ignoreExpression.test( currentChar ) ) {
                            if(currentChar !== "\n"){
                                superfluousSpaceCount++;
                            }else{
                                superfluousSpaceCount = 0;
                            }
    
                            currentChar = text.substring( ++pos, pos + 1 );
                        }
    
                        // split
                        var parts = text.split( "\n" );
                        var reformattedText = "";
    
                        // reconstruct
                        var length = parts.length;
                        for ( var i = 0; i < length; i++ ) {
    
                            // remove leading whitespace (represented by an empty string)
                            if(i === 0 && parts[0]=== ""){
                                continue;   
                            }
    
                            // cleanup, and don't append a trailing newline if we are on the last line
                            reformattedText += parts[i].substring( superfluousSpaceCount ) + ( i == length - 1 ? "" : "\n" );
                        }
    
                        // modify original
                        if ( usingInnerText ) {
                            $obj.get( 0 ).innerText = reformattedText;
                        }
                        else {
                            // This does not appear to execute code in any browser but the onus is on the developer to not 
                            // put raw input from a user anywhere on a page, even if it doesn't execute!
                            $obj.html( reformattedText );
                        }
                    } );
                }
            }
    
            if ( methods[method] ) {
                return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ) );
            }
            else if ( typeof method === "object" || !method ) {
                return methods.init.apply( this, arguments );
            }
            else {
                $.error( "Method " + method + " does not exist on jQuery.prettyPre." );
            }
        }
    } )( jQuery );
    

提交回复
热议问题