1
0
mirror of synced 2025-12-21 19:06:49 -05:00
Files
docs/lib/render-content/plugins/permalinks.js
Kevin Heis d5281724e2 Update permalinks for accessibility (#36714)
Co-authored-by: Peter Bengtsson <mail@peterbe.com>
2023-05-01 14:03:02 +00:00

24 lines
632 B
JavaScript

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' }, [' #']),
]),
]
})
}
}