aboutsummaryrefslogtreecommitdiff
path: root/guidelines/wikipedia/headingOrder.js
blob: 0875d4a65c627261637383ae570df6ee0aca42e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
 * Layout rules for Wikipedia article content
 */
exports.name        = "Headings should be in order.";
exports.description = "Headers should increase sequentially, or reset to a higher section level.";

exports.rule = function(report) {
  findH1s(report);

  checkHeadingOrder(report);
};

/**
 * No H1 headings should be used in the article content.
 */
var findH1s = function(report) {
  $5('#mw-content-text h1').each(
    function(idx, elt){
      report.error("Top-level headings should not be used in article content.");
    });
};


/**
 * Check that headers increase one step at a time, or go back to the
 * beginning (in this case, heading 2).
 *
 * Headers using the <header> tag are except from this check.
 */
var checkHeadingOrder = function(report) {
  var last=1;

  $5('#mw-content-text :header').each(
    function(idx, elt){
      if (elt.tagName == 'header') {
        return;
      }

      // parse the level out of 'h1', 'h2', 'h3', etc.
      var level = parseInt(elt.tagName.substring(1,2));

      if (level > (last + 1)) {
        report.error("Heading is out of order; heading level was: "+level+
                     " but last level was: "+last, elt);
      }
      // set the level regardless, since the error *could* be with the
      // prior level heading, so we don't want to cause more errors
      // erroniously:
      last = level;
    });
};