1
- var _ = require ( 'underscore' ) ;
2
- var _s = require ( 'underscore.string' ) ;
1
+ var max = require ( 'lodash/max' ) ;
2
+ var compact = require ( 'lodash/compact' ) ;
3
+ var times = require ( 'lodash/times' ) ;
4
+
5
+ var trimStart = require ( 'lodash/trimStart' ) ;
6
+ var padEnd = require ( 'lodash/padEnd' ) ;
7
+
3
8
var he = require ( 'he' ) ;
4
9
5
10
var helper = require ( './helper' ) ;
@@ -11,7 +16,7 @@ function formatText(elem, options) {
11
16
if ( options . isInPre ) {
12
17
return text ;
13
18
} else {
14
- return helper . wordwrap ( elem . trimLeadingSpace ? _s . lstrip ( text ) : text , options ) ;
19
+ return helper . wordwrap ( elem . trimLeadingSpace ? trimStart ( text ) : text , options ) ;
15
20
}
16
21
}
17
22
@@ -68,7 +73,7 @@ function formatAnchor(elem, fn, options) {
68
73
text = '' ;
69
74
}
70
75
71
- var result = elem . trimLeadingSpace ? _s . lstrip ( text ) : text ;
76
+ var result = elem . trimLeadingSpace ? trimStart ( text ) : text ;
72
77
73
78
if ( ! options . ignoreHref ) {
74
79
// Get the href, if present
@@ -80,7 +85,7 @@ function formatAnchor(elem, fn, options) {
80
85
if ( options . linkHrefBaseUrl && href . indexOf ( '/' ) === 0 ) {
81
86
href = options . linkHrefBaseUrl + href ;
82
87
}
83
- if ( ! options . hideLinkHrefIfSameAsText || href !== _s . replaceAll ( result , '\n' , '' ) ) {
88
+ if ( ! options . hideLinkHrefIfSameAsText || href !== helper . replaceAll ( result , '\n' , '' ) ) {
84
89
if ( ! options . noLinkBrackets ) {
85
90
result += ' [' + href + ']' ;
86
91
} else {
@@ -97,19 +102,19 @@ function formatAnchor(elem, fn, options) {
97
102
}
98
103
99
104
function formatHorizontalLine ( elem , fn , options ) {
100
- return '\n' + _s . repeat ( '-' , options . wordwrap ) + '\n\n' ;
105
+ return '\n' + '-' . repeat ( options . wordwrap ) + '\n\n' ;
101
106
}
102
107
103
108
function formatListItem ( prefix , elem , fn , options ) {
104
- options = _ . clone ( options ) ;
109
+ options = Object . assign ( { } , options ) ;
105
110
// Reduce the wordwrap for sub elements.
106
111
if ( options . wordwrap ) {
107
112
options . wordwrap -= prefix . length ;
108
113
}
109
114
// Process sub elements.
110
115
var text = fn ( elem . children , options ) ;
111
116
// Replace all line breaks with line break + prefix spacing.
112
- text = text . replace ( / \n / g, '\n' + _s . repeat ( ' ' , prefix . length ) ) ;
117
+ text = text . replace ( / \n / g, '\n' + ' ' . repeat ( prefix . length ) ) ;
113
118
// Add first prefix and line break at the end.
114
119
return prefix + text + '\n' ;
115
120
}
@@ -122,7 +127,7 @@ function formatUnorderedList(elem, fn, options) {
122
127
var nonWhiteSpaceChildren = ( elem . children || [ ] ) . filter ( function ( child ) {
123
128
return child . type !== 'text' || ! whiteSpaceRegex . test ( child . data ) ;
124
129
} ) ;
125
- _ . each ( nonWhiteSpaceChildren , function ( elem ) {
130
+ nonWhiteSpaceChildren . forEach ( function ( elem ) {
126
131
result += formatListItem ( prefix , elem , fn , options ) ;
127
132
} ) ;
128
133
return result + '\n' ;
@@ -152,12 +157,12 @@ function formatOrderedList(elem, fn, options) {
152
157
var start = Number ( elem . attribs . start || '1' ) - 1 ;
153
158
// Calculate the maximum length to i.
154
159
var maxLength = ( nonWhiteSpaceChildren . length + start ) . toString ( ) . length ;
155
- _ . each ( nonWhiteSpaceChildren , function ( elem , i ) {
160
+ nonWhiteSpaceChildren . forEach ( function ( elem , i ) {
156
161
// Use different function depending on type
157
162
var index = typeFunction ( start , i ) ;
158
163
// Calculate the needed spacing for nice indentation.
159
164
var spacing = maxLength - index . toString ( ) . length ;
160
- var prefix = ' ' + index + '. ' + _s . repeat ( ' ' , spacing ) ;
165
+ var prefix = ' ' + index + '. ' + ' ' . repeat ( spacing ) ;
161
166
result += formatListItem ( prefix , elem , fn , options ) ;
162
167
} ) ;
163
168
}
@@ -167,24 +172,24 @@ function formatOrderedList(elem, fn, options) {
167
172
function tableToString ( table ) {
168
173
// Determine space width per column
169
174
// Convert all rows to lengths
170
- var widths = _ . map ( table , function ( row ) {
171
- return _ . map ( row , function ( col ) {
175
+ var widths = table . map ( function ( row ) {
176
+ return row . map ( function ( col ) {
172
177
return col . length ;
173
178
} ) ;
174
179
} ) ;
175
180
// Invert rows with colums
176
181
widths = helper . arrayZip ( widths ) ;
177
182
// Determine the max values for each column
178
- widths = _ . map ( widths , function ( col ) {
179
- return _ . max ( col ) ;
183
+ widths = widths . map ( function ( col ) {
184
+ return max ( col ) ;
180
185
} ) ;
181
186
182
187
// Build the table
183
188
var text = '' ;
184
- _ . each ( table , function ( row ) {
189
+ table . forEach ( function ( row ) {
185
190
var i = 0 ;
186
- _ . each ( row , function ( col ) {
187
- text += _s . rpad ( _s . strip ( col ) , widths [ i ++ ] , ' ' ) + ' ' ;
191
+ row . forEach ( function ( col ) {
192
+ text += padEnd ( col . trim ( ) , widths [ i ++ ] , ' ' ) + ' ' ;
188
193
} ) ;
189
194
text += '\n' ;
190
195
} ) ;
@@ -193,7 +198,7 @@ function tableToString(table) {
193
198
194
199
function formatTable ( elem , fn , options ) {
195
200
var table = [ ] ;
196
- _ . each ( elem . children , tryParseRows ) ;
201
+ elem . children . forEach ( tryParseRows ) ;
197
202
return tableToString ( table ) ;
198
203
199
204
function tryParseRows ( elem ) {
@@ -205,27 +210,27 @@ function formatTable(elem, fn, options) {
205
210
case "tbody" :
206
211
case "tfoot" :
207
212
case "center" :
208
- _ . each ( elem . children , tryParseRows ) ;
213
+ elem . children . forEach ( tryParseRows ) ;
209
214
return ;
210
215
211
216
case 'tr' :
212
217
var rows = [ ] ;
213
- _ . each ( elem . children , function ( elem ) {
214
- var tokens , times ;
218
+ elem . children . forEach ( function ( elem ) {
219
+ var tokens , count ;
215
220
if ( elem . type === 'tag' ) {
216
221
switch ( elem . name . toLowerCase ( ) ) {
217
222
case 'th' :
218
223
tokens = formatHeading ( elem , fn , options ) . split ( '\n' ) ;
219
- rows . push ( _ . compact ( tokens ) ) ;
224
+ rows . push ( compact ( tokens ) ) ;
220
225
break ;
221
226
222
227
case 'td' :
223
228
tokens = fn ( elem . children , options ) . split ( '\n' ) ;
224
- rows . push ( _ . compact ( tokens ) ) ;
229
+ rows . push ( compact ( tokens ) ) ;
225
230
// Fill colspans with empty values
226
231
if ( elem . attribs && elem . attribs . colspan ) {
227
- times = elem . attribs . colspan - 1 || 0 ;
228
- _ . times ( times , function ( ) {
232
+ count = elem . attribs . colspan - 1 || 0 ;
233
+ times ( count , function ( ) {
229
234
rows . push ( [ '' ] ) ;
230
235
} ) ;
231
236
}
@@ -234,8 +239,8 @@ function formatTable(elem, fn, options) {
234
239
}
235
240
} ) ;
236
241
rows = helper . arrayZip ( rows ) ;
237
- _ . each ( rows , function ( row ) {
238
- row = _ . map ( row , function ( col ) {
242
+ rows . forEach ( function ( row ) {
243
+ row = row . map ( function ( col ) {
239
244
return col || '' ;
240
245
} ) ;
241
246
table . push ( row ) ;
0 commit comments