bin/pa11y-ci.js
function loadSitemapIntoConfig(program, config) {
	const sitemapUrl = program.sitemap;
	const sitemapFind = (
		program.sitemapFind ?
		new RegExp(program.sitemapFind, 'gi') :
		null
	);
	const sitemapReplace = program.sitemapReplace || '';
	const sitemapExclude = (
		program.sitemapExclude ?
		new RegExp(program.sitemapExclude, 'gi') :
		null
	);

	return Promise.resolve()
		.then(() => {
			return fetch(sitemapUrl);
		})
		.then(response => {
			return response.text();
		})
		.then(body => {
			const $ = cheerio.load(body, {
				xmlMode: true
			});

			$('url > loc').toArray().forEach(element => {
				let url = $(element).text();
				if (sitemapExclude && url.match(sitemapExclude)) {
					return;
				}
				if (sitemapFind) {
					url = url.replace(sitemapFind, sitemapReplace);
				}
				config.urls.push(url);
			});

			return config;
		})
		.catch(error => {
			if (error.stack && error.stack.includes('node-fetch')) {
				throw new Error(`The sitemap "${sitemapUrl}" could not be loaded`);
			}
			throw new Error(`The sitemap "${sitemapUrl}" could not be parsed`);
		});
}
function loadSitemapIntoConfig(program, config) {
	const sitemapUrl = program.sitemap;
	const sitemapFind = (
		program.sitemapFind ?
		new RegExp(program.sitemapFind, 'gi') :
		null
	);
	const sitemapReplace = program.sitemapReplace || '';
	const sitemapExclude = (
		program.sitemapExclude ?
		new RegExp(program.sitemapExclude, 'gi') :
		null
	);

	return Promise.resolve()
		.then(() => {
			return fetch(sitemapUrl);
		})
		.then(response => {
			return response.text();
		})
		.then(body => {
			const $ = cheerio.load(body, {
				xmlMode: true
			});

			$('url > loc').toArray().forEach(element => {
				let url = $(element).text();
				if (sitemapExclude && url.match(sitemapExclude)) {
					return;
				}
				if (sitemapFind) {
					url = url.replace(sitemapFind, sitemapReplace);
				}
				config.urls.push(url);
			});

			return config;
		})
		.catch(error => {
			if (error.stack && error.stack.includes('node-fetch')) {
				throw new Error(`The sitemap "${sitemapUrl}" could not be loaded`);
			}
			throw new Error(`The sitemap "${sitemapUrl}" could not be parsed`);
		});
}
function loadSitemapIntoConfig ( program , config ) { const sitemapUrl = program . sitemap ; const sitemapFind = ( program . sitemapFind ? new RegExp ( program . sitemapFind , 'gi' ) : null ) ; const sitemapReplace = program . sitemapReplace || '' ; const sitemapExclude = ( program . sitemapExclude ? new RegExp ( program . sitemapExclude , 'gi' ) : null ) ; return Promise . resolve ( ) . then ( ( ) => { return fetch ( sitemapUrl ) ; } ) . then ( response => { return response . text ( ) ; } ) . then ( body => { const $ = cheerio . load ( body , { xmlMode : true } ) ; $ ( 'url > loc' ) . toArray ( ) . forEach ( element => { let url = $ ( element ) . text ( ) ; if ( sitemapExclude && url . match ( sitemapExclude ) ) { return ; } if ( sitemapFind ) { url = url . replace ( sitemapFind , sitemapReplace ) ; } config . urls . push ( url ) ; } ) ; return config ; } ) . catch ( error => { if ( error . stack && error . stack . includes ( 'node-fetch' ) ) { throw new Error ( ` ${ sitemapUrl } ` ) ; } throw new Error ( ` ${ sitemapUrl } ` ) ; } ) ; }
Load a sitemap from a remote URL, parse out the URLs, and add them to an existing config object
Load a sitemap from a remote URL parse out the URLs and add them to an existing config object