Created
June 17, 2019 05:13
-
-
Save p1ho/05f418139187ed6de49d15648a9ad826 to your computer and use it in GitHub Desktop.
Generates HTML report from site-validator-cli exported json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const htmlCreator = require('html-creator') | |
let filename = 'testdata.json' // insert name of exported json here | |
let raw = fs.readFileSync(filename) | |
let rawJson = JSON.parse(raw) | |
let utils = { | |
pass: '<span style="color: green">✔</span>', | |
fail: '<span style="color: red;">❌</span>' | |
} | |
var html = new htmlCreator([ | |
{ type: 'head', content: [ | |
{ type: 'title', content: `Report for ${filename}` }, | |
{ type: 'style', content: ` | |
ul { | |
margin-bottom: 10px; | |
padding: 0; | |
} | |
li { | |
list-style-type: none; | |
} | |
ul.error-report li { | |
margin: 10px 0; | |
} | |
.warning { | |
background-color: #ffff99; | |
} | |
.error { | |
background-color: #ffcccc; | |
} | |
`} | |
]}, | |
{ type: 'body', content:[ | |
{ type: 'h1', content: `Report for ${rawJson.url}` }, | |
{ type: 'h2', | |
content: rawJson.passed ? | |
'<b style="color:green;">Website Passed</b>' : | |
'<b style="color:red">Website Failed</b>' }, | |
{ type: 'section', content: [ | |
{ type: 'h3', content: 'Summary:'}, | |
{ type: 'ul', | |
attributes: { | |
style: 'padding: 0;' | |
}, | |
content: [].concat( | |
rawJson.results.passed.map( x => `<li>${utils.pass + x.url}</li>` ), | |
rawJson.results.failed.map( x => `<li>${utils.fail + x.url}</li>` ) | |
).join('') | |
} | |
]}, | |
{ type: 'hr' }, | |
{ type: 'section', content: [ | |
{type: 'h3', content: 'Error Report:'}, | |
{ type: 'ul', | |
attributes: { | |
class: 'error-report' | |
}, | |
content: [].concat( | |
rawJson.results.failed.map( x => | |
`<li> | |
${utils.fail + x.url} (${x.status}) | |
<ul> | |
${x.errors.map( y => | |
y.type !== undefined ? | |
`<li class="${y.type === 'error' ? 'error' : 'warning'}"> | |
[${y.type.toUpperCase()}] ${y.location}<br> | |
${y.message} | |
</li> | |
` : | |
`<li>${y}</li>` | |
).join('')} | |
</ul> | |
</li>` | |
) | |
).join('') | |
} | |
]} | |
]} | |
]) | |
var render = html.renderHTML() | |
fs.writeFileSync('testdata-report.html', render, 'utf8', (err) => { | |
if (err) { throw err } | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment