import * as aChecker from "accessibility-checker"; import "lume/types.ts"; import { rm } from "node:fs"; // HTML Accessibility Plugin, by dish // version 1.0.0 // Based on IBM's accessibility-checker library export default function () { return (site: Lume.Site) => { site.process([".html"], checkPages); async function checkPages(pages: Lume.Page[]) { const barReport = site.debugBar?.collection("Accessibility Checker"); if (barReport) { barReport.icon = "wheelchair-motion"; barReport.contexts = { "violation": { background: "error", }, "potentialviolation": { background: "error", }, "recommendation": { background: "info", }, "potentialrecommendation": { background: "info", }, }; } console.info("[checkAccessibility] Starting..."); rm(`${Deno.cwd()}/accessibility-results`, { recursive: true, force: true }, (err) => { if (err) throw err; }); for (const page of pages) { const label = page.outputPath.substring(0, page.outputPath.length - 5); try { let res = await aChecker.getCompliance( page.content, label, ); if (aChecker.assertCompliance(res.report) === 0) { (() => {}); } else { barReport?.items.push({ title: page.data.url, details: `${res.report.results.length} errors`, items: Array.from(res.report.results).map((rep) => ({ title: `${rep.ruleId} - ${rep.reasonId}`, context: rep.level, text: rep.message, actions: [ { text: "Highlight", icon: "question", onclick: `highlightElement("${rep.path.dom}", "${page.outputPath}")`, }, { text: "Help", icon: "question", href: rep.help, target: "_blank", }, ], })), }); } } catch (err) { console.error(err); } } await aChecker.close(); } }; } function lookupRule(rules: Object, ruleId: string, reasonId: string) { const rule = rules[ruleId]; const reason: string = rule[reasonId]; return reason; }