|
| 1 | +"use client"; |
1 | 2 | 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 | + |
2 | 11 |
|
3 | 12 | 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 ?? "") |
4 | 38 | return ( |
5 | 39 | <div className="bg-base-200 min-h-full w-80 p-4"> |
6 | 40 | {/* todo: 背景色ほんとにこれでいい? */} |
7 | 41 | <h2 className="hidden lg:block text-xl font-bold mb-4"> |
8 | 42 | {/* サイドバーが常時表示されている場合のみ */} |
9 | 43 | Navbar Title |
10 | 44 | </h2> |
| 45 | + |
11 | 46 | <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 | + ))} |
39 | 62 | </ol> |
40 | 63 | </div> |
41 | 64 | ); |
42 | 65 | } |
| 66 | + |
0 commit comments