Skip to content

Commit

Permalink
Update main heading rules to accept preferred depth
Browse files Browse the repository at this point in the history
Update `first-heading-level` and `no-multiple-toplevel-headings`
to accept a preferred main heading level.  The previous, and
default, state is defined as level 1 headings, but it’s now
possible to specify other levels as well.

Closes GH-51.
  • Loading branch information
michaelmior authored and wooorm committed Apr 3, 2016
1 parent 3fbddf8 commit 7f4a51e
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 16 deletions.
14 changes: 9 additions & 5 deletions doc/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -403,18 +403,20 @@ Options: `boolean`, default: `false`.
### first-heading-level

```md
<!-- Valid: -->
<!-- Valid, when set to `1` -->
# Foo

## Bar

<!-- Invalid: -->
<!-- Invalid, when set to `1` -->
## Foo

# Bar
```

Warn when the first heading has a level other than `1`.
Warn when the first heading has a level other than a specified value.

Options: `number`, default: `1`.

### hard-break-spaces

Expand Down Expand Up @@ -950,19 +952,21 @@ Options: `boolean`, default: `false`.
### no-multiple-toplevel-headings

```md
<!-- Invalid: -->
<!-- Invalid, when set to `1` -->
# Foo

# Bar

<!-- Valid: -->
<!-- Valid, when set to `1` -->
# Foo

## Bar
```

Warn when multiple top-level headings are used.

Options: `number`, default: `1`.

### no-shell-dollars

````md
Expand Down
18 changes: 11 additions & 7 deletions lib/rules/first-heading-level.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
* @license MIT
* @module first-heading-level
* @fileoverview
* Warn when the first heading has a level other than `1`.
* Warn when the first heading has a level other than a specified value.
*
* Options: `number`, default: `1`.
* @example
* <!-- Valid: -->
* <!-- Valid, when set to `1` -->
* # Foo
*
* ## Bar
*
* <!-- Invalid: -->
* <!-- Invalid, when set to `1` -->
* ## Foo
*
* # Bar
Expand All @@ -29,21 +31,23 @@ var visit = require('unist-util-visit');
var position = require('mdast-util-position');

/**
* Warn when the first heading has a level other than `1`.
* Warn when the first heading has a level other than a specified value.
*
* @param {Node} ast - Root node.
* @param {File} file - Virtual file.
* @param {*} preferred - Ignored.
* @param {number?} [preferred=1] - First heading level.
* @param {Function} done - Callback.
*/
function firstHeadingLevel(ast, file, preferred, done) {
var style = preferred && preferred !== true ? preferred : 1;

visit(ast, 'heading', function (node) {
if (position.generated(node)) {
return null;
}

if (node.depth !== 1) {
file.warn('First heading level should be `1`', node);
if (node.depth !== style) {
file.warn('First heading level should be `' + style + '`', node);
}

return false;
Expand Down
11 changes: 7 additions & 4 deletions lib/rules/no-multiple-toplevel-headings.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
* @module no-multiple-toplevel-headings
* @fileoverview
* Warn when multiple top-level headings are used.
*
* Options: `number`, default: `1`.
* @example
* <!-- Invalid: -->
* <!-- Invalid, when set to `1` -->
* # Foo
*
* # Bar
*
* <!-- Valid: -->
* <!-- Valid, when set to `1` -->
* # Foo
*
* ## Bar
Expand All @@ -33,10 +35,11 @@ var position = require('mdast-util-position');
*
* @param {Node} ast - Root node.
* @param {File} file - Virtual file.
* @param {*} preferred - Ignored.
* @param {number?} [preferred=1] - Top heading level.
* @param {Function} done - Callback.
*/
function noMultipleToplevelHeadings(ast, file, preferred, done) {
var style = preferred && preferred !== true ? preferred : 1;
var topLevelheading = false;

visit(ast, 'heading', function (node) {
Expand All @@ -46,7 +49,7 @@ function noMultipleToplevelHeadings(ast, file, preferred, done) {
return;
}

if (node.depth === 1) {
if (node.depth === style) {
if (topLevelheading) {
pos = position.start(node);

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/first-heading-level-invalid-second.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Valid
1 change: 1 addition & 0 deletions test/fixtures/first-heading-level-valid-second.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## Valid
4 changes: 4 additions & 0 deletions test/fixtures/no-multiple-toplevel-headings-invalid-second.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Another heading
---------------

## Another
4 changes: 4 additions & 0 deletions test/fixtures/no-multiple-toplevel-headings-valid-second.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Valid
---------------

### Another heading
16 changes: 16 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,14 @@ describe('Rules', function () {

assertFile('first-heading-level-valid.md', []);
});

describeSetting(2, function () {
assertFile('first-heading-level-invalid-second.md', [
'first-heading-level-invalid-second.md:1:1-1:8: First heading level should be `2`'
]);

assertFile('first-heading-level-valid-second.md', []);
});
});

describeRule('heading-increment', function () {
Expand Down Expand Up @@ -719,6 +727,14 @@ describe('Rules', function () {

assertFile('no-multiple-toplevel-headings-valid.md', []);
});

describeSetting(2, function () {
assertFile('no-multiple-toplevel-headings-invalid-second.md', [
'no-multiple-toplevel-headings-invalid-second.md:4:1-4:11: Don’t use multiple top level headings (4:1)'
]);

assertFile('no-multiple-toplevel-headings-valid-second.md', []);
});
});

describeRule('no-literal-urls', function () {
Expand Down

0 comments on commit 7f4a51e

Please sign in to comment.