-
Notifications
You must be signed in to change notification settings - Fork 19
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
1 parent
fef166b
commit 58a9b85
Showing
16 changed files
with
2,209 additions
and
1,497 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,70 @@ | ||
import React, { ReactElement, useMemo } from 'react'; | ||
import ReactMarkdown from 'react-markdown'; | ||
import React from 'react'; | ||
import ReactMarkdown, { Components } from 'react-markdown'; | ||
|
||
import { getMarkdownElements } from './Markdown'; | ||
import { getMarkdownElements } from './Markdown/markdownElements'; | ||
|
||
type SupportedComponents = Pick< | ||
Components, | ||
'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'p' | 'em' | 'strong' | 'ul' | 'ol' | 'li' | ||
> & { | ||
listItem: SupportedComponents['li']; | ||
emphasis: SupportedComponents['em']; | ||
}; | ||
|
||
type FormattedContentProps = { | ||
className?: string; | ||
children: string; | ||
components?: { | ||
h1?: () => ReactElement; | ||
h2?: () => ReactElement; | ||
h3?: () => ReactElement; | ||
h4?: () => ReactElement; | ||
h5?: () => ReactElement; | ||
h6?: () => ReactElement; | ||
p?: () => ReactElement; | ||
em?: () => ReactElement; | ||
strong?: () => ReactElement; | ||
ul?: () => ReactElement; | ||
ol?: () => ReactElement; | ||
li?: () => ReactElement; | ||
}; | ||
components?: SupportedComponents; | ||
}; | ||
|
||
const { | ||
h1: H1, | ||
h2: H2, | ||
h3: H3, | ||
h4: H4, | ||
h5: H5, | ||
h6: H6, | ||
ol: Ol, | ||
ul: Ul, | ||
li: ListItem, | ||
em: Emphasis, | ||
p: P, | ||
strong: Strong | ||
} = getMarkdownElements(); | ||
|
||
const renderers: SupportedComponents = { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
h1: ({ node, ...props }) => <H1 {...props} />, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
h2: ({ node, ...props }) => <H2 {...props} />, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
h3: ({ node, ...props }) => <H3 {...props} />, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
h4: ({ node, ...props }) => <H4 {...props} />, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
h5: ({ node, ...props }) => <H5 {...props} />, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
h6: ({ node, ...props }) => <H6 {...props} />, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
p: ({ node, children, ...props }) => <P {...props}>{children}</P>, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
emphasis: ({ node, children, ...props }) => <Emphasis {...props}>{children}</Emphasis>, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
strong: ({ node, children, ...props }) => <Strong {...props}>{children}</Strong>, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
ul: ({ node, children, ...props }) => <Ul {...props}>{children}</Ul>, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
ol: ({ node, children, ...props }) => <Ol {...props}>{children}</Ol>, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
listItem: ({ node, children, ...props }) => <ListItem {...props}>{children}</ListItem> | ||
}; | ||
|
||
export const FormattedContent: React.FC<React.PropsWithChildren<FormattedContentProps>> = ({ | ||
children, | ||
components = {}, | ||
...rest | ||
}) => { | ||
const renderers = useMemo(() => { | ||
const { | ||
h1: H1, | ||
h2: H2, | ||
h3: H3, | ||
h4: H4, | ||
h5: H5, | ||
h6: H6, | ||
ol: Ol, | ||
ul: Ul, | ||
li: ListItem, | ||
em: Emphasis, | ||
p: P, | ||
strong: Strong | ||
} = getMarkdownElements(); | ||
|
||
return { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
h1: ({ node, ...props }) => <H1 {...props} />, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
h2: ({ node, ...props }) => <H2 {...props} />, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
h3: ({ node, ...props }) => <H3 {...props} />, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
h4: ({ node, ...props }) => <H4 {...props} />, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
h5: ({ node, ...props }) => <H5 {...props} />, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
h6: ({ node, ...props }) => <H6 {...props} />, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
p: ({ node, children, ...props }) => <P {...props}>{children}</P>, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
emphasis: ({ node, children, ...props }) => <Emphasis {...props}>{children}</Emphasis>, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
strong: ({ node, children, ...props }) => <Strong {...props}>{children}</Strong>, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
ul: ({ node, children, ...props }) => <Ul {...props}>{children}</Ul>, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
ol: ({ node, children, ...props }) => <Ol {...props}>{children}</Ol>, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
listItem: ({ node, children, ...props }) => <ListItem {...props}>{children}</ListItem> | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<ReactMarkdown {...rest} components={{ ...renderers, ...components }}> | ||
{children} | ||
</ReactMarkdown> | ||
); | ||
}; | ||
}) => ( | ||
<ReactMarkdown {...rest} components={{ ...renderers, ...components }}> | ||
{children} | ||
</ReactMarkdown> | ||
); |
Oops, something went wrong.