Skip to content
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

Heading anchor links #247

Merged
merged 6 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion components/content/Content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import Solution from "./Solution"
import { first } from "cypress/types/lodash"
import remarkDirective from "remark-directive"
import remarkDirectiveRehype from "remark-directive-rehype"
import { CodeComponent, CodeProps, ReactMarkdownProps } from "react-markdown/lib/ast-to-react"
import { CodeComponent, CodeProps, HeadingProps, ReactMarkdownProps } from "react-markdown/lib/ast-to-react"
import Callout from "../Callout"
import { Course, Section, Theme } from "lib/material"
import Paragraph from "./Paragraph"
import Heading from "./Heading"

function reactMarkdownRemarkDirective() {
return (tree: any) => {
Expand Down Expand Up @@ -56,6 +57,13 @@ const list = (sectionStr: string) => {
return list
}

const h = (sectionStr: string, tag: string) => {
function h({ node, children, ...props }: HeadingProps) {
return <Heading content={children} section={sectionStr} tag={tag} />
}
return h
}

let solutionCount = 0
function solution({ node, children, ...props }: ReactMarkdownProps) {
solutionCount++
Expand Down Expand Up @@ -161,6 +169,9 @@ const Content: React.FC<Props> = ({ markdown, theme, course, section }) => {
code,
p: p(sectionStr),
li: list(sectionStr),
h2: h(sectionStr, "h2"),
h3: h(sectionStr, "h3"),
h4: h(sectionStr, "h4"),
}}
>
{markdown}
Expand Down
43 changes: 43 additions & 0 deletions components/content/Heading.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React, { useState } from "react"
import CopyToClipboard from "react-copy-to-clipboard"
import { FaLink } from "react-icons/fa"

interface HeadingProps {
content: React.ReactNode
section: string
tag: string
}

const Heading: React.FC<HeadingProps> = ({ content, section, tag }) => {
const Tag = tag as keyof JSX.IntrinsicElements
const headingContent = content?.toString().replaceAll(" ", "-")
const [isCopied, setIsCopied] = useState(false)

const generateHeadingURL = () => {
const href: string = typeof window !== "undefined" ? window.location.href.split("#")[0] : ""
return href + "#" + headingContent ?? ""
}

const onCopyHandler = () => {
setIsCopied(true)
setTimeout(() => {
setIsCopied(false)
}, 1500)
}

return (
<>
<Tag id={headingContent} className="flex gap-3">
{content}
<CopyToClipboard text={generateHeadingURL()}>
<button className="text-xs align-top" onClick={onCopyHandler}>
<FaLink className="group-hover:text-white" />
</button>
</CopyToClipboard>
{isCopied && <span className="text-xs text-green-500 flex items-center">Copied to clipboard!</span>}
</Tag>
</>
)
}

export default Heading