-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat(markdown): refactor paragraph rendering and add Paragraph component (#460)
- Loading branch information
Showing
2 changed files
with
42 additions
and
12 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React from 'react'; | ||
|
||
interface ParagraphProps { | ||
node: any; | ||
children: React.ReactNode; | ||
[key: string]: any; | ||
} | ||
|
||
const isImageNode = (node: any): boolean => { | ||
return node && | ||
node.type === 'element' && | ||
node.tagName === 'img'; | ||
}; | ||
|
||
/** | ||
* A functional component that renders a paragraph element. | ||
* If the paragraph contains an image node, it will render the children directly without a <p> tag. | ||
* | ||
* @param props - The properties passed to the component. | ||
* @param props.node - The node object which may contain children nodes. | ||
* @param props.children - The child elements to be rendered inside the paragraph. | ||
* @param rest - Any additional properties passed to the paragraph element. | ||
* | ||
* @returns A JSX element representing the paragraph or its children. | ||
*/ | ||
const Paragraph: React.FC<ParagraphProps> = (props) => { | ||
const { node, children, ...rest } = props; | ||
const hasImage = node && | ||
node.children && | ||
node.children.some(isImageNode); | ||
|
||
if (hasImage) { | ||
return <>{children}</>; | ||
} | ||
|
||
return <p {...rest}>{children}</p>; | ||
}; | ||
|
||
export default Paragraph; |