-
Notifications
You must be signed in to change notification settings - Fork 0
Add Table of Contents extraction, sidebar, and authoring docs #27
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3ab1a45
Add Table of Contents extraction, sidebar, and authoring docs
KayleeWilliams 541d0fd
Fix heading-anchor mismatches with inline markup and digit slugs
KayleeWilliams 39da784
Narrow root exports and preserve colon/hash in heading titles
KayleeWilliams 1e6afb7
Use empty separator when flattening heading children
KayleeWilliams 67c1af0
Fix TOC anchor extraction
KayleeWilliams 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| "use client"; | ||
|
|
||
| import type { DocsTableOfContentsItem } from "leadtype/llm"; | ||
| import { useEffect, useMemo, useState } from "react"; | ||
| import { cn } from "@/lib/utils"; | ||
| import { Separator } from "./ui/separator"; | ||
|
|
||
| const HEADING_TOP_OFFSET = 104; | ||
| const ACTIVE_LINE_RATIO = 0.35; | ||
| const ACTIVE_LINE_MAX_OFFSET = 220; | ||
| const BOTTOM_SCROLL_TOLERANCE = 4; | ||
|
|
||
| interface TableOfContentsProps { | ||
| items: DocsTableOfContentsItem[]; | ||
| } | ||
|
|
||
| function flattenTocItems( | ||
| items: DocsTableOfContentsItem[] | ||
| ): DocsTableOfContentsItem[] { | ||
| return items.flatMap((item) => [item, ...flattenTocItems(item.children)]); | ||
| } | ||
|
|
||
| function TocItems({ | ||
| activeId, | ||
| onSelect, | ||
| items, | ||
| depth = 0, | ||
| }: { | ||
| activeId?: string; | ||
| onSelect: (id: string) => void; | ||
| items: DocsTableOfContentsItem[]; | ||
| depth?: number; | ||
| }) { | ||
| return ( | ||
| <ul className={cn("space-y-1", depth > 0 && "mt-1 pl-3")}> | ||
| {items.map((item) => ( | ||
| <li key={item.urlWithHash}> | ||
| <a | ||
| aria-current={activeId === item.id ? "location" : undefined} | ||
| className={cn( | ||
| "relative block rounded-md px-2 py-1 text-muted-foreground text-sm leading-5 transition-all duration-200 hover:bg-secondary hover:text-foreground", | ||
| "before:absolute before:inset-y-1 before:left-0 before:w-px before:origin-top before:scale-y-0 before:bg-foreground before:transition-transform before:duration-200", | ||
| depth > 0 && "text-xs", | ||
| activeId === item.id && | ||
| "translate-x-1 bg-secondary text-foreground before:scale-y-100" | ||
| )} | ||
| href={item.urlWithHash} | ||
| onClick={() => { | ||
| onSelect(item.id); | ||
| }} | ||
| > | ||
| {item.title} | ||
| </a> | ||
| {item.children.length > 0 ? ( | ||
| <TocItems | ||
| activeId={activeId} | ||
| depth={depth + 1} | ||
| items={item.children} | ||
| onSelect={onSelect} | ||
| /> | ||
| ) : null} | ||
| </li> | ||
| ))} | ||
| </ul> | ||
| ); | ||
| } | ||
|
|
||
| export function TableOfContents({ items }: TableOfContentsProps) { | ||
| const flatItems = useMemo(() => flattenTocItems(items), [items]); | ||
| const [activeId, setActiveId] = useState<string | undefined>( | ||
| flatItems[0]?.id | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| setActiveId(flatItems[0]?.id); | ||
| }, [flatItems]); | ||
|
|
||
| useEffect(() => { | ||
| if (flatItems.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| const headings = flatItems | ||
| .map((item) => document.getElementById(item.id)) | ||
| .filter((heading): heading is HTMLElement => Boolean(heading)); | ||
|
|
||
| const getActiveHeading = () => { | ||
| const scrollBottom = window.scrollY + window.innerHeight; | ||
| const pageBottom = document.documentElement.scrollHeight; | ||
|
|
||
| if (pageBottom - scrollBottom <= BOTTOM_SCROLL_TOLERANCE) { | ||
| return headings.at(-1); | ||
| } | ||
|
|
||
| const activeLine = Math.max( | ||
| HEADING_TOP_OFFSET, | ||
| Math.min( | ||
| window.innerHeight * ACTIVE_LINE_RATIO, | ||
| HEADING_TOP_OFFSET + ACTIVE_LINE_MAX_OFFSET | ||
| ) | ||
| ); | ||
|
|
||
| let activeHeading = headings[0]; | ||
| for (const heading of headings) { | ||
| if (heading.getBoundingClientRect().top <= activeLine) { | ||
| activeHeading = heading; | ||
| continue; | ||
| } | ||
|
|
||
| break; | ||
| } | ||
|
|
||
| return activeHeading; | ||
| }; | ||
|
|
||
| let animationFrame = 0; | ||
|
|
||
| const updateActiveHeading = () => { | ||
| animationFrame = 0; | ||
| setActiveId(getActiveHeading()?.id); | ||
| }; | ||
|
|
||
| const scheduleActiveHeadingUpdate = () => { | ||
| if (animationFrame !== 0) { | ||
| return; | ||
| } | ||
|
|
||
| animationFrame = window.requestAnimationFrame(updateActiveHeading); | ||
| }; | ||
|
|
||
| const updateFromHash = () => { | ||
| const rawHash = window.location.hash.slice(1); | ||
| let hashId = rawHash; | ||
| try { | ||
| hashId = decodeURIComponent(rawHash); | ||
| } catch { | ||
| // malformed % sequence; fall back to the raw hash | ||
| } | ||
| const hashHeading = headings.find((heading) => heading.id === hashId); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| if (hashHeading) { | ||
| setActiveId(hashHeading.id); | ||
| } | ||
| scheduleActiveHeadingUpdate(); | ||
| }; | ||
|
|
||
| updateFromHash(); | ||
|
|
||
| window.addEventListener("scroll", scheduleActiveHeadingUpdate, { | ||
| passive: true, | ||
| }); | ||
| window.addEventListener("resize", scheduleActiveHeadingUpdate); | ||
| window.addEventListener("hashchange", updateFromHash); | ||
|
|
||
| return () => { | ||
| if (animationFrame !== 0) { | ||
| window.cancelAnimationFrame(animationFrame); | ||
| } | ||
| window.removeEventListener("scroll", scheduleActiveHeadingUpdate); | ||
| window.removeEventListener("resize", scheduleActiveHeadingUpdate); | ||
| window.removeEventListener("hashchange", updateFromHash); | ||
| }; | ||
| }, [flatItems]); | ||
|
|
||
| if (items.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <aside className="sticky top-[calc(var(--docs-anchor-offset-rem)+0.75rem)] hidden max-h-[calc(100svh-var(--docs-anchor-offset-rem)-1.5rem)] self-start overflow-y-auto lg:block"> | ||
| <nav aria-label="On this page" className="space-y-3"> | ||
| <h2 className="px-2 font-medium text-foreground text-xs uppercase tracking-wider"> | ||
| On this page | ||
| </h2> | ||
| <Separator /> | ||
| <TocItems activeId={activeId} items={items} onSelect={setActiveId} /> | ||
| </nav> | ||
| </aside> | ||
| ); | ||
| } | ||
Oops, something went wrong.
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.
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.
🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: inthhq/leadtype
Length of output: 230
🏁 Script executed:
Repository: inthhq/leadtype
Length of output: 963
🏁 Script executed:
Repository: inthhq/leadtype
Length of output: 258
🏁 Script executed:
Repository: inthhq/leadtype
Length of output: 3822
🏁 Script executed:
Repository: inthhq/leadtype
Length of output: 403
Fix double-casts to
AgentReadabilityManifestin both files usingsatisfiesoperatorTwo instances of the problematic
as unknown as AgentReadabilityManifestpattern exist in the codebase (not just one). Both should be replaced consistently usingsatisfiesfor proper structural validation:apps/example/server/utils/agent-readability.ts:20apps/example/src/lib/docs-head.ts:11The
satisfiesoperator validates the object shape at compile time without forcing a type assertion, catching schema drift that double-casts would mask. Apply the same fix to both:Proposed fix for both files
export const agentReadabilityManifest = { ...manifestJson, - version: 1, -} as unknown as AgentReadabilityManifest; + version: 1 as const, +} satisfies AgentReadabilityManifest;(Same pattern applies to
docs-head.ts)Per coding guidelines: "Leverage TypeScript's type narrowing instead of type assertions" and "Use const assertions (
as const) for immutable values and literal types."📝 Committable suggestion
🤖 Prompt for AI Agents