Skip to content

Commit

Permalink
Merge pull request #8 from weegeekps/parse_markdown
Browse files Browse the repository at this point in the history
Parse the description and output the resulting HTML
  • Loading branch information
Adam Morris authored Oct 23, 2019
2 parents 9d50419 + d6601d4 commit 1940b2a
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@types/dompurify": "^0.0.33",
"@types/jest": "24.0.18",
"@types/marked": "^0.6.5",
"@types/node": "12.7.5",
"@types/react": "16.9.2",
"@types/react-dom": "16.9.0",
"@types/react-redux": "^7.1.2",
"dompurify": "^2.0.6",
"marked": "^0.7.0",
"react": "^16.9.0",
"react-dom": "^16.9.0",
"react-redux": "^7.1.1",
Expand Down
37 changes: 37 additions & 0 deletions src/components/Markdown.tsx
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;
1 change: 1 addition & 0 deletions src/components/ProjectCard.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ h6 {
}

.project-card-description {
font-size: 0.9rem;
overflow-wrap: break-word;
word-wrap: break-word;
word-break: break-word;
Expand Down
6 changes: 5 additions & 1 deletion src/components/ProjectCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IProjectInfo } from "../interfaces/IProjectInfo";
import ProjectDetailList from "./ProjectDetailList";
import ProjectCardHeader from "./ProjectCardHeader";
import "./ProjectCard.css";
import Markdown from "./Markdown";

const shouldShowSection = (items?: string[]) => items && items.length > 0;

Expand All @@ -18,7 +19,10 @@ const ProjectCard: React.FC<IProjectCardProps> = props => {
<ProjectCardHeader project={project} />
<div className="project-card-content">
{project.description && (
<p className="project-card-description">{project.description}</p>
<Markdown
className="project-card-description"
body={project.description}
/>
)}
{shouldShowSection(project.task) && (
<ProjectDetailList header="Task" items={project.task} />
Expand Down
20 changes: 20 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,11 @@
dependencies:
"@babel/types" "^7.3.0"

"@types/dompurify@^0.0.33":
version "0.0.33"
resolved "https://registry.yarnpkg.com/@types/dompurify/-/dompurify-0.0.33.tgz#bec490ba5a5b4b31679fb9e5a70e4d2f405516bd"
integrity sha512-lUN9iC6b4txeaEef2PW7zIdhEKAp0Sw9bymOcXXZ7BaepB0nsDJYcLIrFfgpIkRSoZWBJ8IcYunB2hAXuHL1NA==

"@types/eslint-visitor-keys@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#1ee30d79544ca84d68d4b3cdb0af4f205663dd2d"
Expand Down Expand Up @@ -1338,6 +1343,11 @@
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.3.tgz#bdfd69d61e464dcc81b25159c270d75a73c1a636"
integrity sha512-Il2DtDVRGDcqjDtE+rF8iqg1CArehSK84HZJCT7AMITlyXRBpuPhqGLDQMowraqqu1coEaimg4ZOqggt6L6L+A==

"@types/marked@^0.6.5":
version "0.6.5"
resolved "https://registry.yarnpkg.com/@types/marked/-/marked-0.6.5.tgz#3cf2a56ef615dad24aaf99784ef90a9eba4e29d8"
integrity sha512-6kBKf64aVfx93UJrcyEZ+OBM5nGv4RLsI6sR1Ar34bpgvGVRoyTgpxn4ZmtxOM5aDTAaaznYuYUH8bUX3Nk3YA==

"@types/node@12.7.5":
version "12.7.5"
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.7.5.tgz#e19436e7f8e9b4601005d73673b6dc4784ffcc2f"
Expand Down Expand Up @@ -3439,6 +3449,11 @@ domhandler@^2.3.0:
dependencies:
domelementtype "1"

dompurify@^2.0.6:
version "2.0.6"
resolved "https://registry.yarnpkg.com/dompurify/-/dompurify-2.0.6.tgz#0a4196c211ce00e848240e52b1d49261af12a3be"
integrity sha512-1+AOpCYIKoLER/Ykd/Q/i11slhYy6T29/mmDrTsALH0JcMPB0pt9++8eDTGT70tv6ofmmeptrdqzZpmjMfFLRg==

domutils@1.5.1:
version "1.5.1"
resolved "https://registry.yarnpkg.com/domutils/-/domutils-1.5.1.tgz#dcd8488a26f563d61079e48c9f7b7e32373682cf"
Expand Down Expand Up @@ -6133,6 +6148,11 @@ map-visit@^1.0.0:
dependencies:
object-visit "^1.0.0"

marked@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/marked/-/marked-0.7.0.tgz#b64201f051d271b1edc10a04d1ae9b74bb8e5c0e"
integrity sha512-c+yYdCZJQrsRjTPhUx7VKkApw9bwDkNbHUKo1ovgcfDjb2kc8rLuRbIFyXL5WOEUwzSSKo3IXpph2K6DqB/KZg==

md5.js@^1.3.4:
version "1.3.5"
resolved "https://registry.yarnpkg.com/md5.js/-/md5.js-1.3.5.tgz#b5d07b8e3216e3e27cd728d72f70d1e6a342005f"
Expand Down

0 comments on commit 1940b2a

Please sign in to comment.