Skip to content

Commit 4c53200

Browse files
committed
サイドバーを複数言語に対応、スクロール時の挙動を修正
1 parent b5b27cb commit 4c53200

File tree

1 file changed

+63
-40
lines changed

1 file changed

+63
-40
lines changed

app/sidebar.tsx

Lines changed: 63 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,88 @@
11
"use client";
22
import Link from "next/link";
33
import { usePathname } from "next/navigation";
4-
import useSWR, { Fetcher } from 'swr'
4+
import useSWR, { Fetcher } from "swr";
55
import { splitMarkdown } from "./[docs_id]/splitMarkdown";
66

7-
const fetcher: Fetcher<string, string> = (url) => fetch(url).then((r) => r.text())
7+
const fetcher: Fetcher<string, string> = (url) =>
8+
fetch(url).then((r) => r.text());
89

910
export function Sidebar() {
1011
const pathname = usePathname();
1112
const docs_id = pathname.replace(/^\//, "");
12-
const { data, error, isLoading } = useSWR(
13-
`/docs/${docs_id}.md`,
14-
fetcher
15-
)
13+
const { data, error, isLoading } = useSWR(`/docs/${docs_id}.md`, fetcher);
1614

1715
const pages = [
18-
{ id: "python-1", title: "1. 環境構築と基本思想" },
19-
{ id: "python-2", title: "2. 基本構文とデータ型" },
20-
{ id: "python-3", title: "3. リスト、タプル、辞書、セット" },
21-
{ id: "python-4", title: "4. 制御構文と関数" },
22-
{ id: "python-5", title: "5. モジュールとパッケージ" },
23-
{ id: "python-6", title: "6. オブジェクト指向プログラミング" },
24-
{ id: "python-7", title: "7. ファイルの入出力とコンテキストマネージャ" },
25-
{ id: "python-8", title: "8. 例外処理" },
26-
{ id: "python-9", title: "9. ジェネレータとデコレータ" },
16+
{
17+
id: "python",
18+
lang: "Python",
19+
pages: [
20+
{ id: 1, title: "環境構築と基本思想" },
21+
{ id: 2, title: "基本構文とデータ型" },
22+
{ id: 3, title: "リスト、タプル、辞書、セット" },
23+
{ id: 4, title: "制御構文と関数" },
24+
{ id: 5, title: "モジュールとパッケージ" },
25+
{ id: 6, title: "オブジェクト指向プログラミング" },
26+
{
27+
id: 7,
28+
title: "ファイルの入出力とコンテキストマネージャ",
29+
},
30+
{ id: 8, title: "例外処理" },
31+
{ id: 9, title: "ジェネレータとデコレータ" },
32+
],
33+
},
34+
{
35+
id: "cpp",
36+
lang: "C++",
37+
pages: [
38+
{ id: 2, title: "型システムとメモリ" },
39+
{ id: 3, title: "関数と参照" },
40+
],
41+
},
2742
];
2843

29-
if (error) console.error("Sidebar fetch error:", error)
30-
44+
if (error) console.error("Sidebar fetch error:", error);
3145

32-
33-
34-
const splitmdcontent = splitMarkdown(data ?? "")
46+
const splitmdcontent = splitMarkdown(data ?? "");
3547
return (
36-
<div className="bg-base-200 min-h-full w-80 p-4">
48+
<div className="bg-base-200 h-full w-80 overflow-y-auto">
3749
{/* todo: 背景色ほんとにこれでいい? */}
38-
<h2 className="hidden lg:block text-xl font-bold mb-4">
50+
<h2 className="hidden lg:block text-xl font-bold p-4">
3951
{/* サイドバーが常時表示されている場合のみ */}
4052
Navbar Title
4153
</h2>
42-
43-
<ol className="menu w-full list-outside">
44-
{pages.map((page) => (
45-
<li key={page.id}>
46-
<Link href={`/${page.id}`}>{page.title}</Link>
47-
{page.id === docs_id && !isLoading &&(
48-
<ul className="ml-4 mt-2 text-sm">
49-
{splitmdcontent
50-
.slice(1)
51-
.map((section, idx) => (
52-
<li key={idx} style={{ marginLeft: (section.level - 2) + "em"}}>
53-
<Link href={`#${idx+1}`}>{section.title}</Link>
54-
</li>
55-
))}
56-
</ul>
57-
)}
5854

55+
<ul className="menu w-full">
56+
{pages.map((group) => (
57+
<li key={group.id}>
58+
<details open={docs_id.startsWith(`${group.id}-`)}>
59+
<summary>{group.lang}</summary>
60+
<ul>
61+
{group.pages.map((page) => (
62+
<li key={page.id}>
63+
<Link href={`${group.id}-${page.id}`}>
64+
<span className="mr-0">{page.id}.</span>
65+
{page.title}
66+
</Link>
67+
{`${group.id}-${page.id}` === docs_id && !isLoading && (
68+
<ul className="ml-4 text-sm">
69+
{splitmdcontent.slice(1).map((section, idx) => (
70+
<li
71+
key={idx}
72+
style={{ marginLeft: section.level - 2 + "em" }}
73+
>
74+
<Link href={`#${idx + 1}`}>{section.title}</Link>
75+
</li>
76+
))}
77+
</ul>
78+
)}
79+
</li>
80+
))}
81+
</ul>
82+
</details>
5983
</li>
6084
))}
61-
</ol>
85+
</ul>
6286
</div>
6387
);
6488
}
65-

0 commit comments

Comments
 (0)