JSFiddle - React, Tailwind, and code Playground
by Sillvva
HTML
Check out console log
JavaScript
// 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;
}
var code1 = `
<div class="row">
<div class="col-sm-4 menu hidden-xs"><div class="affix-container affix-top" style="width: 360px;"><h1>Test heading</h1></div></div>
</div>
`;
var code2 = `
<div class="row"><div>XYZ 14<p>Hello how ya doin </p></div>
<div class="affix-container affix-top" style="width: 360px;">
<h1>Test heading</h1> </div>
</div>
...