Skip to content

Commit

Permalink
Remove newlines in image alt_text
Browse files Browse the repository at this point in the history
  • Loading branch information
MauroDruwel committed Jan 4, 2025
1 parent afda281 commit dbf0902
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/markitdown/_markitdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,7 @@ def convert_image(self, image, output_dir: str) -> dict:
"""Handles image extraction and saving with collision avoidance and length limits."""
os.makedirs(output_dir, exist_ok=True)

image.alt_text = image.alt_text.replace("\n", " ")
raw_name = image.alt_text or f"image_{hash(image)}"
sanitized_name = self.sanitize_filename(raw_name)
truncated_name = self.truncate_filename(sanitized_name, 251, ".png")
Expand All @@ -760,7 +761,7 @@ def convert_image(self, image, output_dir: str) -> dict:
with image.open() as image_bytes:
with open(image_path, "wb") as img_file:
img_file.write(image_bytes.read())
return {"src": image_path}
return {"src": image_path, "alt": image.alt_text}
except Exception:
# Return an empty src if saving fails
return {"src": ""}
Expand Down

0 comments on commit dbf0902

Please sign in to comment.