import { customers, getCustomer } from "../data/customers";
import { canonicalUrl, frontmatter } from "./frontmatter";
import type { MDXData } from "./utils";
// Agents parse these dates; the human form stays on the HTML page.
const isoDate = (date: Date) => date.toISOString().slice(0, 10);
/**
* Converts MDX content to clean markdown format for AI tools
* Handles YAML frontmatter generation and component serialization
*/
export function convertMdxToMarkdown(data: MDXData): string {
let output = "";
// Step 0: Generate YAML frontmatter from metadata
const { metadata, content, href } = data;
// Same fallbacks as `getPageMetadata`, so the twin carries the
// and meta description the HTML page advertises.
output += frontmatter({
title: metadata.seo?.title ?? metadata.title,
url: canonicalUrl(metadata.seo?.canonical ?? href),
date: metadata.publishedAt ? isoDate(metadata.publishedAt) : undefined,
updated: metadata.updatedAt ? isoDate(metadata.updatedAt) : undefined,
author: metadata.author,
description: metadata.seo?.description ?? metadata.description,
category: metadata.category,
image: metadata.image ? canonicalUrl(metadata.image) : undefined,
});
// Step 1: Strip import statements (confirmed: always at top of file)
let markdown = content.replace(/^import\s+.*$/gm, "");
// Step 2: Convert to markdown links
markdown = markdown.replace(
/]*>([\s\S]*?)<\/ButtonLink>/g,
(_match, href, children) => {
// Extract plain text from children (remove any nested tags)
const text = children.replace(/<[^>]*>/g, "").trim();
return `[${text}](${href})`;
},
);
// Step 3: Convert to markdown images
// Handle with alt attribute
markdown = markdown.replace(
/]*\s+)?src="([^"]*)"(?:[^>]*\s+)?alt="([^"]*)"[^>]*\/?>/g,
(_match, src, alt) => {
return ``;
},
);
// Handle without alt attribute
markdown = markdown.replace(
/]*\s+)?src="([^"]*)"[^>]*\/?>/g,
(_match, src) => {
return ``;
},
);
// Step 4: Replace
components with a note to use GFM tables
// TODO: Convert Table components in MDX source files to GFM tables
markdown = markdown.replace(/