JSFiddle - React, Tailwind, and code Playground

HTML to plaintext

by tonytlwu

HTML

<textarea id="debug"></textarea>
<div id="email" style="display:none;">
		<table cellpadding="0" cellspacing="0" border="0">
			<tr>
				<td>
					<h2>Paragraphs</h2>
					<p class="normal-space">At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. <a href="www.github.com">Github</a>
					</p>
					<p class="normal-space">At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
					</p>
				</td>
				<td></td>
			</tr>
			<tr>
				<td>
					<hr/>
					<h2>Pretty printed table</h2>
					<table id="invoice">
						<thead>
							<tr>
								<th>Article</th>
								<th>Price</th>
								<th>Taxes</th>
								<th>Amount</th>
								<th>Total</th>
							</tr>
						</thead>
						<tbody>
							<tr>
								<td>
									<p>
										Product 1<br />
										<span style="font-size:0.8em">Contains: 1x Product 1</span>
									</p>
								</td>
								<td align="right" valign="top">6,99&euro;</td>
								<td align="right" valign="top">7%</td>
								<td align="right" valign="top">1</td>
								<td align="right" valign="top">6,99€</td>
							</tr>
							<tr>
								<td>Shipment costs</td>
								<td align="right">3,25€</td>
								<td align="right">7%</td>
								<td align="right">1</td>
								<td...

CSS

textarea {
  width: 100%;
  height: 600px;
}

JavaScript

var inlineTags = ['b', 'big', 'i', 'small', 'tt', 'abbr', 'acronym',
  'cite', 'code', 'dfn', 'em', 'kbd', 'strong', 'samp',
  'var', 'a', 'bdo', 'br', 'img', 'map', 'object', 'q',
  'script', 'span', 'sub', 'sup', 'button', 'input', 'label',
  'select', 'textarea'];

function getPlainText (node) {
  let isStartOfLine = true;
  let text = '';

  function appendText (childNodes) {
    for (let index = 0; index < childNodes.length; index++) {
      let node = childNodes[index];

      if (node.nodeType === 3) {
        // text node
        text += node.textContent;
        isStartOfLine = false;
        continue;
      }

      if (node.tagName === 'BR') {
        text += '\n';
        isStartOfLine = true;
        continue;
      }

      if (inlineTags.indexOf(node.tagName) > -1) {
        appendText(node.childNodes);
        continue;
      }

      if (!isStartOfLine) {
        text += '\n';
        isStartOfLine = true;
      }

      appendText(node.childNodes);

      if (!isStartOfLine) {
        text += '\n';
        isStartOfLine = true;
      }
    }
  }

  appendText(node.childNodes);

  if (text.length && text.charAt(text.length - 1) === '\n')
    text = text.substring(0, text.length - 1);

  return text;
}

var plaintext = getPlainText(document.getElementById('email'));
document.getElementById('debug').innerHTML = plaintext;
console.log();