Accessible Emojis with 11ty

As part of the A11y Advent calendar, we discussed emojis and how they are not always quite accessible by default despite being used a lot and being a key communication tool in this day and age. A couple days ago, I posted a tweet about how I found a cheap and lazy way to improve emojis’ accessibility in 11ty.

Quick reminder of what we should do, courtesy of Leonie Watson’s article on the matter:

span role="img" aria-label="Star" title="Star">⭐️span>

This is easy to do in content pages authored in HTML, but becomes more complicated in articles written in Markdown, let alone done retroactively on hundreds of pages. So the idea is to post-process the resulting HTML to wrap emojis with a span as shown above.

Fortunately, 11ty allows us to post-process HTML with transforms. They are very handy to, well, transform a template’s output, such as minifying the resulting HTML for instance.

Here, we want a transform that will:

  1. Find emojis in the HTML,
  2. Find the associated English name for these emojis,
  3. Wrap them in a span with the proper attributes.

Let’s start by creating the boilerplate for our transform:

eleventyConfig.addTransform('emojis', (content, outputPath) => {
return outputPath.endsWith('.html') ? wrapEmojis(content) : content
})

function wrapEmojis(content) {
// Our code here
}

Finding emojis is surprisingly easy thanks to Mathias Bynens’ emoji-regex. This package provides an automatically generated (long) regular expression to find emoji unicode sequences.

From there, we can already wrap our emojis:

// The package exports a function, not a regular expression, so we have to
// call it first to get the regular expression itself.
const emojiRegex = require('emoji-regex/RGI_Emoji')()

function wrapEmojis(content) {
return content.replace(
emojiRegex,
match => `${match}`
)
}

Now we need to figure out the English label a given emoji. It turns out that this is surprisingly difficult. Mathias Bynens explains why:

It’s trickier, as it’s not obvious what the expected output is for many emoji. Should you just use the Unicode names? What about sequences? etc.

Nevertheless, I found emoji-short-name, which is based on emoji-essential, which is a scrap of Unicode.org. This package gives us the English description of an emoji.

// The package exports a function, not a regular expression, so we have to
// call it first to get the regular expression itself.
const emojiRegex = require('emoji-regex/RGI_Emoji')()
const emojiShortName = require('emoji-short-name')

function wrapEmojis(content) {
return content.replace(emojiRegex, wrapEmoji)
}

function wrapEmoji(emoji) {
const label = emojiShortName[content]

return label
? `${label}" title="${label}">${emoji}`
: emoji
}

That’s about it! As I said, pretty cheap to implement. Now I’m going to be honest and don’t know how robust this solution is. Some emojis might be missing (especially when new ones get added) and some descriptions might be sub-optimal. Additionally, it doesn’t check whether an emoji is already properly wrapped, which could cause a double-wrap (although I’d say this could be fixed relatively easily I guess).

Still, it’s a pretty convenient way to make emojis a little more accessible with 11ty!

As part of the A11y Advent calendar, we discussed emojis and how they are not always quite accessible by default despite being used a lot and being a key communication tool in this day and age. A couple days ago, I posted a tweet about how I found a cheap and lazy way to improve emojis’ accessibility in 11ty.

Quick reminder of what we should do, courtesy of Leonie Watson’s article on the matter:

<span role="img" aria-label="Star" title="Star">⭐️span>

This is easy to do in content pages authored in HTML, but becomes more complicated in articles written in Markdown, let alone done retroactively on hundreds of pages. So the idea is to post-process the resulting HTML to wrap emojis with a span as shown above.

Fortunately, 11ty allows us to post-process HTML with transforms. They are very handy to, well, transform a template’s output, such as minifying the resulting HTML for instance.

Here, we want a transform that will:

  1. Find emojis in the HTML,
  2. Find the associated English name for these emojis,
  3. Wrap them in a span with the proper attributes.

Let’s start by creating the boilerplate for our transform:

eleventyConfig.addTransform('emojis', (content, outputPath) => {
return outputPath.endsWith('.html') ? wrapEmojis(content) : content
})

function wrapEmojis(content) {
// Our code here
}

Finding emojis is surprisingly easy thanks to Mathias Bynens’ emoji-regex. This package provides an automatically generated (long) regular expression to find emoji unicode sequences.

From there, we can already wrap our emojis:

// The package exports a function, not a regular expression, so we have to
// call it first to get the regular expression itself.
const emojiRegex = require('emoji-regex/RGI_Emoji')()

function wrapEmojis(content) {
return content.replace(
emojiRegex,
match => `${match}`
)
}

Now we need to figure out the English label a given emoji. It turns out that this is surprisingly difficult. Mathias Bynens explains why:

It’s trickier, as it’s not obvious what the expected output is for many emoji. Should you just use the Unicode names? What about sequences? etc.

Nevertheless, I found emoji-short-name, which is based on emoji-essential, which is a scrap of Unicode.org. This package gives us the English description of an emoji.

// The package exports a function, not a regular expression, so we have to
// call it first to get the regular expression itself.
const emojiRegex = require('emoji-regex/RGI_Emoji')()
const emojiShortName = require('emoji-short-name')

function wrapEmojis(content) {
return content.replace(emojiRegex, wrapEmoji)
}

function wrapEmoji(emoji) {
const label = emojiShortName[content]

return label
? `${label}" title="${label}">${emoji}`
: emoji
}

That’s about it! As I said, pretty cheap to implement. Now I’m going to be honest and don’t know how robust this solution is. Some emojis might be missing (especially when new ones get added) and some descriptions might be sub-optimal. Additionally, it doesn’t check whether an emoji is already properly wrapped, which could cause a double-wrap (although I’d say this could be fixed relatively easily I guess).

Still, it’s a pretty convenient way to make emojis a little more accessible with 11ty!


Print Share Comment Cite Upload Translate
APA
Hugo “Kitty” Giraudel | Sciencx (2024-03-29T11:02:40+00:00) » Accessible Emojis with 11ty. Retrieved from https://www.scien.cx/2021/01/02/accessible-emojis-with-11ty/.
MLA
" » Accessible Emojis with 11ty." Hugo “Kitty” Giraudel | Sciencx - Saturday January 2, 2021, https://www.scien.cx/2021/01/02/accessible-emojis-with-11ty/
HARVARD
Hugo “Kitty” Giraudel | Sciencx Saturday January 2, 2021 » Accessible Emojis with 11ty., viewed 2024-03-29T11:02:40+00:00,<https://www.scien.cx/2021/01/02/accessible-emojis-with-11ty/>
VANCOUVER
Hugo “Kitty” Giraudel | Sciencx - » Accessible Emojis with 11ty. [Internet]. [Accessed 2024-03-29T11:02:40+00:00]. Available from: https://www.scien.cx/2021/01/02/accessible-emojis-with-11ty/
CHICAGO
" » Accessible Emojis with 11ty." Hugo “Kitty” Giraudel | Sciencx - Accessed 2024-03-29T11:02:40+00:00. https://www.scien.cx/2021/01/02/accessible-emojis-with-11ty/
IEEE
" » Accessible Emojis with 11ty." Hugo “Kitty” Giraudel | Sciencx [Online]. Available: https://www.scien.cx/2021/01/02/accessible-emojis-with-11ty/. [Accessed: 2024-03-29T11:02:40+00:00]
rf:citation
» Accessible Emojis with 11ty | Hugo “Kitty” Giraudel | Sciencx | https://www.scien.cx/2021/01/02/accessible-emojis-with-11ty/ | 2024-03-29T11:02:40+00:00
https://github.com/addpipe/simple-recorderjs-demo