Skip to content

Commit

Permalink
Merge pull request #45 from gogglesgogs:blog-headings-link
Browse files Browse the repository at this point in the history
Add permalinks to blog headings
  • Loading branch information
gogglesgogs authored Dec 1, 2024
2 parents a598d9d + a548ae4 commit 69d0dfc
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 0 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
"postcss": "^8.4.49",
"prettier": "^3.3.3",
"prettier-plugin-tailwindcss": "^0.6.8",
"rehype-autolink-headings": "^7.1.0",
"rehype-class-names": "^2.0.0",
"rehype-slug": "^6.0.0",
"remark-frontmatter": "^5.0.0",
"remark-gfm": "^4.0.0",
"remark-mdx-frontmatter": "^5.0.0",
Expand Down
132 changes: 132 additions & 0 deletions pnpm-lock.yaml

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

21 changes: 21 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,24 @@
::-webkit-scrollbar-track {
@apply bg-transparent;
}

.heading {
@apply relative text-nowrap;
}

.heading-link {
@apply absolute -left-[40px] top-1/2 -translate-y-1/2 pr-4 opacity-0 transition-opacity duration-300 ease-in-out;
}

.heading-icon {
@apply flex h-6 w-6 items-center justify-center rounded-md ring-1 ring-black dark:ring-white;
}

h1:hover .heading-link,
h2:hover .heading-link,
h3:hover .heading-link,
h4:hover .heading-link,
h5:hover .heading-link,
h6:hover .heading-link {
opacity: 1;
}
57 changes: 57 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import mdx from '@mdx-js/rollup';
import remarkGfm from 'remark-gfm';
import remarkFrontmatter from 'remark-frontmatter';
import remarkMdxFrontmatter from 'remark-mdx-frontmatter';
import rehypeSlug from 'rehype-slug';
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
import rehypeClassNames from 'rehype-class-names';

/** @type {import('vite').UserConfig} */
export default defineConfig({
Expand All @@ -16,6 +19,60 @@ export default defineConfig({
remarkFrontmatter,
[remarkMdxFrontmatter, { name: 'metadata' }],
],
rehypePlugins: [
rehypeSlug,
[
rehypeClassNames,
{
h1: 'heading',
h2: 'heading',
h3: 'heading',
h4: 'heading',
h5: 'heading',
h6: 'heading',
},
],
[
rehypeAutolinkHeadings,
{
properties: {
className: ['heading-link'],
ariaLabel: 'Link to this heading',
},
content: {
type: 'element',
tagName: 'span',
properties: {
className: ['heading-icon'],
},
children: [
{
type: 'element',
tagName: 'svg',
properties: {
width: '12',
height: '12',
fill: 'none',
'aria-hidden': 'true',
},
children: [
{
type: 'element',
tagName: 'path',
properties: {
d: 'M3.75 1v10M8.25 1v10M1 3.75h10M1 8.25h10',
stroke: 'currentColor',
'stroke-width': '1.5',
'stroke-linecap': 'round',
},
},
],
},
],
},
},
],
],
}),
},
react({ include: /\.(mdx|js|jsx|ts|tsx)$/ }),
Expand Down

0 comments on commit 69d0dfc

Please sign in to comment.