-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Pauline Didier
committed
Aug 9, 2024
1 parent
0bfba86
commit eb44a4e
Showing
5 changed files
with
216 additions
and
87 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,96 @@ | ||
import { marked } from "marked"; | ||
import DOMPurify from "dompurify"; | ||
import Box from "@mui/material/Box"; | ||
|
||
marked.use({ | ||
extensions: [ | ||
{ | ||
name: "heading", | ||
renderer({ text, depth }) { | ||
return `<h${depth + 1}>${text}</h${depth + 1}>`; | ||
}, | ||
}, | ||
{ | ||
name: "image", | ||
renderer(_) { | ||
return "Images are not supported"; | ||
}, | ||
}, | ||
{ | ||
name: "link", | ||
renderer(token) { | ||
if (!token.href) { | ||
return `<p><strong>${token.text}</strong> (link without href)</p>`; | ||
} | ||
return `<a href="${token.href}" target="_blank" rel="noopener noreferrer">${token.text}</a>`; | ||
}, | ||
}, | ||
{ | ||
name: "code", | ||
renderer(token) { | ||
if (!token.text) { | ||
return ""; | ||
} | ||
if (token.lang) { | ||
return `<div><p>\`\`\`${token.lang}</p><pre style="margin-left:1em"><code class="language-${token.lang}">${token.text}</code></pre><p>\`\`\`</p></div>`; | ||
} | ||
return `<div><p>\`\`\`</p><pre style="margin-left:1em"><code class="language-${token.lang}">${token.text}</code></pre><p>\`\`\`</p></div>`; | ||
}, | ||
}, | ||
{ | ||
name: "list", | ||
renderer(token) { | ||
const itemList = token.items.map((i) => { | ||
return ( | ||
"<li style='margin-bottom:0'>" + i.raw.replace(/\n+$/, "") + "</li>" | ||
); | ||
}); | ||
if (token.ordered) { | ||
return `<ol>${itemList.join("\n")}</ol>`; | ||
} else { | ||
return `<ul>${itemList.join("\n")}</ul>`; | ||
} | ||
}, | ||
}, | ||
], | ||
}); | ||
|
||
const domPurityConfig = { | ||
USE_PROFILES: { html: true }, | ||
FORBID_TAGS: ["img"], | ||
ADD_ATTR: ["target"], | ||
}; | ||
|
||
export const DescriptionPreview = ({ | ||
description, | ||
readOnly, | ||
showDescriptionTextField, | ||
sx = {}, | ||
}) => { | ||
if (!description) { | ||
showDescriptionTextField(true); | ||
return null; | ||
} | ||
|
||
const sanitizedHtml = DOMPurify.sanitize( | ||
marked.parse(description), | ||
domPurityConfig | ||
); | ||
|
||
if (readOnly) { | ||
return ( | ||
<Box | ||
dangerouslySetInnerHTML={{ __html: sanitizedHtml }} | ||
sx={{ ...sx }} | ||
></Box> | ||
); | ||
} else { | ||
return ( | ||
<Box | ||
dangerouslySetInnerHTML={{ __html: sanitizedHtml }} | ||
sx={{ ...sx }} | ||
onDoubleClick={showDescriptionTextField} | ||
></Box> | ||
); | ||
} | ||
}; |
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
63 changes: 0 additions & 63 deletions
63
app/src/components/model/panels/left/DescriptionPreview.js
This file was deleted.
Oops, something went wrong.
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