Branchwell

Become a Pro / website · becomeapro.ai / src / components / mdx / mdx-components.tsx

Raw 3.3 KB on 0104128d30a01b5156cca2446fd234b2f1fa3673
1import type { ReactNode } from "react";
2import { CodeBlock } from "@/components/mdx/code-block";
3import { Say } from "@/components/mdx/say";
4import { slugify } from "@/lib/utils";
5
6function textOf(node: ReactNode): string {
7 if (typeof node === "string" || typeof node === "number") return String(node);
8 if (Array.isArray(node)) return node.map(textOf).join("");
9 if (node && typeof node === "object" && "props" in node) {
10 return textOf((node as { props: { children?: ReactNode } }).props.children);
11 }
12 return "";
13}
14
15function H2({ children }: { children?: ReactNode }) {
16 return <h2 id={slugify(textOf(children))}>{children}</h2>;
17}
18function H3({ children }: { children?: ReactNode }) {
19 return <h3 id={slugify(textOf(children))}>{children}</h3>;
20}
21
22const CALLOUT_LABELS = { note: "Note", tip: "Tip", warning: "Heads up", safety: "Safety" };
23
24function Callout({ type = "note", children }: { type?: keyof typeof CALLOUT_LABELS; children: ReactNode }) {
25 const strong = type === "safety" || type === "warning";
26 return (
27 <aside
28 className={`not-prose my-8 rounded-md border p-4 text-[17px] leading-relaxed ${strong ? "border-2 border-foreground" : "border-border"}`}
29 >
30 <p className="m-0 text-sm font-bold uppercase tracking-wide">{CALLOUT_LABELS[type] ?? "Note"}</p>
31 <div className="mt-1 [&>p+p]:mt-2 [&>p]:m-0 [&_a]:underline">{children}</div>
32 </aside>
33 );
34}
35
36// Numbered steps: wrap a run of <Step title="..."> blocks in <Steps>
37function Steps({ children }: { children: ReactNode }) {
38 return <ol className="not-prose my-8 list-none space-y-5 p-0 [counter-reset:step]">{children}</ol>;
39}
40
41function Step({ title, children }: { title: string; children: ReactNode }) {
42 return (
43 <li className="relative pl-11 [counter-increment:step] before:absolute before:left-0 before:top-0 before:flex before:h-8 before:w-8 before:items-center before:justify-center before:rounded-full before:border before:border-foreground before:text-[15px] before:font-bold before:content-[counter(step)]">
44 <p className="m-0 font-bold">{title}</p>
45 <div className="mt-1 text-[17px] leading-relaxed [&_a]:underline [&_code]:rounded [&_code]:bg-[var(--soft)] [&_code]:px-1 [&>p+p]:mt-2 [&>p]:m-0">
46 {children}
47 </div>
48 </li>
49 );
50}
51
52// A simple inline SVG diagram drawn in currentColor (works in light and dark), with an optional caption
53function Diagram({ caption, title, children }: { caption?: string; title?: string; children: ReactNode }) {
54 return (
55 <figure className="diagram not-prose my-8 rounded-md border border-border p-4">
56 {title && <p className="m-0 mb-3 font-bold">{title}</p>}
57 {children}
58 {caption && <figcaption className="mt-3 text-[15px] text-[var(--ash)]">{caption}</figcaption>}
59 </figure>
60 );
61}
62
63// Sources live in front matter only. Old <Resources> blocks render nothing, and
64// external links render as plain text so no page shows an outside link or credit.
65function Hidden() {
66 return null;
67}
68
69function Link({ href = '', children }: { href?: string; children?: ReactNode }) {
70 if (/^https?:/.test(href)) return <>{children}</>;
71 return <a href={href}>{children}</a>;
72}
73
74export const mdxComponents = {
75 h2: H2,
76 h3: H3,
77 pre: CodeBlock,
78 Callout,
79 Steps,
80 Step,
81 Diagram,
82 Say,
83 a: Link,
84 Resources: Hidden,
85 Resource: Hidden,
86};