File tree 2 files changed +42
-12
lines changed
apps/web/src/components/markdown
2 files changed +42
-12
lines changed Original file line number Diff line number Diff line change @@ -8,16 +8,13 @@ import Anchor from './anchor';
8
8
import BlockQuote from './block-quote' ;
9
9
import CodeBlock from './code-block' ;
10
10
import MarkdownImage from './markdown-image' ;
11
+ import Paragraph from './paragraph' ;
11
12
12
13
interface MarkdownRendererProps {
13
14
className ?: string ;
14
15
content : string ;
15
16
}
16
17
17
- const isImageNode = ( node : any ) : node is Element => {
18
- return node && node . type === 'element' && node . tagName === 'img' ;
19
- } ;
20
-
21
18
const MarkdownRenderer : React . FC < MarkdownRendererProps > = ( {
22
19
className,
23
20
content
@@ -27,14 +24,8 @@ const MarkdownRenderer: React.FC<MarkdownRendererProps> = ({
27
24
remarkPlugins = { [ remarkGfm ] }
28
25
rehypePlugins = { [ rehypeRaw , rehypeGithubAlerts ] }
29
26
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 } /> ,
38
29
a : ( props ) => < Anchor { ...props } /> ,
39
30
sup : 'sup' ,
40
31
sub : 'sub' ,
Original file line number Diff line number Diff line change
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 ;
You can’t perform that action at this time.
0 commit comments