"use client"; import { FormEvent, useState } from "react"; import { useRouter } from "next/navigation"; import { normalizeImageUrl } from "@/lib/normalize"; const defaultIntro = `## 新帖内容 - 这里支持 **Markdown** - 图片请上传到 img.020417.xyz 后填入链接 `; export function CreatePostForm() { const [title, setTitle] = useState(""); const [cover, setCover] = useState(""); const [tags, setTags] = useState(""); const [markdown, setMarkdown] = useState(defaultIntro); const [loading, setLoading] = useState(false); const router = useRouter(); async function handleSubmit(e: FormEvent) { e.preventDefault(); setLoading(true); try { const normalizedCover = normalizeImageUrl(cover); const res = await fetch("/api/posts", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ title, markdown, cover: normalizedCover, tags: Array.from( new Set( tags .split(",") .map((t) => t.trim()) .filter(Boolean) ) ) }) }); if (!res.ok) { const data = await res.json().catch(() => ({})); alert(data.error ? JSON.stringify(data.error) : "发布失败"); } else { const data = await res.json(); router.push(`/p/${data.slug}`); router.refresh(); } } finally { setLoading(false); } } return (

新建一条信息

填写后提交,立刻出现在首页最上方。