UNPKG

makestatic-validate-html

Version:

Validates HTML documents

112 lines (97 loc) 2.98 kB
const util = require('util') const repeat = require('string-repeater') const wrap = require('wordwrap') /** * Print the validation result. * * @see https://github.com/validator/validator/wiki/Output%3A-JSON */ function printer (log, result) { let messages = result.messages let i let msg let type let message let method = 'info' let index let space = ' ' let indent = space + space let firstLine let lastLine let firstColumn let lastColumn let position let extractFirst let extractHilite let extractLast let pointer let wrapAt = 80 for (i = 0; i < messages.length; i++) { msg = messages[i] type = msg.type index = (i + 1) + ')' message = msg.message firstLine = msg.firstLine || msg.lastLine lastLine = msg.lastLine /* istanbul ignore next: not sure if firstColumn is optional */ firstColumn = msg.firstColumn || msg.lastColumn lastColumn = msg.lastColumn position = util.format( 'From line %s, column %s; to line %s, column %s', firstLine, firstColumn, lastLine, lastColumn) if (type === 'error') { method = 'error' /* istanbul ignore else: will defer to `info` otherwise */ } else if (type === 'info' && msg.subType === 'warning') { method = 'warn' } // initial empty line, makes parsing the output easier on the eyes if (i === 0) { log[method](space) } // always print url, when there are lots of errors the // context can be lost otherwise log[method]('%s %s', index, msg.url || '') log[method](space) // print position information log[method]('%s', position) log[method](space) /* istanbul ignore else: docs say everything but `type` is optional */ if (message) { if (message.length > wrapAt) { message = wrap(wrapAt)(message) } log[method](message) log[method](space) } /* istanbul ignore else: docs say everything but `type` is optional */ if (msg.extract) { // WARN: quick hack for when the extract contains newlines // WARN: this needs more consideration msg.extract = msg.extract.replace('\n', '\\n') /* istanbul ignore else: docs say everything but `type` is optional */ if (typeof (msg.hiliteStart) === 'number' && typeof (msg.hiliteLength) === 'number') { extractFirst = msg.extract.substr(0, msg.hiliteStart) extractHilite = msg.extract.substr(msg.hiliteStart, msg.hiliteLength) extractLast = msg.extract.substr(msg.hiliteStart + msg.hiliteLength) log[method]( indent + '%s%s%s', extractFirst, extractHilite, extractLast) pointer = repeat('-', indent.length + msg.hiliteStart) pointer += '^' log[method](pointer) log[method](space) } else { log[method](indent + msg.extract) log[method](space) } } } } module.exports = printer