Repository navigation
Releases
Minor bugfix
Merges #22 to fix a bug introduced in the v1.5.0 refactor:
- Prevent HTML entities (like
") in hidden tags (e.g.,<title>) from leaking to the start of the output. - Prevent HTML entities in link text from leaking when the
WithLinksInnerTextoption is not enabled.
v1.5.0
This release fixes various parsing issues and edge cases, mainly when parsing invalid HTML input.
For most cases with valid HTML input, the text output should remain the same.
Main Behavior Changes
-
Custom URL schemes are no longer enabled by default. If you only use the http, https, tel, and sms schemes in link href attributes, you are not affected, as these schemes are enabled by default. Relative paths in href attributes (
fileor./file) are also supported by default. If you use other schemes, such as ftp:// or customapp://deeplink, tags with those hrefs will be ignored unless you whitelist the schemes (along with any others you may need) using WithAllowedURLSchemes(). -
An tag without an href attribute would previously suppress all subsequent output, effectively ignoring the input after that tag.
-
The link order for nested tags was previously incorrect. This change also required a test case update.
- Example Input (WithLinksInnerText):
click <a href="one">here<a href="two"> or</a><span> here</span></a> - Output Before:
click here or <one> here <two> - Output Now:
click here or <two> here <one>
- Example Input (WithLinksInnerText):
API Changes
- No breaking changes. Your code will build with the new version without modifications.
- Added a new option function
WithAllowedURLSchemes()to define a list of allowed URL schemes (http, https, tel, and sms by default).
Code Changes
- HTML entities such as > are now also parsed inside href attributes on tags.
- Control characters, html entities encoding control characters and soft hyphens (0xad) are stripped during conversion.
- tag href attribute values now undergo proper URL parsing using url.Parse and are checked against the list of allowed schemes.
- Fixed a bug where a closing tag for a tag that was never opened (invalid HTML input) would suppress subsequent text.
- Fixed a bug where an tag without an href attribute would suppress subsequent text.
- The whitespace characters allowed between a tag name and an attribute name (the
Wposition in<aWhref="...">) have been extended from a single space () to[ \t\n\r\f/]. - Tag boundary characters
<and>are now ignored when inside single- or double-quoted strings, as in<style title="<">hidden</style>. The implementation is naive but should be sufficient considering the limitations of single-pass parsing and the main library use-case of converting a supposedly-valid HTML newsletter text into a plain text. - Mismatched closing tags (invalid HTML input) no longer break content suppression (
<head></script><title>hidden</title></head>visibleno longer reveals the hidden title). - HTML entities are no longer limited to 10 characters (so
:also works), and;is no longer required for numeric entities. This matches browser behavior; for example,a:bresults ina:b. - Custom tags with an h[1-6] prefix are no longer interpreted as headers (for example,
<h1custom></h1custom>). - Fixed a bug where tags inside suppressed blocks (head, script, and style) would still be converted to text.
- HTML comment blocks (
<!-- ... -->) are now supported.
Test cases in html2text_test.go can be used to study the intended behavior.
If the changes broke your input or you suspect that a test case is wrong, please open an issue or PR.
Full Changelog: v1.3.2...v1.5.0
Support for optional attributes on all tags.
Changes:
- Fix: Support for optional attributes on tags.
- The API remains fully backward-compatible.
Note: Caution if your HTML source is using tags with attributes.
Details:
Previously, tags other than <a> that contained attributes were not converted correctly. For example, <li class="menu-item"> was treated as inline text rather than a list item on a new line. Similarly, other tags with attributes (such as <h[1-6]> or <p>) were converted as inline text instead of being followed by a newline.
Option WithKeepSpaces to keep spaces as they are
Changes:
- another fully-backward-compatible update
- a new WithKeepSpaces option added to preserve spaces as they are (prevents the default behavior where multiple consecutive space characters are collapsed into a single standard space character)
Improved <a> tag handling & optional <li> list formatting
Changes:
- another fully-backward-compatible update
- tag is now case-insensitive and follows official specs, see #16
- added optional WithListSupport and WithListSupportPrefix to make
<li></li>lines formatted with a prefix like " - "
v1.1.0
- same as v1.0.9, just signaling the fact that the new functionality has been added
- fully-backward-compatible update (with any past version)
- functional options (line breaks style and tag inner text preservation)
HTML2TextWithOptions(`click <a href="http://bit.ly/2n4wXRs">news</a>`, WithLinksInnerText()) // produces: click news <http://bit.ly/2n4wXRs>
v1.0.9
- fully-backward-compatible update
- functional options (line breaks style and tag inner text preservation)
HTML2TextWithOptions(`click <a href="http://bit.ly/2n4wXRs">news</a>`, WithLinksInnerText()) // produces: click news <http://bit.ly/2n4wXRs>