first commit
This commit is contained in:
98
frontend/src/lib/legal.ts
Normal file
98
frontend/src/lib/legal.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
export interface LegalPage {
|
||||
slug: string;
|
||||
title: string;
|
||||
content: string;
|
||||
lastUpdated?: string;
|
||||
}
|
||||
|
||||
export interface LegalPageMeta {
|
||||
slug: string;
|
||||
title: string;
|
||||
}
|
||||
|
||||
// Map file names to display titles
|
||||
const titleMap: Record<string, { en: string; es: string }> = {
|
||||
'privacy_policy': { en: 'Privacy Policy', es: 'Política de Privacidad' },
|
||||
'terms_policy': { en: 'Terms & Conditions', es: 'Términos y Condiciones' },
|
||||
'refund_cancelation_policy': { en: 'Refund & Cancellation Policy', es: 'Política de Reembolso y Cancelación' },
|
||||
};
|
||||
|
||||
// Convert file name to URL-friendly slug
|
||||
export function fileNameToSlug(fileName: string): string {
|
||||
return fileName.replace('.md', '').replace(/_/g, '-');
|
||||
}
|
||||
|
||||
// Convert slug back to file name
|
||||
export function slugToFileName(slug: string): string {
|
||||
return slug.replace(/-/g, '_') + '.md';
|
||||
}
|
||||
|
||||
// Get the legal directory path
|
||||
function getLegalDir(): string {
|
||||
return path.join(process.cwd(), 'legal');
|
||||
}
|
||||
|
||||
// Get all legal page slugs for static generation
|
||||
export function getAllLegalSlugs(): string[] {
|
||||
const legalDir = getLegalDir();
|
||||
|
||||
if (!fs.existsSync(legalDir)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const files = fs.readdirSync(legalDir);
|
||||
return files
|
||||
.filter(file => file.endsWith('.md'))
|
||||
.map(file => fileNameToSlug(file));
|
||||
}
|
||||
|
||||
// Get metadata for all legal pages (for navigation/footer)
|
||||
export function getAllLegalPagesMeta(locale: string = 'en'): LegalPageMeta[] {
|
||||
const legalDir = getLegalDir();
|
||||
|
||||
if (!fs.existsSync(legalDir)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const files = fs.readdirSync(legalDir);
|
||||
return files
|
||||
.filter(file => file.endsWith('.md'))
|
||||
.map(file => {
|
||||
const slug = fileNameToSlug(file);
|
||||
const baseFileName = file.replace('.md', '');
|
||||
const titles = titleMap[baseFileName];
|
||||
const title = titles ? titles[locale as 'en' | 'es'] || titles.en : baseFileName.replace(/_/g, ' ');
|
||||
|
||||
return { slug, title };
|
||||
});
|
||||
}
|
||||
|
||||
// Get a specific legal page content
|
||||
export function getLegalPage(slug: string, locale: string = 'en'): LegalPage | null {
|
||||
const legalDir = getLegalDir();
|
||||
const fileName = slugToFileName(slug);
|
||||
const filePath = path.join(legalDir, fileName);
|
||||
|
||||
if (!fs.existsSync(filePath)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const content = fs.readFileSync(filePath, 'utf-8');
|
||||
const baseFileName = fileName.replace('.md', '');
|
||||
const titles = titleMap[baseFileName];
|
||||
const title = titles ? titles[locale as 'en' | 'es'] || titles.en : baseFileName.replace(/_/g, ' ');
|
||||
|
||||
// Try to extract last updated date from content
|
||||
const lastUpdatedMatch = content.match(/Last updated:\s*(.+)/i);
|
||||
const lastUpdated = lastUpdatedMatch ? lastUpdatedMatch[1].trim() : undefined;
|
||||
|
||||
return {
|
||||
slug,
|
||||
title,
|
||||
content,
|
||||
lastUpdated,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user