|
| 1 | +import { Metadata } from "next"; |
1 | 2 | import { notFound } from "next/navigation"; |
2 | 3 | import { getCloudflareContext } from "@opennextjs/cloudflare"; |
3 | 4 | import { readFile } from "node:fs/promises"; |
4 | 5 | import { join } from "node:path"; |
5 | | -import { MarkdownSection, splitMarkdown } from "./splitMarkdown"; |
| 6 | +import { splitMarkdown } from "./splitMarkdown"; |
6 | 7 | import { PageContent } from "./pageContent"; |
7 | 8 | import { ChatHistoryProvider } from "./chatHistory"; |
8 | 9 | import { getChatFromCache } from "@/lib/chatHistory"; |
| 10 | +import { getLanguageName } from "@/pagesList"; |
| 11 | + |
| 12 | +async function getMarkdownContent(docs_id: string): Promise<string> { |
| 13 | + try { |
| 14 | + if (process.env.NODE_ENV === "development") { |
| 15 | + return await readFile( |
| 16 | + join(process.cwd(), "public", "docs", `${docs_id}.md`), |
| 17 | + "utf-8" |
| 18 | + ); |
| 19 | + } else { |
| 20 | + const cfAssets = getCloudflareContext().env.ASSETS; |
| 21 | + const res = await cfAssets!.fetch( |
| 22 | + `https://assets.local/docs/${docs_id}.md` |
| 23 | + ); |
| 24 | + if (!res.ok) { |
| 25 | + notFound(); |
| 26 | + } |
| 27 | + return await res.text(); |
| 28 | + } |
| 29 | + } catch (e) { |
| 30 | + console.error(e); |
| 31 | + notFound(); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +export async function generateMetadata({ |
| 36 | + params, |
| 37 | +}: { |
| 38 | + params: { docs_id: string }; |
| 39 | +}): Promise<Metadata> { |
| 40 | + const { docs_id } = params; |
| 41 | + const mdContent = await getMarkdownContent(docs_id); |
| 42 | + const splitMdContent = splitMarkdown(mdContent); |
| 43 | + |
| 44 | + // 先頭の 第n章: を除いたものをタイトルとする |
| 45 | + const title = splitMdContent[0]?.title?.split(" ").slice(1).join(" "); |
| 46 | + |
| 47 | + const description = splitMdContent[0].content; |
| 48 | + |
| 49 | + const chapter = docs_id.split("-")[1]; |
| 50 | + |
| 51 | + return { |
| 52 | + title: `${getLanguageName(docs_id)}-${chapter}. ${title}`, |
| 53 | + description, |
| 54 | + }; |
| 55 | +} |
9 | 56 |
|
10 | 57 | export default async function Page({ |
11 | 58 | params, |
12 | 59 | }: { |
13 | | - params: Promise<{ docs_id: string }>; |
| 60 | + params: { docs_id: string }; |
14 | 61 | }) { |
15 | | - const { docs_id } = await params; |
16 | | - |
17 | | - let mdContent: Promise<string>; |
18 | | - if (process.env.NODE_ENV === "development") { |
19 | | - mdContent = readFile( |
20 | | - join(process.cwd(), "public", "docs", `${docs_id}.md`), |
21 | | - "utf-8" |
22 | | - ).catch((e) => { |
23 | | - console.error(e); |
24 | | - notFound(); |
25 | | - }); |
26 | | - } else { |
27 | | - const cfAssets = getCloudflareContext().env.ASSETS; |
28 | | - mdContent = cfAssets! |
29 | | - .fetch(`https://assets.local/docs/${docs_id}.md`) |
30 | | - .then(async (res) => { |
31 | | - if (!res.ok) { |
32 | | - notFound(); |
33 | | - } |
34 | | - return res.text(); |
35 | | - }) |
36 | | - .catch((e) => { |
37 | | - console.error(e); |
38 | | - notFound(); |
39 | | - }); |
40 | | - } |
41 | | - |
42 | | - const splitMdContent: Promise<MarkdownSection[]> = mdContent.then((text) => |
43 | | - splitMarkdown(text) |
44 | | - ); |
| 62 | + const { docs_id } = params; |
45 | 63 |
|
| 64 | + const mdContent = getMarkdownContent(docs_id); |
| 65 | + const splitMdContent = mdContent.then((text) => splitMarkdown(text)); |
46 | 66 | const initialChatHistories = getChatFromCache(docs_id); |
47 | 67 |
|
48 | 68 | return ( |
49 | | - <ChatHistoryProvider initialChatHistories={await initialChatHistories} docs_id={docs_id}> |
| 69 | + <ChatHistoryProvider |
| 70 | + initialChatHistories={await initialChatHistories} |
| 71 | + docs_id={docs_id} |
| 72 | + > |
50 | 73 | <PageContent |
51 | 74 | documentContent={await mdContent} |
52 | 75 | splitMdContent={await splitMdContent} |
|
0 commit comments