Skip to content

Instantly share code, notes, and snippets.



Created Jun 6, 2020
What would you like to do?
JSON stringify replacer (no circular deps) pretty printer
import parse5 from 'parse5'
var src = `
{#if name}
console.log('not top level')
{#each cats as cat}
console.log('not top level')
{#await promise}
console.log('not top level')
{:then number}
console.log('not top level')
{:catch error}
console.log('not top level')
<p>{@html <script> consolelog('not top level')</script>}</p>
<!-- p{ color: blue; }</script> -->
<!--<script lang="scss">
p{ color: blue; }
</script> -->
<script>top level script</script>
var abc = parse5.parseFragment(src)
const fs = require('fs')
const path = require('path')
var cache: any = []
fs.writeFileSync('temp.json', JSON.stringify(abc, replacer, 2))
function replacer(key: any, value: any) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// Circular reference found, discard key
// Store value in our collection
return value;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment