const { capitalize, categoryId, isRuleDisabled, isRuleEnabled, mkdir, readFile, rulesIn, writeFile } = require('./utils'); /** @typedef {import('axe-core').RuleMetadata} RuleMeta */ /** * @param {string} str */ const escapeBrackets = (str) => { return str.replace(/ { const ruleName = escapeBrackets(rule.help); return `* [${ruleName}][${rule.ruleId}] (${rule.ruleId})`; }; /** * @param {string} category * @param {RuleMeta[]} rules * @param {string} docVersion */ const createDoc = async (category, rules, docVersion) => { const id = categoryId(category); const name = capitalize(id); const enabledRuleLinks = rules.filter(isRuleEnabled).map(toRuleLink); const disabledRuleLinks = rules.filter(isRuleDisabled).map(toRuleLink); const ruleURLs = rules.map((rule) => { return `[${rule.ruleId}]: https://dequeuniversity.com/rules/axe/${docVersion}/${rule.ruleId}`; }); const content = ` # Axe: ${name} (\`${id}\`) \`axe/${id}\` contains accessibility rules from the axe ${id} category. See the ["Can the hint be configured?"][can configure] section of \`hint-axe\` for examples of enabling, disabling, and changing the severity of individual rules. ## Default rules (WCAG 2.1 Level A and Level AA) ${enabledRuleLinks.join('\n')} ${disabledRuleLinks.length ? ` ## Additional rules (off by default) ${disabledRuleLinks.join('\n')} `: ''} [can configure]: https://webhint.io/docs/user-guide/hints/hint-axe/#can-the-hint-be-configured ${ruleURLs.join('\n')} `; await writeFile(`docs/${id}.md`, content); }; /** * @param {string[]} categories */ const updateReadme = async (categories) => { const filename = 'README.md'; const source = await readFile(filename); const hints = categories.map((category) => { const id = categoryId(category); const name = capitalize(id); return `* [${name} (axe/${id})][axe/${id}]`; }); const hintLinks = categories.map((category) => { const id = categoryId(category); return `[axe/${id}]: https://webhint.io/docs/user-guide/hints/hint-axe/${id}/`; }); const content = source .replace(/()[\s\S]+()/, `$1\n${hints.join('\n')}\n$2`) .replace(/()[\s\S]+()/, `$1\n${hintLinks.join('\n')}\n$2`); await writeFile(filename, content); }; /** * Update the list of sub-hints in `README.md` and generate a * documentation file for each sub-hint under `./docs/`. * * Each documentation file lists all `axe-core` rules assigned to a * given sub-hint with links to the full documentation for each. * * @param {string[]} categories * @param {RuleMeta[]} rules * @param {string} version */ const createDocs = async (categories, rules, version) => { // Use just major.minor (e.g. '3.3' instead of '3.3.1'). const docVersion = version .split('.') .slice(0, 2) .join('.'); await mkdir('docs'); for (const category of categories) { createDoc(category, rulesIn(category, rules), docVersion); } updateReadme(categories); }; module.exports = { createDocs };