diff --git a/app/news/[slug]/page.jsx b/app/news/[slug]/page.jsx new file mode 100644 index 0000000..217abb0 --- /dev/null +++ b/app/news/[slug]/page.jsx @@ -0,0 +1,30 @@ +import {notFound, redirect} from "next/navigation"; +import {news} from "../data.js"; + +function toKebabCase(str) { + return str.toLowerCase().replace(/ /g, "-"); +} + +function slugof(news) { + return news.slug || toKebabCase(news.title); +} + +export async function generateStaticParams() { + return news.map((news) => ({slug: slugof(news)})); +} + +export async function generateMetadata({params}) { + const {slug} = await params; + const blog = news.find((news) => slugof(news) === slug); + if (!blog) notFound(); + return { + title: `${blog.title} | Recho`, + }; +} + +export default async function Page({params}) { + const {slug} = await params; + const blog = news.find((news) => slugof(news) === slug); + if (!blog) notFound(); + redirect(blog.link); +} diff --git a/app/news/data.js b/app/news/data.js new file mode 100644 index 0000000..15cd84f --- /dev/null +++ b/app/news/data.js @@ -0,0 +1,11 @@ +export const news = [ + { + title: "A Lighter Way to Code with Creativity", + publishedAt: "2025-10-01", + summary: "Introducing Recho: a light learning and exploration environment.", + link: "https://medium.com/@subairui/a-lighter-way-to-code-with-creativity-8c0ac739aa6f", + author: "Bairui SU", + image: "a-ligter-way-to-code-with-creativity.png", + slug: "hello-world", + }, +]; diff --git a/app/news/page.jsx b/app/news/page.jsx index c2dabda..6fb9a2e 100644 --- a/app/news/page.jsx +++ b/app/news/page.jsx @@ -1,15 +1,5 @@ import {cn} from "../cn.js"; - -const news = [ - { - title: "A Lighter Way to Code with Creativity", - publishedAt: "2025-10-01", - summary: "Introducing Recho: a light learning and exploration environment.", - link: "https://medium.com/@subairui/a-lighter-way-to-code-with-creativity-8c0ac739aa6f", - author: "Bairui SU", - image: "a-ligter-way-to-code-with-creativity.png", - }, -]; +import {news} from "./data.js"; export default function News() { return (