Skip to content

Commit

Permalink
add no-trailing-punctuation, fix #18
Browse files Browse the repository at this point in the history
  • Loading branch information
hustcc committed Nov 29, 2018
1 parent bfdcb53 commit ce60528
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -56,6 +56,7 @@ lint-md README.md Document.md
| no-space-in-emphasis | emphasis 内容前后不能有空格 | 删除 emphasis 内容中的前后空格即可 |
| no-space-in-link | link 内容前后不能有空格 | 删除 link 内容中的前后空格即可 |
| no-multiple-space-blockquote | blockquote 语法不能包含有多个空格 | 删除 blockquote 内容中多余的空格 |
| no-trailing-punctuation | 标题不能以标点符号结尾 | 删除标题最后的标点符号 |


> 目前仅仅检查了比较通用的类型,**欢迎 pull request**,在 `rules` 中增加自己的规则,开发约束:
Expand Down
19 changes: 19 additions & 0 deletions __tests__/rules/no-trailing-punctuation.spec.js
@@ -0,0 +1,19 @@
const lint = require('../lint');

describe('no-trailing-punctuation', () => {
test('success', () => {
const md = `## header 2`;
expect(lint(md)).toEqual([]);
});

test('fail', () => {
const md = `### header 3**!**`;
expect(lint(md)).toEqual([{
column: 1,
level: 'error',
line: 1,
text: `Header content can not end with symbol: 'header 3!'`,
type: 'no-trailing-punctuation'
}]);
});
});
3 changes: 2 additions & 1 deletion rules/index.js
Expand Up @@ -15,7 +15,8 @@ const PluginClasses = [
require('./no-fullwidth-number'),
require('./no-space-in-emphasis'),
require('./no-space-in-link'),
require('./no-multiple-space-blockquote')
require('./no-multiple-space-blockquote'),
require('./no-trailing-punctuation')
];


Expand Down
38 changes: 38 additions & 0 deletions rules/no-trailing-punctuation.js
@@ -0,0 +1,38 @@
const { Plugin } = require('ast-plugin');
const _ = require('lodash');
const { astToText } = require('./helper/ast');

const Symbols = '.,;:!?。,;:!?…';

/**
* Header 内容不能以标点符号结尾
* no-trailing-punctuation
*/
module.exports = class extends Plugin {

static get type() {
return 'no-trailing-punctuation';
};

pre() {}

visitor() {
return {
heading: ast => {
const text = astToText(ast.node);
const line = ast.node.position.start.line;
const column = ast.node.position.start.column;

if (_.includes(Symbols, _.last(_.trimEnd(text)))) {
this.cfg.throwError({
line,
column,
text: `Header content can not end with symbol: '${text}'`,
});
}
},
}
}

post() {}
};
1 change: 1 addition & 0 deletions tests/no-trailing-punctuation.md
@@ -0,0 +1 @@
### header 3**!**

0 comments on commit ce60528

Please sign in to comment.