-
Notifications
You must be signed in to change notification settings - Fork 2
/
wp-pa11y.js
251 lines (212 loc) · 6.87 KB
/
wp-pa11y.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env node
const fetch = require("node-fetch");
const fs = require("fs");
const https = require("https");
const http = require("http");
const pa11y = require("pa11y");
const htmlReporter = require("pa11y/lib/reporters/html");
const cliReporter = require("pa11y/lib/reporters/cli");
const path = require("path");
const puppeteer = require("puppeteer");
const { XMLParser } = require("fast-xml-parser");
const { program, Option } = require("commander");
// Configuration.
const pkg = require("./package.json");
const name = "wp-pa11y";
const version = pkg.version || "0.0.0";
const { cosmiconfigSync } = require("cosmiconfig");
const cosmicConfig = cosmiconfigSync(name, {
searchPlaces: ["package.json"],
});
const searchedFor = cosmicConfig.search();
if (searchedFor === null || searchedFor.isEmpty) {
console.error(
"Could not find configuration. Please check docs and try again."
);
process.exit(1);
}
// Create base folder for reports to same path as config file.
const configDir = path.dirname(searchedFor.filepath);
const outputDir = path.resolve(configDir, `.${name}`);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
const config = cosmicConfig.load(searchedFor.filepath);
program
.version(version, "-v, --version", "output the current version")
.addOption(
new Option("-o, --output <type>", "output type")
.choices(["console", "html"])
.default("console")
.env("WP_PA11Y_OUTPUT")
);
program.parse();
const opts = program.opts();
const reportType = opts.output || "console";
let configs = config.config;
if (configs.length === 0) {
console.error("No sitemaps to process. Exiting.");
process.exit(0);
}
configs.forEach(async (item) => {
const folderName = new URL(item.url).hostname
.replace("www.", "")
.replace(".", "");
const dir = path.resolve(outputDir, folderName);
if (!fs.existsSync(dir) && reportType === "html") {
fs.mkdirSync(dir, { recursive: true });
}
const urlList = await getUrls(item.url);
const urlObj = {
folderName,
urlList,
};
if (urlObj.urlList?.length > 0) {
runPa11y(urlObj, item.config);
}
});
/**
* Get urls from sitemap
*
* @param {string} url
* @returns {Promise}
*/
async function getUrls(url) {
// Bypass self-signed cert.
const httpsAgent = new https.Agent({
rejectUnauthorized: false,
});
const httpAgent = new http.Agent();
const fetchOptions = {
method: "GET",
agent(_parsedURL) {
// Determine agent based on protocol.
if (_parsedURL.protocol === 'http:') {
return httpAgent;
}
return httpsAgent;
},
};
const response = await fetch(url, fetchOptions);
const content = await response.text();
const parser = new XMLParser();
const data = parser.parse(content);
return data.urlset.url.length
? data.urlset.url.map((link) => link.loc)
: [];
}
/**
* Run Pa11y for given urls
*
* @param {object} urlObj Object containing folder name and url list.
* @param {object} config Object containing pa11y config.
* @return {void}
*/
async function runPa11y(urlObj, config) {
let browser;
let pages = [];
try {
const options = {
log: {
debug: console.log,
error: console.error,
info: console.log,
},
runners: ["axe", "htmlcs"],
};
browser = await puppeteer.launch();
const results = [];
const issueHashes = [];
const { folderName, urlList } = urlObj;
const date = getTimestamp();
const summaryFileName = `${date}-${folderName}-summary.html`;
let summaryResults = {};
for (let i = 0; i < urlList.length; i++) {
pages.push(await browser.newPage());
results[i] = await pa11y(urlList[i], {
...config,
browser,
page: pages[i],
log: options.log,
runners: options.runners,
});
if (i === 0) {
summaryResults = results[i];
summaryResults.issues = [];
}
if (results[i].issues.length > 0) {
// Push only fully unique issues to summary results.
results[i].issues.forEach((issue) => {
const issueHash = `${issue.runner}/${issue.code}/${issue.selector}`;
if (!issueHashes.includes(issueHash)) {
issueHashes.push(issueHash);
summaryResults.issues.push(issue);
}
});
if (reportType === "html") {
// Write both page-specific and summary results to HTML files.
// This allows providing a summary even if the execution is stopped.
const resultObjects = [
{
fileName: summaryFileName,
results: summaryResults,
},
{
fileName: getFileName(urlList[i]),
results: results[i],
},
];
for (const resultObject of resultObjects) {
const htmlResults = await htmlReporter.results(
resultObject.results
);
const htmlOutput = path.resolve(
outputDir,
folderName,
resultObject.fileName
);
fs.writeFileSync(htmlOutput, htmlResults);
}
} else {
console.log(cliReporter.results(results[i]));
}
}
}
if (reportType === "console") {
// Write summary object to CLI.
console.log("Printing summary results of all issues...");
console.log(cliReporter.results(summaryResults));
}
for (const page of pages) {
await page.close();
}
await browser.close();
} catch (error) {
console.error(error.message);
}
}
/**
* Get file name
*
* @param {string} url
* @return {string}
*/
function getFileName(url) {
let fileName = url
.toLowerCase()
.trim()
.replace(/[^\w\s-]/g, "")
.replace(/[\s_-]+/g, "-")
.replace(/^-+|-+$/g, "")
.replace("httpswww", "");
const date = getTimestamp();
return `${date}-${fileName}.html`;
}
/**
* Get current timestamp as a string for file names.
* @return {string}
*/
function getTimestamp() {
const dt = new Date();
return `${dt.getFullYear()}-${dt.getMonth() + 1}-${dt.getDate()}`;
}