-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add table of contents for docs pages (#308)
Co-authored-by: Adil Kairolla <[email protected]> Co-authored-by: SeanCassiere <[email protected]>
- Loading branch information
1 parent
f0505cd
commit 6bfd2c8
Showing
17 changed files
with
195 additions
and
66 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,40 +1,93 @@ | ||
import * as React from 'react' | ||
import { FaEdit } from 'react-icons/fa' | ||
import { marked } from 'marked' | ||
import markedAlert from 'marked-alert' | ||
import { gfmHeadingId, getHeadingList } from 'marked-gfm-heading-id' | ||
import { DocTitle } from '~/components/DocTitle' | ||
import { Markdown } from '~/components/Markdown' | ||
import { Toc } from './Toc' | ||
import { twMerge } from 'tailwind-merge' | ||
|
||
type DocProps = { | ||
title: string | ||
content: string | ||
repo: string | ||
branch: string | ||
filePath: string | ||
shouldRenderToc?: boolean | ||
colorFrom?: string | ||
colorTo?: string | ||
} | ||
|
||
export function Doc({ | ||
title, | ||
content, | ||
repo, | ||
branch, | ||
filePath, | ||
}: { | ||
title: string | ||
content: string | ||
repo: string | ||
branch: string | ||
filePath: string | ||
}) { | ||
shouldRenderToc = false, | ||
colorFrom, | ||
colorTo, | ||
}: DocProps) { | ||
const { markup, headings } = React.useMemo(() => { | ||
const markup = marked.use( | ||
{ gfm: true }, | ||
gfmHeadingId(), | ||
markedAlert() | ||
)(content) as string | ||
|
||
const headings = getHeadingList() | ||
|
||
return { markup, headings } | ||
}, [content]) | ||
|
||
const isTocVisible = shouldRenderToc && headings && headings.length > 1 | ||
|
||
return ( | ||
<div className="p-4 lg:p-6 overflow-auto w-full bg-white/70 dark:bg-black/30 m-2 md:m-4 xl:m-8 rounded-xl"> | ||
{title ? <DocTitle>{title}</DocTitle> : null} | ||
<div className="h-4" /> | ||
<div className="h-px bg-gray-500 opacity-20" /> | ||
<div className="h-4" /> | ||
<div className="prose prose-gray prose-sm prose-p:leading-7 dark:prose-invert max-w-none"> | ||
<Markdown code={content} /> | ||
</div> | ||
<div className="h-12" /> | ||
<div className="w-full h-px bg-gray-500 opacity-30" /> | ||
<div className="py-4 opacity-70"> | ||
<a | ||
href={`https://github.com/${repo}/tree/${branch}/${filePath}`} | ||
className="flex items-center gap-2" | ||
<div className="w-full p-2 md:p-4 xl:p-8"> | ||
<div | ||
className={twMerge( | ||
'flex bg-white/70 dark:bg-black/30 mx-auto rounded-xl max-w-[936px]', | ||
isTocVisible && 'max-w-full' | ||
)} | ||
> | ||
<div | ||
className={twMerge( | ||
'flex overflow-auto flex-col w-full p-4 lg:p-6', | ||
isTocVisible && 'border-r border-gray-500/20 !pr-0' | ||
)} | ||
> | ||
<FaEdit /> Edit on GitHub | ||
</a> | ||
{title ? <DocTitle>{title}</DocTitle> : null} | ||
<div className="h-4" /> | ||
<div className="h-px bg-gray-500 opacity-20" /> | ||
<div className="h-4" /> | ||
<div | ||
className={twMerge( | ||
'prose prose-gray prose-sm prose-p:leading-7 dark:prose-invert max-w-none', | ||
isTocVisible && 'pr-4 lg:pr-6' | ||
)} | ||
> | ||
<Markdown htmlMarkup={markup} /> | ||
</div> | ||
<div className="h-12" /> | ||
<div className="w-full h-px bg-gray-500 opacity-30" /> | ||
<div className="py-4 opacity-70"> | ||
<a | ||
href={`https://github.com/${repo}/tree/${branch}/${filePath}`} | ||
className="flex items-center gap-2" | ||
> | ||
<FaEdit /> Edit on GitHub | ||
</a> | ||
</div> | ||
<div className="h-24" /> | ||
</div> | ||
|
||
{isTocVisible && ( | ||
<div className="max-w-52 w-full hidden 2xl:block transition-all"> | ||
<Toc headings={headings} colorFrom={colorFrom} colorTo={colorTo} /> | ||
</div> | ||
)} | ||
</div> | ||
<div className="h-24" /> | ||
</div> | ||
) | ||
} |
This file contains 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 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 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,59 @@ | ||
import * as React from 'react' | ||
import { twMerge } from 'tailwind-merge' | ||
import { HeadingData } from 'marked-gfm-heading-id' | ||
import { useLocation } from '@tanstack/react-router' | ||
|
||
const headingLevels: Record<number, string> = { | ||
1: 'pl-2', | ||
2: 'pl-2', | ||
3: 'pl-6', | ||
4: 'pl-10', | ||
5: 'pl-14', | ||
6: 'pl-16', | ||
} | ||
|
||
type TocProps = { | ||
headings: HeadingData[] | ||
colorFrom?: string | ||
colorTo?: string | ||
} | ||
|
||
export function Toc({ headings, colorFrom, colorTo }: TocProps) { | ||
const location = useLocation() | ||
|
||
const [hash, setHash] = React.useState('') | ||
|
||
React.useEffect(() => { | ||
setHash(location.hash) | ||
}, [location]) | ||
|
||
return ( | ||
<nav className="flex flex-col p-2 gap-1 sticky top-2 max-h-screen"> | ||
<h3 className="text-[.9em] font-medium px-2">On this page</h3> | ||
|
||
<ul | ||
className={twMerge('flex flex-col overflow-y-auto gap-0.5 text-[.8em]')} | ||
> | ||
{headings?.map((heading) => ( | ||
<li | ||
key={heading.id} | ||
className={twMerge( | ||
'cursor-pointer py-[.1rem] w-full rounded-lg hover:bg-gray-500 hover:bg-opacity-10', | ||
headingLevels[heading.level] | ||
)} | ||
> | ||
<a | ||
title={heading.id} | ||
href={`#${heading.id}`} | ||
aria-current={hash === heading.id && 'location'} | ||
className={`truncate block aria-current:bg-gradient-to-r ${colorFrom} ${colorTo} aria-current:bg-clip-text aria-current:text-transparent`} | ||
dangerouslySetInnerHTML={{ | ||
__html: heading.text, | ||
}} | ||
/> | ||
</li> | ||
))} | ||
</ul> | ||
</nav> | ||
) | ||
} |
This file contains 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 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 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 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 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -56,7 +56,7 @@ function LoginComp() { | |
> | ||
{d} | ||
</div> | ||
), | ||
) | ||
)} | ||
</div> | ||
</Link> | ||
|
Oops, something went wrong.