mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +01:00
Improve highlighting for MD038/no-space-in-code, add more tests.
This commit is contained in:
parent
681e8bae4e
commit
5fee0a921d
4 changed files with 87 additions and 20 deletions
|
|
@ -220,11 +220,12 @@ function lintContent(
|
||||||
// Configure rule
|
// Configure rule
|
||||||
params.options = mergedRules[rule.name];
|
params.options = mergedRules[rule.name];
|
||||||
var errors = [];
|
var errors = [];
|
||||||
function addError(lineNumber, detail, context) {
|
function addError(lineNumber, detail, context, range) {
|
||||||
errors.push({
|
errors.push({
|
||||||
"lineNumber": lineNumber + frontMatterLines.length,
|
"lineNumber": lineNumber + frontMatterLines.length,
|
||||||
"detail": detail || null,
|
"detail": detail || null,
|
||||||
"context": context || null
|
"context": context || null,
|
||||||
|
"range": range || null
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
errors.add = function add(lineNumber) {
|
errors.add = function add(lineNumber) {
|
||||||
|
|
@ -238,7 +239,8 @@ function lintContent(
|
||||||
addError(lineNumber, "Expected: " + expected + "; Actual: " + actual);
|
addError(lineNumber, "Expected: " + expected + "; Actual: " + actual);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
errors.addContext = function addContext(lineNumber, context, left, right) {
|
errors.addContext =
|
||||||
|
function addContext(lineNumber, context, left, right, range) {
|
||||||
if (context.length <= 30) {
|
if (context.length <= 30) {
|
||||||
// Nothing to do
|
// Nothing to do
|
||||||
} else if (left && right) {
|
} else if (left && right) {
|
||||||
|
|
@ -248,7 +250,7 @@ function lintContent(
|
||||||
} else {
|
} else {
|
||||||
context = context.substr(0, 30) + "...";
|
context = context.substr(0, 30) + "...";
|
||||||
}
|
}
|
||||||
addError(lineNumber, null, context);
|
addError(lineNumber, null, context, range);
|
||||||
};
|
};
|
||||||
rule.func(params, errors);
|
rule.func(params, errors);
|
||||||
// Record any errors (significant performance benefit from length check)
|
// Record any errors (significant performance benefit from length check)
|
||||||
|
|
@ -263,9 +265,9 @@ function lintContent(
|
||||||
if (resultVersion === 0) {
|
if (resultVersion === 0) {
|
||||||
return error.lineNumber;
|
return error.lineNumber;
|
||||||
}
|
}
|
||||||
var range = null;
|
var range = error.range;
|
||||||
var regexp = rule.regexp;
|
var regexp = rule.regexp;
|
||||||
if (regexp) {
|
if (!range && regexp) {
|
||||||
if (typeof regexp === "function") {
|
if (typeof regexp === "function") {
|
||||||
regexp = regexp(params.options);
|
regexp = regexp(params.options);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
12
lib/rules.js
12
lib/rules.js
|
|
@ -15,7 +15,6 @@ var listItemMarkerInterruptsRe = /^[\s>]*(?:[*+-]|1\.)\s+/;
|
||||||
var reversedLinkRe = /\([^)]+\)\[[^\]^][^\]]*]/;
|
var reversedLinkRe = /\([^)]+\)\[[^\]^][^\]]*]/;
|
||||||
var spaceAfterBlockQuote = /^\s*(?:>\s+)+\S/;
|
var spaceAfterBlockQuote = /^\s*(?:>\s+)+\S/;
|
||||||
var spaceBeforeHeaderRe = /^\s+\S/;
|
var spaceBeforeHeaderRe = /^\s+\S/;
|
||||||
var spaceInsideCodeRe = /`(?:(?:\s[^`]*)|(?:[^`]*\s))`/;
|
|
||||||
var spaceInsideEmphasisRe = /(\*\*?|__?)(?:(?:\s.+)|(?:.+\s))\1/;
|
var spaceInsideEmphasisRe = /(\*\*?|__?)(?:(?:\s.+)|(?:.+\s))\1/;
|
||||||
var spaceInsideLinkRe = /\[(?:(?:\s[^\]]*)|(?:[^\]]*\s))](?=\(\S*\))/;
|
var spaceInsideLinkRe = /\[(?:(?:\s[^\]]*)|(?:[^\]]*\s))](?=\(\S*\))/;
|
||||||
var tabRe = /\t+/;
|
var tabRe = /\t+/;
|
||||||
|
|
@ -982,7 +981,7 @@ module.exports = [
|
||||||
"desc": "Spaces inside code span elements",
|
"desc": "Spaces inside code span elements",
|
||||||
"tags": [ "whitespace", "code" ],
|
"tags": [ "whitespace", "code" ],
|
||||||
"aliases": [ "no-space-in-code" ],
|
"aliases": [ "no-space-in-code" ],
|
||||||
"regexp": spaceInsideCodeRe,
|
"regexp": null,
|
||||||
"func": function MD038(params, errors) {
|
"func": function MD038(params, errors) {
|
||||||
var inlineCodeSpansRe = /(?:^|[^\\])((`+)((?:.*?[^`])|)\2(?!`))/g;
|
var inlineCodeSpansRe = /(?:^|[^\\])((`+)((?:.*?[^`])|)\2(?!`))/g;
|
||||||
forEachInlineChild(params, "code_inline",
|
forEachInlineChild(params, "code_inline",
|
||||||
|
|
@ -992,10 +991,15 @@ module.exports = [
|
||||||
while ((match = inlineCodeSpansRe.exec(line)) !== null) {
|
while ((match = inlineCodeSpansRe.exec(line)) !== null) {
|
||||||
var inlineCodeSpan = match[1];
|
var inlineCodeSpan = match[1];
|
||||||
var content = match[3];
|
var content = match[3];
|
||||||
|
var length = inlineCodeSpan.length;
|
||||||
|
var column = match.index + 1 + (match[0].length - length);
|
||||||
|
var range = [ column, length ];
|
||||||
if (/^\s([^`]|$)/.test(content)) {
|
if (/^\s([^`]|$)/.test(content)) {
|
||||||
errors.addContext(token.lineNumber, inlineCodeSpan);
|
errors.addContext(
|
||||||
|
token.lineNumber, inlineCodeSpan, true, false, range);
|
||||||
} else if (/[^`]\s$/.test(content)) {
|
} else if (/[^`]\s$/.test(content)) {
|
||||||
errors.addContext(token.lineNumber, inlineCodeSpan, false, true);
|
errors.addContext(
|
||||||
|
token.lineNumber, inlineCodeSpan, false, true, range);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -20,3 +20,10 @@ Space [ inside ](link) text
|
||||||
|
|
||||||
```
|
```
|
||||||
```
|
```
|
||||||
|
|
||||||
|
space `` inside `` code
|
||||||
|
space `inside` of ` code` elements
|
||||||
|
`space` inside `of` code ` elements`
|
||||||
|
space ``inside`` of `` code`` elements
|
||||||
|
`` ` embedded backtick``
|
||||||
|
``embedded backtick` ``
|
||||||
|
|
|
||||||
|
|
@ -71,6 +71,60 @@
|
||||||
"errorContext": "` inside `",
|
"errorContext": "` inside `",
|
||||||
"errorRange": [7, 10]
|
"errorRange": [7, 10]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"lineNumber": 24,
|
||||||
|
"ruleName": "MD038",
|
||||||
|
"ruleAlias": "no-space-in-code",
|
||||||
|
"ruleDescription": "Spaces inside code span elements",
|
||||||
|
"errorDetail": null,
|
||||||
|
"errorContext": "`` inside ``",
|
||||||
|
"errorRange": [ 7, 12 ]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"lineNumber": 25,
|
||||||
|
"ruleName": "MD038",
|
||||||
|
"ruleAlias": "no-space-in-code",
|
||||||
|
"ruleDescription": "Spaces inside code span elements",
|
||||||
|
"errorDetail": null,
|
||||||
|
"errorContext": "` code`",
|
||||||
|
"errorRange": [ 19, 7 ]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"lineNumber": 26,
|
||||||
|
"ruleName": "MD038",
|
||||||
|
"ruleAlias": "no-space-in-code",
|
||||||
|
"ruleDescription": "Spaces inside code span elements",
|
||||||
|
"errorDetail": null,
|
||||||
|
"errorContext": "` elements`",
|
||||||
|
"errorRange": [ 26, 11 ]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"lineNumber": 27,
|
||||||
|
"ruleName": "MD038",
|
||||||
|
"ruleAlias": "no-space-in-code",
|
||||||
|
"ruleDescription": "Spaces inside code span elements",
|
||||||
|
"errorDetail": null,
|
||||||
|
"errorContext": "`` code``",
|
||||||
|
"errorRange": [ 21, 9 ]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"lineNumber": 28,
|
||||||
|
"ruleName": "MD038",
|
||||||
|
"ruleAlias": "no-space-in-code",
|
||||||
|
"ruleDescription": "Spaces inside code span elements",
|
||||||
|
"errorDetail": null,
|
||||||
|
"errorContext": "`` ` embedded backtick``",
|
||||||
|
"errorRange": [ 1, 25 ]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"lineNumber": 29,
|
||||||
|
"ruleName": "MD038",
|
||||||
|
"ruleAlias": "no-space-in-code",
|
||||||
|
"ruleDescription": "Spaces inside code span elements",
|
||||||
|
"errorDetail": null,
|
||||||
|
"errorContext": "``embedded backtick` ``",
|
||||||
|
"errorRange": [ 1, 24 ]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"lineNumber": 19,
|
"lineNumber": 19,
|
||||||
"ruleName": "MD039",
|
"ruleName": "MD039",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue