fix(agent): preserve preview URLs for collection repo models with flat heading structure

Three-part fix for enrich_hf_metadata failing to extract correct preview_url
from HuggingFace collection repos where models share flat heading levels:

1. _strip_standalone_images() now converts <img> tags to markdown image
   syntax ![alt](src) instead of stripping the URL entirely, so the LLM
   can still extract preview URLs.

2. _extract_section() uses a line-count-based forward window (stopping at
   <a id> anchors) for non-heading matches, instead of stopping at the
   very next heading. This prevents same-level sub-headings (# Download,
   # Trigger, # Sample prompt within a single model section) from
   truncating the window before sample images are included.

3. Post-processor preview fallback now filters gallery images to the
   model-specific README section before falling back to the repo-wide
   first image.
This commit is contained in:
Will Miao
2026-07-05 17:05:47 +08:00
parent 5494a70f40
commit 7b19bbb14e
4 changed files with 75 additions and 31 deletions

View File

@@ -803,10 +803,12 @@ pixel art sprite, game asset, transparent background
assert "## Gallery" in result
assert "Some text." in result
def test_html_img_tag_stripped(self):
def test_html_img_tag_converted_to_markdown_image(self):
"""``<img>`` converted to ``![](src)``, preserving URL for LLM."""
md = '## Preview\n<img src="https://cdn.hf.co/img.webp"></img>\n\nDescription.'
result = self._clean(md)
assert "cdn.hf.co" not in result
assert "![](https://cdn.hf.co/img.webp)" in result
assert "cdn.hf.co" in result # URL preserved for LLM extraction
assert "Description." in result
def test_inline_image_within_paragraph_preserved(self):

View File

@@ -242,11 +242,12 @@ After"""
cleaned = R.clean_readme_for_llm(text)
assert "./preview.png" in cleaned
def test_strips_html_img_tag(self, R):
"""``<img src="...">`` → stripped."""
def test_converts_html_img_tag_to_markdown_image(self, R):
"""``<img src="...">`` → ``![](src)`` preserving URL for LLM."""
text = 'before\n<img src="logo.png">\nafter'
cleaned = R.clean_readme_for_llm(text)
assert "logo.png" not in cleaned
assert "![](logo.png)" in cleaned
assert "logo.png" in cleaned # URL preserved for LLM extraction
def test_widget_stripped_frontmatter_preserved(self, R):
"""Widget YAML stripped but ``base_model:`` kept."""