Skip to content

Commit

Permalink
mdast-lint-blank-lines-1-0-2 init
Browse files Browse the repository at this point in the history
  • Loading branch information
vhf committed Dec 18, 2015
0 parents commit 106f578
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8
indent_style = space
indent_size = 2

7 changes: 7 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "airbnb",
"parser": "babel-eslint",
"env": {
"node": true
}
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
*.log
node_modules
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# mdast-lint-blank-lines-1-0-2

This [mdast-lint](https://github.com/wooorm/mdast-lint) rule was created for [free-programming-books-lint](https://github.com/vhf/free-programming-books-lint) to enforce [free-programming-books](https://github.com/vhf/free-programming-books) [formatting guidelines](https://github.com/vhf/free-programming-books/blob/master/CONTRIBUTING.md#formatting).

This rule ensures that a file has

- 2 empty lines between last link and new section
- 1 empty line between heading & first link of its section
- 0 empty line between two list items
- 1 empty line at the end of each .md file

```Text
<!-- Invalid -->
[...]
* [An Awesome Book](http://example.com/example.html)
### Example
* [Another Awesome Book](http://example.com/book.html)
* [Some Other Book](http://example.com/other.html)
<!-- Valid -->
[...]
* [An Awesome Book](http://example.com/example.html)
### Example
* [Another Awesome Book](http://example.com/book.html)
* [Some Other Book](http://example.com/other.html)
```

## Using the rule

### Via `.mdastrc`

```bash
npm install -g mdast
npm install -g mdast-lint
npm install mdast-lint-blank-lines-1-0-2 # local install!
```

Then, set up your `.mdastrc`:

```JSON
{
"plugins": {
"mdast-lint": {
"external": ["mdast-lint-blank-lines-1-0-2"]
}
}
}
```

Now you can use the following command to run the lint:

```bash
mdast --no-stdout xxx.md
```

### Via CLI

```bash
npm install -g mdast
npm install -g mdast-lint
npm install -g mdast-lint-blank-lines-1-0-2 # global install!
mdast --no-stdout -u mdast-lint="external:[\"mdast-lint-blank-lines-1-0-2\"]" xxx.md
```

Note that the `lint=<lint_options>` option only works with `mdast >= 1.1.1`.

This `README.md` is based on [this one](https://github.com/chcokr/mdast-lint-sentence-newline/blob/250b106c9e19b387270099cf16f17a84643f8944/README.md) by [@chcokr](https://github.com/chcokr) (MIT).
38 changes: 38 additions & 0 deletions dist/blank-lines.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

var visit = require('unist-util-visit');
var position = require('mdast-util-position');

function isApplicable(node) {
return ['paragraph', 'heading', 'list'].indexOf(node.type) !== -1;
}

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

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

if (next && isApplicable(node) && isApplicable(next)) {
if (node.type === 'heading' && next.type === 'heading') {
if (position.start(next).line !== position.end(node).line + 2) {
file.warn('Incorrect number of blank lines between headings', node);
}
} else if (node.type === 'heading' && next.type !== 'heading') {
if (position.start(next).line - position.end(node).line !== 2) {
file.warn('Incorrect number of blank lines between heading and section', node);
}
} else if (node.type === 'list') {
if (position.start(next).line - position.end(node).line !== 3) {
file.warn('Incorrect number of blank lines between last section and next heading', node);
}
}
}
});

done();
}

module.exports = blankLines;
40 changes: 40 additions & 0 deletions lib/blank-lines.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const visit = require('unist-util-visit');
const position = require('mdast-util-position');

function isApplicable(node) {
return [
'paragraph',
'heading',
'list',
].indexOf(node.type) !== -1;
}

function blankLines(ast, file, preferred, done) {
visit(ast, (node, index, parent) => {
const next = parent && parent.children[index + 1];

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

if (next && isApplicable(node) && isApplicable(next)) {
if (node.type === 'heading' && next.type === 'heading') {
if (position.start(next).line !== position.end(node).line + 2) {
file.warn('Incorrect number of blank lines between headings', node);
}
} else if (node.type === 'heading' && next.type !== 'heading') {
if (position.start(next).line - position.end(node).line !== 2) {
file.warn('Incorrect number of blank lines between heading and section', node);
}
} else if (node.type === 'list') {
if (position.start(next).line - position.end(node).line !== 3) {
file.warn('Incorrect number of blank lines between last section and next heading', node);
}
}
}
});

done();
}

module.exports = blankLines;
31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "mdast-lint-blank-lines-1-0-2",
"version": "0.1.0",
"description": "Checks that all titles are followed by 1 blank line, then no blank lines, then two blank lines before the next title",
"author": "Victor Felder <[email protected]>",
"repository": {
"type": "git",
"url": "git+https://github.com/vhf/mdast-lint-blank-lines-1-0-2.git"
},
"license": "MIT",
"dependencies": {
"mdast-util-position": "^1.0.0",
"unist-util-visit": "^1.0.0"
},
"devDependencies": {
"babel-core": "^6.3.17",
"babel-eslint": "^5.0.0-beta6",
"babel-preset-es2015": "^6.3.13",
"eslint": "^1.10.3",
"eslint-config-airbnb": "^2.1.1",
"eslint-plugin-react": "^3.11.3"
},
"scripts": {
"build": "babel lib -d dist"
},
"bugs": {
"url": "https://github.com/vhf/mdast-lint-blank-lines-1-0-2/issues"
},
"homepage": "https://github.com/vhf/mdast-lint-blank-lines-1-0-2#readme",
"main": "dist/index.js"
}

0 comments on commit 106f578

Please sign in to comment.