mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +01:00
Reimplement MD038/no-space-in-code to handle multi-backtick strings (fixes #46).
This commit is contained in:
parent
00171da565
commit
31206f9387
3 changed files with 51 additions and 13 deletions
|
|
@ -986,6 +986,11 @@ To fix this, remove the spaces inside the codespan markers:
|
||||||
|
|
||||||
`some text`
|
`some text`
|
||||||
|
|
||||||
|
Note: A single leading or trailing space is allowed if used to separate codespan
|
||||||
|
markers from an embedded backtick:
|
||||||
|
|
||||||
|
`` ` embedded backtick``
|
||||||
|
|
||||||
## MD039 - Spaces inside link text
|
## MD039 - Spaces inside link text
|
||||||
|
|
||||||
Tags: whitespace, links
|
Tags: whitespace, links
|
||||||
|
|
|
||||||
25
lib/rules.js
25
lib/rules.js
|
|
@ -947,20 +947,19 @@ module.exports = [
|
||||||
"aliases": [ "no-space-in-code" ],
|
"aliases": [ "no-space-in-code" ],
|
||||||
"regexp": spaceInsideCodeRe,
|
"regexp": spaceInsideCodeRe,
|
||||||
"func": function MD038(params, errors) {
|
"func": function MD038(params, errors) {
|
||||||
|
var inlineCodeSpansRe = /(`+)((?:.*?[^`])|)\1(?!`)/g;
|
||||||
forEachInlineChild(params, "code_inline",
|
forEachInlineChild(params, "code_inline",
|
||||||
function forToken(token, inline) {
|
function forToken(token) {
|
||||||
var backtickPairs = "^(?:[^`]*`[^`]*`)*[^`]*";
|
var line = params.lines[token.lineNumber - 1];
|
||||||
var escapedContent = escapeForRegExp(token.content);
|
var match = null;
|
||||||
var left = (new RegExp(
|
while ((match = inlineCodeSpansRe.exec(line)) !== null) {
|
||||||
backtickPairs + "(`\\s+" + escapedContent + "\\s*`)"))
|
var inlineCodeSpan = match[0];
|
||||||
.exec(inline.content);
|
var content = match[2];
|
||||||
var right = (new RegExp(
|
if (/^\s([^`]|$)/.test(content)) {
|
||||||
backtickPairs + "(`\\s*" + escapedContent + "\\s+`)"))
|
errors.addContext(token.lineNumber, inlineCodeSpan);
|
||||||
.exec(inline.content);
|
} else if (/[^`]\s$/.test(content)) {
|
||||||
if (left) {
|
errors.addContext(token.lineNumber, inlineCodeSpan, false, true);
|
||||||
errors.addContext(token.lineNumber, left[1]);
|
}
|
||||||
} else if (right) {
|
|
||||||
errors.addContext(token.lineNumber, right[1], false, true);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,10 @@
|
||||||
|
|
||||||
` codespan element with spaces inside ` {MD038}
|
` codespan element with spaces inside ` {MD038}
|
||||||
|
|
||||||
|
empty `` codespan element
|
||||||
|
|
||||||
|
single space ` ` codespan element {MD038}
|
||||||
|
|
||||||
`,`, `.`
|
`,`, `.`
|
||||||
|
|
||||||
`,`, `code`
|
`,`, `code`
|
||||||
|
|
@ -21,3 +25,33 @@ text `code` text `anything` code `end`
|
||||||
text `anything` code `code` text `end`
|
text `anything` code `code` text `end`
|
||||||
|
|
||||||
text `anything` text `anything` code `anything` `code`
|
text `anything` text `anything` code `anything` `code`
|
||||||
|
|
||||||
|
text ``code`` text ``code`` text
|
||||||
|
|
||||||
|
text `` code`` text {MD038}
|
||||||
|
|
||||||
|
text ``code `` text {MD038}
|
||||||
|
|
||||||
|
text ```code``` text ```code``` text
|
||||||
|
|
||||||
|
text ```code``` text `` code`` text {MD038}
|
||||||
|
|
||||||
|
text ```code``` text ``code `` text {MD038}
|
||||||
|
|
||||||
|
``embedded ` backtick`` text `code`
|
||||||
|
|
||||||
|
`backslash does not escape \` backtick`
|
||||||
|
|
||||||
|
`` ` `` text `code`
|
||||||
|
|
||||||
|
` `` ` text `code`
|
||||||
|
|
||||||
|
``` ` leading space allowed for backtick``` text `code`
|
||||||
|
|
||||||
|
``` ` multiple leading spaces not allowed``` text `code` {MD038}
|
||||||
|
|
||||||
|
``trailing space allowed for backtick ` `` text `code`
|
||||||
|
|
||||||
|
``multiple trailing spaces not allowed ` `` text `code` {MD038}
|
||||||
|
|
||||||
|
`` ` leading and trailing space allowed for backtick ` `` text `code`
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue