Skip to content

Commit 4579a9c

Browse files
authored
Merge pull request #462 from 1chooo/feat/#460
feat(markdown): refactor paragraph rendering and add Paragraph component (#460)
2 parents dd15f3c + 30e859f commit 4579a9c

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

Diff for: apps/web/src/components/markdown/markdown-renderer.tsx

+3-12
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@ import Anchor from './anchor';
88
import BlockQuote from './block-quote';
99
import CodeBlock from './code-block';
1010
import MarkdownImage from './markdown-image';
11+
import Paragraph from './paragraph';
1112

1213
interface MarkdownRendererProps {
1314
className?: string;
1415
content: string;
1516
}
1617

17-
const isImageNode = (node: any): node is Element => {
18-
return node && node.type === 'element' && node.tagName === 'img';
19-
};
20-
2118
const MarkdownRenderer: React.FC<MarkdownRendererProps> = ({
2219
className,
2320
content
@@ -27,14 +24,8 @@ const MarkdownRenderer: React.FC<MarkdownRendererProps> = ({
2724
remarkPlugins={[remarkGfm]}
2825
rehypePlugins={[rehypeRaw, rehypeGithubAlerts]}
2926
components={{
30-
p: (props) => {
31-
const { node, children, ...rest } = props;
32-
const hasImage = node && node.children && node.children.some(isImageNode);
33-
if (hasImage) {
34-
return <>{children}</>;
35-
}
36-
return <p {...rest}>{children}</p>;
37-
},
27+
p: ({ node, children, ...props }) =>
28+
<Paragraph node={node} children={children} {...props} />,
3829
a: (props) => <Anchor {...props} />,
3930
sup: 'sup',
4031
sub: 'sub',

Diff for: apps/web/src/components/markdown/paragraph.tsx

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React from 'react';
2+
3+
interface ParagraphProps {
4+
node: any;
5+
children: React.ReactNode;
6+
[key: string]: any;
7+
}
8+
9+
const isImageNode = (node: any): boolean => {
10+
return node &&
11+
node.type === 'element' &&
12+
node.tagName === 'img';
13+
};
14+
15+
/**
16+
* A functional component that renders a paragraph element.
17+
* If the paragraph contains an image node, it will render the children directly without a <p> tag.
18+
*
19+
* @param props - The properties passed to the component.
20+
* @param props.node - The node object which may contain children nodes.
21+
* @param props.children - The child elements to be rendered inside the paragraph.
22+
* @param rest - Any additional properties passed to the paragraph element.
23+
*
24+
* @returns A JSX element representing the paragraph or its children.
25+
*/
26+
const Paragraph: React.FC<ParagraphProps> = (props) => {
27+
const { node, children, ...rest } = props;
28+
const hasImage = node &&
29+
node.children &&
30+
node.children.some(isImageNode);
31+
32+
if (hasImage) {
33+
return <>{children}</>;
34+
}
35+
36+
return <p {...rest}>{children}</p>;
37+
};
38+
39+
export default Paragraph;

0 commit comments

Comments
 (0)