Handle multi-line inline code spans better (fixes #130).

This commit is contained in:
David Anson 2018-07-15 23:05:18 -07:00
parent 6afd61ed66
commit 4865301ce9
5 changed files with 115 additions and 13 deletions

View file

@ -4,28 +4,46 @@
const shared = require("./shared");
const inlineCodeSpansRe = /(?:^|[^\\])((`+)((?:.*?[^`])|)\2(?!`))/g;
const inlineCodeSpansRe = /(?:^|[^\\])((`+)((?:[\s\S]*?[^`])|)\2(?!`))/g;
module.exports = {
"names": [ "MD038", "no-space-in-code" ],
"description": "Spaces inside code span elements",
"tags": [ "whitespace", "code" ],
"function": function MD038(params, onError) {
let lastParent = null;
shared.forEachInlineChild(params, "code_inline",
function forToken(token) {
const line = params.lines[token.lineNumber - 1];
let match = null;
while ((match = inlineCodeSpansRe.exec(line)) !== null) {
function forToken(token, parent) {
if (lastParent !== parent) {
lastParent = parent;
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 content = match[3];
const length = inlineCodeSpan.length;
const column = match.index + 1 + (match[0].length - length);
const range = [ column, length ];
if (/^\s([^`]|$)/.test(content)) {
const leftContent = parent.content.substr(0,
match.index + (match[0].length - inlineCodeSpan.length));
const leftContentLines = leftContent.split(shared.newLineRe);
const inlineCodeSpanLines = inlineCodeSpan.split(shared.newLineRe);
let range = [
leftContentLines[leftContentLines.length - 1].length + 1,
inlineCodeSpanLines[0].length
];
if (leftError) {
shared.addErrorContext(onError, token.lineNumber,
inlineCodeSpan, true, false, range);
} else if (/[^`]\s$/.test(content)) {
shared.addErrorContext(onError, token.lineNumber,
} else {
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);
}
}