How to use amphtml-validator - 10 common examples

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()}
        </head>
        <body dangerouslySetInnerHTML={{ __html: bodyContent }} />
      </Amp.Html>,
    );
    /* eslint-enable */

    const htmlPage = `
        <!doctype html>
        ${html}
      `;

    expect(htmlPage).toMatchSnapshot();

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

    result.errors.forEach(
      ({ line, col, message, specUrl, severity }): void => {
        // 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 ampproject / ampbench / amp-story / linter / index.ts View on Github external
/// <reference path="probe-image-size.d.ts" />

import {readFileSync} from "fs";
import {resolve, URL} from "url";
// tslint:disable-next-line:no-var-requires
const validator = require("amphtml-validator").newInstance(
  // Let's not fetch over the network on every run.
  // Use `yarn run update-validator` to update.
  // tslint:disable-next-line:no-var-requires
  readFileSync(`${__dirname}/validator.js`).toString(),
);
import * as cheerio from "cheerio";
import throat = require("throat");

import {default as fetch, Request, RequestInit, Response} from "node-fetch";
import {basename} from "path";
import * as probe from "probe-image-size";
import * as punycode from "punycode";
import * as readline from "readline";

const CONCURRENCY = 8;
const UA_GOOGLEBOT_MOBILE = [
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 < 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 DefinitelyTyped / DefinitelyTyped / types / amphtml-validator / amphtml-validator-tests.ts View on Github external
(() => {
    const validator = ampHtmlValidator.newInstance("");
    const result = validator.validateString("<html></html>", "AMP4ADS");
    const { status, errors } = result;
})();
github ampproject / ampbench / ampbench_lib.js View on Github external
function lib_init_validator(next) {
  if (amphtml_validator_instance_time < (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) => {
      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,
          },
        })
      }
    }

amphtml-validator

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

Apache-2.0
Latest version published 2 years ago

Package Health Score

74 / 100
Full package analysis