Skip to content

Commit 595313a

Browse files
committed
Fix missing space in blocks of text.
Generally caused by inline anchor tags, but if we start formatting other inline tags, this issue will affect them, too.
1 parent 870ff66 commit 595313a

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

lib/formatter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var helper = require('./helper');
66
function formatText(elem, options) {
77
var text = _s.strip(elem.raw);
88
text = helper.decodeHTMLEntities(text);
9-
return helper.wordwrap(text, options.wordwrap);
9+
return helper.wordwrap(elem.needsSpace ? ' ' + text : text, options.wordwrap);
1010
};
1111

1212
function formatLineBreak(elem, fn, options) {

lib/helper.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ exports.decodeHTMLEntities = function decodeHTMLEntities(text) {
5050
};
5151

5252
exports.wordwrap = function wordwrap(text, max) {
53-
var result = '';
53+
// Preserve leading space
54+
var result = _s.startsWith(text, ' ') ? ' ' : '';
5455
var words = _s.words(text);
55-
var length = 0;
56+
var length = result.length;
5657
var buffer = [];
5758
_.each(words, function(word) {
5859
if (length + word.length > max) {

lib/html-to-text.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ function containsTable(attr, tables) {
7373

7474
function walk(dom, options) {
7575
var result = '';
76+
var whiteSpaceRegex = /\S$/;
7677
_.each(dom, function(elem) {
7778
switch(elem.type) {
7879
case 'tag':
@@ -111,7 +112,12 @@ function walk(dom, options) {
111112
}
112113
break;
113114
case 'text':
114-
if (elem.raw !== '\r\n') result += format.text(elem, options);
115+
if (elem.raw !== '\r\n') {
116+
// Text needs a leading space if `result` currently
117+
// doesn't end with whitespace
118+
elem.needsSpace = whiteSpaceRegex.test(result);
119+
result += format.text(elem, options);
120+
}
115121
break;
116122
default:
117123
if (!_.include(SKIP_TYPES, elem.type)) {

0 commit comments

Comments
 (0)