Skip to content

Commit 6501a2c

Browse files
committed
Add links to headings
1 parent 21a16a9 commit 6501a2c

4 files changed

Lines changed: 74 additions & 5 deletions

File tree

astro.config.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { RehypePlugin } from "@astrojs/markdown-remark";
22
import type { AstroIntegration, AstroUserConfig } from "astro";
3-
import type { Options } from "rehype-class-names";
3+
import type { Options as AutolinkHeadingsOptions } from "rehype-autolink-headings";
4+
import type { Options as ClassNamesOptions } from "rehype-class-names";
45
import type {
56
LegacyAsyncImporter,
67
LegacySharedOptions,
@@ -15,21 +16,23 @@ import path from "node:path";
1516
import nodeUrl from "node:url";
1617
import util from "node:util";
1718

19+
import { rehypeHeadingIds } from "@astrojs/markdown-remark";
1820
import mdx from "@astrojs/mdx";
19-
// @ts-expect-error There's no type declaration but it exists.
21+
// @ts-expect-error - There's no type declaration but it exists.
2022
import remarkA11yEmoji from "@fec/remark-a11y-emoji";
2123
import { addExtension, createFilter, dataToEsm } from "@rollup/pluginutils";
2224
import compress from "astro-compress";
2325
import { walk } from "estree-walker";
2426
import findCacheDirectory from "find-cache-dir";
2527
import gifsicle from "gifsicle";
2628
import { customAlphabet } from "nanoid";
29+
import rehypeAutolinkHeadings from "rehype-autolink-headings";
2730
import rehypeClassNames from "rehype-class-names";
2831
import sharp from "sharp";
2932
sharp.cache(false);
3033

3134
//==================================================
32-
// Astro - Shiki and rehype
35+
// Astro - Markdown transformers and plugins
3336
//==================================================
3437

3538
const classNamesTransformer: ShikiTransformer = {
@@ -41,7 +44,7 @@ const classNamesTransformer: ShikiTransformer = {
4144
node.properties.class = "block-code-token";
4245
}
4346
};
44-
const classNamesPlugin: [RehypePlugin, Options] = [
47+
const classNamesPlugin: [RehypePlugin, ClassNamesOptions] = [
4548
rehypeClassNames,
4649
{ ":not(pre) > code": "inline-code" }
4750
];
@@ -103,6 +106,11 @@ const themeTransformer: ShikiTransformer = {
103106
}
104107
};
105108

109+
const autolinkHeadingsPlugin: [RehypePlugin, AutolinkHeadingsOptions] = [
110+
rehypeAutolinkHeadings,
111+
{ behavior: "wrap" }
112+
];
113+
106114
//==================================================
107115
// Astro - Integrations
108116
//==================================================
@@ -402,7 +410,7 @@ export default <AstroUserConfig>{
402410
transformers: [classNamesTransformer, themeTransformer]
403411
},
404412
remarkPlugins: [remarkA11yEmoji],
405-
rehypePlugins: [classNamesPlugin]
413+
rehypePlugins: [classNamesPlugin, rehypeHeadingIds, autolinkHeadingsPlugin]
406414
},
407415
integrations: [mdx(), optimizeImagesIntegration, compress({ Image: false })],
408416
vite: { css: { preprocessorOptions: { scss } }, plugins: [generateIdsPlugin] }

package-lock.json

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"find-cache-dir": "^5.0.0",
1818
"gifsicle": "^7.0.1",
1919
"nanoid": "^5.0.6",
20+
"rehype-autolink-headings": "^7.1.0",
2021
"rehype-class-names": "^2.0.0",
2122
"sass": "^1.62.1",
2223
"sharp": "^0.33.2"

src/styles/article.scss

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
// Inspired from MDN's heading link styles
2+
h2 a {
3+
text-decoration: none;
4+
5+
&::before {
6+
$hashSize: v-size(3, "em");
7+
8+
content: "#";
9+
display: inline-block;
10+
width: $hashSize;
11+
margin-left: -#{$hashSize};
12+
color: var(--c-accent-1);
13+
font-size: $hashSize;
14+
visibility: hidden;
15+
}
16+
17+
&:hover {
18+
text-decoration: underline solid;
19+
20+
&::before {
21+
visibility: visible;
22+
}
23+
}
24+
}
25+
126
article {
227
padding-bottom: v-size(10);
328

@@ -12,6 +37,7 @@ article {
1237
list-style: circle inside;
1338
}
1439

40+
// Inspired from GitHub's inline code styles
1541
.inline-code {
1642
padding: 0.2em 0.4em;
1743
border-radius: 6px;

0 commit comments

Comments
 (0)