Add MD038, MD039 with tests.

This commit is contained in:
David Anson 2015-04-15 18:24:42 -07:00
parent b85e53a9c4
commit 7e431f4499
6 changed files with 98 additions and 11 deletions

View file

@ -68,6 +68,8 @@ cases come directly from that project.
* **MD035** - Horizontal rule style
* **MD036** - Emphasis used instead of a header
* **MD037** - Spaces inside emphasis markers
* **MD038** - Spaces inside code span elements
* **MD039** - Spaces inside link text
See [Rules.md](doc/Rules.md) for more details.
@ -78,7 +80,7 @@ See [Rules.md](doc/Rules.md) for more details.
* **blank_lines** - MD012, MD022, MD031, MD032
* **blockquote** - MD027, MD028
* **bullet** - MD004, MD005, MD006, MD007, MD032
* **code** - MD014, MD031
* **code** - MD014, MD031, MD038
* **emphasis** - MD036, MD037
* **hard_tab** - MD010
* **headers** - MD001, MD002, MD003, MD018, MD019, MD020, MD021, MD022, MD023,
@ -87,12 +89,12 @@ See [Rules.md](doc/Rules.md) for more details.
* **html** - MD033
* **indentation** - MD005, MD006, MD007, MD027
* **line_length** - MD013
* **links** - MD011, MD034
* **links** - MD011, MD034, MD039
* **ol** - MD029, MD030, MD032
* **spaces** - MD018, MD019, MD020, MD021, MD023
* **ul** - MD004, MD005, MD006, MD007, MD030, MD032
* **url** - MD034
* **whitespace** - MD009, MD010, MD012, MD027, MD028, MD030, MD037
* **whitespace** - MD009, MD010, MD012, MD027, MD028, MD030, MD037, MD038, MD039
## API

View file

@ -824,3 +824,32 @@ Rationale: Emphasis is only parsed as such when the asterisks/underscores
aren't completely surrounded by spaces. This rule attempts to detect where
they were surrounded by spaces, but it appears that emphasized text was
intended by the author.
## MD038 - Spaces inside code span elements
Tags: whitespace, code
This rule is triggered on code span elements that have spaces right inside the
backticks:
` some text `
`some text `
` some text`
To fix this, remove the spaces inside the codespan markers:
`some text`
## MD039 - Spaces inside link text
Tags: whitespace, links
This rule is triggered on links that have spaces surrounding the link text:
[ a link ](http://www.example.com/)
To fix this, remove the spaces surrounding the link text:
[a link](http://www.example.com/)

View file

@ -60,12 +60,14 @@ function forEachLine(params, callback) {
});
}
// Calls the provided function for each text token
function forEachText(params, callback) {
// Calls the provided function for each specified inline child token
function forEachInlineChild(params, type, callback) {
filterTokens(params.tokens, "inline")
.forEach(function forToken(token) {
filterTokens(token.children, "text")
.forEach(callback);
filterTokens(token.children, type)
.forEach(function forChild(child) {
callback(child, token);
});
});
}
@ -275,7 +277,7 @@ module.exports = [
"desc": "Reversed link syntax",
"tags": [ "links" ],
"func": function MD011(params, errors) {
forEachText(params, function forToken(token) {
forEachInlineChild(params, "text", function forToken(token) {
if (/\([^)]+\)\[[^\]]+\]/.test(token.content)) {
errors.push(token.lineNumber);
}
@ -706,12 +708,49 @@ module.exports = [
"desc": "Spaces inside emphasis markers",
"tags": [ "whitespace", "emphasis" ],
"func": function MD037(params, errors) {
forEachText(params, function forToken(token) {
forEachInlineChild(params, "text", function forToken(token) {
if (/\s(\*\*?|__?)\s.+\1/.test(token.content) ||
/(\*\*?|__?).+\s\1\s/.test(token.content)) {
errors.push(token.lineNumber);
}
});
}
},
{
"name": "MD038",
"desc": "Spaces inside code span elements",
"tags": [ "whitespace", "code" ],
"func": function MD038(params, errors) {
forEachInlineChild(params, "code_inline",
function forToken(token, inline) {
if (inline.content.indexOf("`" + token.content + "`") === -1) {
errors.push(token.lineNumber);
}
});
}
},
{
"name": "MD039",
"desc": "Spaces inside link text",
"tags": [ "whitespace", "links" ],
"func": function MD039(params, errors) {
filterTokens(params.tokens, "inline")
.forEach(function forToken(token) {
var inLink = false;
token.children.forEach(function forChild(child) {
if (child.type === "link_open") {
inLink = true;
} else if (child.type === "link_close") {
inLink = false;
} else if ((child.type === "text") &&
inLink &&
(child.content.trim().length !== child.content.length)) {
errors.push(child.lineNumber);
}
});
});
}
}
];

View file

@ -498,7 +498,7 @@ module.exports.badFileSync = function badFileSync(test) {
};
module.exports.readme = function readme(test) {
test.expect(88);
test.expect(92);
var tagToRules = {};
rules.forEach(function forRule(rule) {
rule.tags.forEach(function forTag(tag) {
@ -555,7 +555,7 @@ module.exports.readme = function readme(test) {
};
module.exports.doc = function doc(test) {
test.expect(135);
test.expect(143);
fs.readFile("doc/Rules.md", shared.utf8Encoding,
function readFile(err, contents) {
test.ifError(err);

View file

@ -0,0 +1,7 @@
`normal codespan element`
` codespan element with space inside left` {MD038}
`codespan element with space inside right ` {MD038}
` codespan element with spaces inside ` {MD038}

View file

@ -0,0 +1,10 @@
[foo](http://bar/)
[foo ](http://bar/) {MD039}
[ foo](http://bar/) {MD039}
[ foo ](http://bar/) {MD039}
The following shouldn't break anything:
[![Screenshot.png](/images/Screenshot.png)](/images/Screenshot.png)