chipzoller / hugo-clarity

A theme for Hugo based on VMware Clarity
Other
582 stars 267 forks source link

Use image "title" attribute for captions rather than "alt" attribute #219

Closed rootwork closed 2 years ago

rootwork commented 2 years ago

(Omitting issue template since this is more of a feature request than a bug report.)

Unless I'm mistaken, this change to #206 reverted to using the alt attribute in images to populate captions, rather than title, which is what I'd originally proposed in that PR (mentioned in item 3). Don't get me wrong, I'm really excited you accepted what I know was an enormous PR! And of course it's your decision whether to continue using alt rather than title for captions, but I want to make the case for why I think it should be the latter.

Just to be sure we're on the same page, an image in Markdown defines its attributes this way:

![alt text](/path/to/img.jpg "title text")

On using captions, WebAIM says it succinctly:

figure and figcaption allow a semantic association between a figure and the figure's caption. The figcaption may provide a summary of or additional information about the figure and/or relate the figure back to the containing document. However, any image within a figure must still have an alt attribute value that presents the alternative text of the image - this should not be conveyed via the figcaption.

(Emphasis mine.)

Other useful summaries making this same argument:

So alt text isn't really intended to be the same as a caption. And conversely, even if you don't want to display a caption, you should provide alt text unless it's a purely decorative image.

Summary

Deriving captions from title rather than alt:

Drawbacks

The one obvious downside is that sites that had been using alts for captions until now would suddenly have empty captions if you switched to basing it on title. I'd be up for coming up with a regex script to duplicate alt into title where title doesn't already exist, if that would help assuage that concern.

I suppose it's also a bit more work to type

![alt text, possibly empty](/path/to/img.jpg "intended caption text")

rather than

![intended caption text](/path/to/img.jpg)

but I think that's outweighed by the positives.

chipzoller commented 2 years ago

I think you make a good case and I would like us to align on best practices in the industry. Although I would have to personally go to all of my blog posts (since I use this theme) and make the change, I'm glad to do so. But other than a practical recommendation, what is your suggestion here? Is this in need of code or just documentation?

rootwork commented 2 years ago

The code piece is that the JS is grabbing the content of the alt attribute to populate the captions, so that would have to change to grabbing title instead. I can pull that part out from the original version of the PR and create a new PR if you agree.

Do you want to include some sort of script to auto-duplicate alts to empty titles, or is that too much?

chipzoller commented 2 years ago

cc @onweru

onweru commented 2 years ago

@rootwork, you make a good point. I noticed the change when I reverted but wasn't sure exactly why you did it. Umh, I would suggest that we proceed as follows.

  1. If the title attribute is provided, use it as the caption
  2. Else, use the alt as the caption

Option 1 is of course the ideal, because it follows the industry spec. However, not everyone is aware. I reckon most people don't even add a title attribute in their markdown.

This approach would be backwards compatible.

For the actual implementation, the alt would be the default and the title would be used only where it's defined

let caption = image.title.trim().length ? image.title : image.alt;
rootwork commented 2 years ago

I think that makes sense, yes!

Do you all want any documentation updates showing this? If so I can do a doc-only PR.

onweru commented 2 years ago

@rootwork, please go ahead. Thank you.