Skip to content

Commit 7099360

Browse files
committed
各章の目次表示
1 parent 8a93a9c commit 7099360

File tree

4 files changed

+98
-27
lines changed

4 files changed

+98
-27
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { MarkdownSection } from "./splitMarkdown";
2+
3+
export function splitMarkdownClient(content: string): MarkdownSection[] {
4+
const lines = content.split("\n");
5+
const sections: MarkdownSection[] = [];
6+
let current: MarkdownSection | null = null;
7+
8+
for (const line of lines) {
9+
const match = line.match(/^(#{1,6})\s+(.*)/); // 見出しを検出
10+
if (match) {
11+
if (current) sections.push(current); // 前のセクションを保存
12+
current = {
13+
level: match[1].length, // 見出しレベル(# の数)
14+
title: match[2].trim(), // 見出しタイトル
15+
content: "", // 内容は後続行で追加
16+
};
17+
} else if (current) {
18+
current.content += line + "\n"; // セクション内容に追加
19+
}
20+
}
21+
if (current) sections.push(current); // 最後のセクションを保存
22+
return sections.map((s) => ({ ...s, content: s.content.trim() }));
23+
}

app/sidebar.tsx

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,66 @@
1+
"use client";
12
import Link from "next/link";
3+
import { usePathname } from "next/navigation";
4+
import { useState } from "react";
5+
import { split } from "react-ace";
6+
import useSWR from 'swr'
7+
import { splitMarkdownClient } from "./[docs_id]/splitMarkdownClient";
8+
import { Section } from "./[docs_id]/section";
9+
10+
211

312
export function Sidebar() {
13+
const fetcher = (url: string | URL | Request<unknown, CfProperties<unknown>>) => fetch(url).then((r) => r.text())
14+
const pathname = usePathname();
15+
const docs_id = pathname.replace(/^\//, "");
16+
const { data, error, isLoading } = useSWR(
17+
`/docs/${docs_id}.md`,
18+
fetcher
19+
)
20+
21+
if (isLoading) return <div>Loading...</div>
22+
if (error) return <div>Error: {error.message}</div>
23+
24+
const pages = [
25+
{ id: "python-1", title: "1. 環境構築と基本思想" },
26+
{ id: "python-2", title: "2. 基本構文とデータ型" },
27+
{ id: "python-3", title: "3. リスト、タプル、辞書、セット" },
28+
{ id: "python-4", title: "4. 制御構文と関数" },
29+
{ id: "python-5", title: "5. モジュールとパッケージ" },
30+
{ id: "python-6", title: "6. オブジェクト指向プログラミング" },
31+
{ id: "python-7", title: "7. ファイルの入出力とコンテキストマネージャ" },
32+
{ id: "python-8", title: "8. 例外処理" },
33+
{ id: "python-9", title: "9. ジェネレータとデコレータ" },
34+
];
35+
36+
37+
const splitmdcontent = splitMarkdownClient(data ?? "")
438
return (
539
<div className="bg-base-200 min-h-full w-80 p-4">
640
{/* todo: 背景色ほんとにこれでいい? */}
741
<h2 className="hidden lg:block text-xl font-bold mb-4">
842
{/* サイドバーが常時表示されている場合のみ */}
943
Navbar Title
1044
</h2>
45+
1146
<ol className="menu w-full list-decimal list-outside">
12-
<li>
13-
<Link href="/python-1">1. 環境構築と基本思想</Link>
14-
</li>
15-
<li>
16-
<Link href="/python-2">2. 基本構文とデータ型</Link>
17-
</li>
18-
<li>
19-
<Link href="/python-3">3. リスト、タプル、辞書、セット</Link>
20-
</li>
21-
<li>
22-
<Link href="/python-4">4. 制御構文と関数</Link>
23-
</li>
24-
<li>
25-
<Link href="/python-5">5. モジュールとパッケージ</Link>
26-
</li>
27-
<li>
28-
<Link href="/python-6">6. オブジェクト指向プログラミング</Link>
29-
</li>
30-
<li>
31-
<Link href="/python-7">7. ファイルの入出力とコンテキストマネージャ</Link>
32-
</li>
33-
<li>
34-
<Link href="/python-8">8. 例外処理</Link>
35-
</li>
36-
<li>
37-
<Link href="/python-9">9. ジェネレータとデコレータ</Link>
38-
</li>
47+
{pages.map((page,i) => (
48+
<li key={page.id}>
49+
<Link href={`/${page.id}`}>{page.title}</Link>
50+
{page.id === docs_id && (
51+
<ul className="ml-4 mt-2 list-disc text-sm">
52+
{splitmdcontent
53+
.filter(section => section.level !== 1)
54+
.map((section, idx) => (
55+
<li key={idx}>{section.title}</li>
56+
))}
57+
</ul>
58+
)}
59+
60+
</li>
61+
))}
3962
</ol>
4063
</div>
4164
);
4265
}
66+

package-lock.json

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"react-markdown": "^10.1.0",
3232
"react-syntax-highlighter": "^15.6.1",
3333
"remark-gfm": "^4.0.1",
34+
"swr": "^2.3.6",
3435
"zod": "^4.0.17"
3536
},
3637
"devDependencies": {

0 commit comments

Comments
 (0)