How to use the amphtml-validator.getInstance function in amphtml-validator

To help you get started, we’ve selected a few amphtml-validator examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dfrankland / react-amphtml / src / __tests__ / react-amphtml.spec.tsx View on Github external
<title>react-amphtml</title>
          {ampScripts.getScriptElements()}
        
        
      ,
    );
    /* eslint-enable */

    const htmlPage = `
        
        ${html}
      `;

    expect(htmlPage).toMatchSnapshot();

    const validator = await amphtmlValidator.getInstance();
    const result = validator.validateString(htmlPage);

    result.errors.forEach(
      ({ line, col, message, specUrl, severity }): void =&gt; {
        // eslint-disable-next-line no-console
        (severity === 'ERROR' ? console.error : console.warn)(
          // eslint-disable-line no-console
          `line ${line}, col ${col}: ${message} ${
            specUrl ? ` (see ${specUrl})` : ''
          }`,
        );
      },
    );

    expect(result.status).toBe('PASS');
  });
github tea3 / hexo-generator-amp / lib / filter / amp-validate.js View on Github external

'use strict';

var Promise          = require('bluebird');
var pathFn           = require('path');
var assign           = require('object-assign');
var amphtmlValidator = require('amphtml-validator').getInstance();
var lg               = require('../log.js');
var util             = require('../util.js');
var validPassCnt     = 0;
var validErrorCnt    = 0;

//------------------------------------
// AMP HTML Validate
//------------------------------------
module.exports.amp_validate = function(result){
  if(result.tempData.isCacheUse)return Promise.resolve(result);
  
  lg.setConfig(result.config);
  
  return new Promise(function(resolve , reject){
    
    if(result.config.generator_amp.validateAMP){
github zeit / next.js / test / lib / amp-test-utils.js View on Github external
export async function validateAMP(html) {
  const validator = await amphtmlValidator.getInstance()
  const result = validator.validateString(html)
  if (result.status !== 'PASS') {
    for (let ii = 0; ii &lt; result.errors.length; ii++) {
      const error = result.errors[ii]
      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)
    }
  }
  expect(result.status).toBe('PASS')
}
github ampproject / ampbench / ampbench_lib.js View on Github external
function lib_init_validator(next) {
  if (amphtml_validator_instance_time &lt; (Date.now() - (60*60*1000))) {
    amphtml_validator_instance = null;
  }
  if (amphtml_validator_instance === null) {
    console.log('[VALIDATOR REFRESH]: validator not found or out of date, loading new validator');
    amphtml_validator_instance = amphtml_validator.getInstance().then((instance) =&gt; {
      amphtml_validator_instance = instance;
      amphtml_validator_instance_time = Date.now();
      next();
    });
  } else {
    next();
  }
}
github nuxt-community / amp-module / lib / module.js View on Github external
async function registerValidator (options) {
  const amphtmlValidator = require('amphtml-validator')
  const validator = await amphtmlValidator.getInstance()
  this.nuxt.hook('render:route', (url, { html }, { req }) => {
    const isAMP = req.isAMP

    if (isAMP) {
      const result = validator.validateString(html)
      const isValid = result.status === 'PASS'
      logger.log({
        type: result.status,
        message: (isValid ? chalk.green(result.status) : chalk.red(result.status)) + ' ' + url,
        icon: isValid ? chalk.green('✓') : chalk.red('✕')
      })
      for (const error of result.errors) {
        let msg = 'line ' + error.line + ', col ' + error.col + ': ' + error.message
        if (error.specUrl !== null) {
          msg += ' (see ' + error.specUrl + ')'
        }
github zeit / next.js / packages / next / server / next-dev-server.ts View on Github external
;(this.renderOpts as any).ampValidator = (
      html: string,
      pathname: string
    ) => {
      const validatorPath =
        this.nextConfig.experimental &&
        this.nextConfig.experimental.amp &&
        this.nextConfig.experimental.amp.validator
      return AmpHtmlValidator.getInstance(validatorPath).then(validator => {
        const result = validator.validateString(html)
        ampValidation(
          pathname,
          result.errors
            .filter(e => e.severity === 'ERROR')
            .filter(e => this._filterAmpDevelopmentScript(html, e)),
          result.errors.filter(e => e.severity !== 'ERROR')
        )
      })
    }
    if (fs.existsSync(join(this.dir, 'static'))) {
github bustle / ember-cli-amp / lib / css_validator.js View on Github external
validate(css) {
    this.template = fs.readFileSync(AMP_CSS_HTML_TEMPLATE_PATH, 'utf8');
    var html      = this.template.replace(CSS_PLACEHOLDER, css);

    return Validator.getInstance().then(validator => {
      var result    = validator.validateString(html);
      result.errors = result.errors.map(_convertAMPCSSError);

      return result;
    });
  }
};
github zeit / next.js / packages / next / export / worker.js View on Github external
const validateAmp = async (html, page, validatorPath) => {
      const validator = await AmpHtmlValidator.getInstance(validatorPath)
      const result = validator.validateString(html)
      const errors = result.errors.filter(e => e.severity === 'ERROR')
      const warnings = result.errors.filter(e => e.severity !== 'ERROR')

      if (warnings.length || errors.length) {
        results.ampValidations.push({
          page,
          result: {
            errors,
            warnings,
          },
        })
      }
    }
github Ariel-Rodriguez / react-amp-template / src / utils / ampvalidator.js View on Github external
export const validateMarkup = (markup, ignoreErrors) => {
  debug('validating markup.')
  return amphtmlValidator.getInstance()
    .then((validator) => {
      const validationResult = validator.validateString(markup, 'AMP')
      if (!ignoreErrors && validationResult.status !== 'PASS') {
        validationResult.errors.forEach((error) => {
          debug(`${error.severity} ${error.params}
            line:${error.code} col:${error.col} ${error.specUrl}`)
        })
        throw validationResult.errors
      }
      debug('AMP validation status %s', validationResult.status)
      return markup
    })
}

amphtml-validator

Official validator for AMP HTML (www.ampproject.org)

Apache-2.0
Latest version published 8 months ago

Package Health Score

75 / 100
Full package analysis