-
Notifications
You must be signed in to change notification settings - Fork 1
Highlight current section in sidebar table of contents #79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9911330
Initial plan
Copilot d0c4523
Add context to share dynamicMdContent and highlight current section i…
Copilot b1af976
Move DynamicMdProvider to layout.tsx for sidebar access
Copilot f090192
Remove unnecessary fetch from sidebar and clear context on page change
Copilot 4850b07
Rename variables: dynamicMdContent→sidebarMdContent, localDynamicMdCo…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| "use client"; | ||
|
|
||
| import { createContext, ReactNode, useContext, useState } from "react"; | ||
| import { DynamicMarkdownSection } from "./pageContent"; | ||
|
|
||
| export interface IDynamicMdContext { | ||
| dynamicMdContent: DynamicMarkdownSection[]; | ||
| setDynamicMdContent: React.Dispatch<React.SetStateAction<DynamicMarkdownSection[]>>; | ||
| } | ||
|
|
||
| const DynamicMdContext = createContext<IDynamicMdContext | null>(null); | ||
|
|
||
| export function useDynamicMdContext() { | ||
| const context = useContext(DynamicMdContext); | ||
| if (!context) { | ||
| throw new Error( | ||
| "useDynamicMdContext must be used within a DynamicMdProvider" | ||
| ); | ||
| } | ||
| return context; | ||
| } | ||
|
|
||
| export function useDynamicMdContextOptional() { | ||
| return useContext(DynamicMdContext); | ||
| } | ||
|
|
||
| export function DynamicMdProvider({ | ||
| children, | ||
| }: { | ||
| children: ReactNode; | ||
| }) { | ||
| const [dynamicMdContent, setDynamicMdContent] = | ||
| useState<DynamicMarkdownSection[]>([]); | ||
|
|
||
| return ( | ||
| <DynamicMdContext.Provider value={{ dynamicMdContent, setDynamicMdContent }}> | ||
| {children} | ||
| </DynamicMdContext.Provider> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,18 +6,31 @@ import { splitMarkdown } from "./[docs_id]/splitMarkdown"; | |
| import { pagesList } from "./pagesList"; | ||
| import { AccountMenu } from "./accountMenu"; | ||
| import { ThemeToggle } from "./[docs_id]/themeToggle"; | ||
| import { useDynamicMdContext } from "./[docs_id]/dynamicMdContext"; | ||
|
|
||
| const fetcher: Fetcher<string, string> = (url) => | ||
| fetch(url).then((r) => r.text()); | ||
|
|
||
| export function Sidebar() { | ||
| const pathname = usePathname(); | ||
| const docs_id = pathname.replace(/^\//, ""); | ||
| const { data, error, isLoading } = useSWR(`/docs/${docs_id}.md`, fetcher); | ||
| const { dynamicMdContent } = useDynamicMdContext(); | ||
|
|
||
| // コンテキストが空の場合(まだページがロードされていない場合)はフェッチする | ||
| const { data, error, isLoading } = useSWR( | ||
| dynamicMdContent.length === 0 ? `/docs/${docs_id}.md` : null, | ||
| fetcher | ||
| ); | ||
|
||
|
|
||
| if (error) console.error("Sidebar fetch error:", error); | ||
|
|
||
| const splitmdcontent = splitMarkdown(data ?? ""); | ||
| // コンテキストがある場合はそれを使用、ない場合はフェッチしたデータを使用 | ||
| const splitmdcontent = dynamicMdContent.length > 0 ? dynamicMdContent : splitMarkdown(data ?? ""); | ||
|
|
||
| // 現在表示中のセクション(最初にinViewがtrueのもの)を見つける | ||
| const currentSectionIndex = dynamicMdContent.findIndex( | ||
| (section) => section.inView | ||
| ); | ||
| return ( | ||
| <div className="bg-base-200 h-full w-80 overflow-y-auto"> | ||
| {/* todo: 背景色ほんとにこれでいい? */} | ||
|
|
@@ -67,14 +80,23 @@ export function Sidebar() { | |
| </Link> | ||
| {`${group.id}-${page.id}` === docs_id && !isLoading && ( | ||
| <ul className="ml-4 text-sm"> | ||
| {splitmdcontent.slice(1).map((section, idx) => ( | ||
| <li | ||
| key={idx} | ||
| style={{ marginLeft: section.level - 2 + "em" }} | ||
| > | ||
| <Link href={`#${idx + 1}`}>{section.title}</Link> | ||
| </li> | ||
| ))} | ||
| {splitmdcontent.slice(1).map((section, idx) => { | ||
| // idx + 1 は実際のsectionIndexに対応(slice(1)で最初を除外しているため) | ||
| const isCurrentSection = idx + 1 === currentSectionIndex; | ||
| return ( | ||
| <li | ||
| key={idx} | ||
| style={{ marginLeft: section.level - 2 + "em" }} | ||
| > | ||
| <Link | ||
| href={`#${idx + 1}`} | ||
| className={isCurrentSection ? "font-bold" : ""} | ||
| > | ||
| {section.title} | ||
| </Link> | ||
| </li> | ||
| ); | ||
| })} | ||
| </ul> | ||
| )} | ||
| </li> | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot ページを切り替えた際に前のページのdynamicMdContentが残ってしまっています。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in f090192. Added cleanup effect that clears context when PageContent unmounts.