Option | Default | Applies to | Description | |
---|---|---|---|---|
leadingLineBreaks | 1 , 2 or 3 | all block-level formatters | Number of line breaks to separate previous block from this one. Note that N+1 line breaks are needed to make N empty lines. | |
trailingLineBreaks | 1 or 2 | all block-level formatters | Number of line breaks to separate this block from the next one. Note that N+1 line breaks are needed to make N empty lines. | |
baseUrl | null | anchor , image | Server host for link href attributes and image src attributes relative to the root (the ones that start with / ). For example, with baseUrl = ‘http://asdf.com’ and . the link in the text will be http://asdf.com/dir/subdir . Keep in mind that baseUrl should not end with a / . | |
hideLinkHrefIfSameAsText | false | anchor | By default links are translated in the following way: text => becomes => text [link] . If this option is set to true and link and text are the same, [link] will be omitted and only text will be present. | |
ignoreHref | false | anchor | Ignore all links. Only process internal text of anchor tags. | |
noAnchorUrl | true | anchor | Ignore anchor links (where href=’#. ‘ ). | |
noLinkBrackets | false | anchor | Don’t print brackets around links. | |
itemPrefix | ‘ * ‘ | unorderedList | String prefix for each list item. | |
uppercase | true | heading | By default, headings ( , , etc) are uppercased. Set this to false to leave headings as they are. | |
length | undefined | horizontalLine | Length of the line. If undefined then wordwrap value is used. Falls back to 40 if that’s also disabled. | |
trimEmptyLines | true | blockquote | Trim empty lines from blockquote. While empty lines should be preserved in HTML, space-saving behavior is chosen as default for convenience. | |
uppercaseHeaderCells | true | table , dataTable | By default, heading cells ( | ) are uppercased. Set this to false to leave heading cells as they are. |
maxColumnWidth | 60 | table , dataTable | Data table cell content will be wrapped to fit this width instead of global wordwrap limit. Set to undefined in order to fall back to wordwrap limit. | |
colSpacing | 3 | table , dataTable | Number of spaces between data table columns. | |
rowSpacing | 0 | table , dataTable | Number of empty lines between data table rows. |
formatters option is an object that holds formatting functions. They can be assigned to format different tags by key in the tags option.
Each formatter is a function of four arguments that returns nothing. Arguments are:
- elem — the HTML element to be processed by this formatter;
- walk — recursive function to process the children of this element. Called as walk(elem.children, builder) ;
- builder — BlockTextBuilder object. Manipulate this object state to build the output text;
- formatOptions — options that are specified for a tag, along with this formatter (Note: if you need global html-to-text options — they are accessible via builder.options ).
var htmlToText > = require('html-to-text');var text = htmlToText('Hello World ',formatters:// Create a formatter.'fooBlockFormatter': function (elem, walk, builder, formatOptions)builder.openBlock(formatOptions.leadingLineBreaks || 1);walk(elem.children, builder);builder.addInline('!');builder.closeBlock(formatOptions.trailingLineBreaks || 1);>>,tags:// Assign it to `foo` tags.'foo':format: 'fooBlockFormatter',options: leadingLineBreaks: 1, trailingLineBreaks: 1 >>>>);console.log(text); // Hello World!
Refer to built-in formatters for more examples.
Refer to BlockTextBuilder for available functions and arguments.
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-text 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.
- @mlegenhausen — creator;
- @KillyMXI — maintainer since 2020;
- Everyone else who added something to the tool or helped us shaping it via issues and PRs.
- .
Set to null or false to disable word-wrapping.
By using the format option, you can specify formatting for ALL elements:
Key | Tags |
---|---|
anchor | a |
blockquote | blockquote |
heading | h1 , h2 , h3 , h4 , h5 , h6 |
horizontalLine | hr |
image | img |
lineBreak | br |
| |
orderedList | ol |
paragraph | p , pre |
table | table |
text | |
unorderedList | ul |
. | . |
Each key must be a function which eventually receive elem (the current elem), fn (the next formatting function) and options (the options passed to html-to-text).
var htmlToText = require('html-to-text');var text = htmlToText.fromString('Hello World
',format:heading: function (elem, fn, options)var h = fn(elem.children, options);return '====\n' + h.toUpperCase() + '\n====';>>>);console.log(text);// ====\nHELLO WORLD\n====var text2 = htmlToText.fromString('HelloWorld!',format:div: function (elem, fn, options)const h = fn(elem.children, options);return h + '\n';>>>);console.log(text2);// Hello\nWorld\n!
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-text 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.
html>head>meta charset="utf-8">head>body>table cellpadding="0" cellspacing="0" border="0">tr>td>h2>Paragraphsh2>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">Githuba>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 tableh2>table id="invoice">thead>tr>th>Articleth>th>Priceth>th>Taxesth>th>Amountth>th>Totalth>tr>thead>tbody>tr>td>p>Product 1br />span style="font-size:0.8em">Contains: 1x Product 1span>p>td>td align="right" valign="top">6,99€td>td align="right" valign="top">7%td>td align="right" valign="top">1td>td align="right" valign="top">6,99€td>tr>tr>td>Shipment coststd>td align="right">3,25€td>td align="right">7%td>td align="right">1td>td align="right">3,25€td>tr>tbody>tfoot>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>tfoot>table>td>td>/td>tr>tr>td>hr/>h2>Listsh2>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 tablesh2>table class="address">tr>th align="left">Invoice Addressth>th align="left">Shipment Addressth>tr>tr>td align="left">p>Mr.br/>John Doebr/>Featherstone Street 49br/>28199 Bremenbr/>p>td>td align="left">p>Mr.br/>John Doebr/>Featherstone Street 49br/>28199 Bremenbr/>p>td>tr>table>td>td>/td>tr>tr>td>hr/>h2>Mailto formatingh2>p class="normal-space small">Some Companybr />Some Street 42br />Somewherebr />E-Mail: a href="mailto:test@example.com">Click herea>p>td>tr>table>body>html>
PARAGRAPHSAt vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasdgubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsumdolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod temporinvidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eoset accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no seatakimata sanctus est Lorem ipsum dolor sit amet. Github [www.github.com]At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasdgubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsumdolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod temporinvidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eoset accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no seatakimata sanctus est Lorem ipsum dolor sit amet.--------------------------------------------------------------------------------PRETTY PRINTED TABLEARTICLE PRICE TAXES AMOUNT TOTALProduct 1 6,99€ 7% 1 6,99€Contains: 1x Product 1Shipment 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 kasdgubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.* At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasdgubergren, 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 kasdgubergren, 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 kasdgubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.--------------------------------------------------------------------------------COLUMN LAYOUT WITH TABLESINVOICE ADDRESS SHIPMENT ADDRESSMr. Mr.John Doe John DoeFeatherstone Street 49 Featherstone Street 4928199 Bremen 28199 Bremen--------------------------------------------------------------------------------MAILTO FORMATINGSome CompanySome Street 42SomewhereE-Mail: Click here [test@example.com]
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.