@@ -19,32 +19,41 @@ var stripHtml = function(str) {
19
19
return str . replace ( / < (?: .| \n ) * ?> / gm, '' ) ;
20
20
}
21
21
22
+ var parseRSS1 = function ( xmlObj , callback ) {
23
+ callback ( "RSS 1.0 parsing not yet implemented." )
24
+ }
25
+
26
+ var parseRSS2 = function ( xmlObj , callback ) {
27
+ var json = { feed : { entries : [ ] } } ;
28
+ var channel = xmlObj . rss . channel [ 0 ] ;
29
+ if ( channel [ 'atom:link' ] ) json . feed . feedUrl = channel [ 'atom:link' ] [ 0 ] . href ;
30
+ TOP_FIELDS . forEach ( function ( f ) {
31
+ if ( channel [ f ] ) json . feed [ f ] = channel [ f ] [ 0 ] ;
32
+ } )
33
+ var items = channel . item ;
34
+ ( items || [ ] ) . forEach ( function ( item ) {
35
+ var entry = { } ;
36
+ ITEM_FIELDS . forEach ( function ( f ) {
37
+ if ( item [ f ] ) entry [ f ] = item [ f ] [ 0 ] ;
38
+ } )
39
+ if ( item . description ) {
40
+ entry . content = item . description [ 0 ] ;
41
+ entry . contentSnippet = Entities . decode ( stripHtml ( entry . content ) ) ;
42
+ }
43
+ if ( item . guid ) {
44
+ entry . guid = item . guid [ 0 ] . _ ;
45
+ }
46
+ if ( item . category ) entry . categories = item . category ;
47
+ json . feed . entries . push ( entry ) ;
48
+ } )
49
+ callback ( null , json ) ;
50
+ }
51
+
22
52
Parser . parseString = function ( xml , callback ) {
23
53
XML2JS . parseString ( xml , function ( err , result ) {
24
54
if ( err ) throw err ;
25
- var json = { feed : { entries : [ ] } } ;
26
- var channel = result . rss . channel [ 0 ] ;
27
- if ( channel [ 'atom:link' ] ) json . feed . feedUrl = channel [ 'atom:link' ] [ 0 ] . href ;
28
- TOP_FIELDS . forEach ( function ( f ) {
29
- if ( channel [ f ] ) json . feed [ f ] = channel [ f ] [ 0 ] ;
30
- } )
31
- var items = channel . item ;
32
- ( items || [ ] ) . forEach ( function ( item ) {
33
- var entry = { } ;
34
- ITEM_FIELDS . forEach ( function ( f ) {
35
- if ( item [ f ] ) entry [ f ] = item [ f ] [ 0 ] ;
36
- } )
37
- if ( item . description ) {
38
- entry . content = item . description [ 0 ] ;
39
- entry . contentSnippet = Entities . decode ( stripHtml ( entry . content ) ) ;
40
- }
41
- if ( item . guid ) {
42
- entry . guid = item . guid [ 0 ] . _ ;
43
- }
44
- if ( item . category ) entry . categories = item . category ;
45
- json . feed . entries . push ( entry ) ;
46
- } )
47
- callback ( null , json ) ;
55
+ if ( result . rss && result . rss . $ . version && result . rss . $ . version . indexOf ( '2' ) === 0 ) return parseRSS2 ( result , callback ) ;
56
+ else return parseRSS1 ( result , callback ) ;
48
57
} ) ;
49
58
}
50
59
0 commit comments