Skip to content

Commit 5d79f39

Browse files
authored
add anchor links to headings (#303)
# Description - feat(headings): add anchor links to headings ## Screenshots | before | after | |--------|-------| | ![image](https://user-images.githubusercontent.com/2574275/219791007-b14d54a0-4c2f-4432-90c5-fd586df4c76e.png) | ![image](https://user-images.githubusercontent.com/2574275/219790961-9b1daaf4-99fc-470c-a20b-0116cfb8bfdc.png) |
1 parent f4ff8f8 commit 5d79f39

4 files changed

Lines changed: 242 additions & 122 deletions

File tree

astro.config.mjs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,24 @@ import image from "@astrojs/image";
66
import mdx from "@astrojs/mdx";
77

88
import sitemap from "@astrojs/sitemap";
9+
import rehypeAutolinkHeadings from "rehype-autolink-headings";
10+
import { h } from 'hastscript';
11+
12+
const AnchorLinkIcon = h(
13+
'svg',
14+
{
15+
width: 16,
16+
height: 16,
17+
version: 1.1,
18+
viewBox: '0 0 16 16',
19+
xlmns: 'http://www.w3.org/2000/svg',
20+
},
21+
h('path', {
22+
fillRule: 'evenodd',
23+
fill: 'currentcolor',
24+
d: 'M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z',
25+
})
26+
);
927

1028
// https://astro.build/config
1129
export default defineConfig({
@@ -23,5 +41,29 @@ export default defineConfig({
2341
shikiConfig: {
2442
theme: 'dracula-soft',
2543
},
44+
rehypePlugins: [
45+
[
46+
rehypeAutolinkHeadings,
47+
{
48+
properties: {
49+
class: 'anchorLink',
50+
},
51+
behavior: 'before',
52+
group: ({ tagName }) =>
53+
h(`div.headingWrapper.level-${tagName}.flex.flex-row.items-baseline.prose-a:mr-4`, {
54+
tabIndex: -1,
55+
}),
56+
content: () => [
57+
h(
58+
`span.anchor-icon.align-baseline`,
59+
{
60+
ariaHidden: 'true',
61+
},
62+
AnchorLinkIcon
63+
),
64+
],
65+
},
66+
],
67+
],
2668
},
2769
});

0 commit comments

Comments
 (0)