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

Add pa11y scanner #3949

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const { postsCollection, servicesCollection, tagsCollection } = require('./confi
const { headingLinks } = require('./config/headingLinks');
const { contrastRatio, humanReadableContrastRatio } = require('./config/wcagColorContrast');
const privateLinks = require ('./config/privateLinksList');
const { pa11yScan } = require('./lib/pa11y')

const { imageShortcode, imageWithClassShortcode } = require('./config');

Expand Down Expand Up @@ -321,6 +322,8 @@ module.exports = function (config) { /* eslint-disable-line func-names */
ghostMode: false,
});

config.on('eleventy.after', pa11yScan);

// Set image shortcodes
config.addLiquidShortcode('image', imageShortcode);
config.addLiquidShortcode('image_with_class', imageWithClassShortcode);
Expand Down
1 change: 1 addition & 0 deletions .eleventyignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ README.md
config
docs
spec
lib
24 changes: 0 additions & 24 deletions _includes/layouts/wide.html

This file was deleted.

185 changes: 0 additions & 185 deletions _plugins/pa11y.rb

This file was deleted.

11 changes: 11 additions & 0 deletions lib/commit_differ.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { Differ } = require('./differ');

// Lists files that were modified in the last commit
// Used for CI pa11y checks, to check the last commit
class CommitDiffer extends Differ {
command = 'git diff-tree --no-commit-id --name-only -r $(git rev-parse HEAD)';
}

module.exports = {
CommitDiffer,
}
File renamed without changes.
16 changes: 16 additions & 0 deletions lib/differ.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const {execSync} = require('child_process');

// Lists files that have been modified since the last commit
// Used in local development, to check accessibility prior to
// committing changes
class Differ {
command = 'git ls-files --modified';

changedFiles() {
return String(execSync(this.command)).split(/\n/).filter(x => x !== '')
}
}

module.exports = {
Differ,
}
47 changes: 47 additions & 0 deletions lib/document.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const {execSync} = require('child_process');

// Takes an 11ty "result" (a post or page), the dir (folder/config),
// and a list of changed files from the appropriate differ class.
//
// A Document knows whether or not it's in the list of changed files,
// and Document#shouldScan returns true if it should be included in
// the next pa11y check.
class Document {
constructor(result, dir, changedFiles) {
this.result = result;
this.dir = dir;
this.layout = this.layoutName();
this.changedFiles = changedFiles;
}

shouldScan() {
return this.didSourceChange() || this.didLayoutChange()
}

// Returns whether the input (source) file was modified
// The slice(2) is to remove the "./" at the beginning of the path
didSourceChange() {
return this.changedFiles.includes(this.result.inputPath.slice(2))
}

// Returns whether this file's layout changed
didLayoutChange() {
return this.changedFiles.includes(`${this.dir.layouts}${this.layout}`)
}

// Gets the name of the layout for this file. If there's no layout,
// assume it uses the "default" layout.
// @todo Change this from system `grep` to parsing the YAML frontmatter
// @todo Assuming the "default" may not be a safe assumption
//
layoutName() {
let maybeMatch;
const layoutMatcher = `grep -i "^\s*layout: " "${this.result.inputPath}"` /* eslint-disable-line no-useless-escape */
try { maybeMatch = execSync(layoutMatcher) } catch { return 'default' }
return String(maybeMatch).trim().split(/layout: /)[1]
}
}

module.exports = {
Document,
}
File renamed without changes.
Loading
Loading