archivo del paquete→ .shtukbase/packages/design-a11y/checks/headings.ts
checks/headings.ts
29 líneas, 1.3 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- // A11Y-R05. Headings of an HTML page go in order: a heading is at most one level deeper than the one before it
- // (h2 → h3, not h2 → h4), and the page has one <h1>. Going back up (h4 → h2) is fine.
- // Only HTML pages: in TSX a component's headings join the headings of other components, so the order is seen
- // only on the rendered page — there the judge (judges/keyboard-and-structure.md) looks.
- import { elements } from '../../design-base/lib/markup.ts';
- import { lineAt, report } from '../../design-base/lib/files.ts';
- import { markupFiles } from './a11ylib.ts';
- export const RULE = { id: 'a11y-r05', title: 'Headings go in order' };
- export function check(root = process.cwd()): string[] {
- const out: string[] = [];
- for (const f of markupFiles(root)) {
- if (!f.html) continue;
- const hs = elements(f.text, /h[1-6]/);
- let prev = 0;
- for (const h of hs) {
- const level = Number(h.name[1]);
- if (prev && level > prev + 1)
- out.push(`${f.rel}:${lineAt(f.text, h.start)}: <h${level}> right after <h${prev}> — a level is skipped, <h${prev + 1}> is needed`);
- prev = level;
- }
- const ones = hs.filter((h) => h.name === 'h1');
- if (ones.length > 1) out.push(`${f.rel}:${lineAt(f.text, ones[1].start)}: the page has ${ones.length} <h1> headings — one is needed`);
- }
- return out;
- }
- if (import.meta.main) report(check());