From 404a602b0625b0a68575c65887fef87409b0b057 Mon Sep 17 00:00:00 2001 From: hustcc Date: Thu, 29 Nov 2018 13:04:21 +0800 Subject: [PATCH] add no-space-in-link, fix #14 --- README.md | 1 + __tests__/rules/no-space-in-link.spec.js | 19 +++++++++++++ rules/helper/space-rule.js | 2 +- rules/index.js | 3 +- rules/no-space-in-link.js | 36 ++++++++++++++++++++++++ tests/no-space-in-link.md | 4 +++ 6 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 __tests__/rules/no-space-in-link.spec.js create mode 100644 rules/no-space-in-link.js create mode 100644 tests/no-space-in-link.md diff --git a/README.md b/README.md index 772c10f..3947266 100644 --- a/README.md +++ b/README.md @@ -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` 中增加自己的规则,开发约束: diff --git a/__tests__/rules/no-space-in-link.spec.js b/__tests__/rules/no-space-in-link.spec.js new file mode 100644 index 0000000..717acf5 --- /dev/null +++ b/__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' + }]); + }); +}); diff --git a/rules/helper/space-rule.js b/rules/helper/space-rule.js index 0e4cc91..5a6a085 100644 --- a/rules/helper/space-rule.js +++ b/rules/helper/space-rule.js @@ -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 个字符 }); } } diff --git a/rules/index.js b/rules/index.js index 6ccab2e..4e99d39 100644 --- a/rules/index.js +++ b/rules/index.js @@ -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') ]; diff --git a/rules/no-space-in-link.js b/rules/no-space-in-link.js new file mode 100644 index 0000000..c9d6032 --- /dev/null +++ b/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() {} +}; diff --git a/tests/no-space-in-link.md b/tests/no-space-in-link.md new file mode 100644 index 0000000..32ca654 --- /dev/null +++ b/tests/no-space-in-link.md @@ -0,0 +1,4 @@ +[hello world](https://atool.vip) + + +[ hello, ~~world~~ ](https://atool.vip)