Skip to content

Commit

Permalink
Add option to allow tight lists in no-missing-blank-lines
Browse files Browse the repository at this point in the history
This new feature adds support for allowing missing blank lines between
block-level nodes in lists, through passing `{exceptTightLists: true}`
to `no-missing-blank-lines` (default: false).

Closes GH-85.
  • Loading branch information
wooorm committed Aug 14, 2016
1 parent 956f380 commit a4085f2
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 6 deletions.
35 changes: 34 additions & 1 deletion doc/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -1790,7 +1790,11 @@ mailto:[email protected]

## `no-missing-blank-lines`

Warn for missing blank lines before a block node.
Warn when missing blank lines before a block node.

This rule can be configured to allow tight list items
without blank lines between their contents through
`exceptTightLists: true` (default: false).

When this rule is turned on, the following file
`valid.md` is ok:
Expand All @@ -1799,6 +1803,12 @@ When this rule is turned on, the following file
# Foo

## Bar

- Paragraph

+ List.

Paragraph.
```

When this rule is turned on, the following file
Expand All @@ -1807,6 +1817,29 @@ When this rule is turned on, the following file
```markdown
# Foo
## Bar

- Paragraph
+ List.

Paragraph.
```

```text
2:1-2:7: Missing blank line before block node
5:3-5:10: Missing blank line before block node
```

When this rule is `{ exceptTightLists: true }`, the following file
`tight.md` is **not** ok:

```markdown
# Foo
## Bar

- Paragraph
+ List.

Paragraph.
```

```text
Expand Down
46 changes: 41 additions & 5 deletions lib/rules/no-missing-blank-lines.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,52 @@
* @license MIT
* @module no-missing-blank-lines
* @fileoverview
* Warn for missing blank lines before a block node.
* Warn when missing blank lines before a block node.
*
* This rule can be configured to allow tight list items
* without blank lines between their contents through
* `exceptTightLists: true` (default: false).
*
* @example {"name": "valid.md"}
*
* # Foo
*
* ## Bar
*
* - Paragraph
*
* + List.
*
* Paragraph.
*
* @example {"name": "invalid.md", "label": "input"}
*
* # Foo
* ## Bar
*
* - Paragraph
* + List.
*
* Paragraph.
*
* @example {"name": "invalid.md", "label": "output"}
*
* 2:1-2:7: Missing blank line before block node
* 5:3-5:10: Missing blank line before block node
*
* @example {"name": "tight.md", "setting": {"exceptTightLists": true}, "label": "input"}
*
* # Foo
* ## Bar
*
* - Paragraph
* + List.
*
* Paragraph.
*
* @example {"name": "tight.md", "setting": {"exceptTightLists": true}, "label": "output"}
*
* 2:1-2:7: Missing blank line before block node
*/

'use strict';
Expand All @@ -38,18 +68,24 @@ module.exports = noMissingBlankLines;
* @param {Node} ast - Root node.
* @param {File} file - Virtual file.
*/
function noMissingBlankLines(ast, file) {
function noMissingBlankLines(ast, file, options) {
var allow = (options || {}).exceptTightLists;

visit(ast, function (node, index, parent) {
var next = parent && parent.children[index + 1];

if (position.generated(node)) {
return;
}

if (allow && parent && parent.type === 'listItem') {
return;
}

if (
next &&
isApplicable(node) &&
isApplicable(next) &&
applicable(node) &&
applicable(next) &&
position.start(next).line === position.end(node).line + 1
) {
file.warn('Missing blank line before block node', next);
Expand All @@ -63,7 +99,7 @@ function noMissingBlankLines(ast, file) {
* @param {Node} node - Node to test.
* @return {boolean} - Whether or not `node` is applicable.
*/
function isApplicable(node) {
function applicable(node) {
return [
'paragraph',
'blockquote',
Expand Down

0 comments on commit a4085f2

Please sign in to comment.