This repository was archived by the owner on Jan 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
fix: table of content indentation based on heading level #203
Merged
sauravpanda
merged 3 commits into
main
from
202-bug-table-of-content-is-not-showing-all-the-items-correctly
Dec 10, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -17,7 +17,7 @@ export function TableOfContents({ publishDate, modifiedDate, author, locale }: T | |||||
| const t = getTranslation(locale as keyof typeof locales); | ||||||
|
|
||||||
| useEffect(() => { | ||||||
| const elements = Array.from(document.querySelectorAll('h2:not([data-toc-ignore]), h3:not([data-toc-ignore]), h4:not([data-toc-ignore])')); | ||||||
| const elements = Array.from(document.querySelectorAll('h2:not([data-toc-ignore]), h3:not([data-toc-ignore]), h4:not([data-toc-ignore]), h5:not([data-toc-ignore])')); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment: Inefficient DOM querying in useEffect. Solution: Consider using a more efficient method to track headings or optimize the way headings are queried.
Suggested change
|
||||||
| setHeadings(elements as HTMLHeadingElement[]); | ||||||
|
|
||||||
| const observer = new IntersectionObserver( | ||||||
|
|
@@ -106,19 +106,35 @@ export function TableOfContents({ publishDate, modifiedDate, author, locale }: T | |||||
|
|
||||||
| <ul className="space-y-2"> | ||||||
| {headings.map((heading) => { | ||||||
| const level = parseInt(heading.tagName[1]) - 2; | ||||||
| const level = parseInt(heading.tagName[1]) - 1; | ||||||
| const indentClass = { | ||||||
| 0: '', | ||||||
| 1: 'ml-3', // h2 | ||||||
| 2: 'ml-6', // h3 | ||||||
| 3: 'ml-9', // h4 | ||||||
| 4: 'ml-12', // h5 | ||||||
| 5: 'ml-15', // h6 | ||||||
| }[level] || ''; | ||||||
|
|
||||||
| console.log(heading.textContent, level, indentClass); | ||||||
| return ( | ||||||
| <li key={heading.id}> | ||||||
| <a | ||||||
| href={`#${heading.id}`} | ||||||
| onClick={(e) => handleClick(e, heading.id)} | ||||||
| className={` | ||||||
| text-sm block px-3 py-2 transition-colors duration-200 rounded-md | ||||||
| ${level > 0 ? 'pl-' + (level * 4 + 3) : ''} | ||||||
| text-sm block px-3 py-2 rounded-md | ||||||
| ${indentClass} | ||||||
| transition-all duration-200 ease-in-out | ||||||
| relative | ||||||
| before:absolute before:left-0 before:top-1/2 before:-translate-y-1/2 | ||||||
| before:h-4 before:w-0.5 before:bg-primary before:opacity-0 | ||||||
| before:transition-all before:duration-200 | ||||||
| hover:before:opacity-100 | ||||||
| ${ | ||||||
| activeId === heading.id | ||||||
| ? 'text-primary font-medium bg-primary/5' | ||||||
| : 'text-muted-foreground hover:text-foreground hover:bg-muted/40' | ||||||
| ? 'text-primary font-medium bg-primary/5 before:opacity-100' | ||||||
| : 'text-muted-foreground hover:text-foreground hover:bg-muted/40 hover:translate-x-1' | ||||||
| } | ||||||
| `} | ||||||
| > | ||||||
|
|
||||||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.