-
Notifications
You must be signed in to change notification settings - Fork 2
/
HtmlDemo.js
47 lines (43 loc) · 1.57 KB
/
HtmlDemo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import React from 'react';
import {CodeHighlighter} from 'components/atoms';
import * as styles from './demo.module.css';
const codeBlocks = nodes =>
nodes
.filter(node => node.props.originalType === 'pre')
.map(pre => pre.props.children)
.filter(codeEl => codeEl.props.mdxType === 'code');
const makeSrcString = nodes => {
return codeBlocks(nodes)
.map(block => {
if (block.props.className === 'language-js') {
return `<script>\n${block.props.children}\n</script>`;
} else if (block.props.className === 'language-css') {
return `<style>\n${block.props.children}\n</style>`;
} else if (block.props.className === 'language-html') {
return block.props.children;
} else {
throw new Error('JsDemo code blocks must be html, css, or js');
}
})
.join('\n\n');
};
const HtmlDemo = ({children, srcString, lineColors, extra, replace = {}}) => {
let src = srcString || makeSrcString(React.Children.toArray(children));
const display = children || <CodeHighlighter lineColors={lineColors}>{src}</CodeHighlighter>;
for (let key in replace) {
src = src.replaceAll(key, replace[key]);
}
return (
<div className={styles.wrapper}>
<div className={styles.tab}>
<div className={styles.label}>Code</div>
<div className={styles.code}>{display}</div>
</div>
<div className={styles.tab}>
<div className={styles.label}>Result</div>
<iframe className={styles.output} srcDoc={[src, extra].join('\n')}></iframe>
</div>
</div>
);
};
export default HtmlDemo;