@@ -245,17 +245,29 @@ def process_html(
245245 if len (desc ) >= 10 :
246246 meta ["description" ] = desc
247247
248+ # ---------- IMAGE PICKING (body only, non-AVIF) ----------
248249 if add_image :
249- if first_image := soup .find ("img" ):
250- img_src = first_image .get ("src" , "" )
251- if img_src and (
252- img_src .startswith (("http://" , "https://" , "/" )) or not img_src .startswith (("javascript:" , "data:" ))
253- ):
254- meta ["image" ] = img_src
255- elif youtube_ids := get_youtube_video_ids (soup ):
256- meta ["image" ] = f"https://img.youtube.com/vi/{ youtube_ids [0 ]} /maxresdefault.jpg"
257- elif default_image :
258- meta ["image" ] = default_image
250+ search_root = soup .find ("article" , class_ = "md-content__inner" ) or soup .body or soup
251+
252+ image_src : str | None = None
253+ for img in search_root .find_all ("img" , src = True ):
254+ src = img ["src" ]
255+ lower = src .lower ()
256+ if not lower or ".avif" in lower :
257+ continue
258+ if lower .startswith (("javascript:" , "data:" )):
259+ continue
260+ image_src = src
261+ break
262+
263+ if not image_src and (youtube_ids := get_youtube_video_ids (soup )):
264+ image_src = f"https://img.youtube.com/vi/{ youtube_ids [0 ]} /maxresdefault.jpg"
265+ if not image_src and default_image :
266+ image_src = default_image
267+
268+ if image_src :
269+ meta ["image" ] = image_src
270+ # ---------------------------------------------------------
259271
260272 # Add meta tags to head
261273 if not soup .find ("meta" , attrs = {"name" : "title" }):
0 commit comments