mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-21 21:30:47 +02:00
Refactor micromark token handling to remove optional Token.htmlFlowChildren property and make related code more efficient for a ~6% elapsed time reduction.
This commit is contained in:
parent
e282874fe3
commit
24c97a54fb
16 changed files with 274 additions and 283 deletions
|
@ -1150,9 +1150,24 @@ var flatTokensSymbol = Symbol("flat-tokens");
|
|||
* @property {number} endColumn End column (1-based).
|
||||
* @property {string} text Token text.
|
||||
* @property {Token[]} children Child tokens.
|
||||
* @property {Token[]} [htmlFlowChildren] Child tokens for htmlFlow.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Returns whether a token is an htmlFlow type containing an HTML comment.
|
||||
*
|
||||
* @param {Token} token Micromark token.
|
||||
* @returns {boolean} True iff token is htmlFlow containing a comment.
|
||||
*/
|
||||
function isHtmlFlowComment(token) {
|
||||
var text = token.text,
|
||||
type = token.type;
|
||||
if (type === "htmlFlow" && text.startsWith("<!--") && text.endsWith("-->")) {
|
||||
var comment = text.slice(4, -3);
|
||||
return !comment.startsWith(">") && !comment.startsWith("->") && !comment.endsWith("-") && !comment.includes("--");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a Markdown document and returns Micromark events.
|
||||
*
|
||||
|
@ -1205,6 +1220,7 @@ function micromarkParseWithOffset(markdown, micromarkOptions, referencesDefined,
|
|||
var history = [current];
|
||||
var reparseOptions = null;
|
||||
var lines = null;
|
||||
var skipHtmlFlowChildren = false;
|
||||
var _iterator = _createForOfIteratorHelper(events),
|
||||
_step;
|
||||
try {
|
||||
|
@ -1222,7 +1238,7 @@ function micromarkParseWithOffset(markdown, micromarkOptions, referencesDefined,
|
|||
var endColumn = end["column"],
|
||||
endLine = end["line"];
|
||||
var text = context.sliceSerialize(token);
|
||||
if (kind === "enter") {
|
||||
if (kind === "enter" && !skipHtmlFlowChildren) {
|
||||
var previous = current;
|
||||
history.push(previous);
|
||||
current = {
|
||||
|
@ -1234,7 +1250,11 @@ function micromarkParseWithOffset(markdown, micromarkOptions, referencesDefined,
|
|||
text: text,
|
||||
"children": []
|
||||
};
|
||||
if (current.type === "htmlFlow") {
|
||||
previous.children.push(current);
|
||||
flatTokens.push(current);
|
||||
// @ts-ignore
|
||||
if (current.type === "htmlFlow" && !isHtmlFlowComment(current)) {
|
||||
skipHtmlFlowChildren = true;
|
||||
if (!reparseOptions || !lines) {
|
||||
reparseOptions = _objectSpread(_objectSpread({}, micromarkOptions), {}, {
|
||||
"extensions": [{
|
||||
|
@ -1246,15 +1266,20 @@ function micromarkParseWithOffset(markdown, micromarkOptions, referencesDefined,
|
|||
lines = markdown.split(newLineRe);
|
||||
}
|
||||
var reparseMarkdown = lines.slice(current.startLine - 1, current.endLine).join("\n");
|
||||
current.htmlFlowChildren = micromarkParseWithOffset(reparseMarkdown, reparseOptions, referencesDefined, current.startLine - 1);
|
||||
var tokens = micromarkParseWithOffset(reparseMarkdown, reparseOptions, referencesDefined, current.startLine - 1);
|
||||
current.children = tokens;
|
||||
flatTokens.push.apply(flatTokens, _toConsumableArray(tokens[flatTokensSymbol]));
|
||||
}
|
||||
previous.children.push(current);
|
||||
flatTokens.push(current);
|
||||
} else if (kind === "exit") {
|
||||
Object.freeze(current.children);
|
||||
Object.freeze(current);
|
||||
// @ts-ignore
|
||||
current = history.pop();
|
||||
if (type === "htmlFlow") {
|
||||
skipHtmlFlowChildren = false;
|
||||
}
|
||||
if (!skipHtmlFlowChildren) {
|
||||
Object.freeze(current.children);
|
||||
Object.freeze(current);
|
||||
// @ts-ignore
|
||||
current = history.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1327,20 +1352,6 @@ function filterByTypes(tokens, allowed) {
|
|||
return filterByPredicate(tokens, predicate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter a list of Micromark tokens for HTML tokens.
|
||||
*
|
||||
* @param {Token[]} tokens Micromark tokens.
|
||||
* @returns {Token[]} Filtered tokens.
|
||||
*/
|
||||
function filterByHtmlTokens(tokens) {
|
||||
return filterByPredicate(tokens, function (token) {
|
||||
return token.type === "htmlText";
|
||||
}, function (token) {
|
||||
return token.htmlFlowChildren || token.children;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the heading level of a Micromark heading tokan.
|
||||
*
|
||||
|
@ -1432,7 +1443,6 @@ function tokenIfType(token, type) {
|
|||
}
|
||||
module.exports = {
|
||||
"parse": micromarkParse,
|
||||
filterByHtmlTokens: filterByHtmlTokens,
|
||||
filterByPredicate: filterByPredicate,
|
||||
filterByTypes: filterByTypes,
|
||||
getHeadingLevel: getHeadingLevel,
|
||||
|
@ -4836,7 +4846,7 @@ module.exports = {
|
|||
|
||||
// For every top-level list...
|
||||
var topLevelLists = filterByPredicate(parsers.micromark.tokens, isList, function (token) {
|
||||
return isList(token) ? [] : token.children;
|
||||
return isList(token) || token.type === "htmlFlow" ? [] : token.children;
|
||||
});
|
||||
var _iterator = _createForOfIteratorHelper(topLevelLists),
|
||||
_step;
|
||||
|
@ -4903,7 +4913,7 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
|
|||
var _require = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"),
|
||||
addError = _require.addError;
|
||||
var _require2 = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs"),
|
||||
filterByHtmlTokens = _require2.filterByHtmlTokens,
|
||||
filterByTypes = _require2.filterByTypes,
|
||||
getHtmlTagInfo = _require2.getHtmlTagInfo;
|
||||
var nextLinesRe = /[\r\n][\s\S]*$/;
|
||||
module.exports = {
|
||||
|
@ -4916,7 +4926,8 @@ module.exports = {
|
|||
allowedElements = allowedElements.map(function (element) {
|
||||
return element.toLowerCase();
|
||||
});
|
||||
var _iterator = _createForOfIteratorHelper(filterByHtmlTokens(params.parsers.micromark.tokens)),
|
||||
var tokens = params.parsers.micromark.tokens;
|
||||
var _iterator = _createForOfIteratorHelper(filterByTypes(tokens, ["htmlText"])),
|
||||
_step;
|
||||
try {
|
||||
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
||||
|
@ -5159,14 +5170,12 @@ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructur
|
|||
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
||||
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
||||
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
||||
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
||||
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
||||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
||||
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
||||
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
||||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
||||
var _require = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"),
|
||||
addError = _require.addError;
|
||||
var _require2 = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs"),
|
||||
filterByPredicate = _require2.filterByPredicate;
|
||||
module.exports = {
|
||||
"names": ["MD037", "no-space-in-emphasis"],
|
||||
"description": "Spaces inside emphasis markers",
|
||||
|
@ -5180,95 +5189,100 @@ module.exports = {
|
|||
var marker = _arr[_i];
|
||||
emphasisTokensByMarker.set(marker, []);
|
||||
}
|
||||
var pending = _toConsumableArray(parsers.micromark.tokens);
|
||||
var token = null;
|
||||
while (token = pending.shift()) {
|
||||
// Use reparsed children of htmlFlow tokens
|
||||
if (token.type === "htmlFlow") {
|
||||
pending.unshift.apply(pending, _toConsumableArray(token.htmlFlowChildren));
|
||||
continue;
|
||||
}
|
||||
pending.push.apply(pending, _toConsumableArray(token.children));
|
||||
|
||||
// Build lists of bare tokens for each emphasis marker type
|
||||
var _iterator = _createForOfIteratorHelper(emphasisTokensByMarker.values()),
|
||||
_step;
|
||||
try {
|
||||
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
||||
var emphasisTokens = _step.value;
|
||||
emphasisTokens.length = 0;
|
||||
var tokens = filterByPredicate(parsers.micromark.tokens, function (token) {
|
||||
return token.children.some(function (child) {
|
||||
return child.type === "data";
|
||||
});
|
||||
});
|
||||
var _iterator = _createForOfIteratorHelper(tokens),
|
||||
_step;
|
||||
try {
|
||||
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
||||
var token = _step.value;
|
||||
// Build lists of bare tokens for each emphasis marker type
|
||||
var _iterator2 = _createForOfIteratorHelper(emphasisTokensByMarker.values()),
|
||||
_step2;
|
||||
try {
|
||||
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
||||
var emphasisTokens = _step2.value;
|
||||
emphasisTokens.length = 0;
|
||||
}
|
||||
} catch (err) {
|
||||
_iterator2.e(err);
|
||||
} finally {
|
||||
_iterator2.f();
|
||||
}
|
||||
} catch (err) {
|
||||
_iterator.e(err);
|
||||
} finally {
|
||||
_iterator.f();
|
||||
}
|
||||
var _iterator2 = _createForOfIteratorHelper(token.children),
|
||||
_step2;
|
||||
try {
|
||||
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
||||
var child = _step2.value;
|
||||
var text = child.text,
|
||||
type = child.type;
|
||||
if (type === "data" && text.length <= 3) {
|
||||
var _emphasisTokens = emphasisTokensByMarker.get(text);
|
||||
if (_emphasisTokens) {
|
||||
_emphasisTokens.push(child);
|
||||
var _iterator3 = _createForOfIteratorHelper(token.children),
|
||||
_step3;
|
||||
try {
|
||||
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
||||
var child = _step3.value;
|
||||
var text = child.text,
|
||||
type = child.type;
|
||||
if (type === "data" && text.length <= 3) {
|
||||
var _emphasisTokens = emphasisTokensByMarker.get(text);
|
||||
if (_emphasisTokens) {
|
||||
_emphasisTokens.push(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process bare tokens for each emphasis marker type
|
||||
} catch (err) {
|
||||
_iterator3.e(err);
|
||||
} finally {
|
||||
_iterator3.f();
|
||||
}
|
||||
var _iterator4 = _createForOfIteratorHelper(emphasisTokensByMarker.entries()),
|
||||
_step4;
|
||||
try {
|
||||
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
|
||||
var entry = _step4.value;
|
||||
var _entry = _slicedToArray(entry, 2),
|
||||
_marker = _entry[0],
|
||||
_emphasisTokens2 = _entry[1];
|
||||
for (var i = 0; i + 1 < _emphasisTokens2.length; i += 2) {
|
||||
// Process start token of start/end pair
|
||||
var startToken = _emphasisTokens2[i];
|
||||
var startLine = lines[startToken.startLine - 1];
|
||||
var startSlice = startLine.slice(startToken.endColumn - 1);
|
||||
var startMatch = startSlice.match(/^\s+\S/);
|
||||
if (startMatch) {
|
||||
var _startMatch = _slicedToArray(startMatch, 1),
|
||||
startSpaceCharacter = _startMatch[0];
|
||||
var startContext = "".concat(_marker).concat(startSpaceCharacter);
|
||||
addError(onError, startToken.startLine, undefined, startContext, [startToken.startColumn, startContext.length], {
|
||||
"editColumn": startToken.endColumn,
|
||||
"deleteCount": startSpaceCharacter.length - 1
|
||||
});
|
||||
}
|
||||
|
||||
// Process bare tokens for each emphasis marker type
|
||||
} catch (err) {
|
||||
_iterator2.e(err);
|
||||
} finally {
|
||||
_iterator2.f();
|
||||
}
|
||||
var _iterator3 = _createForOfIteratorHelper(emphasisTokensByMarker.entries()),
|
||||
_step3;
|
||||
try {
|
||||
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
||||
var entry = _step3.value;
|
||||
var _entry = _slicedToArray(entry, 2),
|
||||
_marker = _entry[0],
|
||||
_emphasisTokens2 = _entry[1];
|
||||
for (var i = 0; i + 1 < _emphasisTokens2.length; i += 2) {
|
||||
// Process start token of start/end pair
|
||||
var startToken = _emphasisTokens2[i];
|
||||
var startLine = lines[startToken.startLine - 1];
|
||||
var startSlice = startLine.slice(startToken.endColumn - 1);
|
||||
var startMatch = startSlice.match(/^\s+\S/);
|
||||
if (startMatch) {
|
||||
var _startMatch = _slicedToArray(startMatch, 1),
|
||||
startSpaceCharacter = _startMatch[0];
|
||||
var startContext = "".concat(_marker).concat(startSpaceCharacter);
|
||||
addError(onError, startToken.startLine, undefined, startContext, [startToken.startColumn, startContext.length], {
|
||||
"editColumn": startToken.endColumn,
|
||||
"deleteCount": startSpaceCharacter.length - 1
|
||||
});
|
||||
}
|
||||
|
||||
// Process end token of start/end pair
|
||||
var endToken = _emphasisTokens2[i + 1];
|
||||
var endLine = lines[endToken.startLine - 1];
|
||||
var endSlice = endLine.slice(0, endToken.startColumn - 1);
|
||||
var endMatch = endSlice.match(/\S\s+$/);
|
||||
if (endMatch) {
|
||||
var _endMatch = _slicedToArray(endMatch, 1),
|
||||
endSpaceCharacter = _endMatch[0];
|
||||
var endContext = "".concat(endSpaceCharacter).concat(_marker);
|
||||
addError(onError, endToken.startLine, undefined, endContext, [endToken.endColumn - endContext.length, endContext.length], {
|
||||
"editColumn": endToken.startColumn - (endSpaceCharacter.length - 1),
|
||||
"deleteCount": endSpaceCharacter.length - 1
|
||||
});
|
||||
// Process end token of start/end pair
|
||||
var endToken = _emphasisTokens2[i + 1];
|
||||
var endLine = lines[endToken.startLine - 1];
|
||||
var endSlice = endLine.slice(0, endToken.startColumn - 1);
|
||||
var endMatch = endSlice.match(/\S\s+$/);
|
||||
if (endMatch) {
|
||||
var _endMatch = _slicedToArray(endMatch, 1),
|
||||
endSpaceCharacter = _endMatch[0];
|
||||
var endContext = "".concat(endSpaceCharacter).concat(_marker);
|
||||
addError(onError, endToken.startLine, undefined, endContext, [endToken.endColumn - endContext.length, endContext.length], {
|
||||
"editColumn": endToken.startColumn - (endSpaceCharacter.length - 1),
|
||||
"deleteCount": endSpaceCharacter.length - 1
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
_iterator4.e(err);
|
||||
} finally {
|
||||
_iterator4.f();
|
||||
}
|
||||
} catch (err) {
|
||||
_iterator3.e(err);
|
||||
} finally {
|
||||
_iterator3.f();
|
||||
}
|
||||
} catch (err) {
|
||||
_iterator.e(err);
|
||||
} finally {
|
||||
_iterator.f();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -5738,26 +5752,19 @@ module.exports = {
|
|||
var includeCodeBlocks = codeBlocks === undefined ? true : !!codeBlocks;
|
||||
var htmlElements = params.config.html_elements;
|
||||
var includeHtmlElements = htmlElements === undefined ? true : !!htmlElements;
|
||||
var scannedTypes = new Set(["data", "htmlFlowData"]);
|
||||
var scannedTypes = new Set(["data"]);
|
||||
if (includeCodeBlocks) {
|
||||
scannedTypes.add("codeFlowValue");
|
||||
scannedTypes.add("codeTextData");
|
||||
}
|
||||
if (includeHtmlElements) {
|
||||
scannedTypes.add("htmlFlowData");
|
||||
scannedTypes.add("htmlTextData");
|
||||
}
|
||||
var contentTokens = filterByPredicate(params.parsers.micromark.tokens, function (token) {
|
||||
return scannedTypes.has(token.type);
|
||||
}, function (token) {
|
||||
var children = token.children;
|
||||
var htmlFlowChildren = token.htmlFlowChildren,
|
||||
text = token.text,
|
||||
type = token.type;
|
||||
if (!includeHtmlElements && type === "htmlFlow") {
|
||||
children = text.startsWith("<!--") ?
|
||||
// Remove comment content
|
||||
[] :
|
||||
// Examine htmlText content
|
||||
htmlFlowChildren;
|
||||
}
|
||||
return children.filter(function (t) {
|
||||
return token.children.filter(function (t) {
|
||||
return !ignoredChildTypes.has(t.type);
|
||||
});
|
||||
});
|
||||
|
@ -6027,14 +6034,18 @@ var _require = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"),
|
|||
addError = _require.addError,
|
||||
emphasisOrStrongStyleFor = _require.emphasisOrStrongStyleFor;
|
||||
var _require2 = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs"),
|
||||
filterByTypes = _require2.filterByTypes,
|
||||
filterByPredicate = _require2.filterByPredicate,
|
||||
tokenIfType = _require2.tokenIfType;
|
||||
var intrawordRe = /\w/;
|
||||
var impl = function impl(params, onError, type, asterisk, underline) {
|
||||
var style = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : "consistent";
|
||||
var lines = params.lines,
|
||||
parsers = params.parsers;
|
||||
var emphasisTokens = filterByTypes(parsers.micromark.tokens, [type]);
|
||||
var emphasisTokens = filterByPredicate(parsers.micromark.tokens, function (token) {
|
||||
return token.type === type;
|
||||
}, function (token) {
|
||||
return token.type === "htmlFlow" ? [] : token.children;
|
||||
});
|
||||
var _iterator = _createForOfIteratorHelper(emphasisTokens),
|
||||
_step;
|
||||
try {
|
||||
|
@ -6114,7 +6125,6 @@ var _require = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"),
|
|||
addError = _require.addError,
|
||||
addErrorDetailIf = _require.addErrorDetailIf;
|
||||
var _require2 = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs"),
|
||||
filterByHtmlTokens = _require2.filterByHtmlTokens,
|
||||
filterByTypes = _require2.filterByTypes,
|
||||
getHtmlTagInfo = _require2.getHtmlTagInfo;
|
||||
|
||||
|
@ -6179,7 +6189,7 @@ module.exports = {
|
|||
} finally {
|
||||
_iterator.f();
|
||||
}
|
||||
var _iterator2 = _createForOfIteratorHelper(filterByHtmlTokens(tokens)),
|
||||
var _iterator2 = _createForOfIteratorHelper(filterByTypes(tokens, ["htmlText"])),
|
||||
_step2;
|
||||
try {
|
||||
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
||||
|
|
|
@ -23,9 +23,32 @@ const flatTokensSymbol = Symbol("flat-tokens");
|
|||
* @property {number} endColumn End column (1-based).
|
||||
* @property {string} text Token text.
|
||||
* @property {Token[]} children Child tokens.
|
||||
* @property {Token[]} [htmlFlowChildren] Child tokens for htmlFlow.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Returns whether a token is an htmlFlow type containing an HTML comment.
|
||||
*
|
||||
* @param {Token} token Micromark token.
|
||||
* @returns {boolean} True iff token is htmlFlow containing a comment.
|
||||
*/
|
||||
function isHtmlFlowComment(token) {
|
||||
const { text, type } = token;
|
||||
if (
|
||||
(type === "htmlFlow") &&
|
||||
text.startsWith("<!--") &&
|
||||
text.endsWith("-->")
|
||||
) {
|
||||
const comment = text.slice(4, -3);
|
||||
return (
|
||||
!comment.startsWith(">") &&
|
||||
!comment.startsWith("->") &&
|
||||
!comment.endsWith("-") &&
|
||||
!comment.includes("--")
|
||||
);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a Markdown document and returns Micromark events.
|
||||
*
|
||||
|
@ -91,13 +114,14 @@ function micromarkParseWithOffset(
|
|||
const history = [ current ];
|
||||
let reparseOptions = null;
|
||||
let lines = null;
|
||||
let skipHtmlFlowChildren = false;
|
||||
for (const event of events) {
|
||||
const [ kind, token, context ] = event;
|
||||
const { type, start, end } = token;
|
||||
const { "column": startColumn, "line": startLine } = start;
|
||||
const { "column": endColumn, "line": endLine } = end;
|
||||
const text = context.sliceSerialize(token);
|
||||
if (kind === "enter") {
|
||||
if ((kind === "enter") && !skipHtmlFlowChildren) {
|
||||
const previous = current;
|
||||
history.push(previous);
|
||||
current = {
|
||||
|
@ -109,7 +133,11 @@ function micromarkParseWithOffset(
|
|||
text,
|
||||
"children": []
|
||||
};
|
||||
if (current.type === "htmlFlow") {
|
||||
previous.children.push(current);
|
||||
flatTokens.push(current);
|
||||
// @ts-ignore
|
||||
if ((current.type === "htmlFlow") && !isHtmlFlowComment(current)) {
|
||||
skipHtmlFlowChildren = true;
|
||||
if (!reparseOptions || !lines) {
|
||||
reparseOptions = {
|
||||
...micromarkOptions,
|
||||
|
@ -126,20 +154,25 @@ function micromarkParseWithOffset(
|
|||
const reparseMarkdown = lines
|
||||
.slice(current.startLine - 1, current.endLine)
|
||||
.join("\n");
|
||||
current.htmlFlowChildren = micromarkParseWithOffset(
|
||||
const tokens = micromarkParseWithOffset(
|
||||
reparseMarkdown,
|
||||
reparseOptions,
|
||||
referencesDefined,
|
||||
current.startLine - 1
|
||||
);
|
||||
current.children = tokens;
|
||||
flatTokens.push(...tokens[flatTokensSymbol]);
|
||||
}
|
||||
previous.children.push(current);
|
||||
flatTokens.push(current);
|
||||
} else if (kind === "exit") {
|
||||
Object.freeze(current.children);
|
||||
Object.freeze(current);
|
||||
// @ts-ignore
|
||||
current = history.pop();
|
||||
if (type === "htmlFlow") {
|
||||
skipHtmlFlowChildren = false;
|
||||
}
|
||||
if (!skipHtmlFlowChildren) {
|
||||
Object.freeze(current.children);
|
||||
Object.freeze(current);
|
||||
// @ts-ignore
|
||||
current = history.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -211,20 +244,6 @@ function filterByTypes(tokens, allowed) {
|
|||
return filterByPredicate(tokens, predicate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter a list of Micromark tokens for HTML tokens.
|
||||
*
|
||||
* @param {Token[]} tokens Micromark tokens.
|
||||
* @returns {Token[]} Filtered tokens.
|
||||
*/
|
||||
function filterByHtmlTokens(tokens) {
|
||||
return filterByPredicate(
|
||||
tokens,
|
||||
(token) => token.type === "htmlText",
|
||||
(token) => token.htmlFlowChildren || token.children
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the heading level of a Micromark heading tokan.
|
||||
*
|
||||
|
@ -318,7 +337,6 @@ function tokenIfType(token, type) {
|
|||
|
||||
module.exports = {
|
||||
"parse": micromarkParse,
|
||||
filterByHtmlTokens,
|
||||
filterByPredicate,
|
||||
filterByTypes,
|
||||
getHeadingLevel,
|
||||
|
|
|
@ -46,7 +46,9 @@ module.exports = {
|
|||
const topLevelLists = filterByPredicate(
|
||||
parsers.micromark.tokens,
|
||||
isList,
|
||||
(token) => (isList(token) ? [] : token.children)
|
||||
(token) => (
|
||||
(isList(token) || (token.type === "htmlFlow")) ? [] : token.children
|
||||
)
|
||||
);
|
||||
for (const list of topLevelLists) {
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
"use strict";
|
||||
|
||||
const { addError } = require("../helpers");
|
||||
const { filterByHtmlTokens, getHtmlTagInfo } =
|
||||
const { filterByTypes, getHtmlTagInfo } =
|
||||
require("../helpers/micromark.cjs");
|
||||
|
||||
const nextLinesRe = /[\r\n][\s\S]*$/;
|
||||
|
@ -16,7 +16,8 @@ module.exports = {
|
|||
let allowedElements = params.config.allowed_elements;
|
||||
allowedElements = Array.isArray(allowedElements) ? allowedElements : [];
|
||||
allowedElements = allowedElements.map((element) => element.toLowerCase());
|
||||
for (const token of filterByHtmlTokens(params.parsers.micromark.tokens)) {
|
||||
const { tokens } = params.parsers.micromark;
|
||||
for (const token of filterByTypes(tokens, [ "htmlText" ])) {
|
||||
const htmlTagInfo = getHtmlTagInfo(token);
|
||||
if (
|
||||
htmlTagInfo &&
|
||||
|
|
16
lib/md037.js
16
lib/md037.js
|
@ -3,6 +3,7 @@
|
|||
"use strict";
|
||||
|
||||
const { addError } = require("../helpers");
|
||||
const { filterByPredicate } = require("../helpers/micromark.cjs");
|
||||
|
||||
module.exports = {
|
||||
"names": [ "MD037", "no-space-in-emphasis" ],
|
||||
|
@ -16,16 +17,11 @@ module.exports = {
|
|||
for (const marker of [ "_", "__", "___", "*", "**", "***" ]) {
|
||||
emphasisTokensByMarker.set(marker, []);
|
||||
}
|
||||
const pending = [ ...parsers.micromark.tokens ];
|
||||
let token = null;
|
||||
while ((token = pending.shift())) {
|
||||
|
||||
// Use reparsed children of htmlFlow tokens
|
||||
if (token.type === "htmlFlow") {
|
||||
pending.unshift(...token.htmlFlowChildren);
|
||||
continue;
|
||||
}
|
||||
pending.push(...token.children);
|
||||
const tokens = filterByPredicate(
|
||||
parsers.micromark.tokens,
|
||||
(token) => token.children.some((child) => child.type === "data")
|
||||
);
|
||||
for (const token of tokens) {
|
||||
|
||||
// Build lists of bare tokens for each emphasis marker type
|
||||
for (const emphasisTokens of emphasisTokensByMarker.values()) {
|
||||
|
|
21
lib/md044.js
21
lib/md044.js
|
@ -29,27 +29,22 @@ module.exports = {
|
|||
const htmlElements = params.config.html_elements;
|
||||
const includeHtmlElements =
|
||||
(htmlElements === undefined) ? true : !!htmlElements;
|
||||
const scannedTypes = new Set([ "data", "htmlFlowData" ]);
|
||||
const scannedTypes = new Set([ "data" ]);
|
||||
if (includeCodeBlocks) {
|
||||
scannedTypes.add("codeFlowValue");
|
||||
scannedTypes.add("codeTextData");
|
||||
}
|
||||
if (includeHtmlElements) {
|
||||
scannedTypes.add("htmlFlowData");
|
||||
scannedTypes.add("htmlTextData");
|
||||
}
|
||||
const contentTokens =
|
||||
filterByPredicate(
|
||||
params.parsers.micromark.tokens,
|
||||
(token) => scannedTypes.has(token.type),
|
||||
(token) => {
|
||||
let { children } = token;
|
||||
const { htmlFlowChildren, text, type } = token;
|
||||
if (!includeHtmlElements && (type === "htmlFlow")) {
|
||||
children = text.startsWith("<!--") ?
|
||||
// Remove comment content
|
||||
[] :
|
||||
// Examine htmlText content
|
||||
htmlFlowChildren;
|
||||
}
|
||||
return children.filter((t) => !ignoredChildTypes.has(t.type));
|
||||
}
|
||||
(token) => (
|
||||
token.children.filter((t) => !ignoredChildTypes.has(t.type))
|
||||
)
|
||||
);
|
||||
const exclusions = [];
|
||||
const autoLinked = new Set();
|
||||
|
|
|
@ -3,15 +3,18 @@
|
|||
"use strict";
|
||||
|
||||
const { addError, emphasisOrStrongStyleFor } = require("../helpers");
|
||||
const { filterByTypes, tokenIfType } = require("../helpers/micromark.cjs");
|
||||
const { filterByPredicate, tokenIfType } = require("../helpers/micromark.cjs");
|
||||
|
||||
const intrawordRe = /\w/;
|
||||
|
||||
const impl =
|
||||
(params, onError, type, asterisk, underline, style = "consistent") => {
|
||||
const { lines, parsers } = params;
|
||||
const emphasisTokens =
|
||||
filterByTypes(parsers.micromark.tokens, [ type ]);
|
||||
const emphasisTokens = filterByPredicate(
|
||||
parsers.micromark.tokens,
|
||||
(token) => token.type === type,
|
||||
(token) => ((token.type === "htmlFlow") ? [] : token.children)
|
||||
);
|
||||
for (const token of emphasisTokens) {
|
||||
const { children } = token;
|
||||
const childType = `${type}Sequence`;
|
||||
|
|
|
@ -3,8 +3,7 @@
|
|||
"use strict";
|
||||
|
||||
const { addError, addErrorDetailIf } = require("../helpers");
|
||||
const { filterByHtmlTokens, filterByTypes, getHtmlTagInfo } =
|
||||
require("../helpers/micromark.cjs");
|
||||
const { filterByTypes, getHtmlTagInfo } = require("../helpers/micromark.cjs");
|
||||
|
||||
// Regular expression for identifying HTML anchor names
|
||||
const idRe = /\sid\s*=\s*['"]?([^'"\s>]+)/iu;
|
||||
|
@ -68,7 +67,7 @@ module.exports = {
|
|||
}
|
||||
|
||||
// Process HTML anchors
|
||||
for (const token of filterByHtmlTokens(tokens)) {
|
||||
for (const token of filterByTypes(tokens, [ "htmlText" ])) {
|
||||
const htmlTagInfo = getHtmlTagInfo(token);
|
||||
if (htmlTagInfo && !htmlTagInfo.close) {
|
||||
const anchorMatch = idRe.exec(token.text) ||
|
||||
|
|
|
@ -19,3 +19,11 @@ also` __bad__ {MD050}
|
|||
This `is
|
||||
also
|
||||
very` __bad__ {MD050}
|
||||
|
||||
<p>HTML __should__ *be* ignored</p>
|
||||
|
||||
<p>
|
||||
HTML __should__ *be* ignored
|
||||
</p>
|
||||
|
||||
<!-- markdownlint-configure-file { "no-inline-html": false } -->
|
||||
|
|
|
@ -74,7 +74,22 @@ code
|
|||
|
||||
text
|
||||
|
||||
<p>
|
||||
1. Not a
|
||||
2. list
|
||||
</p>
|
||||
|
||||
<p>1. Not a list</p>
|
||||
|
||||
<p>
|
||||
* Not a
|
||||
* list
|
||||
</p>
|
||||
|
||||
<p>* Not a list</p>
|
||||
|
||||
<!-- markdownlint-configure-file {
|
||||
"no-inline-html": false,
|
||||
"ul-style": false,
|
||||
"ol-prefix": false,
|
||||
"fenced-code-language": false
|
||||
|
|
|
@ -26,17 +26,28 @@ test("getMicromarkEvents/filterByPredicate", async(t) => {
|
|||
t.plan(1);
|
||||
const content = await testContent;
|
||||
const events = getMicromarkEvents(content);
|
||||
let inHtmlFlow = false;
|
||||
const eventTypes = events
|
||||
.filter((event) => event[0] === "enter")
|
||||
.filter((event) => {
|
||||
const result = !inHtmlFlow && (event[0] === "enter");
|
||||
if (event[1].type === "htmlFlow") {
|
||||
inHtmlFlow = !inHtmlFlow;
|
||||
}
|
||||
return result;
|
||||
})
|
||||
.map((event) => event[1].type);
|
||||
const tokens = parse(content);
|
||||
const filtered = filterByPredicate(tokens, () => true);
|
||||
const filtered = filterByPredicate(
|
||||
tokens,
|
||||
() => true,
|
||||
(token) => ((token.type === "htmlFlow") ? [] : token.children)
|
||||
);
|
||||
const tokenTypes = filtered.map((token) => token.type);
|
||||
t.deepEqual(tokenTypes, eventTypes);
|
||||
});
|
||||
|
||||
test("filterByTypes", async(t) => {
|
||||
t.plan(6);
|
||||
t.plan(8);
|
||||
const filtered = filterByTypes(
|
||||
await testTokens,
|
||||
[ "atxHeadingText", "codeText", "htmlText", "setextHeadingText" ]
|
||||
|
|
|
@ -912,7 +912,7 @@ test("readme", async(t) => {
|
|||
});
|
||||
|
||||
test("validateJsonUsingConfigSchemaStrict", (t) => {
|
||||
t.plan(159);
|
||||
t.plan(160);
|
||||
const configRe =
|
||||
/^[\s\S]*<!-- markdownlint-configure-file ([\s\S]*) -->[\s\S]*$/;
|
||||
const ignoreFiles = new Set([
|
||||
|
|
|
@ -2574,57 +2574,6 @@ Generated by [AVA](https://avajs.dev).
|
|||
},
|
||||
{
|
||||
children: [
|
||||
{
|
||||
children: [],
|
||||
endColumn: 4,
|
||||
endLine: 43,
|
||||
startColumn: 1,
|
||||
startLine: 43,
|
||||
text: '<p>',
|
||||
type: 'htmlFlowData',
|
||||
},
|
||||
{
|
||||
children: [],
|
||||
endColumn: 1,
|
||||
endLine: 44,
|
||||
startColumn: 4,
|
||||
startLine: 43,
|
||||
text: `␊
|
||||
`,
|
||||
type: 'lineEnding',
|
||||
},
|
||||
{
|
||||
children: [],
|
||||
endColumn: 11,
|
||||
endLine: 44,
|
||||
startColumn: 1,
|
||||
startLine: 44,
|
||||
text: 'HTML block',
|
||||
type: 'htmlFlowData',
|
||||
},
|
||||
{
|
||||
children: [],
|
||||
endColumn: 1,
|
||||
endLine: 45,
|
||||
startColumn: 11,
|
||||
startLine: 44,
|
||||
text: `␊
|
||||
`,
|
||||
type: 'lineEnding',
|
||||
},
|
||||
{
|
||||
children: [],
|
||||
endColumn: 5,
|
||||
endLine: 45,
|
||||
startColumn: 1,
|
||||
startLine: 45,
|
||||
text: '</p>',
|
||||
type: 'htmlFlowData',
|
||||
},
|
||||
],
|
||||
endColumn: 5,
|
||||
endLine: 45,
|
||||
htmlFlowChildren: [
|
||||
{
|
||||
children: [
|
||||
{
|
||||
|
@ -2717,6 +2666,8 @@ Generated by [AVA](https://avajs.dev).
|
|||
type: 'content',
|
||||
},
|
||||
],
|
||||
endColumn: 5,
|
||||
endLine: 45,
|
||||
startColumn: 1,
|
||||
startLine: 43,
|
||||
text: `<p>␊
|
||||
|
@ -3224,47 +3175,6 @@ Generated by [AVA](https://avajs.dev).
|
|||
],
|
||||
endColumn: 35,
|
||||
endLine: 51,
|
||||
htmlFlowChildren: [
|
||||
{
|
||||
children: [
|
||||
{
|
||||
children: [
|
||||
{
|
||||
children: [
|
||||
{
|
||||
children: [],
|
||||
endColumn: 35,
|
||||
endLine: 51,
|
||||
startColumn: 1,
|
||||
startLine: 51,
|
||||
text: '<!-- markdownlint-disable-file -->',
|
||||
type: 'htmlTextData',
|
||||
},
|
||||
],
|
||||
endColumn: 35,
|
||||
endLine: 51,
|
||||
startColumn: 1,
|
||||
startLine: 51,
|
||||
text: '<!-- markdownlint-disable-file -->',
|
||||
type: 'htmlText',
|
||||
},
|
||||
],
|
||||
endColumn: 35,
|
||||
endLine: 51,
|
||||
startColumn: 1,
|
||||
startLine: 51,
|
||||
text: '<!-- markdownlint-disable-file -->',
|
||||
type: 'paragraph',
|
||||
},
|
||||
],
|
||||
endColumn: 35,
|
||||
endLine: 51,
|
||||
startColumn: 1,
|
||||
startLine: 51,
|
||||
text: '<!-- markdownlint-disable-file -->',
|
||||
type: 'content',
|
||||
},
|
||||
],
|
||||
startColumn: 1,
|
||||
startLine: 51,
|
||||
text: '<!-- markdownlint-disable-file -->',
|
||||
|
|
Binary file not shown.
|
@ -10917,6 +10917,14 @@ Generated by [AVA](https://avajs.dev).
|
|||
This \`is␊
|
||||
also␊
|
||||
very\` **bad** {MD050}␊
|
||||
␊
|
||||
<p>HTML __should__ *be* ignored</p>␊
|
||||
␊
|
||||
<p>␊
|
||||
HTML __should__ *be* ignored␊
|
||||
</p>␊
|
||||
␊
|
||||
<!-- markdownlint-configure-file { "no-inline-html": false } -->␊
|
||||
`,
|
||||
}
|
||||
|
||||
|
@ -30582,7 +30590,7 @@ Generated by [AVA](https://avajs.dev).
|
|||
insertText: `␊
|
||||
`,
|
||||
},
|
||||
lineNumber: 83,
|
||||
lineNumber: 98,
|
||||
ruleDescription: 'Files should end with a single newline character',
|
||||
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md047.md',
|
||||
ruleNames: [
|
||||
|
@ -30679,7 +30687,22 @@ Generated by [AVA](https://avajs.dev).
|
|||
␊
|
||||
text␊
|
||||
␊
|
||||
<p>␊
|
||||
1. Not a␊
|
||||
2. list␊
|
||||
</p>␊
|
||||
␊
|
||||
<p>1. Not a list</p>␊
|
||||
␊
|
||||
<p>␊
|
||||
* Not a␊
|
||||
* list␊
|
||||
</p>␊
|
||||
␊
|
||||
<p>* Not a list</p>␊
|
||||
␊
|
||||
<!-- markdownlint-configure-file {␊
|
||||
"no-inline-html": false,␊
|
||||
"ul-style": false,␊
|
||||
"ol-prefix": false,␊
|
||||
"fenced-code-language": false␊
|
||||
|
|
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue