Skip to content

Instantly share code, notes, and snippets.

@yosemsweet
Created March 13, 2014 00:08
Show Gist options
  • Save yosemsweet/9519360 to your computer and use it in GitHub Desktop.
Save yosemsweet/9519360 to your computer and use it in GitHub Desktop.
Left align contents of a <pre><code> tag.
$('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