Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import mdx from "@astrojs/mdx";

import sitemap from "@astrojs/sitemap";

import rehypeSlug from "rehype-slug";
import rehypeAutolinkHeadings from "rehype-autolink-headings";
import rehypeToc from "rehype-toc";

// https://astro.build/config
export default defineConfig({
site: 'https://thedevexchange.com',
Expand All @@ -22,6 +26,51 @@ export default defineConfig({

trailingSlash: 'always',

markdown: {
rehypePlugins: [
rehypeSlug,
[rehypeAutolinkHeadings, { behavior: 'append' }],
[rehypeToc, {
headings: ['h1', 'h2', 'h3'],
cssClasses: {
toc: 'toc-sidebar',
link: 'toc-link',
},
nav: false,
customizeTOC: (toc) => {
// Convert ol to ul recursively
const convertToUl = (node) => {
if (node.tagName === 'ol') {
node.tagName = 'ul';
}
if (node.children) {
node.children.forEach(convertToUl);
}
return node;
Comment on lines +42 to +49
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Consider keeping ol and hiding the numbering via CSS

};

const ulToc = convertToUl(toc);

// Add "Table of Contents" heading
return {
type: 'element',
tagName: 'nav',
properties: { className: ['toc-container', 'mt-5'] },
children: [
{
type: 'element',
tagName: 'div',
properties: { className: ['toc-header'] },
children: [{ type: 'text', value: 'Table of Contents', }]
},
ulToc
]
};
}
}],
],
},

image: {
service: {
entrypoint: 'astro/assets/services/sharp',
Expand Down
72 changes: 72 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"astro-expressive-code": "^0.41.2",
"react": "^19.1.0",
"react-dom": "^19.1.0",
"rehype-autolink-headings": "^7.1.0",
"rehype-slug": "^6.0.0",
"rehype-toc": "^3.0.2",
"tailwindcss": "^4.1.8"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/[slug].astro
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const canonicalURL = new URL(Astro.url.pathname, Astro.site);
</h1>

<p>By {post.data.author}</p>
<p class="mb-16">{dateFormat(post.data.released)} • {timeToRead(post)}min read</p>
<p>{dateFormat(post.data.released)} • {timeToRead(post)}min read</p>
<Prose>
<Content/>
</Prose>
Expand Down
87 changes: 87 additions & 0 deletions src/styles/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,91 @@
.prose .expressive-code {
margin-bottom: 1.25em;
}

/* Table of Contents Styles */
.toc-container {
border: var(--zag-stroke) solid;
border-color: var(--color-zag-dark-muted);
padding: 1rem;
margin: 1.5rem 0;
background-color: var(--color-neutral-50);

&:where(.dark, .dark *) {
border-color: var(--color-zag-light-muted);
background-color: var(--color-neutral-800);
}

/* Mobile: inline */
@media (max-width: 1279px) {
display: block;
width: 100%;
}

/* Desktop: sidebar on the right, positioned outside content */
@media (min-width: 90rem) {
position: fixed;
top: 5rem;
right: max(1rem, calc((100vw - 80rem) / 2 - 4rem));
width: 16rem;
margin: 0;
max-height: calc(100vh - 10rem);
overflow-y: auto;
}

/* Extra wide screens: position it further out */
@media (min-width: 96rem) {
right: max(2rem, calc((100vw - 90rem) / 2 - 1rem));
width: 18rem;
}
}

.toc-header {
font-weight: 600;
font-size: 0.875rem;
text-transform: uppercase;
letter-spacing: 0.05em;
margin-bottom: 0.75rem;
color: var(--color-zag-dark-muted);

&:where(.dark, .dark *) {
color: var(--color-zag-light-muted);
}
}

.toc-sidebar {
list-style: none;
padding-left: 0;
margin: 0;
font-size: 0.875rem;
line-height: 1.6;

li {
margin: 0.25rem 0;
}

ol {
list-style: none;
padding-left: 1rem;
margin: 0.25rem 0;
}
}

.toc-link {
color: var(--color-zag-dark);
text-decoration: none;
transition: color var(--zag-transition-duration) var(--zag-transition-timing-function);

&:hover {
color: var(--color-zag-accent-dark);
text-decoration: underline;
}

&:where(.dark, .dark *) {
color: var(--color-zag-light);

&:hover {
color: var(--color-zag-accent-light);
}
}
}
}