@@ -6,18 +6,31 @@ import { splitMarkdown } from "./[docs_id]/splitMarkdown";
66import { pagesList } from "./pagesList" ;
77import { AccountMenu } from "./accountMenu" ;
88import { ThemeToggle } from "./[docs_id]/themeToggle" ;
9+ import { useDynamicMdContextOptional } from "./[docs_id]/dynamicMdContext" ;
910
1011const fetcher : Fetcher < string , string > = ( url ) =>
1112 fetch ( url ) . then ( ( r ) => r . text ( ) ) ;
1213
1314export function Sidebar ( ) {
1415 const pathname = usePathname ( ) ;
1516 const docs_id = pathname . replace ( / ^ \/ / , "" ) ;
16- const { data, error, isLoading } = useSWR ( `/docs/${ docs_id } .md` , fetcher ) ;
17+ const dynamicMdContextValue = useDynamicMdContextOptional ( ) ;
18+
19+ // コンテキストが利用可能な場合はそれを使用し、そうでない場合はフェッチする
20+ const { data, error, isLoading } = useSWR (
21+ dynamicMdContextValue ? null : `/docs/${ docs_id } .md` ,
22+ fetcher
23+ ) ;
1724
1825 if ( error ) console . error ( "Sidebar fetch error:" , error ) ;
1926
20- const splitmdcontent = splitMarkdown ( data ?? "" ) ;
27+ // コンテキストがある場合はそれを使用、ない場合はフェッチしたデータを使用
28+ const splitmdcontent = dynamicMdContextValue ?. dynamicMdContent ?? splitMarkdown ( data ?? "" ) ;
29+
30+ // 現在表示中のセクション(最初にinViewがtrueのもの)を見つける
31+ const currentSectionIndex = dynamicMdContextValue ?. dynamicMdContent . findIndex (
32+ ( section ) => section . inView
33+ ) ?? - 1 ;
2134 return (
2235 < div className = "bg-base-200 h-full w-80 overflow-y-auto" >
2336 { /* todo: 背景色ほんとにこれでいい? */ }
@@ -67,14 +80,23 @@ export function Sidebar() {
6780 </ Link >
6881 { `${ group . id } -${ page . id } ` === docs_id && ! isLoading && (
6982 < ul className = "ml-4 text-sm" >
70- { splitmdcontent . slice ( 1 ) . map ( ( section , idx ) => (
71- < li
72- key = { idx }
73- style = { { marginLeft : section . level - 2 + "em" } }
74- >
75- < Link href = { `#${ idx + 1 } ` } > { section . title } </ Link >
76- </ li >
77- ) ) }
83+ { splitmdcontent . slice ( 1 ) . map ( ( section , idx ) => {
84+ // idx + 1 は実際のsectionIndexに対応(slice(1)で最初を除外しているため)
85+ const isCurrentSection = idx + 1 === currentSectionIndex ;
86+ return (
87+ < li
88+ key = { idx }
89+ style = { { marginLeft : section . level - 2 + "em" } }
90+ >
91+ < Link
92+ href = { `#${ idx + 1 } ` }
93+ className = { isCurrentSection ? "font-bold" : "" }
94+ >
95+ { section . title }
96+ </ Link >
97+ </ li >
98+ ) ;
99+ } ) }
78100 </ ul >
79101 ) }
80102 </ li >
0 commit comments