MCPcopy Create free account
hub / github.com/bxcodec/github-readme-medium-recent-article / extractFirstImageFromHTML

Function extractFirstImageFromHTML

app/util/medium.ts:69–91  ·  view source on GitHub ↗
(html: string)

Source from the content-addressed store, hash-verified

67};
68
69function 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}

Callers 1

getArticleFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected