-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Dylan Decrulle <[email protected]>
- Loading branch information
Showing
11 changed files
with
77 additions
and
139 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
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 |
---|---|---|
@@ -1,39 +1,36 @@ | ||
import type { ComponentProps } from 'react'; | ||
import Markdown from 'react-markdown'; | ||
import { MDLabelLink } from './MDLabelLink'; | ||
import { Fragment, type PropsWithChildren } from 'react'; | ||
import Markdown, { type Components } from 'react-markdown'; | ||
import { MarkdownLink } from './MarkdownLink'; | ||
import remarkBreaks from 'remark-breaks'; | ||
import emoji from 'remark-emoji'; | ||
|
||
type Props = { expression: string }; | ||
|
||
export const MDLabel = ({ expression }: Props) => { | ||
export function MDLabel({ expression }: Props) { | ||
const hasParagraphs = /\n\n/.test(expression); | ||
const components = { | ||
p: hasParagraphs ? 'p' : Fragment, | ||
br: 'br', | ||
a: MarkdownA, | ||
} as Partial<Components>; | ||
return ( | ||
<Markdown | ||
components={renderComponentsFor(expression)} | ||
components={components} | ||
remarkPlugins={[[emoji, { accessible: true }], remarkBreaks]} | ||
> | ||
{expression} | ||
</Markdown> | ||
); | ||
}; | ||
|
||
const renderComponentsFor = ( | ||
expression: string | ||
): ComponentProps<typeof Markdown>['components'] => { | ||
const components = { | ||
p: (props) => <>{props.children}</>, | ||
br: 'br', | ||
a: (props) => ( | ||
<MDLabelLink {...({ ...props } as ComponentProps<typeof MDLabelLink>)} /> | ||
), | ||
} satisfies ComponentProps<typeof Markdown>['components']; | ||
|
||
if (/\n\n/.test(expression)) { | ||
return { | ||
...components, | ||
p: 'p', | ||
}; | ||
} | ||
|
||
return components; | ||
} | ||
const MarkdownA = ({ | ||
title, | ||
href, | ||
children, | ||
}: PropsWithChildren<{ title?: string; href: string }>) => { | ||
const tooltip = title ? <MDLabel expression={title} /> : null; | ||
return ( | ||
<MarkdownLink href={href} tooltip={tooltip}> | ||
{children} | ||
</MarkdownLink> | ||
); | ||
}; |
This file was deleted.
Oops, something went wrong.
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,43 @@ | ||
import { useId, type PropsWithChildren, type ReactNode } from 'react'; | ||
import { Tooltip } from 'react-tooltip'; | ||
import { RouterLink } from './RouterLink'; | ||
import { slottableComponent } from '../HOC/slottableComponent'; | ||
|
||
type Props = PropsWithChildren<{ href: string; tooltip?: ReactNode }>; | ||
|
||
export const MarkdownLink = slottableComponent<Props>( | ||
'MarkdownLink', | ||
({ href, children, tooltip }) => { | ||
const id = useId(); | ||
|
||
const extraProps = tooltip | ||
? { 'data-tooltip-id': `tooltip-${id}`, className: 'link-md' } | ||
: {}; | ||
const isAbsoluteLink = href.trim().startsWith('/'); | ||
|
||
return ( | ||
<> | ||
{isAbsoluteLink ? ( | ||
<RouterLink to={href} id={id} {...extraProps}> | ||
{children} | ||
</RouterLink> | ||
) : ( | ||
<a | ||
href={href} | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
id={id} | ||
{...extraProps} | ||
> | ||
{children} | ||
</a> | ||
)} | ||
{tooltip && ( | ||
<Tooltip className="tooltip-content" id={`tooltip-${id}`}> | ||
{tooltip} | ||
</Tooltip> | ||
)} | ||
</> | ||
); | ||
} | ||
); |
Empty file.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.