-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
feat: add TOC sidebar for README and Markdown files #36250
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
Open
hamkido
wants to merge
22
commits into
go-gitea:main
Choose a base branch
from
hamkido:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+394
−11
Open
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
19fa68a
feat: add TOC sidebar for README and Markdown files
hamkido 098321f
fix: use CSS variable for font-weight in TOC sidebar
hamkido f543ab2
Merge branch 'main' into main
hamkido 1348cb0
Merge branch 'go-gitea:main' into main
hamkido 2d7b11e
feat: add TOC sidebar for README and Markdown files
hamkido e7fb9af
refactor: enhance TOC position updates with ResizeObserver and Inters…
hamkido 7386cc0
refactor: rename TOC to sidebar and update related functionality
hamkido 694d551
refactor: improve sidebar positioning and visibility logic
hamkido ed9eb57
feat: introduce SidebarTocHeaders for improved sidebar TOC generation
hamkido b92be9c
refactor: streamline sidebar positioning logic
hamkido 1aa08e2
refactor: enhance sidebar height and visibility logic
hamkido 4687803
fix: use requestAnimationFrame for smooth sidebar scroll tracking
hamkido 55e2cd3
fix: use correct segment selector for TOC sidebar positioning
hamkido cbbbf09
fix: lower TOC sidebar breakpoint to 768px for tablet support
hamkido ba42ce0
fix: use btn-octicon style for TOC toggle button
hamkido 548e7ba
fix: remove explicit icon size for TOC button to match other btn-octicon
hamkido 4267f69
fix: reset button default styles for btn-octicon
hamkido 3c5621c
refactor: replace scroll event with IntersectionObserver for sidebar …
hamkido fd754aa
refactor: clean up sidebar TOC structure comments
hamkido dc873a7
style: enhance sidebar toggle button appearance
hamkido c5a65b9
refactor: remove deprecated SidebarTocNode and clean up sidebar TOC r…
hamkido 1aea6c4
Update modules/markup/sidebar_toc.go
hamkido 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
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 |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // Copyright 2024 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package markup | ||
|
|
||
| import ( | ||
| "html" | ||
| "html/template" | ||
| "net/url" | ||
| "strings" | ||
|
|
||
| "code.gitea.io/gitea/modules/translation" | ||
| ) | ||
|
|
||
| // RenderSidebarTocHTML renders a list of headers into HTML for sidebar TOC display. | ||
| // It generates a <details> element with nested <ul> lists representing the header hierarchy. | ||
| func RenderSidebarTocHTML(headers []Header, lang string) template.HTML { | ||
| if len(headers) == 0 { | ||
| return "" | ||
| } | ||
|
|
||
| var sb strings.Builder | ||
|
|
||
| // Start with <details open> | ||
| sb.WriteString(`<details open>`) | ||
| sb.WriteString(`<summary>`) | ||
| sb.WriteString(html.EscapeString(translation.NewLocale(lang).TrString("toc"))) | ||
| sb.WriteString(`</summary>`) | ||
|
|
||
| // Find the minimum level to start with | ||
| minLevel := 6 | ||
| for _, header := range headers { | ||
| if header.Level < minLevel { | ||
| minLevel = header.Level | ||
| } | ||
| } | ||
|
|
||
| // Build nested list structure | ||
| currentLevel := minLevel | ||
| sb.WriteString(`<ul>`) | ||
| openLists := 1 | ||
|
|
||
| for _, header := range headers { | ||
| // Close lists if we need to go up levels | ||
| for currentLevel > header.Level { | ||
| sb.WriteString(`</ul>`) | ||
| openLists-- | ||
| currentLevel-- | ||
| } | ||
|
|
||
| // Open new lists if we need to go down levels | ||
| for currentLevel < header.Level { | ||
| sb.WriteString(`<ul>`) | ||
| openLists++ | ||
| currentLevel++ | ||
| } | ||
|
|
||
| // Write the list item with link | ||
| sb.WriteString(`<li>`) | ||
| sb.WriteString(`<a href="#`) | ||
| sb.WriteString(url.QueryEscape(header.ID)) | ||
| sb.WriteString(`">`) | ||
| sb.WriteString(html.EscapeString(header.Text)) | ||
| sb.WriteString(`</a>`) | ||
| sb.WriteString(`</li>`) | ||
| } | ||
|
|
||
| // Close all remaining open lists | ||
| for openLists > 0 { | ||
| sb.WriteString(`</ul>`) | ||
| openLists-- | ||
| } | ||
|
|
||
| sb.WriteString(`</details>`) | ||
|
|
||
| return template.HTML(sb.String()) | ||
| } | ||
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
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
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
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.