-
Notifications
You must be signed in to change notification settings - Fork 900
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core/presentation): allow markdown in ValidationMessage
- Loading branch information
1 parent
aef69cf
commit 075bce1
Showing
5 changed files
with
38 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.Markdown { | ||
p { | ||
margin: 0; | ||
} | ||
|
||
p + p { | ||
margin-top: 4px; | ||
} | ||
} |
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,40 +1,40 @@ | ||
import * as React from 'react'; | ||
import { HtmlRenderer, Parser } from 'commonmark'; | ||
import * as DOMPurify from 'dompurify'; | ||
import './Markdown.less'; | ||
|
||
export interface IMarkdownProps { | ||
[key: string]: any; | ||
|
||
/** markdown */ | ||
message: string; | ||
|
||
/** optional tag */ | ||
tag?: string; | ||
|
||
/** The className(s) to apply to the tag (.Markdown class is always applied) */ | ||
className?: string; | ||
} | ||
|
||
/** | ||
* Renders markdown into a div (or some other tag) | ||
* Extra props are passed through to the rendered tag | ||
*/ | ||
export class Markdown extends React.Component<IMarkdownProps> { | ||
public static defaultProps: Partial<IMarkdownProps> = { | ||
tag: 'div', | ||
}; | ||
|
||
private parser: Parser = new Parser(); | ||
private renderer: HtmlRenderer = new HtmlRenderer(); | ||
export function Markdown(props: IMarkdownProps) { | ||
const { message, tag: tagProp, className: classNameProp, tag = 'div', ...rest } = props; | ||
|
||
public render() { | ||
const { message, tag, ...rest } = this.props; | ||
const parser = React.useMemo(() => new Parser(), []); | ||
const renderer = React.useMemo(() => new HtmlRenderer(), []); | ||
const className = `Markdown ${classNameProp || ''}`; | ||
|
||
if (message == null) { | ||
return null; | ||
} | ||
if (message == null) { | ||
return null; | ||
} | ||
|
||
const restProps = rest as React.DOMAttributes<any>; | ||
const parsed = this.parser.parse(message.toString()); | ||
const rendered = this.renderer.render(parsed); | ||
restProps.dangerouslySetInnerHTML = { __html: DOMPurify.sanitize(rendered) }; | ||
const restProps = rest as React.DOMAttributes<any>; | ||
const parsed = parser.parse(message.toString()); | ||
const rendered = renderer.render(parsed); | ||
restProps.dangerouslySetInnerHTML = { __html: DOMPurify.sanitize(rendered) }; | ||
|
||
return React.createElement(tag, restProps); | ||
} | ||
return React.createElement(tag, { ...restProps, className }); | ||
} |
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