-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shift markdown parsing into helper function
Slight changes in function in that table elements are now allowed in the footer as well as the main-narrative-markdown. This was commented as a "to-do" in the code
- Loading branch information
1 parent
0d2eaf4
commit 83b71a4
Showing
3 changed files
with
37 additions
and
67 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
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,33 @@ | ||
import marked from "marked"; | ||
import dompurify from "dompurify"; | ||
|
||
dompurify.addHook("afterSanitizeAttributes", (node) => { | ||
// Set external links to open in a new tab | ||
if ('href' in node && location.hostname !== node.hostname) { | ||
node.setAttribute('target', '_blank'); | ||
node.setAttribute('rel', 'noreferrer nofollow'); | ||
} | ||
// Find nodes that contain images and add imageContainer class to update styling | ||
const nodeContainsImg = ([...node.childNodes].filter((child) => child.localName === 'img')).length > 0; | ||
if (nodeContainsImg) { | ||
// For special case of image links, set imageContainer on outer parent | ||
if (node.localName === 'a') { | ||
node.parentNode.className += ' imageContainer'; | ||
} else { | ||
node.className += ' imageContainer'; | ||
} | ||
} | ||
}); | ||
|
||
const ALLOWED_TAGS = ['div', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'em', 'strong', 'del', 'ol', 'ul', 'li', 'a', 'img']; | ||
ALLOWED_TAGS.push('#text', 'code', 'pre', 'hr', 'table', 'thead', 'tbody', 'th', 'tr', 'td'); | ||
|
||
const ALLOWED_ATTR = ['href', 'src', 'width', 'height', 'alt']; | ||
|
||
export const parseMarkdown = (mdString) => { | ||
const sanitizer = dompurify.sanitize; | ||
const sanitizerConfig = {ALLOWED_TAGS, ALLOWED_ATTR, KEEP_CONTENT: false, ALLOW_DATA_ATTR: false}; | ||
const rawDescription = marked(mdString); | ||
const cleanDescription = sanitizer(rawDescription, sanitizerConfig); | ||
return cleanDescription; | ||
}; |