slug i18n

This commit is contained in:
2026-01-20 23:43:01 +01:00
parent f62485a67d
commit abf283c9ab
9 changed files with 267 additions and 42 deletions

View File

@@ -1,6 +1,7 @@
import fs from 'fs';
import path from 'path';
import matter from 'gray-matter';
import { mapSlugToFileSlug } from './slugs';
export interface PostFrontmatter {
title: string;
@@ -18,8 +19,10 @@ export interface PostMdx {
}
export async function getPostBySlug(slug: string, locale: string): Promise<PostMdx | null> {
// Map translated slug to file slug
const fileSlug = await mapSlugToFileSlug(slug, locale);
const postsDir = path.join(process.cwd(), 'data', 'blog', locale);
const filePath = path.join(postsDir, `${slug}.mdx`);
const filePath = path.join(postsDir, `${fileSlug}.mdx`);
if (!fs.existsSync(filePath)) {
return null;
@@ -29,7 +32,7 @@ export async function getPostBySlug(slug: string, locale: string): Promise<PostM
const { data, content } = matter(fileContent);
return {
slug,
slug: fileSlug,
frontmatter: data as PostFrontmatter,
content,
};