feat: complete MDX migration, stabilize environment & verify via E2E tests

Former-commit-id: ec3e64156a2e182535cbdcf0d975cd54603a517d
This commit is contained in:
2026-05-03 18:46:41 +02:00
parent e57661a586
commit b13f628b55
131 changed files with 1388 additions and 25288 deletions

View File

@@ -1,193 +1,76 @@
import { getPayload } from 'payload';
import configPromise from '@payload-config';
import fs from 'fs';
import path from 'path';
import matter from 'gray-matter';
import { mapSlugToFileSlug } from './slugs';
import { config } from '@/lib/config';
const CONTENT_DIR = path.join(process.cwd(), 'content');
export interface PageFrontmatter {
title: string;
excerpt: string;
featuredImage: string | null;
excerpt?: string;
featuredImage?: string | null;
focalX?: number;
focalY?: number;
layout?: 'default' | 'fullBleed';
public?: boolean;
description?: string;
}
export interface PageData {
slug: string;
frontmatter: PageFrontmatter;
content: any; // Lexical AST Document
}
function mapDoc(doc: any): PageData {
return {
slug: doc.slug,
frontmatter: {
title: doc.title,
excerpt: doc.excerpt || '',
featuredImage:
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
? doc.featuredImage.sizes?.card?.url || doc.featuredImage.url
: null,
focalX:
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
? doc.featuredImage.focalX
: 50,
focalY:
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
? doc.featuredImage.focalY
: 50,
layout: doc.layout || 'default',
} as PageFrontmatter,
content: doc.content as any,
};
content: string;
}
export async function getPageBySlug(slug: string, locale: string): Promise<PageData | null> {
// Guard against invalid locales hitting the DB (e.g. favicon.ico, assets)
if (locale !== 'de' && locale !== 'en') {
const fileSlug = await mapSlugToFileSlug(slug, locale);
const filePath = path.join(CONTENT_DIR, locale, `${fileSlug}.mdx`);
if (!fs.existsSync(filePath)) {
return null;
}
try {
const payload = await getPayload({ config: configPromise });
const fileSlug = await mapSlugToFileSlug(slug, locale);
const source = fs.readFileSync(filePath, 'utf8');
const { content, data } = matter(source);
// Try finding exact match first
let result = await payload.find({
collection: 'pages',
where: {
and: [
{ slug: { equals: fileSlug } },
...(!config.showDrafts ? [{ _status: { equals: 'published' } }] : []),
],
},
locale: locale as any,
depth: 1,
limit: 1,
});
// Fallback: search ALL locales
if (result.docs.length === 0) {
const crossResult = await payload.find({
collection: 'pages',
where: {
and: [
{ slug: { equals: fileSlug } },
...(!config.showDrafts ? [{ _status: { equals: 'published' } }] : []),
],
},
locale: 'all',
depth: 1,
limit: 1,
});
if (crossResult.docs.length > 0) {
// Fetch missing exact match by internal id
result = await payload.find({
collection: 'pages',
where: {
id: { equals: crossResult.docs[0].id },
},
locale: locale as any,
depth: 1,
limit: 1,
});
}
}
if (result.docs.length > 0) {
const doc = result.docs[0];
return {
slug: doc.slug,
frontmatter: {
title: doc.title,
excerpt: doc.excerpt || '',
featuredImage:
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
? doc.featuredImage.sizes?.card?.url || doc.featuredImage.url
: null,
focalX:
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
? doc.featuredImage.focalX
: 50,
focalY:
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
? doc.featuredImage.focalY
: 50,
layout:
doc.layout === 'fullBleed' || doc.layout === 'default'
? doc.layout
: ('default' as const),
},
content: doc.content,
};
}
return null;
} catch (error) {
console.error(`[Payload] getPageBySlug failed for ${slug}:`, error);
return null;
}
return {
slug,
frontmatter: data as PageFrontmatter,
content,
};
}
export async function getAllPages(locale: string): Promise<PageData[]> {
if (locale !== 'de' && locale !== 'en') {
return [];
}
const localeDir = path.join(CONTENT_DIR, locale);
if (!fs.existsSync(localeDir)) return [];
try {
const payload = await getPayload({ config: configPromise });
const files = fs.readdirSync(localeDir).filter(f => f.endsWith('.mdx'));
const pages = await Promise.all(
files.map(async f => {
const slug = f.replace('.mdx', '');
return await getPageBySlug(slug, locale);
})
);
const result = await payload.find({
collection: 'pages' as any,
locale: locale as any,
limit: 100,
});
return (result.docs as any[]).map(mapDoc);
} catch (error) {
console.error(`[Payload] getAllPages failed for ${locale}:`, error);
return [];
}
return pages.filter((p): p is PageData => p !== null);
}
export async function getAllPagesMetadata(locale: string): Promise<Partial<PageData>[]> {
if (locale !== 'de' && locale !== 'en') {
return [];
}
export async function getAllPagesMetadata(locale: string): Promise<PageData[]> {
const localeDir = path.join(CONTENT_DIR, locale);
if (!fs.existsSync(localeDir)) return [];
try {
const payload = await getPayload({ config: configPromise });
const result = await payload.find({
collection: 'pages' as any,
locale: locale as any,
limit: 100,
});
return (result.docs as any[]).map((doc: any) => ({
slug: doc.slug,
frontmatter: {
title: doc.title,
excerpt: doc.excerpt || '',
featuredImage:
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
? doc.featuredImage.sizes?.card?.url || doc.featuredImage.url
: null,
focalX:
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
? doc.featuredImage.focalX
: 50,
focalY:
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
? doc.featuredImage.focalY
: 50,
} as PageFrontmatter,
}));
} catch (error) {
console.error(`[Payload] getAllPagesMetadata failed for ${locale}:`, error);
return [];
}
const files = fs.readdirSync(localeDir).filter(f => f.endsWith('.mdx'));
return files.map(f => {
const slug = f.replace('.mdx', '');
const filePath = path.join(localeDir, f);
const source = fs.readFileSync(filePath, 'utf8');
const { data } = matter(source);
return {
slug,
frontmatter: data as PageFrontmatter,
content: '', // Metadata only
};
});
}