ShtukbaseStandard
package file→ .shtukbase/packages/design-a11y/checks/headings.ts

checks/headings.ts

29 lines, 1.3 KB.

  1. // A11Y-R05. Headings of an HTML page go in order: a heading is at most one level deeper than the one before it
  2. // (h2 → h3, not h2 → h4), and the page has one <h1>. Going back up (h4 → h2) is fine.
  3. // Only HTML pages: in TSX a component's headings join the headings of other components, so the order is seen
  4. // only on the rendered page — there the judge (judges/keyboard-and-structure.md) looks.
  5. import { elements } from '../../design-base/lib/markup.ts';
  6. import { lineAt, report } from '../../design-base/lib/files.ts';
  7. import { markupFiles } from './a11ylib.ts';
  8. export const RULE = { id: 'a11y-r05', title: 'Headings go in order' };
  9. export function check(root = process.cwd()): string[] {
  10. const out: string[] = [];
  11. for (const f of markupFiles(root)) {
  12. if (!f.html) continue;
  13. const hs = elements(f.text, /h[1-6]/);
  14. let prev = 0;
  15. for (const h of hs) {
  16. const level = Number(h.name[1]);
  17. if (prev && level > prev + 1)
  18. out.push(`${f.rel}:${lineAt(f.text, h.start)}: <h${level}> right after <h${prev}> — a level is skipped, <h${prev + 1}> is needed`);
  19. prev = level;
  20. }
  21. const ones = hs.filter((h) => h.name === 'h1');
  22. if (ones.length > 1) out.push(`${f.rel}:${lineAt(f.text, ones[1].start)}: the page has ${ones.length} <h1> headings — one is needed`);
  23. }
  24. return out;
  25. }
  26. if (import.meta.main) report(check());