Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture Screen Per URL in a sitemap #74

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
45 changes: 44 additions & 1 deletion bin/pa11y-ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const globby = require('globby');
const protocolify = require('protocolify');
const pkg = require('../package.json');
const program = require('commander');

const filenamify = require('filenamify');

// Here we're using Commander to specify the CLI options
program
Expand Down Expand Up @@ -196,9 +196,42 @@ function defaultConfig(config) {
if (program.json) {
delete config.defaults.log;
}

// If default screenCapture is given, It should be added to urls also
config = updateConfigUrlsScreenCapture(config);

return config;
}

// Update the config.urls array by using config.default.screenCapture
function updateConfigUrlsScreenCapture(config) {
const urls = [];
if (config.defaults.screenCapture) {
for (const key in config.urls) {
if (typeof config.urls[key] === 'string') {
urls.push({
url: config.urls[key],
screenCapture: config.defaults.screenCapture.replace('filename', getFileNameFromUrl(config.urls[key]))
});
} else {
if (Object.prototype.hasOwnProperty.call(config.urls[key], 'screenCapture') === false) {
config.urls[key].screenCapture = config.defaults.screenCapture.replace('filename', getFileNameFromUrl(config.urls[key]));
}

urls.push(config.urls[key]);
}
}

config.urls = urls;
}
return config;
}

// Generate a file name string from url
function getFileNameFromUrl(url) {
return filenamify(url, {replacement: '_'});
}

// Load a sitemap from a remote URL, parse out the
// URLs, and add them to an existing config object
function loadSitemapIntoConfig(program, config) {
Expand Down Expand Up @@ -235,6 +268,16 @@ function loadSitemapIntoConfig(program, config) {
if (sitemapFind) {
url = url.replace(sitemapFind, sitemapReplace);
}

// If default screenCapture is set, the screen captured per url in sitemap.
// The filename should be like /abs/path/to/filename.png, the "filename" is replaced.
if (config.defaults.screenCapture) {
url = {
url: url,
screenCapture: config.defaults.screenCapture.replace('filename', getFileNameFromUrl(url))
};
}

config.urls.push(url);
});

Expand Down
Loading