-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
33 lines (26 loc) · 1.16 KB
/
index.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
const util = require('util');
const ansiHTML = require('ansi-html');
const addScript = (content) => {
// It's really important that the HTML strings are on one line.
// I've seen instances of some 11ty templates converting new lines into paragraphs
const containerStyles = `background: black; color: #fff; padding: 5px;`;
const stringifiedContent = JSON.stringify(`<pre style="${containerStyles}">${content}</pre>`);
return `<script>(function(){const content = document.createElement('div');document.body.prepend(content);content.innerHTML = ${stringifiedContent}})();</script>`;
}
let dumpFilter = (object) => {
// Let's be sure to escape HTML characters in the util.inspect results
const safeContent = util.inspect(object, { colors:true })
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
return addScript(ansiHTML(safeContent))
}
dumpFilter.keys = (object) => {
if (typeof object === 'object' && object !== null) {
return addScript(JSON.stringify(Object.keys(object)))
}
return null;
}
module.exports = dumpFilter;