Skip to content

Commit edc23f3

Browse files
authored
Merge pull request #21 from jhackshaw/ignore-directories
2 parents c86e420 + 5d05752 commit edc23f3

File tree

6 files changed

+57
-4
lines changed

6 files changed

+57
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ package = "netlify-plugin-a11y"
4444
]
4545

4646
# # optional config
47+
# ignoreDirectories = ['/admin'] # explicitly ignore these directories
48+
4749
# resultMode = "warn" # is "error" by default
4850

4951
# # Developer only

manifest.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ name: netlify-plugin-a11y
22
inputs:
33
- name: checkPaths
44
required: true
5+
- name: ignoreDirectories
6+
required: false
57
- name: resultMode
68
default: error
79
- name: debugMode

plugin/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ const pluginCore = require('./pluginCore');
99

1010
module.exports = {
1111
async onPostBuild({
12-
inputs: { checkPaths, resultMode, debugMode },
12+
inputs: { checkPaths, ignoreDirectories, resultMode, debugMode },
1313
constants: { PUBLISH_DIR },
1414
utils: { build }
1515
}) {
1616
const htmlFilePaths = await pluginCore.generateFilePaths({
1717
fileAndDirPaths: checkPaths,
18+
ignoreDirectories: ignoreDirectories || [],
1819
PUBLISH_DIR
1920
});
2021
if (debugMode) {

plugin/pluginCore.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,29 @@ const runPa11yOnFile = async function(htmlFilePath, build) {
3535

3636
exports.generateFilePaths = async function({
3737
fileAndDirPaths, // array, mix of html and directories
38+
ignoreDirectories = [],
3839
PUBLISH_DIR,
3940
testMode,
4041
debugMode
4142
}) {
43+
const excludeDirGlobs = ignoreDirectories.map(
44+
// add ! and strip leading slash
45+
(dir) => `!${dir.replace(/^\/+/, "")}`
46+
);
4247
const htmlFilePaths = await Promise.all(
43-
fileAndDirPaths.map(fileAndDirPath => findHtmlFiles(`${PUBLISH_DIR}/${fileAndDirPath}`))
48+
fileAndDirPaths.map(fileAndDirPath =>
49+
findHtmlFiles(`${PUBLISH_DIR}${fileAndDirPath}`, excludeDirGlobs)
50+
)
4451
)
4552
return [].concat(...htmlFilePaths)
4653
};
4754

48-
const findHtmlFiles = async function(fileAndDirPath) {
55+
const findHtmlFiles = async function (fileAndDirPath, directoryFilter) {
4956
if (await isDirectory(fileAndDirPath)) {
50-
const fileInfos = await readdirp.promise(fileAndDirPath, { fileFilter: '*.html' })
57+
const fileInfos = await readdirp.promise(fileAndDirPath, {
58+
fileFilter: '*.html',
59+
directoryFilter
60+
})
5161
return fileInfos.map(({ fullPath }) => fullPath)
5262
}
5363

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
7+
<title>Document</title>
8+
</head>
9+
<body></body>
10+
</html>

tests/generateFilePaths/this.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,31 @@ test('generateFilePaths works', async () => {
1111
});
1212
expect(results).toMatchSnapshot();
1313
});
14+
15+
const pathInResults = (expectedPath, results) => {
16+
return results.findIndex((r) => r.endsWith(expectedPath)) != -1;
17+
};
18+
19+
test("ignoreDirectories works including leading slash", async () => {
20+
const results = await pluginCore.generateFilePaths({
21+
fileAndDirPaths: ["/"],
22+
ignoreDirectories: ["/admin"],
23+
PUBLISH_DIR,
24+
});
25+
expect(pathInResults("publishDir/blog/post1.html", results)).toBe(true);
26+
expect(pathInResults("publishDir/about.html", results)).toBe(true);
27+
expect(pathInResults("publishDir/index.html", results)).toBe(true);
28+
expect(pathInResults("publishDir/admin/index.html", results)).toBe(false);
29+
});
30+
31+
test("ignoreDirectories works without leading slash", async () => {
32+
const results = await pluginCore.generateFilePaths({
33+
fileAndDirPaths: ["/"],
34+
ignoreDirectories: ["admin"],
35+
PUBLISH_DIR,
36+
});
37+
expect(pathInResults("publishDir/blog/post1.html", results)).toBe(true);
38+
expect(pathInResults("publishDir/about.html", results)).toBe(true);
39+
expect(pathInResults("publishDir/index.html", results)).toBe(true);
40+
expect(pathInResults("publishDir/admin/index.html", results)).toBe(false);
41+
});

0 commit comments

Comments
 (0)