mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2026-03-02 16:40:16 +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
|
||||
params.options = mergedRules[rule.name];
|
||||
var errors = [];
|
||||
function addError(lineNumber, detail, context) {
|
||||
function addError(lineNumber, detail, context, range) {
|
||||
errors.push({
|
||||
"lineNumber": lineNumber + frontMatterLines.length,
|
||||
"detail": detail || null,
|
||||
"context": context || null
|
||||
"context": context || null,
|
||||
"range": range || null
|
||||
});
|
||||
}
|
||||
errors.add = function add(lineNumber) {
|
||||
|
|
@ -238,18 +239,19 @@ function lintContent(
|
|||
addError(lineNumber, "Expected: " + expected + "; Actual: " + actual);
|
||||
}
|
||||
};
|
||||
errors.addContext = function addContext(lineNumber, context, left, right) {
|
||||
if (context.length <= 30) {
|
||||
// Nothing to do
|
||||
} else if (left && right) {
|
||||
context = context.substr(0, 15) + "..." + context.substr(-15);
|
||||
} else if (right) {
|
||||
context = "..." + context.substr(-30);
|
||||
} else {
|
||||
context = context.substr(0, 30) + "...";
|
||||
}
|
||||
addError(lineNumber, null, context);
|
||||
};
|
||||
errors.addContext =
|
||||
function addContext(lineNumber, context, left, right, range) {
|
||||
if (context.length <= 30) {
|
||||
// Nothing to do
|
||||
} else if (left && right) {
|
||||
context = context.substr(0, 15) + "..." + context.substr(-15);
|
||||
} else if (right) {
|
||||
context = "..." + context.substr(-30);
|
||||
} else {
|
||||
context = context.substr(0, 30) + "...";
|
||||
}
|
||||
addError(lineNumber, null, context, range);
|
||||
};
|
||||
rule.func(params, errors);
|
||||
// Record any errors (significant performance benefit from length check)
|
||||
if (errors.length) {
|
||||
|
|
@ -263,9 +265,9 @@ function lintContent(
|
|||
if (resultVersion === 0) {
|
||||
return error.lineNumber;
|
||||
}
|
||||
var range = null;
|
||||
var range = error.range;
|
||||
var regexp = rule.regexp;
|
||||
if (regexp) {
|
||||
if (!range && regexp) {
|
||||
if (typeof regexp === "function") {
|
||||
regexp = regexp(params.options);
|
||||
}
|
||||
|
|
|
|||
12
lib/rules.js
12
lib/rules.js
|
|
@ -15,7 +15,6 @@ var listItemMarkerInterruptsRe = /^[\s>]*(?:[*+-]|1\.)\s+/;
|
|||
var reversedLinkRe = /\([^)]+\)\[[^\]^][^\]]*]/;
|
||||
var spaceAfterBlockQuote = /^\s*(?:>\s+)+\S/;
|
||||
var spaceBeforeHeaderRe = /^\s+\S/;
|
||||
var spaceInsideCodeRe = /`(?:(?:\s[^`]*)|(?:[^`]*\s))`/;
|
||||
var spaceInsideEmphasisRe = /(\*\*?|__?)(?:(?:\s.+)|(?:.+\s))\1/;
|
||||
var spaceInsideLinkRe = /\[(?:(?:\s[^\]]*)|(?:[^\]]*\s))](?=\(\S*\))/;
|
||||
var tabRe = /\t+/;
|
||||
|
|
@ -982,7 +981,7 @@ module.exports = [
|
|||
"desc": "Spaces inside code span elements",
|
||||
"tags": [ "whitespace", "code" ],
|
||||
"aliases": [ "no-space-in-code" ],
|
||||
"regexp": spaceInsideCodeRe,
|
||||
"regexp": null,
|
||||
"func": function MD038(params, errors) {
|
||||
var inlineCodeSpansRe = /(?:^|[^\\])((`+)((?:.*?[^`])|)\2(?!`))/g;
|
||||
forEachInlineChild(params, "code_inline",
|
||||
|
|
@ -992,10 +991,15 @@ module.exports = [
|
|||
while ((match = inlineCodeSpansRe.exec(line)) !== null) {
|
||||
var inlineCodeSpan = match[1];
|
||||
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)) {
|
||||
errors.addContext(token.lineNumber, inlineCodeSpan);
|
||||
errors.addContext(
|
||||
token.lineNumber, inlineCodeSpan, true, false, range);
|
||||
} else if (/[^`]\s$/.test(content)) {
|
||||
errors.addContext(token.lineNumber, inlineCodeSpan, false, true);
|
||||
errors.addContext(
|
||||
token.lineNumber, inlineCodeSpan, false, true, range);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue