mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 22:10:13 +01:00
Handle multi-line inline code spans better (fixes #130).
This commit is contained in:
parent
6afd61ed66
commit
4865301ce9
5 changed files with 115 additions and 13 deletions
42
lib/md038.js
42
lib/md038.js
|
|
@ -4,28 +4,46 @@
|
||||||
|
|
||||||
const shared = require("./shared");
|
const shared = require("./shared");
|
||||||
|
|
||||||
const inlineCodeSpansRe = /(?:^|[^\\])((`+)((?:.*?[^`])|)\2(?!`))/g;
|
const inlineCodeSpansRe = /(?:^|[^\\])((`+)((?:[\s\S]*?[^`])|)\2(?!`))/g;
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
"names": [ "MD038", "no-space-in-code" ],
|
"names": [ "MD038", "no-space-in-code" ],
|
||||||
"description": "Spaces inside code span elements",
|
"description": "Spaces inside code span elements",
|
||||||
"tags": [ "whitespace", "code" ],
|
"tags": [ "whitespace", "code" ],
|
||||||
"function": function MD038(params, onError) {
|
"function": function MD038(params, onError) {
|
||||||
|
let lastParent = null;
|
||||||
shared.forEachInlineChild(params, "code_inline",
|
shared.forEachInlineChild(params, "code_inline",
|
||||||
function forToken(token) {
|
function forToken(token, parent) {
|
||||||
const line = params.lines[token.lineNumber - 1];
|
if (lastParent !== parent) {
|
||||||
let match = null;
|
lastParent = parent;
|
||||||
while ((match = inlineCodeSpansRe.exec(line)) !== null) {
|
inlineCodeSpansRe.lastIndex = 0;
|
||||||
|
}
|
||||||
|
const match = inlineCodeSpansRe.exec(parent.content);
|
||||||
|
const content = match[3];
|
||||||
|
const leftError = /^\s([^`]|$)/.test(content);
|
||||||
|
const rightError = /[^`]\s$/.test(content);
|
||||||
|
if (leftError || rightError) {
|
||||||
const inlineCodeSpan = match[1];
|
const inlineCodeSpan = match[1];
|
||||||
const content = match[3];
|
const leftContent = parent.content.substr(0,
|
||||||
const length = inlineCodeSpan.length;
|
match.index + (match[0].length - inlineCodeSpan.length));
|
||||||
const column = match.index + 1 + (match[0].length - length);
|
const leftContentLines = leftContent.split(shared.newLineRe);
|
||||||
const range = [ column, length ];
|
const inlineCodeSpanLines = inlineCodeSpan.split(shared.newLineRe);
|
||||||
if (/^\s([^`]|$)/.test(content)) {
|
let range = [
|
||||||
|
leftContentLines[leftContentLines.length - 1].length + 1,
|
||||||
|
inlineCodeSpanLines[0].length
|
||||||
|
];
|
||||||
|
if (leftError) {
|
||||||
shared.addErrorContext(onError, token.lineNumber,
|
shared.addErrorContext(onError, token.lineNumber,
|
||||||
inlineCodeSpan, true, false, range);
|
inlineCodeSpan, true, false, range);
|
||||||
} else if (/[^`]\s$/.test(content)) {
|
} else {
|
||||||
shared.addErrorContext(onError, token.lineNumber,
|
if (inlineCodeSpanLines.length > 1) {
|
||||||
|
range = [
|
||||||
|
1,
|
||||||
|
inlineCodeSpanLines[inlineCodeSpanLines.length - 1].length
|
||||||
|
];
|
||||||
|
}
|
||||||
|
shared.addErrorContext(onError,
|
||||||
|
token.lineNumber + content.split(shared.newLineRe).length - 1,
|
||||||
inlineCodeSpan, false, true, range);
|
inlineCodeSpan, false, true, range);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -235,7 +235,7 @@ function forEachInlineChild(params, type, callback) {
|
||||||
filterTokens(params, "inline", function forToken(token) {
|
filterTokens(params, "inline", function forToken(token) {
|
||||||
token.children.forEach(function forChild(child) {
|
token.children.forEach(function forChild(child) {
|
||||||
if (child.type === type) {
|
if (child.type === type) {
|
||||||
callback(child);
|
callback(child, token);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -33,3 +33,13 @@ some *space* in *some * emphasis
|
||||||
some *space* in **some ** emphasis
|
some *space* in **some ** emphasis
|
||||||
some _space_ in _ some_ emphasis
|
some _space_ in _ some_ emphasis
|
||||||
some __space__ in __ some __ emphasis
|
some __space__ in __ some __ emphasis
|
||||||
|
|
||||||
|
Text
|
||||||
|
text ` code
|
||||||
|
span` text
|
||||||
|
text.
|
||||||
|
|
||||||
|
Text
|
||||||
|
text `code
|
||||||
|
span ` text
|
||||||
|
text.
|
||||||
|
|
|
||||||
|
|
@ -151,6 +151,22 @@
|
||||||
"errorContext": "``embedded backtick` ``",
|
"errorContext": "``embedded backtick` ``",
|
||||||
"errorRange": [ 1, 24 ]
|
"errorRange": [ 1, 24 ]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"lineNumber": 38,
|
||||||
|
"ruleNames": [ "MD038", "no-space-in-code" ],
|
||||||
|
"ruleDescription": "Spaces inside code span elements",
|
||||||
|
"errorDetail": null,
|
||||||
|
"errorContext": "` code\nspan`",
|
||||||
|
"errorRange": [ 6, 6 ]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"lineNumber": 44,
|
||||||
|
"ruleNames": [ "MD038", "no-space-in-code" ],
|
||||||
|
"ruleDescription": "Spaces inside code span elements",
|
||||||
|
"errorDetail": null,
|
||||||
|
"errorContext": "`code\nspan `",
|
||||||
|
"errorRange": [ 1, 7 ]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"lineNumber": 19,
|
"lineNumber": 19,
|
||||||
"ruleNames": [ "MD039", "no-space-in-links" ],
|
"ruleNames": [ "MD039", "no-space-in-links" ],
|
||||||
|
|
|
||||||
58
test/line-breaks-inside-code-spans.md
Normal file
58
test/line-breaks-inside-code-spans.md
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
# Line breaks inside code spans
|
||||||
|
|
||||||
|
Text `code
|
||||||
|
span` text.
|
||||||
|
|
||||||
|
Text `code
|
||||||
|
span` text `code
|
||||||
|
span` text.
|
||||||
|
|
||||||
|
Text `code
|
||||||
|
span` text `code
|
||||||
|
span` text `code
|
||||||
|
span` text.
|
||||||
|
|
||||||
|
Text `code
|
||||||
|
span
|
||||||
|
code
|
||||||
|
span` text.
|
||||||
|
|
||||||
|
Text `code
|
||||||
|
span` text `code span`
|
||||||
|
text `code span` text.
|
||||||
|
|
||||||
|
Text `code
|
||||||
|
span` text `code span` text
|
||||||
|
`code span` text.
|
||||||
|
|
||||||
|
`code
|
||||||
|
span` `span`
|
||||||
|
`span`
|
||||||
|
|
||||||
|
Text
|
||||||
|
text ` code {MD038}
|
||||||
|
span` text
|
||||||
|
text.
|
||||||
|
|
||||||
|
Text
|
||||||
|
text `code
|
||||||
|
span ` text {MD038}
|
||||||
|
text.
|
||||||
|
|
||||||
|
Text
|
||||||
|
text `code
|
||||||
|
span code
|
||||||
|
span` text
|
||||||
|
text.
|
||||||
|
|
||||||
|
Text
|
||||||
|
text ` code {MD038}
|
||||||
|
span code
|
||||||
|
span` text
|
||||||
|
text.
|
||||||
|
|
||||||
|
Text
|
||||||
|
text `code
|
||||||
|
span code
|
||||||
|
span ` text {MD038}
|
||||||
|
text.
|
||||||
Loading…
Add table
Add a link
Reference in a new issue