Skip to content

Instantly share code, notes, and snippets.

@MKRhere
Created May 2, 2018 10:21
Show Gist options
  • Save MKRhere/2b1117e988d774081254abf76333f7b9 to your computer and use it in GitHub Desktop.
Save MKRhere/2b1117e988d774081254abf76333f7b9 to your computer and use it in GitHub Desktop.
HTML Prettifier
// Original: https://jsfiddle.net/buksy/rxucg1gd/
// Parameters:
// code - (string) code you wish to format
// stripWhiteSpaces - (boolean) do you wish to remove multiple whitespaces coming after each other?
// stripEmptyLines - (boolean) do you wish to remove empty lines?
var formatCode = function(code, stripWhiteSpaces, stripEmptyLines) {
"use strict";
var whitespace = ' '.repeat(4); // Default indenting 4 whitespaces
var currentIndent = 0;
var char = null;
var nextChar = null;
var result = '';
for(var pos=0; pos <= code.length; pos++) {
char = code.substr(pos, 1);
nextChar = code.substr(pos+1, 1);
// If opening tag, add newline character and indention
if(char === '<' && nextChar !== '/') {
result += '\n' + whitespace.repeat(currentIndent);
currentIndent++;
}
// if Closing tag, add newline and indention
else if(char === '<' && nextChar === '/') {
// If there're more closing tags than opening
if(--currentIndent < 0) currentIndent = 0;
result += '\n' + whitespace.repeat(currentIndent);
}
// remove multiple whitespaces
else if(stripWhiteSpaces === true && char === ' ' && nextChar === ' ') char = '';
// remove empty lines
else if(stripEmptyLines === true && char === '\n' ) {
//debugger;
if(code.substr(pos, code.substr(pos).indexOf("<")).trim() === '' ) char = '';
}
result += char;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment