Skip to content

Commit b65073a

Browse files
committed
chore: implemented custom astro components for mdx
1 parent b4722aa commit b65073a

File tree

7 files changed

+97
-3
lines changed

7 files changed

+97
-3
lines changed

astro.config.mjs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { rehypeHeadingIds } from "@astrojs/markdown-remark";
1313

1414
import sectionize from "@hbsnow/rehype-sectionize";
1515

16+
import rehypeShiftHeading from "rehype-shift-heading";
17+
1618
// https://astro.build/config
1719
export default defineConfig({
1820
site: "https://ringoldsdev.github.io/",
@@ -30,8 +32,20 @@ export default defineConfig({
3032
rehypePlugins: [
3133
rehypeHeadingIds,
3234
sectionize,
33-
rehypeAutolinkHeadings,
34-
// [rehypeAutolinkHeadings, { behavior: "wrap" }],
35+
[rehypeShiftHeading, { shift: 1 }],
36+
// [
37+
// rehypeAutolinkHeadings,
38+
// {
39+
// behavior: "append",
40+
// content: {
41+
// type: Node,
42+
// value: `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="size-6">
43+
// <path strokeLinecap="round" strokeLinejoin="round" d="M13.19 8.688a4.5 4.5 0 0 1 1.242 7.244l-4.5 4.5a4.5 4.5 0 0 1-6.364-6.364l1.757-1.757m13.35-.622 1.757-1.757a4.5 4.5 0 0 0-6.364-6.364l-4.5 4.5a4.5 4.5 0 0 0 1.242 7.244" />
44+
// </svg>
45+
// `,
46+
// },
47+
// },
48+
// ],
3549
],
3650
},
3751
});

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
"alpinejs": "^3.14.9",
2222
"astro": "^5.7.12",
2323
"rehype-autolink-headings": "^7.1.0",
24+
"rehype-shift-heading": "^2.0.0",
2425
"rehype-slug": "^6.0.0",
2526
"remark-toc": "^9.0.0",
27+
"slugify": "^1.6.6",
2628
"tailwindcss": "^4.1.5"
2729
},
2830
"devDependencies": {

pnpm-lock.yaml

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

src/components/h2.astro

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
import slugify from 'slugify';
3+
4+
const id = await Astro.slots.render("default").then((content) => {
5+
return slugify(content, { lower: true });
6+
});
7+
8+
---
9+
10+
<h2 id={id}>
11+
<slot />
12+
<a href=`#${id}`>@</a>
13+
</h2>

src/components/h3.astro

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
import slugify from 'slugify';
3+
4+
const id = await Astro.slots.render("default").then((content) => {
5+
return slugify(content, { lower: true });
6+
});
7+
8+
---
9+
10+
<h3 id={id}>
11+
<slot />
12+
<a href=`#${id}`>@</a>
13+
</h3>

src/components/h4.astro

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
import slugify from 'slugify';
3+
4+
const id = await Astro.slots.render("default").then((content) => {
5+
return slugify(content, { lower: true });
6+
});
7+
8+
---
9+
10+
<h4 id={id}>
11+
<slot />
12+
<a href=`#${id}`>@</a>
13+
</h4>

src/pages/blog/[id].astro

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
import { getCollection, render } from 'astro:content';
33
import BaseLayout from "../../layouts/BaseLayout.astro";
44
5+
import h2 from '../../components/H2.astro';
6+
import h3 from '../../components/h3.astro';
7+
import h4 from '../../components/h4.astro';
8+
59
export async function getStaticPaths() {
610
const posts = await getCollection('blog');
711
return posts.map(post => ({
@@ -13,6 +17,12 @@ export async function getStaticPaths() {
1317
const { post } = Astro.props;
1418
const { Content, headings } = await render(post);
1519
20+
const components = {
21+
h2,
22+
h3,
23+
h4
24+
};
25+
1626
---
1727

1828
<!-- https://medium.com/@rezahedi/how-to-build-table-of-contents-in-astro-and-sectionize-the-markdown-content-78bee84e6a7f -->
@@ -34,7 +44,7 @@ const { Content, headings } = await render(post);
3444
{post.data.description}
3545
</p>
3646

37-
<Content/>
47+
<Content components={components}/>
3848
</article>
3949
</div>
4050
</BaseLayout>

0 commit comments

Comments
 (0)