1
- /* eslint-disable object-shorthand, prefer-template */
2
- const Handlebars = require ( 'handlebars' ) ;
1
+ const globby = require ( 'globby' ) ;
2
+ const path = require ( 'path' ) ;
3
+
3
4
const pagination = require ( './pagination' ) ;
5
+
6
+ const basePath = path . join ( __dirname , '..' ) ;
7
+ const files = globby . sync ( [ 'helper/*.js' , '**/helpers/*.js' , '!helper/index.js' , '!helper/pagination.js' ] , { cwd : basePath } ) ; // eslint-disable-line no-sync
8
+
9
+ const helpers = files . reduce ( ( result , file ) => {
10
+ return Object . assign ( result , require ( path . join ( basePath , file ) ) ) ;
11
+ } , { } ) ;
12
+
4
13
const url = require ( 'url' ) ;
5
14
6
15
const jobStatus = {
@@ -30,10 +39,6 @@ module.exports = function () {
30
39
return isIndexPage ( page ) && page . title . toLowerCase ( ) . replace ( ' ' , '-' ) !== page . category ;
31
40
} ;
32
41
33
- const normalizeString = ( str ) => {
34
- return str . toLowerCase ( ) . replace ( / [ ^ a - z 0 - 9 ] / gi, '-' ) ;
35
- } ;
36
-
37
42
const sortPageByAlpha = ( l , r ) => {
38
43
if ( l . title > r . title ) {
39
44
return 1 ;
@@ -75,138 +80,11 @@ module.exports = function () {
75
80
} ;
76
81
77
82
const self = {
78
- /* eslint-disable object-shorthand */
79
- capitalize : function ( str ) {
83
+ capitalize : ( str ) => {
80
84
const filtered = str . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, ' ' ) ;
81
85
82
86
return filtered . charAt ( 0 ) . toUpperCase ( ) + filtered . slice ( 1 ) ;
83
87
} ,
84
- /* eslint-enable object-shorthand */
85
- /**
86
- * Handlebars Comparison Helpers
87
- * Copyright (c) 2013 Jon Schlinkert, Brian Woodward, contributors
88
- * Licensed under the MIT License (MIT).
89
- * https://github.com/helpers/handlebars-helpers/blob/a3683bab5519882927de527077c34a98ac22067b/lib/comparison.js#L48
90
- * Modified to fit sonarwhal Website
91
- */
92
- /**
93
- * {{#compare}}...{{/compare}}
94
- *
95
- * @credit : OOCSS
96
- * @param left value
97
- * @param operator The operator, must be between quotes ">", "=", "<=", etc...
98
- * @param right value
99
- * @param options option object sent by handlebars
100
- * @return {String } formatted html
101
- *
102
- * @example :
103
- * {{#compare unicorns "<" ponies}}
104
- * I knew it, unicorns are just low-quality ponies!
105
- * {{/compare}}
106
- *
107
- * {{#compare value ">=" 10}}
108
- * The value is greater or equal than 10
109
- * {{else}}
110
- * The value is lower than 10
111
- * {{/compare}}
112
- */
113
- compare : function ( left , operator , right , options ) { // eslint-disable-line object-shorthand
114
- if ( arguments . length < 3 ) {
115
- throw new Error ( 'Handlebars Helper "compare" needs 2 parameters' ) ;
116
- }
117
-
118
- /* eslint-disable no-param-reassign */
119
- if ( ! options ) {
120
- options = right ;
121
- right = operator ;
122
- operator = '===' ;
123
- }
124
- /* eslint-enable no-param-reassign */
125
-
126
- const operators = {
127
- '!=' : function ( l , r ) {
128
- return l !== r ;
129
- } ,
130
- '!==' : function ( l , r ) {
131
- return l !== r ;
132
- } ,
133
- '<' : function ( l , r ) {
134
- return l < r ;
135
- } ,
136
- '<=' : function ( l , r ) {
137
- return l <= r ;
138
- } ,
139
- '==' : function ( l , r ) {
140
- return l === r ;
141
- } ,
142
- '===' : function ( l , r ) {
143
- if ( typeof l === 'string' && typeof r === 'string' ) {
144
- /* eslint-disable no-param-reassign */
145
- l = normalizeString ( l ) ;
146
- r = normalizeString ( r ) ;
147
- /* eslint-enable no-param-reassign */
148
- }
149
-
150
- return l === r ;
151
- } ,
152
- '>' : function ( l , r ) {
153
- return l > r ;
154
- } ,
155
- '>=' : function ( l , r ) {
156
- return l >= r ;
157
- } ,
158
- includes : function ( collection , member ) {
159
- const normalizedR = member ? normalizeString ( member ) : member ;
160
- const normalizedL = collection . split ( / , * / g) . map ( function ( element ) { //eslint-disable-line prefer-arrow-callback
161
- return normalizeString ( element ) ;
162
- } ) ;
163
-
164
- return normalizedL . indexOf ( normalizedR ) !== - 1 ;
165
- } ,
166
- typeof : function ( l , r ) {
167
- return typeof l === r ;
168
- } ,
169
- '||' : function ( l , r ) {
170
- return l || r ;
171
- }
172
- } ;
173
-
174
- if ( ! operators [ operator ] ) {
175
- throw new Error ( 'Handlebars Helper "compare" doesn\'t know the operator ' + operator ) ;
176
- }
177
-
178
- const result = operators [ operator ] ( left , right ) ;
179
-
180
- if ( result ) {
181
- return options . fn ( this ) ;
182
- }
183
-
184
- return options . inverse ( this ) ;
185
- } ,
186
- cutCodeString : function ( codeString ) {
187
- return self . shortenString ( codeString , 150 ) ;
188
- } ,
189
- cutString : function ( string , maxLength ) {
190
- const minLength = 0.8 * maxLength ;
191
- const preferredStopChars = / [ ^ a - z A - Z 0 - 9 ] / g;
192
- let chunk ;
193
-
194
- for ( let i = minLength ; i < maxLength ; i ++ ) {
195
- // Start looking for preferred stop characters.
196
- if ( preferredStopChars . test ( string [ i ] ) ) {
197
- chunk = string . slice ( 0 , i ) ;
198
-
199
- break ;
200
- }
201
- }
202
-
203
- chunk = chunk || string . slice ( 0 , maxLength ) ;
204
-
205
- return chunk ;
206
- } ,
207
- cutUrlString : function ( urlString ) {
208
- return self . shortenString ( urlString , 25 ) ;
209
- } ,
210
88
filterErrorsAndWarnings : ( results ) => {
211
89
if ( ! results ) {
212
90
return results ;
@@ -231,16 +109,10 @@ module.exports = function () {
231
109
if ( match ) {
232
110
const ruleName = match . pop ( ) ;
233
111
234
- return ' packages/' + ruleName + ' /README.md' ;
112
+ return ` packages/${ ruleName } /README.md` ;
235
113
}
236
114
237
- return 'packages/sonarwhal/' + originalFile ;
238
- } ,
239
- getLength : function ( messages , unit ) {
240
- const length = messages . length ;
241
- const units = self . pluralize ( unit , length ) ;
242
-
243
- return length + ' ' + units ;
115
+ return `packages/sonarwhal/${ originalFile } ` ;
244
116
} ,
245
117
getPagesByToCTitle : ( title , pages ) => {
246
118
return pages [ title ] . filter ( ( page ) => {
@@ -318,27 +190,6 @@ module.exports = function () {
318
190
isPending : ( status ) => {
319
191
return status === jobStatus . pending ;
320
192
} ,
321
- linkify : function ( msg ) {
322
- const regex = / ( h t t p s ? : \/ \/ [ a - z A - Z 0 - 9 . \\ / ? : @ \- _ = # ] + \. [ a - z A - Z 0 - 9 & . \\ / ? : @ - _ = # ] * ) \s [ a - z A - Z ] / g;
323
- // Modified use of regular expression in https://stackoverflow.com/a/39220764
324
- // Should match:
325
- // [email protected] has 2 known vulnerabilities (1 medium, 1 low). See https://snyk.io/vuln/npm:jquery for more information.
326
- // [email protected] has 3 known vulnerabilities (3 high). See https://snyk.io/vuln/npm:angular for more information.
327
- // Shouldn't match (shortened url):
328
- // File https://www.odysys.com/ … hedule-Your-Demo-Now.png could be around 37.73kB (78%) smaller.
329
- const match = regex . exec ( msg ) ;
330
- const escapedMsg = Handlebars . Utils . escapeExpression ( msg ) ;
331
-
332
- if ( ! match ) {
333
- return escapedMsg ;
334
- }
335
-
336
- const urlMatch = match . pop ( ) ;
337
- const escapedUrlMatch = Handlebars . Utils . escapeExpression ( urlMatch ) ;
338
- const newMsg = escapedMsg . replace ( escapedUrlMatch , '<a href="' + urlMatch + '">' + escapedUrlMatch + '</a>' ) ;
339
-
340
- return new Handlebars . SafeString ( newMsg ) ;
341
- } ,
342
193
noIssue : ( category ) => {
343
194
return category . rules . every ( ( rule ) => {
344
195
return rule . status === ruleStatus . pass ;
@@ -355,13 +206,6 @@ module.exports = function () {
355
206
return className . toLowerCase ( ) . trim ( )
356
207
. replace ( / [ ^ a - z 0 - 9 ] / gi, '-' ) ;
357
208
} ,
358
- normalizePosition : function ( position ) {
359
- if ( ! position || parseInt ( position ) === - 1 ) {
360
- return '' ;
361
- }
362
-
363
- return ':' + position ;
364
- } ,
365
209
or : ( l , r ) => {
366
210
return l || r ;
367
211
} ,
@@ -375,13 +219,6 @@ module.exports = function () {
375
219
passWarnings : ( statistics ) => {
376
220
return statistics && statistics . warnings === 0 ;
377
221
} ,
378
- pluralize : function ( text , count ) {
379
- return text + ( count === 1 ? '' : 's' ) ;
380
- } ,
381
- reverseString : function ( str ) {
382
- return str . split ( '' ) . reverse ( )
383
- . join ( '' ) ;
384
- } ,
385
222
sanitize : ( permalink ) => {
386
223
return permalink . replace ( / \/ i n d e x .h t m l / g, '/' ) ;
387
224
} ,
@@ -390,18 +227,6 @@ module.exports = function () {
390
227
return accumulator || value ;
391
228
} ) ;
392
229
} ,
393
- // Solution inspired by https://stackoverflow.com/a/10903003
394
- shortenString : function ( string , maxLength ) {
395
- if ( ! string || string . length < maxLength * 2 ) {
396
- return string ;
397
- }
398
-
399
- const headChunk = self . cutString ( string , maxLength ) ;
400
- const reverseTailChunk = self . cutString ( self . reverseString ( string ) , maxLength ) ;
401
- const tailChunk = self . reverseString ( reverseTailChunk ) ;
402
-
403
- return headChunk + ' … ' + tailChunk ;
404
- } ,
405
230
showMdContent : ( page ) => {
406
231
// If the markdown Content should be used.
407
232
const guildeIndexes = [ 'contributor guide' , 'user guide' ] ;
@@ -444,5 +269,5 @@ module.exports = function () {
444
269
}
445
270
} ;
446
271
447
- return self ;
272
+ return Object . assign ( self , helpers ) ;
448
273
} ;
0 commit comments