-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: docs anchor links for headings (#214)
closes #136 once merged, all docs headings will turn into a hyperlink with an anchor / ID. Simply click the heading you wish to get the URL for, and it will populate in your navigation bar.
- Loading branch information
Showing
2 changed files
with
47 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { ReactNode } from "react"; | ||
|
||
function textToKebabCase(text: string): string { | ||
// Convert to lowercase | ||
let kebabCase = text.toLowerCase(); | ||
|
||
// Replace non-alphanumeric characters with hyphens | ||
kebabCase = kebabCase.replace(/[^a-z0-9]+/g, "-"); | ||
|
||
// Remove leading and trailing hyphens | ||
kebabCase = kebabCase.replace(/^-+|-+$/g, ""); | ||
|
||
return kebabCase; | ||
} | ||
|
||
const DocsHeading = ({ | ||
as, | ||
children, | ||
...restProps | ||
}: { | ||
as: "h1" | "h2" | "h3" | "h4" | "h5" | "h6"; | ||
children?: ReactNode; | ||
}) => { | ||
const Tag = as; | ||
const id = textToKebabCase(children?.toString() || ""); | ||
return ( | ||
<Tag {...restProps} style={{ position: "relative" }}> | ||
<span | ||
style={{ position: "absolute", top: -80 }} | ||
id={id} | ||
role="presentation" | ||
></span> | ||
<a href={`#${id}`} style={{ textDecoration: "none", color: "inherit" }}> | ||
{children} | ||
</a> | ||
</Tag> | ||
); | ||
}; | ||
|
||
export default DocsHeading; |
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