7
7
8
8
var _ = require ( 'lodash' ) ;
9
9
var Promise = require ( 'bluebird' ) ;
10
+ var sanitizeHtml = require ( 'sanitize-html' ) ;
10
11
11
12
module . exports = {
12
13
@@ -22,39 +23,39 @@ module.exports = {
22
23
} ,
23
24
24
25
description : {
25
- type : Sequelize . TEXT ,
26
+ type : Sequelize . TEXT
26
27
} ,
27
28
28
29
usage : {
29
- type : Sequelize . TEXT ,
30
+ type : Sequelize . TEXT
30
31
} ,
31
32
32
33
details : {
33
- type : Sequelize . TEXT ,
34
+ type : Sequelize . TEXT
34
35
} ,
35
36
36
37
value : {
37
- type : Sequelize . TEXT ,
38
+ type : Sequelize . TEXT
38
39
} ,
39
40
40
41
references : {
41
- type : Sequelize . TEXT ,
42
+ type : Sequelize . TEXT
42
43
} ,
43
44
44
45
note : {
45
- type : Sequelize . TEXT ,
46
+ type : Sequelize . TEXT
46
47
} ,
47
48
48
49
author : {
49
- type : Sequelize . TEXT ,
50
+ type : Sequelize . TEXT
50
51
} ,
51
52
52
53
seealso : {
53
- type : Sequelize . TEXT ,
54
+ type : Sequelize . TEXT
54
55
} ,
55
56
56
57
examples : {
57
- type : Sequelize . TEXT ,
58
+ type : Sequelize . TEXT
58
59
} ,
59
60
60
61
sourceJSON : {
@@ -65,7 +66,6 @@ module.exports = {
65
66
66
67
} ,
67
68
associations : function ( ) {
68
-
69
69
Topic . belongsTo ( PackageVersion ,
70
70
{
71
71
as : 'package_version' ,
@@ -115,7 +115,7 @@ module.exports = {
115
115
. replace ( ':version' , encodeURIComponent ( this . package_version . version ) )
116
116
. replace ( ':topic' , encodeURIComponent ( this . name ) )
117
117
. replace ( '/api/' , '/' ) ;
118
- } else return sails . getUrlFor ( { target : 'Topic.findById' } )
118
+ } return sails . getUrlFor ( { target : 'Topic.findById' } )
119
119
. replace ( ':id' , encodeURIComponent ( this . id ) )
120
120
. replace ( '/api/' , '/' ) ;
121
121
} ,
@@ -125,9 +125,9 @@ module.exports = {
125
125
. replace ( ':name' , encodeURIComponent ( this . package_version . package_name ) )
126
126
. replace ( ':version' , encodeURIComponent ( this . package_version . version ) )
127
127
. replace ( ':topic' , encodeURIComponent ( this . name ) ) ;
128
- } else return sails . getUrlFor ( { target : 'Topic.findById' } )
128
+ } return sails . getUrlFor ( { target : 'Topic.findById' } )
129
129
. replace ( ':id' , encodeURIComponent ( this . id ) ) ;
130
- } ,
130
+ }
131
131
} ,
132
132
133
133
classMethods : {
@@ -140,8 +140,8 @@ module.exports = {
140
140
var options = _ . mergeWith ( {
141
141
where : criteria ,
142
142
include : [
143
- { model : Argument , as : 'arguments' , attributes : [ 'name' , 'description' , 'topic_id' ] , separate :true } ,
144
- { model : Section , as : 'sections' , attributes : [ 'name' , 'description' , 'topic_id' ] , separate :true } ,
143
+ { model : Argument , as : 'arguments' , attributes : [ 'name' , 'description' , 'topic_id' ] , separate : true } ,
144
+ { model : Section , as : 'sections' , attributes : [ 'name' , 'description' , 'topic_id' ] , separate : true } ,
145
145
{ model : Tag , as : 'keywords' , attributes : [ 'name' ] } ,
146
146
{ model : Alias , as : 'aliases' , attributes : [ 'name' , 'topic_id' ] , separate : true } ,
147
147
{ model : Example , as : 'user_examples' ,
@@ -162,23 +162,24 @@ module.exports = {
162
162
package_name : packageName
163
163
} ;
164
164
165
- if ( version ) { //if version is specified, find in this one
165
+ if ( version ) { // if version is specified, find in this one
166
166
packageCriteria . version = version ;
167
167
}
168
168
169
169
var params = {
170
170
where : { name : name } ,
171
171
include : [
172
172
{ model : PackageVersion , as : 'package_version' , attributes : [ 'package_name' , 'version' ] , where : packageCriteria }
173
- ] ,
173
+ ]
174
174
} ;
175
175
176
176
return Promise . join (
177
177
Topic . findAll ( params ) ,
178
178
Topic . findByAliasInPackage ( packageName , name , version ) ,
179
179
function ( topics , aliasesTopics ) {
180
180
return topics . concat ( aliasesTopics ) . sort ( PackageService . compareVersions ( 'desc' , function ( topic ) {
181
- return topic . package_version . version } )
181
+ return topic . package_version . version ;
182
+ } )
182
183
) [ 0 ] ;
183
184
} ) ;
184
185
} ,
@@ -188,7 +189,7 @@ module.exports = {
188
189
package_name : packageName
189
190
} ;
190
191
191
- if ( version ) { //if version is specified, find in this one
192
+ if ( version ) { // if version is specified, find in this one
192
193
packageCriteria . version = version ;
193
194
}
194
195
@@ -200,16 +201,19 @@ module.exports = {
200
201
} ;
201
202
202
203
return Topic . findAll ( params ) . then ( function ( topicInstances ) {
203
- if ( topicInstances ) return topicInstances . sort ( PackageService . compareVersions ( 'desc' , function ( topic ) {
204
- return topic . package_version . version } )
204
+ if ( topicInstances ) {
205
+ return topicInstances . sort ( PackageService . compareVersions ( 'desc' , function ( topic ) {
206
+ return topic . package_version . version ;
207
+ } )
205
208
) [ 0 ] ;
206
- else return topicInstances ;
209
+ }
210
+ return topicInstances ;
207
211
} ) ;
208
212
} ,
209
213
210
214
createWithRdFile : function ( opts ) {
211
215
var rdJSON = opts . input ;
212
- return sequelize . transaction ( function ( t ) {
216
+ return sequelize . transaction ( function ( t ) {
213
217
var attributes = [
214
218
'name' ,
215
219
'title' ,
@@ -225,11 +229,20 @@ module.exports = {
225
229
226
230
var topic = _ . pick ( rdJSON , attributes ) ;
227
231
232
+ if ( topic . usage !== null && typeof topic . usage === 'object' ) {
233
+ topic . usage = topic . usage . contents ;
234
+ }
235
+
236
+ topic . examples = sanitizeHtml ( topic . examples , {
237
+ allowedTags : [ 'a' ] ,
238
+ allowedAttributes : { }
239
+ } ) ;
240
+
228
241
var arrayToString = function ( val ) {
229
242
if ( val instanceof Array ) {
230
- if ( _ . isEmpty ( val ) ) return "" ;
231
- else return val . join ( " \n\n" ) ;
232
- } else return val ;
243
+ if ( _ . isEmpty ( val ) ) return '' ;
244
+ return val . join ( ' \n\n' ) ;
245
+ } return val ;
233
246
} ;
234
247
235
248
topic = _ . mapValues ( topic , arrayToString ) ;
@@ -245,14 +258,13 @@ module.exports = {
245
258
var packageVersion = {
246
259
package_name : opts . packageName ,
247
260
version : opts . packageVersion ,
248
- description : "" ,
249
- license : ""
261
+ description : '' ,
262
+ license : ''
250
263
} ;
251
264
252
265
return PackageVersion . upsertPackageVersion ( packageVersion , {
253
266
transaction : t
254
267
} ) . spread ( function ( version , created ) {
255
-
256
268
topic . package_version_id = version . id ;
257
269
258
270
@@ -281,12 +293,12 @@ module.exports = {
281
293
{ model : Alias , as : 'aliases' }
282
294
]
283
295
} ) ,
284
- Tag . bulkCreate ( keywordsRecords , { transaction :t , ignoreDuplicates : true } )
296
+ Tag . bulkCreate ( keywordsRecords , { transaction : t , ignoreDuplicates : true } )
285
297
. then ( function ( instances ) {
286
- var names = _ . map ( instances , function ( inst ) {
298
+ var names = _ . map ( instances , function ( inst ) {
287
299
return inst . name ;
288
300
} ) ;
289
- return Tag . findAll ( { where : { name : { $in : names } } , transaction :t } ) ;
301
+ return Tag . findAll ( { where : { name : { $in : names } } , transaction : t } ) ;
290
302
} ) ,
291
303
function ( instanceCreatedArray , keywordsInstances ) {
292
304
var topicInstance = instanceCreatedArray [ 0 ] ;
@@ -329,11 +341,9 @@ module.exports = {
329
341
topicInstance . setKeywords ( keywordsInstances , { transaction : t } ) ,
330
342
topicInstance . save ( { transaction : t } )
331
343
] ) . then ( _ . partial ( Topic . findOnePopulated , { id : topicInstance . id } , { transaction : t } ) ) ;
332
- } ) ;
344
+ } ) ;
333
345
} ) ;
334
-
335
346
} ) ;
336
-
337
347
}
338
348
}
339
349
}
0 commit comments