From e447442d5d232f4bb9ae4e079f08b570c78c5ba4 Mon Sep 17 00:00:00 2001 From: jennspencer Date: Thu, 9 May 2024 13:29:36 -0700 Subject: [PATCH] UX fixes --- components/HTMLBlock/index.tsx | 9 +++-- docs/html-tests.md | 71 ++++++++++++++++++++++++++++++++++ docs/sanitizing-tests.md | 23 +++++++++-- example/docs.ts | 3 ++ 4 files changed, 99 insertions(+), 7 deletions(-) create mode 100644 docs/html-tests.md diff --git a/components/HTMLBlock/index.tsx b/components/HTMLBlock/index.tsx index e3e9f88d9..af60f5649 100644 --- a/components/HTMLBlock/index.tsx +++ b/components/HTMLBlock/index.tsx @@ -1,5 +1,5 @@ import React, { useEffect } from 'react'; -import { renderToString, renderToStaticMarkup } from 'react-dom/server'; +import { renderToStaticMarkup } from 'react-dom/server'; const MATCH_SCRIPT_TAGS = /]*>([\s\S]*?)<\/script *>\n?/gim; @@ -15,7 +15,10 @@ const extractScripts = (html: string = ''): [string, () => void] => { const HTMLBlock = props => { const { children, runScripts, safeMode = false } = props; - const html = renderToString(<>{children}); + let html = children; + + if (typeof html !== 'string') html = renderToStaticMarkup(html); + const [cleanedHtml, exec] = extractScripts(html); useEffect(() => { @@ -25,7 +28,7 @@ const HTMLBlock = props => { if (safeMode) { return (
-        
+        {cleanedHtml}
       
); } diff --git a/docs/html-tests.md b/docs/html-tests.md new file mode 100644 index 000000000..0c6884379 --- /dev/null +++ b/docs/html-tests.md @@ -0,0 +1,71 @@ +--- +title: 'HTML Blocks' +--- + +## JSX (with no HTML attributes) + + + +

Header 0

+

Paragraph with italics and bold stuff.

+
I am actually a div!
+
+ + +## JSX + + + +

Header 1

+

Paragraph with italics and bold stuff.

+
+ Behold, I am blue and indented! +
+
+ + +## HTML as a prop + + + + +## HTML in template literal + + + {` + +

Header 3

+

Paragraph with italics and bold stuff.

+
+ Behold, I am blue and indented! +
+ `} +
+ + +## JSX in safe mode + + + +

Header 4

+

Paragraph with italics and bold stuff.

+
+ Behold, I am blue and indented! +
+
+ + +## HTML in safe mode + + +{` + +

Header 5

+

Paragraph with italics and bold stuff.

+
+ Behold, I am blue and indented! +
+`} +
\ No newline at end of file diff --git a/docs/sanitizing-tests.md b/docs/sanitizing-tests.md index 4a48e5b69..a646bcca3 100644 --- a/docs/sanitizing-tests.md +++ b/docs/sanitizing-tests.md @@ -1,7 +1,22 @@ +## Sanitizing `style` tags + + + + +## Sanitizing `style` attributes + +

fish content

+ + ## Sanitizing html blocks - -

Header

-

hello there

-
\ No newline at end of file +[block:html] +{ + "html": "" +} +[/block] diff --git a/example/docs.ts b/example/docs.ts index 2eba3b255..e3964a994 100644 --- a/example/docs.ts +++ b/example/docs.ts @@ -15,6 +15,8 @@ import gettingStarted from '../docs/getting-started.md'; // @ts-ignore import headings from '../docs/headings.md'; // @ts-ignore +import htmlBlocks from '../docs/html-tests.md'; +// @ts-ignore import images from '../docs/images.md'; // @ts-ignore import lists from '../docs/lists.md'; @@ -41,6 +43,7 @@ const fixtures = Object.entries({ features, gettingStarted, headings, + htmlBlocks, images, lists, sanitizingTests,