Skip to content

Commit db0a353

Browse files
committed
fix table of contents anchor link navigation on Links page
1 parent afec5b5 commit db0a353

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

docs/working-notes/todo3.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ custom my-prose-links
500500
https://github.com/sindresorhus/github-markdown-css/issues/104
501501
https://github.com/sindresorhus/github-markdown-css
502502

503-
table of contents id links
503+
table of contents id links
504504
a href open in new tab
505505
fix links page links color for history back view transition
506506

@@ -513,5 +513,7 @@ ask about app start handler for log env vars
513513
fix image sizes for gallery and other
514514
update bg color meta tag
515515
fix links page on user dark mode preference text color
516+
517+
Links page open links in new tab
516518
------------
517519
```

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"dotenv": "^16.4.5",
4343
"feed": "^4.2.2",
4444
"giscus": "^1.5.0",
45+
"github-slugger": "^2.0.0",
4546
"mdast-util-to-string": "^4.0.0",
4647
"nanostores": "^0.10.3",
4748
"object-treeify": "^4.0.1",

src/components/Heading.astro

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
import { Icon } from 'astro-icon/components';
3+
4+
import GithubSlugger from 'github-slugger';
5+
6+
const slugger = new GithubSlugger();
7+
8+
const { as: Component, text } = Astro.props;
9+
10+
// id is without #
11+
const slug = slugger.slug(text);
12+
const href = `#${slug}`;
13+
---
14+
15+
<div class="flex gap-2 items-center group">
16+
<a id={slug} href={href} class="flex target:!outline-none">
17+
<Icon
18+
name="mdi:link-variant"
19+
class="inline-block h-5 w-5 text-transparent group-hover:text-captions"
20+
/>
21+
</a>
22+
<Component>
23+
<slot />
24+
</Component>
25+
</div>

src/pages/links.astro

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import '@/assets/styles/github-markdown.css';
55
import { Markdown } from 'astro-remote';
66
77
import List from '@/layouts/List.astro';
8+
import Heading from '@/components/Heading.astro';
89
import { BOOKMARKS } from '@/constants/data';
910
import { SELECTORS } from '@/constants/dom';
1011
import { getPageMetadata } from '@/utils/metadata';
@@ -16,8 +17,6 @@ const readmeContent = await fetch(README_RAW_URL).then((response) => response.te
1617
const metadata = getPageMetadata('lists/links');
1718
1819
const { GITHUB_MARKDOWN_BODY_ID } = SELECTORS;
19-
20-
// not generating id="..." links href="#..."
2120
---
2221

2322
<List {metadata}>
@@ -28,7 +27,11 @@ const { GITHUB_MARKDOWN_BODY_ID } = SELECTORS;
2827
>
2928
</p>
3029

31-
<Markdown content={readmeContent} sanitize={{ dropElements: ['h1'] }} />
30+
<Markdown
31+
content={readmeContent}
32+
sanitize={{ dropElements: ['h1'], allowComponents: true }}
33+
components={{ Heading }}
34+
/>
3235
</div>
3336
</List>
3437

0 commit comments

Comments
 (0)