slug i18n
This commit is contained in:
15
lib/mdx.ts
15
lib/mdx.ts
@@ -1,6 +1,7 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import matter from 'gray-matter';
|
||||
import { mapSlugToFileSlug } from './slugs';
|
||||
|
||||
export interface ProductFrontmatter {
|
||||
title: string;
|
||||
@@ -18,30 +19,32 @@ export interface ProductMdx {
|
||||
}
|
||||
|
||||
export async function getProductBySlug(slug: string, locale: string): Promise<ProductMdx | null> {
|
||||
// Map translated slug to file slug
|
||||
const fileSlug = await mapSlugToFileSlug(slug, locale);
|
||||
const productsDir = path.join(process.cwd(), 'data', 'products', locale);
|
||||
|
||||
// Try exact slug first
|
||||
let filePath = path.join(productsDir, `${slug}.mdx`);
|
||||
let filePath = path.join(productsDir, `${fileSlug}.mdx`);
|
||||
|
||||
if (!fs.existsSync(filePath)) {
|
||||
// Try with -2 suffix (common in the dumped files)
|
||||
filePath = path.join(productsDir, `${slug}-2.mdx`);
|
||||
filePath = path.join(productsDir, `${fileSlug}-2.mdx`);
|
||||
}
|
||||
|
||||
if (!fs.existsSync(filePath)) {
|
||||
// Fallback to English if locale is not 'en'
|
||||
if (locale !== 'en') {
|
||||
const enProductsDir = path.join(process.cwd(), 'data', 'products', 'en');
|
||||
let enFilePath = path.join(enProductsDir, `${slug}.mdx`);
|
||||
let enFilePath = path.join(enProductsDir, `${fileSlug}.mdx`);
|
||||
if (!fs.existsSync(enFilePath)) {
|
||||
enFilePath = path.join(enProductsDir, `${slug}-2.mdx`);
|
||||
enFilePath = path.join(enProductsDir, `${fileSlug}-2.mdx`);
|
||||
}
|
||||
|
||||
if (fs.existsSync(enFilePath)) {
|
||||
const fileContent = fs.readFileSync(enFilePath, 'utf8');
|
||||
const { data, content } = matter(fileContent);
|
||||
return {
|
||||
slug,
|
||||
slug: fileSlug,
|
||||
frontmatter: {
|
||||
...data,
|
||||
isFallback: true
|
||||
@@ -57,7 +60,7 @@ export async function getProductBySlug(slug: string, locale: string): Promise<Pr
|
||||
const { data, content } = matter(fileContent);
|
||||
|
||||
return {
|
||||
slug,
|
||||
slug: fileSlug,
|
||||
frontmatter: data as ProductFrontmatter,
|
||||
content,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user