@@ -14,6 +14,17 @@ export default class WebRss {
1414 } ) ) ,
1515 } ) ;
1616
17+ private imageUrlRegex = new RegExp (
18+ [
19+ "(http(s?):)" ,
20+ "([/|.\\w\\s-])*" ,
21+ "\\.(?:jpe?g|gif|png|webp)" ,
22+ ] . join ( "" ) ,
23+ "i"
24+ ) ;
25+
26+ private imageAltRegex = new RegExp ( / < i m g \b [ ^ > ] * ?(?: t i t l e = " ( [ ^ " ] + ) " | a l t = " ( [ ^ " ] + ) " ) [ ^ > ] * ?> / , "i" ) ;
27+
1728 async getLastPost ( url : string ) : Promise < ParsedEntry | undefined > {
1829 const feed = await this . getFeed ( url ) ;
1930 if ( ! feed ) {
@@ -63,6 +74,7 @@ export default class WebRss {
6374 author : entry . creator || entry . author || feed . title || null ,
6475 channel : feed . title || null ,
6576 image : entry . mediaThumbnail ?. url || this . extractImageFromEnclosure ( entry ) || this . extractFirstImageFromContent ( entry . content || "" ) || null ,
77+ imageAlt : this . extractFirstImageAltFromContent ( entry . content || "" ) || null ,
6678 postText : entry . contentSnippet || null ,
6779 postDescription : entry . summary || null ,
6880 } ;
@@ -76,14 +88,11 @@ export default class WebRss {
7688 }
7789
7890 private extractFirstImageFromContent ( content : string ) : string | null {
79- const exp = new RegExp (
80- [
81- "(http(s?):)" ,
82- "([/|.\\w\\s-])*" ,
83- "\\.(?:jpe?g|gif|png|webp)" ,
84- ] . join ( "" ) ,
85- "i"
86- ) ;
87- return content . match ( exp ) ?. [ 0 ] || null ;
91+ return content . match ( this . imageUrlRegex ) ?. [ 0 ] || null ;
92+ }
93+
94+ private extractFirstImageAltFromContent ( content : string ) : string | null {
95+ const match = content . match ( this . imageAltRegex ) ;
96+ return match ?. [ 1 ] || match ?. [ 2 ] || null ;
8897 }
8998}
0 commit comments