Skip to content

Commit 84f32c4

Browse files
committed
feat(a11y): improve a11y on section heading anchor links
1 parent 1c3b53b commit 84f32c4

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

site/src/libs/astro.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import mdx from '@astrojs/mdx'
55
import sitemap from '@astrojs/sitemap'
66
import type { AstroIntegration } from 'astro'
77
import autoImport from 'astro-auto-import'
8-
import type { Element } from 'hast'
8+
import type { Element, Text } from 'hast'
99
import rehypeAutolinkHeadings from 'rehype-autolink-headings'
1010
import { getConfig } from './config'
1111
import { rehypeBsTable } from './rehype'
@@ -58,7 +58,30 @@ export function bootstrap(): AstroIntegration[] {
5858
rehypeAutolinkHeadings,
5959
{
6060
behavior: 'append',
61-
content: [{ type: 'text', value: ' ' }],
61+
content: (element: Element) => [
62+
{
63+
type: 'element',
64+
tagName: 'span',
65+
children: [
66+
{
67+
type: 'text',
68+
value: `Link to this section: ${(element.children[0] as Text).value}`
69+
}
70+
],
71+
properties: { class: 'visually-hidden' }
72+
},
73+
{
74+
type: 'element',
75+
tagName: 'span',
76+
children: [
77+
{
78+
type: 'text',
79+
value: ' #'
80+
}
81+
],
82+
properties: { ariaHidden: true }
83+
}
84+
],
6285
properties: { class: 'anchor-link' },
6386
test: (element: Element) => element.tagName.match(headingsRangeRegex)
6487
}

site/src/scss/_anchor.scss

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
opacity: 0;
77
@include transition(color .15s ease-in-out, opacity .15s ease-in-out);
88

9-
&::after {
10-
content: "#";
11-
}
12-
139
&:focus,
1410
&:hover,
1511
:hover > &,

0 commit comments

Comments
 (0)