|
| 1 | +"use client"; |
1 | 2 | import Link from "next/link"; |
| 3 | +import { usePathname } from "next/navigation"; |
| 4 | +import useSWR, { Fetcher } from 'swr' |
| 5 | +import { splitMarkdown } from "./[docs_id]/splitMarkdown"; |
| 6 | + |
| 7 | +const fetcher: Fetcher<string, string> = (url) => fetch(url).then((r) => r.text()) |
2 | 8 |
|
3 | 9 | export function Sidebar() { |
| 10 | + const pathname = usePathname(); |
| 11 | + const docs_id = pathname.replace(/^\//, ""); |
| 12 | + const { data, error, isLoading } = useSWR( |
| 13 | + `/docs/${docs_id}.md`, |
| 14 | + fetcher |
| 15 | + ) |
| 16 | + |
| 17 | + 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. ジェネレータとデコレータ" }, |
| 27 | + ]; |
| 28 | + |
| 29 | + if (error) console.error("Sidebar fetch error:", error) |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | + const splitmdcontent = splitMarkdown(data ?? "") |
4 | 35 | return ( |
5 | 36 | <div className="bg-base-200 min-h-full w-80 p-4"> |
6 | 37 | {/* todo: 背景色ほんとにこれでいい? */} |
7 | 38 | <h2 className="hidden lg:block text-xl font-bold mb-4"> |
8 | 39 | {/* サイドバーが常時表示されている場合のみ */} |
9 | 40 | Navbar Title |
10 | 41 | </h2> |
11 | | - <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> |
| 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 | + )} |
| 58 | + |
| 59 | + </li> |
| 60 | + ))} |
39 | 61 | </ol> |
40 | 62 | </div> |
41 | 63 | ); |
42 | 64 | } |
| 65 | + |
0 commit comments