title: Javascript: How to correct indentation in HTML string?
body: <p>Given a string like this:</p>
  
  <pre><code>' \n &lt;div id="a"&gt;\n &lt;span class="b"&gt;\n&lt;span&gt;Hello&lt;/span&gt;\n\n\n&lt;/span&gt; &lt;input type="text"&gt;\n  \n&lt;/div&gt;\n '
  </code></pre>
  
  <p>I'd like to format it like this:</p>
  
  <pre><code>&lt;div id="a"&gt;
    &lt;span class="b"&gt;
      &lt;span&gt;Hello&lt;/span&gt;
    &lt;/span&gt;
    &lt;input type="text"&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>i.e. the result should be: (assume 2 spaces for indentation)</p>
  
  <pre><code>'&lt;div id="a"&gt;\n  &lt;span class="b"&gt;\n    &lt;span&gt;\n      Hello\n    &lt;/span&gt;\n  &lt;/span&gt;\n  &lt;input type="text"&gt;\n&lt;/div&gt;'
  </code></pre>
  
  <p>What's the most elegant way to achieve this? Are there any known libraries that do that?</p>
  
  <p><strong>Note:</strong></p>
  
  <ul>
  <li>I'm not looking for HTML syntax highlighting, just indentation correction</li>
  <li>I'm not looking to support the whole HTML spec, correcting basic HTML like the example above would suffice</li>
  </ul>