diff --git a/doc/rules.md b/doc/rules.md index bcc74a78..c53f2333 100644 --- a/doc/rules.md +++ b/doc/rules.md @@ -403,18 +403,20 @@ Options: `boolean`, default: `false`. ### first-heading-level ```md - + # Foo ## Bar - + ## 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 @@ -950,12 +952,12 @@ Options: `boolean`, default: `false`. ### no-multiple-toplevel-headings ```md - + # Foo # Bar - + # Foo ## Bar @@ -963,6 +965,8 @@ Options: `boolean`, default: `false`. Warn when multiple top-level headings are used. + Options: `number`, default: `1`. + ### no-shell-dollars ````md diff --git a/lib/rules/first-heading-level.js b/lib/rules/first-heading-level.js index 95a1078b..441ea1c7 100644 --- a/lib/rules/first-heading-level.js +++ b/lib/rules/first-heading-level.js @@ -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 - * + * * # Foo * * ## Bar * - * + * * ## Foo * * # Bar @@ -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; diff --git a/lib/rules/no-multiple-toplevel-headings.js b/lib/rules/no-multiple-toplevel-headings.js index 770e67d6..0dc66d68 100644 --- a/lib/rules/no-multiple-toplevel-headings.js +++ b/lib/rules/no-multiple-toplevel-headings.js @@ -5,13 +5,15 @@ * @module no-multiple-toplevel-headings * @fileoverview * Warn when multiple top-level headings are used. + * + * Options: `number`, default: `1`. * @example - * + * * # Foo * * # Bar * - * + * * # Foo * * ## Bar @@ -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) { @@ -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); diff --git a/test/fixtures/first-heading-level-invalid-second.md b/test/fixtures/first-heading-level-invalid-second.md new file mode 100644 index 00000000..ad0eb404 --- /dev/null +++ b/test/fixtures/first-heading-level-invalid-second.md @@ -0,0 +1 @@ +# Valid diff --git a/test/fixtures/first-heading-level-valid-second.md b/test/fixtures/first-heading-level-valid-second.md new file mode 100644 index 00000000..dfeb664e --- /dev/null +++ b/test/fixtures/first-heading-level-valid-second.md @@ -0,0 +1 @@ +## Valid diff --git a/test/fixtures/no-multiple-toplevel-headings-invalid-second.md b/test/fixtures/no-multiple-toplevel-headings-invalid-second.md new file mode 100644 index 00000000..b86bbb84 --- /dev/null +++ b/test/fixtures/no-multiple-toplevel-headings-invalid-second.md @@ -0,0 +1,4 @@ +Another heading +--------------- + +## Another diff --git a/test/fixtures/no-multiple-toplevel-headings-valid-second.md b/test/fixtures/no-multiple-toplevel-headings-valid-second.md new file mode 100644 index 00000000..bb77d90d --- /dev/null +++ b/test/fixtures/no-multiple-toplevel-headings-valid-second.md @@ -0,0 +1,4 @@ +Valid +--------------- + +### Another heading diff --git a/test/index.js b/test/index.js index 745f81f4..1489d648 100644 --- a/test/index.js +++ b/test/index.js @@ -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 () { @@ -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 () {