File tree Expand file tree Collapse file tree 3 files changed +11
-4
lines changed Expand file tree Collapse file tree 3 files changed +11
-4
lines changed Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ var helper = require('./helper');
6
6
function formatText ( elem , options ) {
7
7
var text = _s . strip ( elem . raw ) ;
8
8
text = helper . decodeHTMLEntities ( text ) ;
9
- return helper . wordwrap ( text , options . wordwrap ) ;
9
+ return helper . wordwrap ( elem . needsSpace ? ' ' + text : text , options . wordwrap ) ;
10
10
} ;
11
11
12
12
function formatLineBreak ( elem , fn , options ) {
Original file line number Diff line number Diff line change @@ -50,9 +50,10 @@ exports.decodeHTMLEntities = function decodeHTMLEntities(text) {
50
50
} ;
51
51
52
52
exports . wordwrap = function wordwrap ( text , max ) {
53
- var result = '' ;
53
+ // Preserve leading space
54
+ var result = _s . startsWith ( text , ' ' ) ? ' ' : '' ;
54
55
var words = _s . words ( text ) ;
55
- var length = 0 ;
56
+ var length = result . length ;
56
57
var buffer = [ ] ;
57
58
_ . each ( words , function ( word ) {
58
59
if ( length + word . length > max ) {
Original file line number Diff line number Diff line change @@ -73,6 +73,7 @@ function containsTable(attr, tables) {
73
73
74
74
function walk ( dom , options ) {
75
75
var result = '' ;
76
+ var whiteSpaceRegex = / \S $ / ;
76
77
_ . each ( dom , function ( elem ) {
77
78
switch ( elem . type ) {
78
79
case 'tag' :
@@ -111,7 +112,12 @@ function walk(dom, options) {
111
112
}
112
113
break ;
113
114
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
+ }
115
121
break ;
116
122
default :
117
123
if ( ! _ . include ( SKIP_TYPES , elem . type ) ) {
You can’t perform that action at this time.
0 commit comments