What is html-to-text?
The html-to-text npm package is designed to convert HTML documents into plain text. It can handle various HTML elements, preserving the basic structure and formatting in a text-only format. This is particularly useful for extracting text from HTML emails, web pages, or any HTML content for use in text-based formats.
What are html-to-text's main functionalities?
Convert HTML to plain text
This feature allows you to convert a string of HTML into a plain text format, with options such as word wrapping.
const htmlToText = require('html-to-text');
const html = '<h1>Hello World</h1>';
const text = htmlToText.fromString(html, {
wordwrap: 130
});
console.log(text); // Outputs: 'Hello World'
Handling of tables
This feature enables the conversion of HTML tables into a tabular text format, preserving the layout of the data.
const htmlToText = require('html-to-text');
const html = '<table><tr><td>Foo</td><td>Bar</td></tr></table>';
const text = htmlToText.fromString(html, {
tables: true
});
console.log(text); // Outputs a text representation of the table
Handling of lists
This feature converts HTML lists into plain text, with customizable prefixes for list items.
const htmlToText = require('html-to-text');
const html = '<ul><li>Item 1</li><li>Item 2</li></ul>';
const text = htmlToText.fromString(html, {
unorderedListItemPrefix: '- '
});
console.log(text); // Outputs: '- Item 1\n- Item 2'
Handling of hyperlinks
This feature allows for the conversion of hyperlinks into plain text, with options to hide or display the URL based on the link text.
const htmlToText = require('html-to-text');
const html = '<a href='https://example.com'>Example</a>';
const text = htmlToText.fromString(html, {
hideLinkHrefIfSameAsText: true
});
console.log(text); // Outputs: 'Example'
Other packages similar to html-to-text
turndown
Turndown is an HTML to Markdown converter that allows users to convert HTML content to Markdown format. It is different from html-to-text as it focuses on Markdown output rather than plain text.
html2plaintext
html2plaintext is another package that converts HTML to plain text. It aims to produce human-readable text similar to what a browser would render, but it may have different parsing and formatting options compared to html-to-text.
html-to-text-cli
html-to-text-cli is a command-line interface for converting HTML to plain text. While it uses the html-to-text package under the hood, it is specifically designed for CLI usage and may offer a different user experience.
node-html-to-text
A simple converter that parses HTML and returns beautiful text. It was mainly designed to transform HTML E-Mail templates to a text representation. So it is currently optimized for table layouts.
Features:
- Transform headlines to uppercase text.
- Convert tables to an appropiate text representation with rows and columns.
- Word wrapping for paragraphs (default 80 chars)
- Automatic extraction of href information from links.
<br>
conversion to \n
Installation
npm install html-to-text
Or when you want to use it as command line interface it is recommended to install it globally via
npm install html-to-text -g
Usage
You can read from a file via:
var htmlToText = require('html-to-text');
htmlToText.fromFile(path.join(__dirname, 'test.html'), {
tables: ['#invoice', '.address']
}, function(err, text) {
if (err) return console.error(err);
console.log(text);
});
or directly from a string:
var htmlToText = require('html-to-text');
var text = htmlToText.fromString('<h1>Hello World</h1>', {
wordwrap: 130
});
console.log(text);
Options:
You can configure the behaviour of html-to-text with the following options:
tables
allows to select certain tables by the class
or id
attribute from the HTML document. This is necessary because the majority of HTML E-Mails uses a table based layout. Prefix your table selectors with an .
for the class
and with a #
for the id
attribute. All other tables are ignored. You can assign true
to this attribute to select all tables. Default: []
wordwrap
defines after how many chars a line break should follow in p
elements. Default: 80
Command Line Interface
It is possible to use html-to-text as command line interface. This allows an easy validation of your generated text and the integration in other systems that does not run on node.js.
html-to-text
uses stdin
and stdout
for data in and output. So you can use html-to-html
the following way:
cat example/test.html | html-to-text > test.txt
There also all options available as described above. You can use them like this:
cat example/test.html | html-to-text --tables=#invoice,.address --wordwrap=100 > test.txt
The tables
option has to be declared as comma separated list without whitespaces.
Example
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<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">
<tr>
<th>Article</th>
<th>Price</th>
<th>Taxes</th>
<th>Amount</th>
<th>Total</th>
</tr>
<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€</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 align="right">3,25€</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td colspan="3">to pay: 10,24€</td>
</tr>
<tr>
<td></td>
<td></td>
<td colspan="3">Taxes 7%: 0,72€</td>
</tr>
</table>
</td>
<td></td>
</tr>
<tr>
<td>
<hr/>
<h2>Lists</h2>
<ul>
<li>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.</li>
<li>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.</li>
</ul>
<ol>
<li>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.</li>
<li>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.</li>
</ol>
</td>
</tr>
<tr>
<td>
<hr />
<h2>Column Layout with tables</h2>
<table class="address">
<tr>
<th align="left">Invoice Address</th>
<th align="left">Shipment Address</th>
</tr>
<tr>
<td align="left">
<p>
Mr.<br/>
John Doe<br/>
Featherstone Street 49<br/>
28199 Bremen<br/>
</p>
</td>
<td align="left">
<p>
Mr.<br/>
John Doe<br/>
Featherstone Street 49<br/>
28199 Bremen<br/>
</p>
</td>
</tr>
</table>
</td>
<td></td>
</tr>
<tr>
<td>
<hr/>
<h2>Mailto formating</h2>
<p class="normal-space small">
Some Company<br />
Some Street 42<br />
Somewhere<br />
E-Mail: <a href="mailto:test@example.com">Click here</a>
</p>
</td>
</tr>
</table>
</body>
</html>
Gets converted to:
PARAGRAPHS
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.www.github.com
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.
--------------------------------------------------------------------------------
PRETTY PRINTED TABLE
ARTICLE PRICE TAXES AMOUNT TOTAL
Product 1 6,99€ 7% 1 6,99€
Contains: 1x Product 1
Shipment costs 3,25€ 7% 1 3,25€
to pay: 10,24€
Taxes 7%: 0,72€
--------------------------------------------------------------------------------
LISTS
* 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.
* 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.
1. 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.
2. 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.
--------------------------------------------------------------------------------
COLUMN LAYOUT WITH TABLES
INVOICE ADDRESS SHIPMENT ADDRESS
Mr. Mr.
John Doe John Doe
Featherstone Street 49 Featherstone Street 49
28199 Bremen 28199 Bremen
--------------------------------------------------------------------------------
MAILTO FORMATING
Some Company
Some Street 42
Somewhere
E-Mail:test@example.com
License
(The MIT License)
Copyright (c) 2012 Malte Legenhausen <legenhausen@werk85.de>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.