mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 14:00:13 +01:00
Refactor to remove forEachLine and getLineMetadata helpers, reimplement MD012/MD018/MD020/MD031 using micromark tokens.
This commit is contained in:
parent
7efc2605b1
commit
c8fd9eb4b3
13 changed files with 198 additions and 308 deletions
|
|
@ -19,32 +19,6 @@ be useful to custom rule authors and may avoid duplicating code.
|
|||
|
||||
## Examples
|
||||
|
||||
### Using Helpers from a Custom Rule
|
||||
|
||||
```javascript
|
||||
const { forEachLine, getLineMetadata } = require("markdownlint-rule-helpers");
|
||||
|
||||
/** @type import("markdownlint").Rule */
|
||||
module.exports = {
|
||||
"names": [ "every-n-lines" ],
|
||||
"description": "Rule that reports an error every N lines",
|
||||
"tags": [ "test" ],
|
||||
"parser": "none",
|
||||
"function": (params, onError) => {
|
||||
const n = params.config.n || 2;
|
||||
forEachLine(getLineMetadata(params), (line, lineIndex) => {
|
||||
const lineNumber = lineIndex + 1;
|
||||
if ((lineNumber % n) === 0) {
|
||||
onError({
|
||||
"lineNumber": lineNumber,
|
||||
"detail": "Line number " + lineNumber
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
### Applying Recommended Fixes
|
||||
|
||||
```javascript
|
||||
|
|
|
|||
|
|
@ -312,85 +312,6 @@ function filterTokens(params, type, handler) {
|
|||
}
|
||||
module.exports.filterTokens = filterTokens;
|
||||
|
||||
/**
|
||||
* @typedef {Array} LineMetadata
|
||||
*/
|
||||
|
||||
/**
|
||||
* Gets a line metadata array.
|
||||
*
|
||||
* @param {Object} params RuleParams instance.
|
||||
* @returns {LineMetadata} Line metadata.
|
||||
*/
|
||||
function getLineMetadata(params) {
|
||||
const lineMetadata = params.lines.map(
|
||||
(line, index) => [ line, index, false, 0, false, false, false, false ]
|
||||
);
|
||||
filterTokens(params, "fence", (token) => {
|
||||
lineMetadata[token.map[0]][3] = 1;
|
||||
lineMetadata[token.map[1] - 1][3] = -1;
|
||||
for (let i = token.map[0] + 1; i < token.map[1] - 1; i++) {
|
||||
lineMetadata[i][2] = true;
|
||||
}
|
||||
});
|
||||
filterTokens(params, "code_block", (token) => {
|
||||
for (let i = token.map[0]; i < token.map[1]; i++) {
|
||||
lineMetadata[i][2] = true;
|
||||
}
|
||||
});
|
||||
filterTokens(params, "table_open", (token) => {
|
||||
for (let i = token.map[0]; i < token.map[1]; i++) {
|
||||
lineMetadata[i][4] = true;
|
||||
}
|
||||
});
|
||||
filterTokens(params, "list_item_open", (token) => {
|
||||
let count = 1;
|
||||
for (let i = token.map[0]; i < token.map[1]; i++) {
|
||||
lineMetadata[i][5] = count;
|
||||
count++;
|
||||
}
|
||||
});
|
||||
filterTokens(params, "hr", (token) => {
|
||||
lineMetadata[token.map[0]][6] = true;
|
||||
});
|
||||
filterTokens(params, "html_block", (token) => {
|
||||
for (let i = token.map[0]; i < token.map[1]; i++) {
|
||||
lineMetadata[i][7] = true;
|
||||
}
|
||||
});
|
||||
return lineMetadata;
|
||||
}
|
||||
module.exports.getLineMetadata = getLineMetadata;
|
||||
|
||||
/**
|
||||
* @callback EachLineCallback
|
||||
* @param {string} line Line content.
|
||||
* @param {number} lineIndex Line index (0-based).
|
||||
* @param {boolean} inCode Iff in a code block.
|
||||
* @param {number} onFence + if open, - if closed, 0 otherwise.
|
||||
* @param {boolean} inTable Iff in a table.
|
||||
* @param {boolean} inItem Iff in a list item.
|
||||
* @param {boolean} inBreak Iff in semantic break.
|
||||
* @param {boolean} inHtml Iff in HTML block.
|
||||
* @returns {void}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Calls the provided function for each line.
|
||||
*
|
||||
* @param {LineMetadata} lineMetadata Line metadata object.
|
||||
* @param {EachLineCallback} handler Function taking (line, lineIndex, inCode,
|
||||
* onFence, inTable, inItem, inBreak).
|
||||
* @returns {void}
|
||||
*/
|
||||
function forEachLine(lineMetadata, handler) {
|
||||
for (const metadata of lineMetadata) {
|
||||
// @ts-ignore
|
||||
handler(...metadata);
|
||||
}
|
||||
}
|
||||
module.exports.forEachLine = forEachLine;
|
||||
|
||||
// Returns (nested) lists as a flat array (in order)
|
||||
module.exports.flattenLists = function flattenLists(tokens) {
|
||||
const flattenedLists = [];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue