Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make first-heading-level configurable #51

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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