Skip to content

Commit

Permalink
feat: set aria-hidden: true to <figcaption> so that screen reader…
Browse files Browse the repository at this point in the history
…s don't even read `<img>`'s `alt` and `<figcaption>`
  • Loading branch information
akabekobeko committed Apr 9, 2021
1 parent b33537e commit 91bb30c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/plugins/figure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ const isImgProperty = (name: string): boolean => {
*/
const wrapFigureImg = (img: HastNode, parent: HastNode) => {
parent.tagName = 'figure';
parent.children.push(h('figcaption', img.properties.alt));
parent.children.push(
h('figcaption', { 'aria-hidden': 'true' }, [img.properties.alt]),
);

// Move to parent because `id` attribute is unique
if (img.properties.id) {
Expand Down
6 changes: 3 additions & 3 deletions tests/remark2rehype/figure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ it(
url: "./img.png"
alt: "caption"
`,
`<figure><img src="./img.png" alt="caption"><figcaption>caption</figcaption></figure>`,
`<figure><img src="./img.png" alt="caption"><figcaption aria-hidden="true">caption</figcaption></figure>`,
),
);

Expand Down Expand Up @@ -56,7 +56,7 @@ it(
alt: "caption"
`,
stripIndent`
<figure><img src="./img.png" alt="caption"><figcaption>caption</figcaption></figure>
<figure><img src="./img.png" alt="caption"><figcaption aria-hidden="true">caption</figcaption></figure>
<p>text <img src="./img.png" alt="caption"></p>
`,
),
Expand All @@ -75,6 +75,6 @@ it(
alt: "caption"
data: {"hProperties":{"id":"image","data-sample":"sample"}}
`,
`<figure id="image" title="title" data-sample="sample"><img src="./img.png" alt="caption" title="title" data-sample="sample"><figcaption>caption</figcaption></figure>`,
`<figure id="image" title="title" data-sample="sample"><img src="./img.png" alt="caption" title="title" data-sample="sample"><figcaption aria-hidden="true">caption</figcaption></figure>`,
),
);

0 comments on commit 91bb30c

Please sign in to comment.