1
1
import { parseString } from "xml2js"
2
2
3
- import type { AtomResult , RSSChannel , RSSItem , RSSResult } from "../types"
3
+ import type {
4
+ AtomElement ,
5
+ AtomResult ,
6
+ RSSChannel ,
7
+ RSSItem ,
8
+ RSSResult ,
9
+ } from "../types"
4
10
import { isValidDate } from "../utils/date"
5
11
6
12
/**
@@ -71,17 +77,13 @@ export const fetchRSS = async (xmlUrl: string | string[]) => {
71
77
} )
72
78
// Map to RSSItem object
73
79
. map ( ( entry ) => {
74
- const getImgSrc = ( ) : string => {
75
- const imgRegEx = / h t t p s ? : \/ \/ [ ^ " ] * ?\. ( j p e ? g | p n g | w e b p ) / g
76
- const content = entry . content ?. [ 0 ] ?. _ || ""
77
- const summary = entry . summary ?. [ 0 ] ?. _ || ""
78
- const contentMatch = content . match ( imgRegEx )
79
- const summaryMatch = summary . match ( imgRegEx )
80
- if ( contentMatch ) return contentMatch [ 0 ]
81
- if ( summaryMatch ) return summaryMatch [ 0 ]
82
- return feedImage || ""
80
+ const getString = ( el ?: AtomElement [ ] ) : string => {
81
+ if ( ! el ) return ""
82
+ const [ firstEl ] = el
83
+ if ( typeof firstEl === "string" ) return firstEl
84
+ return firstEl . _ || ""
83
85
}
84
- const getLink = ( ) : string => {
86
+ const getHref = ( ) : string => {
85
87
if ( ! entry . link ) {
86
88
console . warn ( `No link found for RSS url: ${ url } ` )
87
89
return ""
@@ -90,15 +92,18 @@ export const fetchRSS = async (xmlUrl: string | string[]) => {
90
92
if ( typeof link === "string" ) return link
91
93
return link . $ . href || ""
92
94
}
93
- const getTitle = ( ) : string => {
94
- const title = entry . title [ 0 ]
95
- if ( typeof title === "string" ) return title
96
- return title . _ || ""
95
+ const getImgSrc = ( ) : string => {
96
+ const imgRegEx = / h t t p s ? : \/ \/ [ ^ " ] * ?\. ( j p e ? g | p n g | w e b p ) / g
97
+ const contentMatch = getString ( entry . content ) . match ( imgRegEx )
98
+ if ( contentMatch ) return contentMatch [ 0 ]
99
+ const summaryMatch = getString ( entry . summary ) . match ( imgRegEx )
100
+ if ( summaryMatch ) return summaryMatch [ 0 ]
101
+ return feedImage || ""
97
102
}
98
103
return {
99
104
pubDate : entry . updated [ 0 ] ,
100
- title : getTitle ( ) ,
101
- link : getLink ( ) ,
105
+ title : getString ( entry . title ) ,
106
+ link : getHref ( ) ,
102
107
imgSrc : getImgSrc ( ) ,
103
108
source,
104
109
sourceUrl,
0 commit comments