| 
									
										
										
										
											2018-05-15 06:12:05 +01:00
										 |  |  | import Parser from 'rss-parser'; | 
					
						
							|  |  |  | import _ from 'lodash'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const parser = new Parser(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const mediumFeed = 'https://medium.freecodecamp.org/feed'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getExtract(str) { | 
					
						
							|  |  |  |   return str.slice(0, str.indexOf('</p>') + 4); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function addResponsiveClass(str) { | 
					
						
							| 
									
										
										
										
											2019-02-06 14:19:58 +00:00
										 |  |  |   return str.replace(/<img/g, '<img class="img-responsive"'); | 
					
						
							| 
									
										
										
										
											2018-05-15 06:12:05 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function getMediumFeed() { | 
					
						
							|  |  |  |   return new Promise((resolve, reject) => { | 
					
						
							|  |  |  |     parser.parseURL(mediumFeed, (err, feed) => { | 
					
						
							|  |  |  |       if (err) { | 
					
						
							|  |  |  |         reject(err); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const items = feed.items | 
					
						
							| 
									
										
										
										
											2019-02-18 19:32:49 +00:00
										 |  |  |         .map(item => | 
					
						
							|  |  |  |           _.pick(item, ['title', 'link', 'isoDate', 'content:encoded']) | 
					
						
							| 
									
										
										
										
											2018-05-15 06:12:05 +01:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2019-02-18 19:32:49 +00:00
										 |  |  |         .map(item => ({ | 
					
						
							|  |  |  |           ...item, | 
					
						
							|  |  |  |           extract: getExtract(item['content:encoded']) | 
					
						
							|  |  |  |         })) | 
					
						
							| 
									
										
										
										
											2018-05-15 06:12:05 +01:00
										 |  |  |         .map(item => _.omit(item, ['content:encoded'])) | 
					
						
							| 
									
										
										
										
											2019-02-18 19:32:49 +00:00
										 |  |  |         .map(item => ({ ...item, extract: addResponsiveClass(item.extract) })); | 
					
						
							| 
									
										
										
										
											2018-05-15 06:12:05 +01:00
										 |  |  |       resolve(items); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } |