Skip to content

Commit e0e0791

Browse files
chore(examples): add index.html that parses in realtime
1 parent 3a2f26e commit e0e0791

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

examples/index.html

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<textarea cols="50" rows="5"><p>Hello, world!</p></textarea>
2+
<pre><code></code></pre>
3+
<script src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
4+
<script src="../dist/html-react-parser.min.js"></script>
5+
<script>
6+
var code = document.querySelector('code');
7+
var textarea = document.querySelector('textarea');
8+
9+
function renderOutput(event) {
10+
cache = [];
11+
code.innerText = JSON.stringify(
12+
window.HTMLReactParser(event.target.value),
13+
replacer,
14+
2
15+
);
16+
}
17+
18+
textarea.addEventListener('input', renderOutput);
19+
renderOutput({ target: textarea });
20+
21+
/**
22+
* @see {@link https://stackoverflow.com/questions/11616630/json-stringify-avoid-typeerror-converting-circular-structure-to-json}
23+
*/
24+
var cache;
25+
function replacer(key, value) {
26+
if (typeof value === 'object' && value !== null) {
27+
if (cache.indexOf(value) !== -1) {
28+
// duplicate reference found
29+
return '[Circular]';
30+
}
31+
// store value in our collection
32+
cache.push(value);
33+
}
34+
return value;
35+
}
36+
</script>

0 commit comments

Comments
 (0)