Skip to content

Commit 50a6d50

Browse files
committed
ページのタイトルを設定
1 parent 6bc5eba commit 50a6d50

File tree

5 files changed

+85
-35
lines changed

5 files changed

+85
-35
lines changed

app/[docs_id]/page.tsx

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,75 @@
1+
import { Metadata } from "next";
12
import { notFound } from "next/navigation";
23
import { getCloudflareContext } from "@opennextjs/cloudflare";
34
import { readFile } from "node:fs/promises";
45
import { join } from "node:path";
5-
import { MarkdownSection, splitMarkdown } from "./splitMarkdown";
6+
import { splitMarkdown } from "./splitMarkdown";
67
import { PageContent } from "./pageContent";
78
import { ChatHistoryProvider } from "./chatHistory";
89
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+
}
956

1057
export default async function Page({
1158
params,
1259
}: {
13-
params: Promise<{ docs_id: string }>;
60+
params: { docs_id: string };
1461
}) {
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;
4563

64+
const mdContent = getMarkdownContent(docs_id);
65+
const splitMdContent = mdContent.then((text) => splitMarkdown(text));
4666
const initialChatHistories = getChatFromCache(docs_id);
4767

4868
return (
49-
<ChatHistoryProvider initialChatHistories={await initialChatHistories} docs_id={docs_id}>
69+
<ChatHistoryProvider
70+
initialChatHistories={await initialChatHistories}
71+
docs_id={docs_id}
72+
>
5073
<PageContent
5174
documentContent={await mdContent}
5275
splitMdContent={await splitMdContent}

app/layout.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import { SidebarMdProvider } from "./sidebar";
1111
import { RuntimeProvider } from "./terminal/runtime";
1212

1313
export const metadata: Metadata = {
14-
title: "Create Next App",
15-
description: "Generated by create next app",
14+
title: {
15+
template: "%s - my.code();",
16+
default: "my.code();",
17+
},
1618
};
1719

1820
export default function RootLayout({

app/page.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
import { Metadata } from "next";
12
import Link from "next/link";
23
import { pagesList } from "./pagesList";
34

5+
export const metadata: Metadata = {
6+
title: "my.code(); へようこそ",
7+
description:
8+
"環境構築不要、その場で実践。AIアシスタントとの対話履歴があなただけの教材へと進化する、新しいプログラミング学習サイトです。",
9+
};
10+
411
export default function Home() {
512
return (
613
<div className="p-4">

app/terminal/layout.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Metadata } from "next";
2+
3+
export const metadata: Metadata = {
4+
title: "Runtime Test Page",
5+
robots: {
6+
index: false,
7+
follow: false,
8+
},
9+
};
10+
11+
export default function TerminalLayout({
12+
children,
13+
}: {
14+
children: React.ReactNode;
15+
}) {
16+
return <div>{children}</div>;
17+
}

app/terminal/page.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"use client";
2+
23
import { Heading } from "@/[docs_id]/markdown";
34
import "mocha/mocha.css";
45
import { Fragment, useEffect, useRef, useState } from "react";

0 commit comments

Comments
 (0)