Skip to content

Commit

Permalink
fix: compat mode in ssr (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafegoldberg authored Nov 12, 2020
1 parent 4a74db4 commit 64f1135
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions components/HTMLBlock.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable no-eval
/* eslint-disable no-eval,no-underscore-dangle
*/
const React = require('react');
const PropTypes = require('prop-types');
Expand All @@ -7,26 +7,28 @@ const PropTypes = require('prop-types');
* @arg {string} html the HTML from which to extract script tags.
*/
const extractScripts = html => {
if (typeof window === 'undefined' || !html) return () => {};
if (typeof window === 'undefined' || !html) return [() => {}, ''];

const regex = /<script\b[^>]*>([\s\S]*?)<\/script>/gim;
const scripts = [...html.matchAll(regex)].map(m => m[1].trim());
const cleaned = html.replace(regex, '') || '';
const exec = () => scripts.map(js => window.eval(js));

return () => scripts.map(js => window.eval(js));
return [exec, cleaned];
};

class HTMLBlock extends React.Component {
constructor(props) {
super(props);
if ('scripts' in this.props) this.runScripts = extractScripts(this.props.html);
[this.runScripts, this.clean] = extractScripts(this.props.html);
}

componentDidMount() {
if ('scripts' in this.props) this.runScripts();
}

render() {
const { html: __html } = this.props;
const __html = this.clean;
return <div className="rdmd-html" dangerouslySetInnerHTML={{ __html }} />;
}
}
Expand Down

0 comments on commit 64f1135

Please sign in to comment.