// シンプルなAMP HTML検証ケース: Hellow Worldを AMP Validate var amphtmlValidator = require('amphtml-validator'); var samplehtml = "<html>Hello, world.</html>";
amphtmlValidator.getInstance().then(function (validator) { var result = validator.validateString(samplehtml); ((result.status === 'PASS') ? console.log : console.error)(result.status); for (var ii = 0; ii < result.errors.length; ii++) { var error = result.errors[ii]; var msg = 'line ' + error.line + ', col ' + error.col + ': ' + error.message; if (error.specUrl !== null) { msg += ' (see ' + error.specUrl + ')'; } ((error.severity === 'ERROR') ? console.error : console.warn)(msg); } });
最後にnode.jsを実行します。
bash
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
$ node index.js FAIL line 1, col 0: The mandatory attribute '⚡' is missing in tag 'html ⚡ for top-level html'. (see https://www.ampproject.org/docs/reference/spec.html#required-markup) line 1, col 0: The parent tag of tag 'html ⚡ for top-level html' is '$root', but it can only be '!doctype'. (see https://www.ampproject.org/docs/reference/spec.html#required-markup) line 1, col 5: Tag or text which is only allowed inside the body section found outside of the body section. (see ) line 1, col 25: The mandatory tag 'html doctype' is missing or incorrect. (see https://www.ampproject.org/docs/reference/spec.html#required-markup) line 1, col 25: The mandatory tag 'html ⚡ for top-level html' is missing or incorrect. (see https://www.ampproject.org/docs/reference/spec.html#required-markup) line 1, col 25: The mandatory tag 'head' is missing or incorrect. (see https://www.ampproject.org/docs/reference/spec.html#required-markup) line 1, col 25: The mandatory tag 'link rel=canonical' is missing or incorrect. (see https://www.ampproject.org/docs/reference/spec.html#required-markup) line 1, col 25: The mandatory tag 'meta charset=utf-8' is missing or incorrect. (see https://www.ampproject.org/docs/reference/spec.html#required-markup) line 1, col 25: The mandatory tag 'meta name=viewport' is missing or incorrect. (see https://www.ampproject.org/docs/reference/spec.html#required-markup) line 1, col 25: The mandatory tag 'amphtml engine v0.js script' is missing or incorrect. (see https://www.ampproject.org/docs/reference/spec.html#required-markup) line 1, col 25: The mandatory tag 'noscript enclosure for boilerplate' is missing or incorrect. (see https://github.com/ampproject/amphtml/blob/master/spec/amp-boilerplate.md) line 1, col 25: The mandatory tag 'head > style[amp-boilerplate]' is missing or incorrect. (see https://github.com/ampproject/amphtml/blob/master/spec/amp-boilerplate.md) line 1, col 25: The mandatory tag 'noscript > style[amp-boilerplate]' is missing or incorrect. (see https://github.com/ampproject/amphtml/blob/master/spec/amp-boilerplate.md)