From 106f578e06664ba88c47796001657a63a8e7f42c Mon Sep 17 00:00:00 2001 From: Victor Felder Date: Fri, 18 Dec 2015 11:02:21 +0100 Subject: [PATCH] mdast-lint-blank-lines-1-0-2 init --- .babelrc | 3 ++ .editorconfig | 10 ++++++ .eslintrc | 7 +++++ .gitignore | 3 ++ README.md | 74 +++++++++++++++++++++++++++++++++++++++++++++ dist/blank-lines.js | 38 +++++++++++++++++++++++ lib/blank-lines.js | 40 ++++++++++++++++++++++++ package.json | 31 +++++++++++++++++++ 8 files changed, 206 insertions(+) create mode 100644 .babelrc create mode 100644 .editorconfig create mode 100644 .eslintrc create mode 100644 .gitignore create mode 100644 README.md create mode 100644 dist/blank-lines.js create mode 100644 lib/blank-lines.js create mode 100644 package.json diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..c13c5f6 --- /dev/null +++ b/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": ["es2015"] +} diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..76d6dab --- /dev/null +++ b/.editorconfig @@ -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 + diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..4d60e9d --- /dev/null +++ b/.eslintrc @@ -0,0 +1,7 @@ +{ + "extends": "airbnb", + "parser": "babel-eslint", + "env": { + "node": true + } +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d7e3a4e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.DS_Store +*.log +node_modules diff --git a/README.md b/README.md new file mode 100644 index 0000000..9bf2e40 --- /dev/null +++ b/README.md @@ -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 + + +[...] +* [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) + + + +[...] +* [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=` 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). diff --git a/dist/blank-lines.js b/dist/blank-lines.js new file mode 100644 index 0000000..4295239 --- /dev/null +++ b/dist/blank-lines.js @@ -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; \ No newline at end of file diff --git a/lib/blank-lines.js b/lib/blank-lines.js new file mode 100644 index 0000000..2993684 --- /dev/null +++ b/lib/blank-lines.js @@ -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; diff --git a/package.json b/package.json new file mode 100644 index 0000000..e39c9a2 --- /dev/null +++ b/package.json @@ -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 ", + "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" +}