1
0
mirror of synced 2025-12-22 03:16:52 -05:00

Update permalinks for accessibility (#36714)

Co-authored-by: Peter Bengtsson <mail@peterbe.com>
This commit is contained in:
Kevin Heis
2023-05-01 07:03:02 -07:00
committed by GitHub
parent 1c37cfa2c8
commit d5281724e2
28 changed files with 165 additions and 274 deletions

View File

@@ -0,0 +1,23 @@
import { visit } from 'unist-util-visit'
import { h } from 'hastscript'
const matcher = (node) =>
node.type === 'element' &&
['h1', 'h2', 'h3', 'h4', 'h5', 'h6'].includes(node.tagName) &&
node.properties?.id
export default function permalinks() {
return (tree) => {
visit(tree, matcher, (node) => {
const { id } = node.properties
const text = node.children
node.properties.tabIndex = -1
node.children = [
h('a', { class: 'permalink', href: `#${id}` }, [
...text,
h('span', { class: 'permalink-symbol', ariaHidden: 'true' }, [' #']),
]),
]
})
}
}