amphtml-validator Node.js package (Beta!)
Using the command-line tool (Beta!)
- Type
./index.sh
in this directory to get started:
$ ./index.sh ../testdata/feature_tests/minimum_valid_amp.html
../testdata/feature_tests/minimum_valid_amp.html: PASS
$ ./index.sh ../testdata/feature_tests/several_errors.html
../testdata/feature_tests/several_errors.html:23:2 The attribute 'charset' may not appear in tag 'meta name= and content='.
../testdata/feature_tests/several_errors.html:26:2 The tag 'script' is disallowed except in specific forms.
../testdata/feature_tests/several_errors.html:32:2 The mandatory attribute 'height' is missing in tag 'amp-img'. (see https://www.ampproject.org/docs/reference/amp-img.html)
../testdata/feature_tests/several_errors.html:34:2 The attribute 'width' in tag 'amp-ad' is set to the invalid value '100%'. (see https://www.ampproject.org/docs/reference/amp-ad.html)
...
If you wish to install the Validator as a system command,
install the NPM package manager (e.g. using apt-get in Ubuntu Linux) and
run npm install -g
in this directory. After that, you may type
amphtml-validator
in any directory to invoke the validator.
Using the NodeJS API (Beta!)
This API is new and still experimental, feedback is especially welcome. We may
try to port to earlier versions of NodeJS if sufficient interest exists.
'use strict';
const ampValidator = require('amphtml-validator');
ampValidator.getInstance().then((validator) => {
const result = validator.validateString('<html>Hello, world.</html>');
((result.status === 'PASS') ? console.log : console.error)(result.status);
for (const error of result.errors) {
let 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);
}
});