html-to-text
Advanced tools
Comparing version
# Changelog | ||
## Version 2.1.1 | ||
* Extra space ploblem fixed. #88 | ||
## Version 2.1.0 | ||
@@ -4,0 +8,0 @@ |
@@ -8,3 +8,3 @@ var _ = require('underscore'); | ||
function formatText(elem, options) { | ||
var text = (options.isInPre ? elem.raw : _s.strip(elem.raw)); | ||
var text = elem.raw; | ||
text = he.decode(text, options.decodeOptions); | ||
@@ -15,3 +15,3 @@ | ||
} else { | ||
return helper.wordwrap(elem.needsSpace ? ' ' + text : text, options); | ||
return helper.wordwrap(elem.trimLeadingSpace ? _s.lstrip(text) : text, options); | ||
} | ||
@@ -63,7 +63,9 @@ } | ||
var storedCharCount = options.lineCharCount; | ||
var result = _s.strip(fn(elem.children || [], options)); | ||
if (!result) { | ||
result = ''; | ||
var text = fn(elem.children || [], options); | ||
if (!text) { | ||
text = ''; | ||
} | ||
var result = elem.trimLeadingSpace ? _s.lstrip(text) : text; | ||
if (!options.ignoreHref) { | ||
@@ -78,3 +80,3 @@ // Get the href, if present | ||
} | ||
if (!options.hideLinkHrefIfSameAsText || href != result.replace('\n', '')) { | ||
if (!options.hideLinkHrefIfSameAsText || href != _s.replaceAll(result, '\n', '')) { | ||
result += ' [' + href + ']'; | ||
@@ -87,3 +89,3 @@ } | ||
return formatText({ raw: result || href, needsSpace: elem.needsSpace }, options); | ||
return formatText({ raw: result || href, trimLeadingSpace: elem.trimLeadingSpace }, options); | ||
} | ||
@@ -90,0 +92,0 @@ |
@@ -69,3 +69,3 @@ var _ = require('underscore'); | ||
// Add buffer to result if we can't fit any more words in the buffer. | ||
if ((max || max === 0) && | ||
if ((max || max === 0) && length > 0 && | ||
((length + word.length > max) || (length + word.indexOf('\n') > max))) | ||
@@ -105,3 +105,11 @@ { | ||
result += buffer.join(' '); | ||
return _s.rstrip(result); | ||
// Preserve trailing space | ||
if (!_s.endsWith(text, ' ')) { | ||
result = _s.rtrim(result); | ||
} else if (!_s.endsWith(result, ' ')) { | ||
result = result + ' '; | ||
} | ||
return result; | ||
}; | ||
@@ -113,6 +121,2 @@ | ||
exports.isArray = function isArray(x) { | ||
return Object.prototype.toString.call(x) === "[object Array]"; | ||
}; | ||
exports.splitCssSearchTag = function splitCssSearchTag(tagString) { | ||
@@ -119,0 +123,0 @@ function getParams(re, string) { |
@@ -49,3 +49,3 @@ var fs = require('fs'); | ||
var result = ''; | ||
var baseElements = helper.isArray(options.baseElement) ? options.baseElement : [options.baseElement]; | ||
var baseElements = Array.isArray(options.baseElement) ? options.baseElement : [options.baseElement]; | ||
for (var idx = 0; idx < baseElements.length; ++idx) { | ||
@@ -109,3 +109,3 @@ result += walk(filterBody(handler.dom, options, baseElements[idx]), options); | ||
} | ||
var whiteSpaceRegex = /\S$/; | ||
var whiteSpaceRegex = /\s$/; | ||
_.each(dom, function(elem) { | ||
@@ -119,5 +119,5 @@ switch(elem.type) { | ||
case 'a': | ||
// Inline element needs a leading space if `result` currently | ||
// doesn't end with whitespace | ||
elem.needsSpace = whiteSpaceRegex.test(result); | ||
// Inline element needs its leading space to be trimmed if `result` | ||
// currently ends with whitespace | ||
elem.trimLeadingSpace = whiteSpaceRegex.test(result); | ||
result += format.anchor(elem, walk, options); | ||
@@ -164,5 +164,5 @@ break; | ||
if (elem.raw !== '\r\n') { | ||
// Text needs a leading space if `result` currently | ||
// doesn't end with whitespace | ||
elem.needsSpace = whiteSpaceRegex.test(result); | ||
// Text needs its leading space to be trimmed if `result` | ||
// currently ends with whitespace | ||
elem.trimLeadingSpace = whiteSpaceRegex.test(result); | ||
result += format.text(elem, options); | ||
@@ -169,0 +169,0 @@ } |
{ | ||
"name": "html-to-text", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "Advanced html to plain text converter", | ||
@@ -24,3 +24,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"he": "^0.5.0", | ||
"he": "^1.0.0", | ||
"htmlparser": "^1.7.7", | ||
@@ -27,0 +27,0 @@ "optimist": "^0.6.1", |
@@ -31,3 +31,3 @@ # node-html-to-text | ||
``` | ||
```javascript | ||
var htmlToText = require('html-to-text'); | ||
@@ -37,3 +37,3 @@ | ||
tables: ['#invoice', '.address'] | ||
}, function(err, text) { | ||
}, (err, text) => { | ||
if (err) return console.error(err); | ||
@@ -46,3 +46,3 @@ console.log(text); | ||
``` | ||
```javascript | ||
var htmlToText = require('html-to-text'); | ||
@@ -95,3 +95,3 @@ | ||
``` | ||
```html | ||
<html> | ||
@@ -291,3 +291,3 @@ <head> | ||
Copyright (c) 2015 werk85 <legenhausen@werk85.de> | ||
Copyright (c) 2016 werk85 <legenhausen@werk85.de> | ||
@@ -312,3 +312,1 @@ Permission is hereby granted, free of charge, to any person obtaining | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
[](http://githalytics.com/werk85/node-html-to-text) |
@@ -55,3 +55,3 @@ var expect = require('chai').expect; | ||
var testString = "<p>We appreciate your business. And we hope you'll check out our <a href=\"http://example.com/\">new products</a>!</p>"; | ||
expect(htmlToText.fromString(testString, {} )).to.equal('We appreciate your business. And we hope you\'ll check out our new products\n[http://example.com/] !'); | ||
expect(htmlToText.fromString(testString, {} )).to.equal('We appreciate your business. And we hope you\'ll check out our new products\n[http://example.com/]!'); | ||
}); | ||
@@ -149,3 +149,3 @@ | ||
'; | ||
var resultExpected = 'Good morning Jacob,Lorem ipsum dolor sit amet\n\nLorem ipsum dolor sit amet.\n\n * run in the park (in progress)'; | ||
var resultExpected = 'Good morning Jacob, Lorem ipsum dolor sit amet\n\nLorem ipsum dolor sit amet.\n\n * run in the park (in progress)'; | ||
var result = htmlToText.fromString(html, { wordwrap: false }); | ||
@@ -169,3 +169,3 @@ expect(result).to.equal(resultExpected); | ||
'; | ||
var resultExpected = 'Good morning Jacob,Lorem ipsum dolor sit amet.'; | ||
var resultExpected = 'Good morning Jacob, Lorem ipsum dolor sit amet.'; | ||
var result = htmlToText.fromString(html, { tables: true }); | ||
@@ -172,0 +172,0 @@ expect(result).to.equal(resultExpected); |
@@ -61,2 +61,2 @@ PARAGRAPHS | ||
We appreciate your business. And we hope you'll check out our new products | ||
[http://example.com/] ! | ||
[http://example.com/]! |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
68525
0.11%885
0.45%307
-0.65%