Skip to content

Commit 7527879

Browse files
authored
Merge pull request #19 from ut-code/TOC
各章の目次表示
2 parents 8a93a9c + 6899f4e commit 7527879

File tree

6 files changed

+81
-34
lines changed

6 files changed

+81
-34
lines changed

app/[docs_id]/page.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ export default async function Page({
4141
return (
4242
<div className="p-4">
4343
{splitMdContent.map((section, index) => (
44-
<Section key={index} section={section} />
44+
<div key={index} id={`${index}`}>
45+
<Section key={index} section={section} />
46+
</div>
4547
))}
4648
</div>
4749
);

app/[docs_id]/splitMarkdown.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
"use server";
2-
31
import { unified } from "unified";
42
import remarkParse from "remark-parse";
53
import remarkGfm from "remark-gfm";
@@ -13,9 +11,9 @@ export interface MarkdownSection {
1311
* Markdownコンテンツを見出しごとに分割し、
1412
* 見出しのレベルとタイトル、内容を含むオブジェクトの配列を返す。
1513
*/
16-
export async function splitMarkdown(
14+
export function splitMarkdown(
1715
content: string
18-
): Promise<MarkdownSection[]> {
16+
): MarkdownSection[] {
1917
const tree = unified().use(remarkParse).use(remarkGfm).parse(content);
2018
// console.log(tree.children.map(({ type, position }) => ({ type, position: JSON.stringify(position) })));
2119
const headingNodes = tree.children.filter((node) => node.type === "heading");

app/sidebar.tsx

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,65 @@
1+
"use client";
12
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())
28

39
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 ?? "")
435
return (
536
<div className="bg-base-200 min-h-full w-80 p-4">
637
{/* todo: 背景色ほんとにこれでいい? */}
738
<h2 className="hidden lg:block text-xl font-bold mb-4">
839
{/* サイドバーが常時表示されている場合のみ */}
940
Navbar Title
1041
</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+
))}
3961
</ol>
4062
</div>
4163
);
4264
}
65+

app/terminal/python/pyodide.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ export function PyodideProvider({ children }: { children: ReactNode }) {
289289
return output;
290290
});
291291
},
292-
[files]
292+
[files, writeFile]
293293
);
294294

295295
/**

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)