Reimplement MD038/no-space-in-code to handle multi-backtick strings (fixes #46).

This commit is contained in:
David Anson 2017-02-18 22:56:06 -08:00
parent 00171da565
commit 31206f9387
3 changed files with 51 additions and 13 deletions

View file

@ -986,6 +986,11 @@ To fix this, remove the spaces inside the codespan markers:
`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
Tags: whitespace, links

View file

@ -947,20 +947,19 @@ module.exports = [
"aliases": [ "no-space-in-code" ],
"regexp": spaceInsideCodeRe,
"func": function MD038(params, errors) {
var inlineCodeSpansRe = /(`+)((?:.*?[^`])|)\1(?!`)/g;
forEachInlineChild(params, "code_inline",
function forToken(token, inline) {
var backtickPairs = "^(?:[^`]*`[^`]*`)*[^`]*";
var escapedContent = escapeForRegExp(token.content);
var left = (new RegExp(
backtickPairs + "(`\\s+" + escapedContent + "\\s*`)"))
.exec(inline.content);
var right = (new RegExp(
backtickPairs + "(`\\s*" + escapedContent + "\\s+`)"))
.exec(inline.content);
if (left) {
errors.addContext(token.lineNumber, left[1]);
} else if (right) {
errors.addContext(token.lineNumber, right[1], false, true);
function forToken(token) {
var line = params.lines[token.lineNumber - 1];
var match = null;
while ((match = inlineCodeSpansRe.exec(line)) !== null) {
var inlineCodeSpan = match[0];
var content = match[2];
if (/^\s([^`]|$)/.test(content)) {
errors.addContext(token.lineNumber, inlineCodeSpan);
} else if (/[^`]\s$/.test(content)) {
errors.addContext(token.lineNumber, inlineCodeSpan, false, true);
}
}
});
}

View file

@ -6,6 +6,10 @@
` codespan element with spaces inside ` {MD038}
empty `` codespan element
single space ` ` codespan element {MD038}
`,`, `.`
`,`, `code`
@ -21,3 +25,33 @@ text `code` text `anything` code `end`
text `anything` code `code` text `end`
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`