Skip to content

Commit

Permalink
add no-space-in-link, fix #14
Browse files Browse the repository at this point in the history
  • Loading branch information
hustcc committed Nov 29, 2018
1 parent 9e71ef4 commit 404a602
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -54,6 +54,7 @@ lint-md README.md Document.md
| use-standard-ellipsis | 使用标准规范的省略号 | 使用标准规范的省略号‘……’ / ‘...’ |
| no-fullwidth-number | 不能用全角数字 | 注意输入法切换为半角输入 |
| no-space-in-emphasis | emphasis 内容前后不能有空格 | 删除 emphasis 内容中的前后空格即可 |
| no-space-in-link | link 内容前后不能有空格 | 删除 link 内容中的前后空格即可 |


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

describe('no-space-in-links', () => {
test('success', () => {
const md = `[hello world](https://atool.vip)`;
expect(lint(md)).toEqual([]);
});

test('fail', () => {
const md = `[ hello, ~~world~~ ](https://atool.vip)`;
expect(lint(md)).toEqual([{
column: 1,
level: 'error',
line: 1,
text: `Link content can not start / end with space: ' hello, world '`,
type: 'no-space-in-link'
}]);
});
});
2 changes: 1 addition & 1 deletion rules/helper/space-rule.js
Expand Up @@ -16,7 +16,7 @@ module.exports = (ast, matches, cb) => {
cb({
line,
column: column + i + 1, // column 从 i 开始
text: subErrorStr(text, i, 12), // substring 12 个字符
text: `No space between Chinese and alphabet / number: ${subErrorStr(text, i, 12)}`, // substring 12 个字符
});
}
}
Expand Down
3 changes: 2 additions & 1 deletion rules/index.js
Expand Up @@ -13,7 +13,8 @@ const PluginClasses = [
require('./no-special-characters'),
require('./use-standard-ellipsis'),
require('./no-fullwidth-number'),
require('./no-space-in-emphasis')
require('./no-space-in-emphasis'),
require('./no-space-in-link')
];


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

/**
* Link 内容前后不能有空格
* no-space-in-link
*/
module.exports = class extends Plugin {

static get type() {
return 'no-space-in-link';
};

pre() {}

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

if (_.startsWith(text, ' ') || _.endsWith(text, ' ')) {
this.cfg.throwError({
line,
column,
text: `Link content can not start / end with space: '${text}'`,
});
}
},
}
}

post() {}
};
4 changes: 4 additions & 0 deletions tests/no-space-in-link.md
@@ -0,0 +1,4 @@
[hello world](https://atool.vip)


[ hello, ~~world~~ ](https://atool.vip)

0 comments on commit 404a602

Please sign in to comment.