4

Accessible Emojis with 11ty

 3 years ago
source link: https://hugogiraudel.com/2021/01/02/accessible-emojis-with-11ty/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Accessible Emojis with 11tySkip to main content

January 2nd, 2021 · ~2 minutes

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 => `<span role="img">${match}</span>`
)
}

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
? `<span role="img" aria-label="${label}" title="${label}">${emoji}</span>`
: 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!


Recommend

  • 3

    11ty Tutorial: Cranking Your Jamstack Blog Up to 11! November 12, 2020 ...

  • 19
    • www.aleksandrhovhannisyan.com 2 years ago
    • Cache

    11ty: The Good, the Bad, and the... Possum?

    11ty: The Good, the Bad, and the... Possum? For two years, my blog ran on Jekyll, one of the oldest and most popular static site generators around. Jekyll is often listed alongside other static site generators like Hugo, Gats...

  • 4
    • www.aleksandrhovhannisyan.com 2 years ago
    • Cache

    Automate Netlify Redirects with 11ty

    Automate Netlify Redirects with 11ty Proper 301 redirects are essential for SEO, but not all frameworks and hosting services provide a straightforward way to set them up. Most static site generators like Gatsby and Jekyll req...

  • 2
    • www.aleksandrhovhannisyan.com 2 years ago
    • Cache

    Passing Object Arguments to Liquid Shortcodes in 11ty

    Passing Object Arguments to Liquid Shortcodes in 11ty If you're using Liquid as your template language in 11ty, you may have run into a minor annoyance with shortcodes. Whereas Nunjucks allows you to pass in named arguments t...

  • 3

    Ben Holmes (the creator of Slinkity) posted on Smashing Magazine today about the creation of the Slinkity plugin for 11ty. After reading it, I had to...

  • 12

    Building The SSG I’ve Always Wanted: An 11ty, Vite And JAM Sandwich ...

  • 7
    • www.aleksandrhovhannisyan.com 2 years ago
    • Cache

    Lazily Loading Images with the 11ty Image Plugin

    Lazily Loading Images with the 11ty Image Plugin Images are a core part of the web, but they don't come for free. While imagery can enrich your content and create a more engaging user experience, it can also slow down your si...

  • 3
    • www.aleksandrhovhannisyan.com 2 years ago
    • Cache

    Configuring Web Fonts in 11ty with Global Data

    Configuring Web Fonts in 11ty with Global Data My old strategy for managing web fonts on my site went something like this: Rather than linking to Google Fonts, I would self-host the fonts that I needed for improved performanc...

  • 8
    • www.aleksandrhovhannisyan.com 2 years ago
    • Cache

    Custom Markdown Components in 11ty

    Custom Markdown Components in 11ty Feb 03, 2022 Like many static site generators, 11ty automatically converts Markdown templates to HTML, allowing you to focus on authoring content in a plaintext fo...

  • 5
    • blog.jetbrains.com 2 years ago
    • Cache

    Using Storybook With 11ty

    All Things Web Using Storybook With 11ty

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK