-
Notifications
You must be signed in to change notification settings - Fork 528
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
98 additions
and
4 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,42 @@ | ||
import { stripIndent } from '@/lib/utils'; | ||
import Markdown, { Components } from 'react-markdown'; | ||
import InlineCode from './InlineCode'; | ||
import { LuExternalLink } from 'react-icons/lu'; | ||
import { useLocation } from 'wouter'; | ||
|
||
function CustomAnchor({ href, children }: { href?: string, children: React.ReactNode }) { | ||
const setLocation = useLocation()[1]; | ||
const isExternal = href?.startsWith('http'); | ||
const onClick = () => { | ||
if (isExternal) { | ||
window.open(href, '_blank'); | ||
} else { | ||
setLocation(href ?? '/'); | ||
} | ||
} | ||
return <button onClick={onClick} className="text-pink-600 no-underline hover:underline"> | ||
{children} | ||
{isExternal && <LuExternalLink className="inline ml-1 mb-1" />} | ||
</button> | ||
} | ||
|
||
// NOTE: we might not even need this | ||
// https://tailwindcss.com/docs/typography-plugin#advanced-topics | ||
const customComponents: Components = { | ||
// blockquote: ({ children }) => <blockquote className="border-l-4 border-pink-600 pl-2">{children}</blockquote>, | ||
code: ({ children }) => <InlineCode className="not-prose">{children}</InlineCode>, | ||
pre: ({ children }) => <pre className="not-prose bg-muted p-2 rounded">{children}</pre>, | ||
a: ({ children, href }) => <CustomAnchor href={href}>{children}</CustomAnchor>, | ||
} | ||
|
||
|
||
type Props = { | ||
md: string; | ||
}; | ||
export default function MarkdownProse({ md }: Props) { | ||
return ( | ||
<Markdown components={customComponents} className="prose dark:prose-invert"> | ||
{stripIndent(md.replace(/\n/g, ' \n'))} | ||
</Markdown> | ||
); | ||
} |
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