-
Notifications
You must be signed in to change notification settings - Fork 9
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
1 parent
8ac2507
commit 3ca6ecc
Showing
10 changed files
with
64 additions
and
113 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React, { useEffect } from 'react'; | ||
import { renderToString, renderToStaticMarkup } from 'react-dom/server'; | ||
|
||
const MATCH_SCRIPT_TAGS = /<script\b[^>]*>([\s\S]*?)<\/script *>\n?/gim; | ||
|
||
const extractScripts = (html: string = ''): [string, () => void] => { | ||
const scripts: string[] = []; | ||
let match: RegExpExecArray | null; | ||
while ((match = MATCH_SCRIPT_TAGS.exec(html)) !== null) { | ||
scripts.push(match[1]); | ||
} | ||
const cleaned = html.replace(MATCH_SCRIPT_TAGS, ''); | ||
return [cleaned, () => scripts.map(js => window.eval(js))]; | ||
}; | ||
|
||
const HTMLBlock = props => { | ||
const { children, runScripts, safeMode = false } = props; | ||
const html = renderToString(<>{children}</>); | ||
const [cleanedHtml, exec] = extractScripts(html); | ||
|
||
useEffect(() => { | ||
Check failure on line 21 in components/HTMLBlock/index.tsx GitHub Actions / Test (lts/-1, 16)__tests__/components/HTMLBlock.test.jsx
Check failure on line 21 in components/HTMLBlock/index.tsx GitHub Actions / Test (lts/-1, 17)__tests__/components/HTMLBlock.test.jsx
Check failure on line 21 in components/HTMLBlock/index.tsx GitHub Actions / Test (lts/-1, 18)__tests__/components/HTMLBlock.test.jsx
|
||
if (typeof window !== 'undefined' && typeof runScripts === 'boolean' && runScripts) exec(); | ||
}, [runScripts, exec]); | ||
|
||
if (safeMode) { | ||
return ( | ||
<pre className="html-unsafe"> | ||
<code dangerouslySetInnerHTML={{ __html: renderToStaticMarkup(cleanedHtml) }} /> | ||
</pre> | ||
); | ||
} | ||
|
||
return <div className="rdmd-html" dangerouslySetInnerHTML={{ __html: html }} />; | ||
}; | ||
|
||
export default HTMLBlock; |
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 |
---|---|---|
@@ -1,22 +1,7 @@ | ||
|
||
## Sanitizing `style` tags | ||
|
||
<style> | ||
* { | ||
background-color: olive; | ||
} | ||
</style> | ||
|
||
|
||
## Sanitizing `style` attributes | ||
|
||
<p style="background-color: salmon">fish content</p> | ||
|
||
|
||
## Sanitizing html blocks | ||
|
||
[block:html] | ||
{ | ||
"html": "<style>* { border: 3px solid magenta; }</style>" | ||
} | ||
[/block] | ||
<HTMLBlock> | ||
<h2>Header</h2> | ||
<p>hello there</p> | ||
</HTMLBlock> |
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 |
---|---|---|
|
@@ -3,4 +3,5 @@ export enum NodeTypes { | |
emoji = 'emoji', | ||
i = 'i', | ||
image = 'image', | ||
htmlBlock = 'html-block', | ||
} |
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { HTMLBlock } from '../../types'; | ||
|
||
const htmlBlock = (node: HTMLBlock) => { | ||
const html = node.data.hProperties.html; | ||
return `<HTMLBlock>${JSON.stringify({ html }, null, 2)}</HTMLBlock>`; | ||
} | ||
|
||
export default htmlBlock; |
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