import type { ReactNode } from "react";
import { CodeBlock } from "@/components/mdx/code-block";
import { Say } from "@/components/mdx/say";
import { slugify } from "@/lib/utils";
function textOf(node: ReactNode): string {
if (typeof node === "string" || typeof node === "number") return String(node);
if (Array.isArray(node)) return node.map(textOf).join("");
if (node && typeof node === "object" && "props" in node) {
return textOf((node as { props: { children?: ReactNode } }).props.children);
}
return "";
}
function H2({ children }: { children?: ReactNode }) {
return
{children}
;
}
function H3({ children }: { children?: ReactNode }) {
return
{children}
;
}
const CALLOUT_LABELS = { note: "Note", tip: "Tip", warning: "Heads up", safety: "Safety" };
function Callout({ type = "note", children }: { type?: keyof typeof CALLOUT_LABELS; children: ReactNode }) {
const strong = type === "safety" || type === "warning";
return (
);
}
// Numbered steps: wrap a run of blocks in
function Steps({ children }: { children: ReactNode }) {
return {children};
}
function Step({ title, children }: { title: string; children: ReactNode }) {
return (
{title}
{children}
);
}
// A simple inline SVG diagram drawn in currentColor (works in light and dark), with an optional caption
function Diagram({ caption, title, children }: { caption?: string; title?: string; children: ReactNode }) {
return (
{title &&
{title}
}
{children}
{caption && {caption}}
);
}
// Sources live in front matter only. Old blocks render nothing, and
// external links render as plain text so no page shows an outside link or credit.
function Hidden() {
return null;
}
function Link({ href = '', children }: { href?: string; children?: ReactNode }) {
if (/^https?:/.test(href)) return <>{children}>;
return {children};
}
export const mdxComponents = {
h2: H2,
h3: H3,
pre: CodeBlock,
Callout,
Steps,
Step,
Diagram,
Say,
a: Link,
Resources: Hidden,
Resource: Hidden,
};