|
|
|
|
|
import AmpHtmlValidator from 'next/dist/compiled/amphtml-validator' |
|
|
|
|
|
export async function validateAMP( html) { |
|
|
const validatorPath = getBundledAmpValidatorFilepath() |
|
|
const validator = await getAmpValidatorInstance(validatorPath) |
|
|
const result = validator.validateString(html) |
|
|
const errors = result.errors.filter((error) => { |
|
|
if (error.severity === 'ERROR') { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( |
|
|
error.code === 'DISALLOWED_ATTR' && |
|
|
error.params[0] === 'blocking' && |
|
|
error.params[1] === 'link' |
|
|
) { |
|
|
return false |
|
|
} |
|
|
|
|
|
|
|
|
if ( |
|
|
error.code === 'MANDATORY_ATTR_MISSING' && |
|
|
error.params[0] === 'type' && |
|
|
error.params[1] === 'template' |
|
|
) { |
|
|
return false |
|
|
} |
|
|
|
|
|
|
|
|
if ( |
|
|
error.code === 'MISSING_REQUIRED_EXTENSION' && |
|
|
error.params[0] === 'template' && |
|
|
error.params[1] === 'amp-mustache' |
|
|
) { |
|
|
return false |
|
|
} |
|
|
return true |
|
|
} |
|
|
return false |
|
|
}) |
|
|
const warnings = result.errors.filter((error) => { |
|
|
return error.severity !== 'ERROR' |
|
|
}) |
|
|
|
|
|
expect({ errors, warnings }).toEqual({ |
|
|
errors: [], |
|
|
warnings: [], |
|
|
}) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const instancePromises = new Map() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getAmpValidatorInstance( |
|
|
validatorPath |
|
|
) { |
|
|
let promise = instancePromises.get(validatorPath) |
|
|
if (!promise) { |
|
|
|
|
|
promise = AmpHtmlValidator.getInstance(validatorPath) |
|
|
instancePromises.set(validatorPath, promise) |
|
|
} |
|
|
return promise |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getBundledAmpValidatorFilepath() { |
|
|
return require.resolve( |
|
|
'next/dist/compiled/amphtml-validator/validator_wasm.js' |
|
|
) |
|
|
} |
|
|
|