| 67 | }; |
| 68 | |
| 69 | function extractFirstImageFromHTML(html: string): ImageData | null { |
| 70 | const dom = new JSDOM(html); |
| 71 | const document = dom.window.document; |
| 72 | |
| 73 | // Try different strategies to find the first image |
| 74 | const imageSelectors = [ |
| 75 | 'figure img', // Case 1: Image inside figure |
| 76 | '.medium-feed-image img', // Case 2: Medium feed specific |
| 77 | 'img' // Case 3: Any image as fallback |
| 78 | ]; |
| 79 | |
| 80 | for (const selector of imageSelectors) { |
| 81 | const img = document.querySelector(selector) as HTMLImageElement; |
| 82 | if (img) { |
| 83 | return { |
| 84 | src: img.src, |
| 85 | alt: img.alt || '', // Use empty string if alt is not present |
| 86 | }; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return null; // Return null if no images are found |
| 91 | } |