Update MD037/no-space-in-emphasis to ignore content of math blocks when used with markdown-it-texmath (fixes #357).

This commit is contained in:
David Anson 2021-01-24 17:50:39 -08:00
parent d6cd840e7d
commit df4aa9f4e8
5 changed files with 89 additions and 37 deletions

View file

@ -228,44 +228,59 @@ function filterTokens(params, type, handler) {
});
}
module.exports.filterTokens = filterTokens;
/**
* Returns whether a token is a math block (created by markdown-it-texmath).
*
* @param {Object} token MarkdownItToken instance.
* @returns {boolean} True iff token is a math block.
*/
function isMathBlock(token) {
return ((token.tag === "math") &&
token.type.startsWith("math_block") &&
!token.type.endsWith("_end"));
}
// Get line metadata array
module.exports.getLineMetadata = function getLineMetadata(params) {
var lineMetadata = params.lines.map(function mapLine(line, index) {
return [line, index, false, 0, false, false];
});
filterTokens(params, "fence", function forToken(token) {
var lineMetadata = params.lines.map(function (line, index) { return [line, index, false, 0, false, false, false, false]; });
filterTokens(params, "fence", function (token) {
lineMetadata[token.map[0]][3] = 1;
lineMetadata[token.map[1] - 1][3] = -1;
for (var i = token.map[0] + 1; i < token.map[1] - 1; i++) {
lineMetadata[i][2] = true;
}
});
filterTokens(params, "code_block", function forToken(token) {
filterTokens(params, "code_block", function (token) {
for (var i = token.map[0]; i < token.map[1]; i++) {
lineMetadata[i][2] = true;
}
});
filterTokens(params, "table_open", function forToken(token) {
filterTokens(params, "table_open", function (token) {
for (var i = token.map[0]; i < token.map[1]; i++) {
lineMetadata[i][4] = true;
}
});
filterTokens(params, "list_item_open", function forToken(token) {
filterTokens(params, "list_item_open", function (token) {
var count = 1;
for (var i = token.map[0]; i < token.map[1]; i++) {
lineMetadata[i][5] = count;
count++;
}
});
filterTokens(params, "hr", function forToken(token) {
filterTokens(params, "hr", function (token) {
lineMetadata[token.map[0]][6] = true;
});
params.tokens.filter(isMathBlock).forEach(function (token) {
for (var i = token.map[0]; i < token.map[1]; i++) {
lineMetadata[i][7] = true;
}
});
return lineMetadata;
};
// Calls the provided function for each line (with context)
module.exports.forEachLine = function forEachLine(lineMetadata, handler) {
lineMetadata.forEach(function forMetadata(metadata) {
// Parameters: line, lineIndex, inCode, onFence, inTable, inItem, inBreak
// Parameters:
// line, lineIndex, inCode, onFence, inTable, inItem, inBreak, inMath
handler.apply(void 0, metadata);
});
};
@ -277,11 +292,9 @@ module.exports.flattenLists = function flattenLists(params) {
var nesting = 0;
var nestingStack = [];
var lastWithMap = { "map": [0, 1] };
params.tokens.forEach(function forToken(token) {
if ((token.type === "math_block") &&
(token.tag === "math") &&
token.map[1]) {
// markdown-it-texmath package does not account for math_block_end
params.tokens.forEach(function (token) {
if (isMathBlock(token) && token.map[1]) {
// markdown-it-texmath plugin does not account for math_block_end
token.map[1]++;
}
if ((token.type === "bullet_list_open") ||
@ -3269,13 +3282,13 @@ module.exports = {
// Initialize
var ignoreMarkersByLine = emphasisMarkersInContent(params);
resetRunTracking();
forEachLine(lineMetadata(), function (line, lineIndex, inCode, onFence, inTable, inItem, onBreak) {
forEachLine(lineMetadata(), function (line, lineIndex, inCode, onFence, inTable, inItem, onBreak, inMath) {
var onItemStart = (inItem === 1);
if (inCode || inTable || onBreak || onItemStart || isBlankLine(line)) {
// Emphasis resets when leaving a block
resetRunTracking();
}
if (inCode || onBreak) {
if (inCode || onBreak || inMath) {
// Emphasis has no meaning here
return;
}