Convert HTML to Plain Text While Preserving Block Boundaries
Practical guide to converting HTML to plain text without merging headings, paragraphs, list items, or table cells. Includes ordered steps, a before-and-after example, and result checks.
What converting HTML to plain text does
Converting HTML to plain text removes markup such as tags, attributes, comments, scripts, and styling so that the remaining content can be used in places that do not support HTML. The operation works on markup that represents visible document structure, such as paragraphs, headings, lists, and tables. The important difference from simply deleting every tag is that block boundaries must be preserved. Without that extra handling, a heading and the following paragraph can run together as one line, list items can become a single string, and table cells can lose their column separation.
In this guide, the result is plain text with block-level elements still recognizable as separate units. Inline markup, such as bold, italic, code, and links, is removed without adding visible decoration unless a conversion rule explicitly maps it to something else. The guide also treats scripts and styles as non-content by default because their contents are not normally part of the visible text of a document.
How to produce and check the intended result
Ordered steps
- 1. Decide which block elements must become separate text units. The minimum set is usually paragraph, heading, list item, table row, and table cell, with other block containers handled as fallback boundaries.
- 2. Remove content that is not visible text, including script and style elements. Do this before other processing so that JavaScript or CSS does not leak into the output.
- 3. Insert a newline at each block boundary. Use the newline convention of the target environment, typically a single line feed, and apply it once where the block changes so that lists and multiline containers do not gain extra blank lines.
- 4. Strip the remaining tags. Decode character references such as &, <, and " back into their plain-text characters.
- 5. Normalize whitespace per block. Collapse runs of spaces and tabs to one space at the edges of the block, but preserve the space where inline elements were removed when the surrounding text would otherwise join.
Before-and-after example
The following example illustrates the expected behavior. The HTML uses a heading, a paragraph, a list, and a table. The plain-text version keeps each block on its own line and keeps table cells separated by a tab.
<h1>Summary</h1><p>This is a <strong>short</strong> example.</p><ul><li>One</li><li>Two & three</li></ul><table><tr><th>Name</th><th>Value</th></tr><tr><td>Width</td><td>12</td></tr></table>
Summary
This is a short example.
One
Two & three
Name Value
Width 12
This is an illustrative input and expected shape of the output, not a claim that every converter will produce exactly this whitespace. The result should contain no angle-bracket tags, no script text, and no merged block lines.
Result check
- There are no remaining tags, attributes, comments, or script/style contents.
- Headings, paragraphs, list items, and table rows appear on separate lines; table cells remain separated by a visible delimiter such as a tab.
- Character references are decoded, and spaces at line boundaries have been trimmed.
Because the operation produces a new plain-text document rather than modifying the HTML in place, preserve the original HTML before running the conversion. Keep the source file or source string until the text output has been checked against it.
Applying the behavior in common environments
In JavaScript, the built-in innerText property is convenient because it returns the rendered text of an element with block boundaries, but its result depends on layout and is not available in every runtime. The textContent property removes markup, but it does not add block separators, so adjacent blocks can merge. A controlled conversion is therefore better implemented by first replacing block-level closing tags with newlines, then stripping tags and decoding entities.
In C#, a plain regex replace that removes tags is not enough for block preservation. A practical pipeline is to remove script and style ranges, replace designated block-closing tags with newline characters, remove all remaining tags, and decode HTML entities before trimming whitespace at line boundaries.
Documented behavior varies by library and engine. HtmlAgilityPack can parse an HTML document and expose InnerText or node-level text, but the exact treatment of whitespace and tags such as br or script depends on the library version and how node text is extracted. A conversion built for consistent plain-text output should explicitly define which elements create boundaries and which elements are discarded, rather than assuming a library default will preserve all meaningful structure.
What to verify and what remains source-specific
The core behavior is clear: remove non-content markup, keep inline text together, and place a newline at each meaningful block change. The exact rendered result can vary with line-break elements, table layout, preformatted text, and custom elements. Evidence provided in this guide does not guarantee identical whitespace across browsers, runtimes, or HTML parsers. When exact output matters, test the chosen converter with representative input that includes a list item, a table cell, an entity, and adjacent inline elements.