Files
zhimingge/components/learn/markdown-content.tsx
T
dekun a487b17165 Fix learn 404, coin animation, full regions, and AI key errors.
Use numeric /learn/{num} routes, register Tailwind coin animations with方孔铜钱 UI, expand to 34 provinces, and surface missing OPENAI_API_KEY clearly.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-10 21:32:20 +08:00

79 lines
2.1 KiB
TypeScript

import Link from "next/link";
import Markdown from "react-markdown";
import type { LearnVariant } from "@/lib/content/zhouyi";
function resolveLearnHref(
href: string | undefined,
variant: LearnVariant,
): string | undefined {
if (!href) {
return href;
}
if (href.startsWith("http://") || href.startsWith("https://")) {
return href;
}
const base = variant === "traditional" ? "/learn" : "/learn/other";
if (href === "index.md" || href === "./index.md") {
return base;
}
if (href === "other/index.md") {
return "/learn/other";
}
if (href.endsWith("/index.md")) {
const mark = href.replace(/\/index\.md$/, "").replace(/^\.\//, "");
if (mark.startsWith("other/")) {
const folder = mark.slice("other/".length);
const num = folder.split(".")[0];
return `/learn/other/${num}`;
}
const num = mark.split(".")[0];
return `${base}/${num}`;
}
return href;
}
export default function MarkdownContent({
content,
variant = "traditional",
}: {
content: string;
variant?: LearnVariant;
}) {
return (
<Markdown
className="prose max-w-none dark:prose-invert prose-headings:scroll-mt-20 prose-a:text-primary"
components={{
a: ({ href, children, ...props }) => {
const resolved = resolveLearnHref(href, variant);
if (resolved?.startsWith("/")) {
return (
<Link href={resolved} {...props}>
{children}
</Link>
);
}
return (
<a href={resolved} {...props}>
{children}
</a>
);
},
img: ({ src, alt, ...props }) => {
if (typeof src === "string" && !src.startsWith("http")) {
return (
<span className="my-2 block rounded border border-dashed p-4 text-center text-sm text-muted-foreground">
[{alt || "卦象图片"}]
</span>
);
}
return <img src={src} alt={alt} {...props} />;
},
}}
>
{content}
</Markdown>
);
}