Created
March 13, 2014 00:08
-
-
Save yosemsweet/9519360 to your computer and use it in GitHub Desktop.
Left align contents of a <pre><code> tag.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('pre > code').each(function(index) { | |
var _this = $(this); | |
var text = _this.text(); | |
lines = text.split(/\r\n|[\n\v\f\r\x85\u2028\u2029]/); | |
indent = 0; | |
var firstCharFinder = /^\s*\S/i; | |
var leftTrimLength = 0; | |
var contentStarted = false; | |
adjusted_lines = $.map(lines, function(line, i) { | |
if(!contentStarted) { | |
var firstChar = firstCharFinder.exec(line) | |
if(firstChar) { | |
contentStarted = true; | |
leftTrimLength = firstChar[0].length - 1; | |
return line.substring(leftTrimLength); | |
} else { | |
return ""; //empty string for line with no content | |
} | |
} else { | |
return line.substring(leftTrimLength); | |
} | |
}); | |
_this.text(adjusted_lines.join("\n")); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment