Skip to content

Commit 5f5c9f4

Browse files
authored
Improve meta image selection (#170)
Signed-off-by: Glenn Jocher <[email protected]>
1 parent 4b1b534 commit 5f5c9f4

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

plugin/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
22

3-
__version__ = "0.2.0"
3+
__version__ = "0.2.1"
44

55
from .main import MetaPlugin
66
from .postprocess import postprocess_site

plugin/processor.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)