Skip to content

Commit

Permalink
add anchor links to headings (#303)
Browse files Browse the repository at this point in the history
# 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)
|
  • Loading branch information
oscard0m committed Apr 20, 2023
1 parent f4ff8f8 commit 5d79f39
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 122 deletions.
42 changes: 42 additions & 0 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@ import image from "@astrojs/image";
import mdx from "@astrojs/mdx";

import sitemap from "@astrojs/sitemap";
import rehypeAutolinkHeadings from "rehype-autolink-headings";
import { h } from 'hastscript';

const AnchorLinkIcon = h(
'svg',
{
width: 16,
height: 16,
version: 1.1,
viewBox: '0 0 16 16',
xlmns: 'http://www.w3.org/2000/svg',
},
h('path', {
fillRule: 'evenodd',
fill: 'currentcolor',
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',
})
);

// https://astro.build/config
export default defineConfig({
Expand All @@ -23,5 +41,29 @@ export default defineConfig({
shikiConfig: {
theme: 'dracula-soft',
},
rehypePlugins: [
[
rehypeAutolinkHeadings,
{
properties: {
class: 'anchorLink',
},
behavior: 'before',
group: ({ tagName }) =>
h(`div.headingWrapper.level-${tagName}.flex.flex-row.items-baseline.prose-a:mr-4`, {
tabIndex: -1,
}),
content: () => [
h(
`span.anchor-icon.align-baseline`,
{
ariaHidden: 'true',
},
AnchorLinkIcon
),
],
},
],
],
},
});

1 comment on commit 5d79f39

@vercel
Copy link

@vercel vercel bot commented on 5d79f39 Apr 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

getbem-github-io – ./

getbem-github-io-git-master-getbem.vercel.app
getbem-github-io-getbem.vercel.app
getbem-github-io.vercel.app

Please sign in to comment.