fix(blog): table of contents scroll links
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 14s
Build & Deploy / 🧪 QA (push) Has started running
Build & Deploy / 🏗️ Build (push) Has been cancelled
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled

Fixes an issue where TOC links wouldn't scroll due to

ID generation mismatches on MDX headers containing

formatted text or German umlauts.
This commit is contained in:
2026-02-21 21:42:13 +01:00
parent a5384134e7
commit 36455ef479
2 changed files with 76 additions and 27 deletions

View File

@@ -10,6 +10,7 @@ import PowerCTA from '@/components/blog/PowerCTA';
import StickyNarrative from '@/components/blog/StickyNarrative';
import TechnicalGrid from '@/components/blog/TechnicalGrid';
import ComparisonGrid from '@/components/blog/ComparisonGrid';
import { generateHeadingId, getTextContent } from '@/lib/blog';
export const mdxComponents = {
VisualLinkPreview,
@@ -36,17 +37,28 @@ export const mdxComponents = {
className="inline-flex items-center gap-3 px-6 py-3 bg-primary text-white font-bold rounded-xl hover:bg-accent hover:text-primary-dark transition-all duration-300 no-underline my-8 group shadow-lg hover:shadow-xl hover:-translate-y-1"
>
<svg className="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"
/>
</svg>
<span>{children}</span>
<span className="text-xs opacity-50 font-normal group-hover:opacity-100 transition-opacity">(PDF)</span>
<span className="text-xs opacity-50 font-normal group-hover:opacity-100 transition-opacity">
(PDF)
</span>
</a>
);
}
if (href?.startsWith('/')) {
return (
<Link href={href} {...props} className="text-primary font-medium hover:underline decoration-2 underline-offset-2 transition-all">
<Link
href={href}
{...props}
className="text-primary font-medium hover:underline decoration-2 underline-offset-2 transition-all"
>
{children}
</Link>
);
@@ -61,18 +73,19 @@ export const mdxComponents = {
>
{children}
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14" />
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14"
/>
</svg>
</a>
);
},
img: (props: any) => (
<AnimatedImage src={props.src} alt={props.alt} />
),
img: (props: any) => <AnimatedImage src={props.src} alt={props.alt} />,
h2: ({ children, ...props }: any) => {
const id = typeof children === 'string'
? children.toLowerCase().replace(/[^\w\s-]/g, '').replace(/\s+/g, '-')
: props.id;
const id = props.id || generateHeadingId(getTextContent(children));
return (
<SplitHeading {...props} id={id} className="mt-16 mb-6 pb-3 border-b-2 border-primary/20">
{children}
@@ -80,9 +93,7 @@ export const mdxComponents = {
);
},
h3: ({ children, ...props }: any) => {
const id = typeof children === 'string'
? children.toLowerCase().replace(/[^\w\s-]/g, '').replace(/\s+/g, '-')
: props.id;
const id = props.id || generateHeadingId(getTextContent(children));
return (
<h3 {...props} id={id} className="text-2xl font-bold text-text-primary mt-12 mb-4">
{children}
@@ -108,17 +119,22 @@ export const mdxComponents = {
<li {...props} className="text-lg text-text-secondary flex items-start gap-3">
<span className="text-primary mt-1.5 flex-shrink-0">
<svg className="w-5 h-5" fill="currentColor" viewBox="0 0 20 20">
<path fillRule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clipRule="evenodd" />
<path
fillRule="evenodd"
d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z"
clipRule="evenodd"
/>
</svg>
</span>
<span className="flex-1">{children}</span>
</li>
),
blockquote: ({ children, ...props }: any) => (
<blockquote {...props} className="my-8 pl-6 border-l-4 border-primary bg-neutral-light/30 py-4 pr-6 rounded-r-lg">
<div className="text-lg text-text-primary italic">
{children}
</div>
<blockquote
{...props}
className="my-8 pl-6 border-l-4 border-primary bg-neutral-light/30 py-4 pr-6 rounded-r-lg"
>
<div className="text-lg text-text-primary italic">{children}</div>
</blockquote>
),
strong: ({ children, ...props }: any) => (
@@ -144,7 +160,10 @@ export const mdxComponents = {
</div>
),
thead: ({ children, ...props }: any) => (
<thead {...props} className="bg-neutral-50 text-text-primary font-semibold border-b border-neutral-200">
<thead
{...props}
className="bg-neutral-50 text-text-primary font-semibold border-b border-neutral-200"
>
{children}
</thead>
),

View File

@@ -109,7 +109,12 @@ export async function getAllPostsMetadata(locale: string): Promise<Partial<PostM
export async function getAdjacentPosts(
slug: string,
locale: string,
): Promise<{ prev: PostMdx | null; next: PostMdx | null; isPrevRandom?: boolean; isNextRandom?: boolean }> {
): Promise<{
prev: PostMdx | null;
next: PostMdx | null;
isPrevRandom?: boolean;
isNextRandom?: boolean;
}> {
const posts = await getAllPosts(locale);
const currentIndex = posts.findIndex((post) => post.slug === slug);
@@ -127,7 +132,7 @@ export async function getAdjacentPosts(
let isPrevRandom = false;
const getRandomPost = (excludeSlugs: string[]) => {
const available = posts.filter(p => !excludeSlugs.includes(p.slug));
const available = posts.filter((p) => !excludeSlugs.includes(p.slug));
if (available.length === 0) return null;
return available[Math.floor(Math.random() * available.length)];
};
@@ -154,17 +159,42 @@ export function getReadingTime(content: string): number {
return Math.ceil(minutes);
}
export function generateHeadingId(text: string): string {
let id = text.toLowerCase();
id = id.replace(/ä/g, 'ae');
id = id.replace(/ö/g, 'oe');
id = id.replace(/ü/g, 'ue');
id = id.replace(/ß/g, 'ss');
id = id.replace(/[*_`]/g, '');
id = id.replace(/[^\w\s-]/g, '');
id = id
.replace(/\s+/g, '-')
.replace(/-+/g, '-')
.replace(/^-+|-+$/g, '');
return id || 'heading';
}
export function getTextContent(node: any): string {
if (typeof node === 'string') return node;
if (typeof node === 'number') return node.toString();
if (Array.isArray(node)) return node.map(getTextContent).join('');
if (node && typeof node === 'object' && node.props && node.props.children) {
return getTextContent(node.props.children);
}
return '';
}
export function getHeadings(content: string): { id: string; text: string; level: number }[] {
const headingLines = content.split('\n').filter((line) => line.match(/^#{2,3}\s/));
return headingLines.map((line) => {
const level = line.match(/^#+/)?.[0].length || 0;
const text = line.replace(/^#+\s/, '').trim();
const id = text
.toLowerCase()
.replace(/[^\w\s-]/g, '')
.replace(/\s+/g, '-');
const rawText = line.replace(/^#+\s/, '').trim();
const cleanText = rawText.replace(/[*_`]/g, '');
const id = generateHeadingId(cleanText);
return { id, text, level };
return { id, text: cleanText, level };
});
}