Skip to content

alt attribute on img tag is missing #1230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
reiz opened this issue Apr 1, 2025 · 1 comment
Open

alt attribute on img tag is missing #1230

reiz opened this issue Apr 1, 2025 · 1 comment

Comments

@reiz
Copy link

reiz commented Apr 1, 2025

The alt attribute on img tags is always missing. I have expected to set the alt attribute by adding a caption (figcaption). But that's not happening. Especially for SEO optimised sites the alt attribute is very important. How can I set it? Anny help is appreciated!

@reiz
Copy link
Author

reiz commented Apr 1, 2025

Currently I'm solving it like this:

<% xml = Nokogiri::HTML5.fragment blog.content %>
<% xml.xpath(".//img").each do |img| %>
    <% figcaption = img.parent.xpath("figcaption").first %>
    <% next if figcaption.nil? || figcaption.text.to_s.strip.empty? %>
    <% img.set_attribute("alt", figcaption.text.to_s.strip) %>
<% end %>
<%= xml.to_s.html_safe %>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant