2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2018-04-27 22:05:34 -07:00
|
|
|
const shared = require("./shared");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2018-07-15 23:05:18 -07:00
|
|
|
const inlineCodeSpansRe = /(?:^|[^\\])((`+)((?:[\s\S]*?[^`])|)\2(?!`))/g;
|
2018-01-21 21:44:25 -08:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
"names": [ "MD038", "no-space-in-code" ],
|
|
|
|
"description": "Spaces inside code span elements",
|
|
|
|
"tags": [ "whitespace", "code" ],
|
|
|
|
"function": function MD038(params, onError) {
|
2018-07-15 23:05:18 -07:00
|
|
|
let lastParent = null;
|
2018-01-21 21:44:25 -08:00
|
|
|
shared.forEachInlineChild(params, "code_inline",
|
2018-07-15 23:05:18 -07:00
|
|
|
function forToken(token, parent) {
|
|
|
|
if (lastParent !== parent) {
|
|
|
|
lastParent = parent;
|
|
|
|
inlineCodeSpansRe.lastIndex = 0;
|
|
|
|
}
|
|
|
|
const match = inlineCodeSpansRe.exec(parent.content);
|
2019-01-06 22:07:10 -08:00
|
|
|
const content = (match || [])[3];
|
2018-07-15 23:05:18 -07:00
|
|
|
const leftError = /^\s([^`]|$)/.test(content);
|
|
|
|
const rightError = /[^`]\s$/.test(content);
|
|
|
|
if (leftError || rightError) {
|
2018-04-27 22:05:34 -07:00
|
|
|
const inlineCodeSpan = match[1];
|
2018-07-15 23:05:18 -07:00
|
|
|
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) {
|
2018-01-21 21:44:25 -08:00
|
|
|
shared.addErrorContext(onError, token.lineNumber,
|
|
|
|
inlineCodeSpan, true, false, range);
|
2018-07-15 23:05:18 -07:00
|
|
|
} else {
|
|
|
|
if (inlineCodeSpanLines.length > 1) {
|
|
|
|
range = [
|
|
|
|
1,
|
|
|
|
inlineCodeSpanLines[inlineCodeSpanLines.length - 1].length
|
|
|
|
];
|
|
|
|
}
|
|
|
|
shared.addErrorContext(onError,
|
|
|
|
token.lineNumber + content.split(shared.newLineRe).length - 1,
|
2018-01-21 21:44:25 -08:00
|
|
|
inlineCodeSpan, false, true, range);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|