chore: merge branch fix/mobile-kontakt-button and fix og-image.test.ts
This commit is contained in:
15
lib/blog.ts
15
lib/blog.ts
@@ -56,9 +56,12 @@ export function isPostVisible(post: { frontmatter: { date: string; public?: bool
|
||||
return !(postDate > now && config.isProduction);
|
||||
}
|
||||
|
||||
import { mapSlugToFileSlug, mapFileSlugToTranslated } from './slugs';
|
||||
|
||||
export async function getPostBySlug(slug: string, locale: string): Promise<PostData | null> {
|
||||
try {
|
||||
const filePath = path.join(process.cwd(), 'content', 'posts', `${slug}.mdx`);
|
||||
const fileSlug = await mapSlugToFileSlug(slug, locale);
|
||||
const filePath = path.join(process.cwd(), 'content', 'posts', `${fileSlug}.mdx`);
|
||||
const fileContent = await fs.readFile(filePath, 'utf-8');
|
||||
const { data, content } = matter(fileContent);
|
||||
|
||||
@@ -77,8 +80,10 @@ export async function getPostBySlug(slug: string, locale: string): Promise<PostD
|
||||
// Not JSON
|
||||
}
|
||||
|
||||
const resolvedSlug = await mapFileSlugToTranslated(fileSlug, locale);
|
||||
|
||||
return {
|
||||
slug,
|
||||
slug: resolvedSlug,
|
||||
frontmatter: {
|
||||
title: data.title || '',
|
||||
date: data.date || '',
|
||||
@@ -98,8 +103,10 @@ export async function getPostBySlug(slug: string, locale: string): Promise<PostD
|
||||
}
|
||||
|
||||
export async function getPostSlugs(slug: string, locale: string): Promise<Record<string, string>> {
|
||||
// Mock function, simply returns the slug for all locales
|
||||
return { de: slug, en: slug };
|
||||
const fileSlug = await mapSlugToFileSlug(slug, locale);
|
||||
const de = await mapFileSlugToTranslated(fileSlug, 'de');
|
||||
const en = await mapFileSlugToTranslated(fileSlug, 'en');
|
||||
return { de, en };
|
||||
}
|
||||
|
||||
export async function getAllPosts(locale: string): Promise<PostData[]> {
|
||||
|
||||
12
lib/pages.ts
12
lib/pages.ts
@@ -2,6 +2,7 @@ import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
import matter from 'gray-matter';
|
||||
import { config } from '@/lib/config';
|
||||
import { mapSlugToFileSlug } from './slugs';
|
||||
|
||||
export interface PageFrontmatter {
|
||||
title: string;
|
||||
@@ -47,11 +48,18 @@ function mapDoc(doc: any): PageData {
|
||||
|
||||
export async function getPageBySlug(slug: string, locale: string): Promise<PageData | null> {
|
||||
try {
|
||||
const filePath = path.join(process.cwd(), 'content', 'pages', `${slug}.mdx`);
|
||||
const fileSlug = await mapSlugToFileSlug(slug, locale);
|
||||
const filePath = path.join(process.cwd(), 'content', 'pages', `${fileSlug}.mdx`);
|
||||
const fileContent = await fs.readFile(filePath, 'utf-8');
|
||||
const { data, content } = matter(fileContent);
|
||||
|
||||
let parsedContent = content;
|
||||
// Fix MDX data props dropped by next-mdx-remote
|
||||
// Payload serializes as data={{"items":...}} which Acorn treats as a Block statement.
|
||||
const fixedContent = content.replace(/data=\{\{([\s\S]*?)\}\}/g, (match, p1) => {
|
||||
return 'data="{' + p1.replace(/"/g, '"') + '}"';
|
||||
});
|
||||
|
||||
let parsedContent = fixedContent;
|
||||
try {
|
||||
if (content.trim().startsWith('{')) {
|
||||
parsedContent = JSON.parse(content);
|
||||
|
||||
@@ -39,7 +39,13 @@ export async function getProductBySlug(slug: string, locale: string): Promise<Pr
|
||||
const fileContent = await fs.readFile(filePath, 'utf-8');
|
||||
const { data, content } = matter(fileContent);
|
||||
|
||||
let parsedContent = content;
|
||||
// Fix MDX data props dropped by next-mdx-remote
|
||||
// Payload serializes as data={{"items":...}} which Acorn treats as a Block statement.
|
||||
const fixedContent = content.replace(/data=\{\{([\s\S]*?)\}\}/g, (match, p1) => {
|
||||
return 'data="{' + p1.replace(/"/g, '"') + '}"';
|
||||
});
|
||||
|
||||
let parsedContent = fixedContent;
|
||||
try {
|
||||
if (content.trim().startsWith('{')) {
|
||||
parsedContent = JSON.parse(content);
|
||||
|
||||
13
lib/slugs.ts
13
lib/slugs.ts
@@ -31,6 +31,11 @@ export async function mapSlugToFileSlug(translatedSlug: string, locale: string):
|
||||
return slugs.categories[translatedSlug as keyof typeof slugs.categories];
|
||||
}
|
||||
|
||||
// Check posts
|
||||
if ((slugs as any).posts && translatedSlug in (slugs as any).posts) {
|
||||
return (slugs as any).posts[translatedSlug];
|
||||
}
|
||||
|
||||
// Return original slug if no mapping found
|
||||
return translatedSlug;
|
||||
}
|
||||
@@ -45,7 +50,7 @@ export async function mapFileSlugToTranslated(fileSlug: string, locale: string):
|
||||
const messages = getMessages(locale);
|
||||
const slugs = messages.Slugs;
|
||||
|
||||
const sections = [slugs.pages, slugs.products, slugs.categories];
|
||||
const sections = [slugs.pages, slugs.products, slugs.categories, (slugs as any).posts];
|
||||
|
||||
for (const sectionData of sections) {
|
||||
if (sectionData && typeof sectionData === 'object') {
|
||||
@@ -63,16 +68,16 @@ export async function mapFileSlugToTranslated(fileSlug: string, locale: string):
|
||||
|
||||
/**
|
||||
* Gets all translated slugs for a section
|
||||
* @param section - The section name (pages, products, categories)
|
||||
* @param section - The section name (pages, products, categories, posts)
|
||||
* @param locale - The current locale
|
||||
* @returns Object mapping translated slugs to file slugs
|
||||
*/
|
||||
export async function getSlugMappings(
|
||||
section: 'pages' | 'products' | 'categories',
|
||||
section: 'pages' | 'products' | 'categories' | 'posts',
|
||||
locale: string,
|
||||
): Promise<Record<string, string>> {
|
||||
const messages = getMessages(locale);
|
||||
const sectionData = messages.Slugs[section];
|
||||
const sectionData = (messages.Slugs as any)[section];
|
||||
|
||||
if (sectionData && typeof sectionData === 'object') {
|
||||
return sectionData as Record<string, string>;
|
||||
|
||||
Reference in New Issue
Block a user