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');
66function 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
1212function formatLineBreak ( elem , fn , options ) {
Original file line number Diff line number Diff line change @@ -50,9 +50,10 @@ exports.decodeHTMLEntities = function decodeHTMLEntities(text) {
5050} ;
5151
5252exports . 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 ) {
Original file line number Diff line number Diff line change @@ -73,6 +73,7 @@ function containsTable(attr, tables) {
7373
7474function 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 ) ) {
You can’t perform that action at this time.
0 commit comments