-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from weegeekps/parse_markdown
Parse the description and output the resulting HTML
- Loading branch information
Showing
5 changed files
with
67 additions
and
1 deletion.
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,37 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import marked from "marked"; | ||
import purify from "dompurify"; | ||
|
||
interface IMarkdown { | ||
className: string; | ||
body: string; | ||
} | ||
|
||
const parseAndSanitizeMarkdown = (md: string): Promise<string> => { | ||
return new Promise((resolve, reject) => { | ||
marked(md, (err, result) => { | ||
if (err) reject(err); | ||
|
||
// Marked does not sanitize the html, so we must do so ourselves. | ||
resolve(purify.sanitize(result)); | ||
}); | ||
}); | ||
}; | ||
|
||
const Markdown: React.FC<IMarkdown> = props => { | ||
const { className, body: unsafeMarkdown } = props; | ||
|
||
const [safeHtml, setSafeHtml] = useState({ __html: "" }); | ||
|
||
// This effect is for performance reasons. It will only execute if the value | ||
// of the body prop is updated. | ||
useEffect(() => { | ||
parseAndSanitizeMarkdown(unsafeMarkdown).then(result => | ||
setSafeHtml({ __html: result }) | ||
); | ||
}, [unsafeMarkdown]); | ||
|
||
return <div className={className} dangerouslySetInnerHTML={safeHtml}></div>; | ||
}; | ||
|
||
export default Markdown; |
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