"use client";
import Link from "next/link";
import { useEffect, useState } from "react";
import type { NewsItem } from "@/data/news";
import { getInitialLocale, getLocaleMessages, type Locale } from "@/lib/i18n";
import { PageBreadcrumb } from "@/components/layout/page-breadcrumb";
import { NewsShare } from "@/components/news/news-share";
function renderMarkdown(markdown: string) {
const escape = (text: string) => text.replace(/&/g, "&").replace(//g, ">");
const inline = (text: string) => escape(text)
.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, '
')
.replace(/\*\*(.+?)\*\*/g, "$1")
.replace(/\*(.+?)\*/g, "$1")
.replace(/`(.+?)`/g, "$1");
const output: string[] = [];
let paragraph: string[] = [];
let list: "ul" | "ol" | null = null;
let items: string[] = [];
const flushParagraph = () => { if (paragraph.length) { output.push(`
${inline(paragraph.join(" ").trim())}
`); paragraph = []; } };
const flushList = () => { if (list && items.length) output.push(`<${list}>${items.map((item) => `${inline(item)}`).join("")}${list}>`); list = null; items = []; };
for (const rawLine of markdown.replace(/\r\n?/g, "\n").split("\n")) {
const line = rawLine.trim();
if (!line) { flushParagraph(); flushList(); continue; }
const heading = line.match(/^(#{1,3})\s+(.+)$/);
if (heading) { flushParagraph(); flushList(); output.push(`${inline(heading[2])}`); continue; }
const unordered = line.match(/^[-*]\s+(.+)$/);
const ordered = line.match(/^\d+[.)]\s+(.+)$/);
if (unordered || ordered) { flushParagraph(); const next = unordered ? "ul" : "ol"; if (list && list !== next) flushList(); list = next; items.push((unordered ?? ordered)![1]); continue; }
flushList(); paragraph.push(line);
}
flushParagraph(); flushList();
return output.join("");
}
export function NewsDetailClient({ catalog, slug }: { catalog: Record; slug: string }) {
const [locale, setLocale] = useState("zh-CN");
const messages = getLocaleMessages(locale);
const item = catalog[locale].find((entry) => entry.slug === slug) ?? catalog["zh-CN"].find((entry) => entry.slug === slug);
useEffect(() => {
const syncLocale = () => setLocale(getInitialLocale());
syncLocale();
window.addEventListener("kr-locale-change", syncLocale);
window.addEventListener("storage", syncLocale);
return () => {
window.removeEventListener("kr-locale-change", syncLocale);
window.removeEventListener("storage", syncLocale);
};
}, []);
if (!item) return 未找到该文章。
{messages.common.backToNews}
;
return
← {messages.common.backToNews}
{item.category}
{item.title}
;
}