Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
UX fixes
  • Loading branch information
jennspencer committed May 9, 2024
commit e447442d5d232f4bb9ae4e079f08b570c78c5ba4
9 changes: 6 additions & 3 deletions components/HTMLBlock/index.tsx
Original file line number Diff line number Diff line change
@@ -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 = /<script\b[^>]*>([\s\S]*?)<\/script *>\n?/gim;

Expand All @@ -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(() => {
Expand All @@ -25,7 +28,7 @@ const HTMLBlock = props => {
if (safeMode) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

safeMode was my silly attempt to make suggested edits safer. Suggested edits could be submitted by anonymous users. And it features a preview mode, so if the submitter wasn't an admin, we wouldn't render the HTML. 🤷🏼

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that was the usage for us sure, but i'm wondering about the 1000 or downloads a week of @readme/markdown? is this new improved renderer going to be internal only or are we improving on the current one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, we can figure out the final details in another PR since it's a bit out of scope for this one 😅

return (
<pre className="html-unsafe">
<code dangerouslySetInnerHTML={{ __html: renderToStaticMarkup(cleanedHtml) }} />
<code>{cleanedHtml}</code>
</pre>
);
}
Expand Down
71 changes: 71 additions & 0 deletions docs/html-tests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
title: 'HTML Blocks'
---

## JSX (with no HTML attributes)

<HTMLBlock runScripts={true}>
<script>console.log(true, 0);</script>
<h3>Header 0</h3>
<p>Paragraph with <em>italics</em> and <strong>bold stuff</strong>.</p>
<div>I am actually a div!</div>
</HTMLBlock>


## JSX

<HTMLBlock>
<script>console.log(true, 1);</script>
<h3>Header 1</h3>
<p>Paragraph with <em>italics</em> and <strong>bold stuff</strong>.</p>
<div style={{ paddingLeft: "20px", color: "blue" }}>
Behold, I am blue and indented!
</div>
</HTMLBlock>


## HTML as a prop

<HTMLBlock children='<script>console.log(true, 2);</script><h3>Header 2</h3>
<p>Paragraph with <em>italics</em> and <strong>bold stuff</strong>.</p>
<div style="padding-left: 20px; color: blue">Behold, I am blue and indented!</div>' />


## HTML in template literal

<HTMLBlock>
{`
<script>console.log(true, 3);</script>
<h3>Header 3</h3>
<p>Paragraph with <em>italics</em> and <strong>bold stuff</strong>.</p>
<div style="padding-left: 20px; color: blue">
Behold, I am blue and indented!
</div>
`}
</HTMLBlock>


## JSX in safe mode

<HTMLBlock safeMode={true}>
<script>console.log(true, 4);</script>
<h3>Header 4</h3>
<p>Paragraph with <em>italics</em> and <strong>bold stuff</strong>.</p>
<div style={{ paddingLeft: "20px", color: "blue" }}>
Behold, I am blue and indented!
</div>
</HTMLBlock>


## HTML in safe mode

<HTMLBlock safeMode={true}>
{`
<script>console.log(true, 5);</script>
<h3>Header 5</h3>
<p>Paragraph with <em>italics</em> and <strong>bold stuff</strong>.</p>
<div style="padding-left: 20px; color: blue">
Behold, I am blue and indented!
</div>
`}
</HTMLBlock>
23 changes: 19 additions & 4 deletions docs/sanitizing-tests.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@

## Sanitizing `style` tags

<style>
* {
background-color: olive;
}
</style>


## Sanitizing `style` attributes

<p style="background-color: salmon">fish content</p>


## Sanitizing html blocks

<HTMLBlock>
<h2>Header</h2>
<p>hello there</p>
</HTMLBlock>
[block:html]
{
"html": "<style>* { border: 3px solid magenta; }</style>"
}
[/block]
3 changes: 3 additions & 0 deletions example/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -41,6 +43,7 @@ const fixtures = Object.entries({
features,
gettingStarted,
headings,
htmlBlocks,
images,
lists,
sanitizingTests,
Expand Down