mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +01:00
Simplify handling of internal lineMetadata array (slightly faster now).
This commit is contained in:
parent
51a57a07c8
commit
b77dd5ccd3
1 changed files with 11 additions and 24 deletions
|
|
@ -137,28 +137,24 @@ function makeTokenCache(params) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize line metadata array
|
// Initialize line metadata array
|
||||||
// 0x7: inCode
|
const lineMetadata = params.lines.map(function mapLine(line, index) {
|
||||||
// 0x6: onFence (start)
|
return [ line, index, false, 0, false ];
|
||||||
// 0x2: onFence (end)
|
});
|
||||||
// 0x1: code body
|
|
||||||
// 0x8: inTable
|
|
||||||
const lineMetadata = new Array(params.lines.length);
|
|
||||||
lineMetadata.fill(0);
|
|
||||||
filterTokens(params, "fence", function forToken(token) {
|
filterTokens(params, "fence", function forToken(token) {
|
||||||
lineMetadata[token.map[0]] = 6;
|
lineMetadata[token.map[0]][3] = 1;
|
||||||
lineMetadata[token.map[1] - 1] = 2;
|
lineMetadata[token.map[1] - 1][3] = -1;
|
||||||
for (let i = token.map[0] + 1; i < token.map[1] - 1; i++) {
|
for (let i = token.map[0] + 1; i < token.map[1] - 1; i++) {
|
||||||
lineMetadata[i] = 1;
|
lineMetadata[i][2] = true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
filterTokens(params, "code_block", function forToken(token) {
|
filterTokens(params, "code_block", function forToken(token) {
|
||||||
for (let i = token.map[0]; i < token.map[1]; i++) {
|
for (let i = token.map[0]; i < token.map[1]; i++) {
|
||||||
lineMetadata[i] = 1;
|
lineMetadata[i][2] = true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
filterTokens(params, "table_open", function forToken(token) {
|
filterTokens(params, "table_open", function forToken(token) {
|
||||||
for (let i = token.map[0]; i < token.map[1]; i++) {
|
for (let i = token.map[0]; i < token.map[1]; i++) {
|
||||||
lineMetadata[i] += 8;
|
lineMetadata[i][4] = true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -211,18 +207,9 @@ module.exports.makeTokenCache = makeTokenCache;
|
||||||
|
|
||||||
// Calls the provided function for each line (with context)
|
// Calls the provided function for each line (with context)
|
||||||
module.exports.forEachLine = function forEachLine(callback) {
|
module.exports.forEachLine = function forEachLine(callback) {
|
||||||
// Invoke callback
|
tokenCache.lineMetadata.forEach(function forMetadata(metadata) {
|
||||||
tokenCache.params.lines.forEach(function forLine(line, lineIndex) {
|
// Parameters: line, lineIndex, inCode, onFence, inTable
|
||||||
const metadata = tokenCache.lineMetadata[lineIndex];
|
callback.apply(this, metadata);
|
||||||
const inCode = !!(metadata & 7);
|
|
||||||
const onFence = (((metadata & 6) >> 1) || 2) - 2;
|
|
||||||
const inTable = !!(metadata & 8);
|
|
||||||
callback(
|
|
||||||
line,
|
|
||||||
lineIndex,
|
|
||||||
inCode,
|
|
||||||
onFence,
|
|
||||||
inTable);
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue