Promote applyFix and applyFixes helpers into core library.

This commit is contained in:
David Anson 2024-10-06 17:24:44 -07:00
parent e0219411c6
commit 4e30462216
13 changed files with 898 additions and 823 deletions

View file

@ -768,6 +768,44 @@ Type: `Object`
Configuration object.
### Fixing
Rules that can be fixed automatically include a `fixInfo` property which is
outlined in the [documentation for custom rules](doc/CustomRules.md#authoring).
To apply fixes consistently, the `applyFix`/`applyFixes` methods may be used:
```javascript
/**
* Applies the specified fix to a Markdown content line.
*
* @param {string} line Line of Markdown content.
* @param {RuleOnErrorFixInfo} fixInfo RuleOnErrorFixInfo instance.
* @param {string} [lineEnding] Line ending to use.
* @returns {string | null} Fixed content or null if deleted.
*/
function applyFix(line, fixInfo, lineEnding = "\n") { ... }
/**
* Applies as many of the specified fixes as possible to Markdown content.
*
* @param {string} input Lines of Markdown content.
* @param {RuleOnErrorInfo[]} errors RuleOnErrorInfo instances.
* @returns {string} Fixed content.
*/
function applyFixes(input, errors) { ... }
```
Invoking `applyFixes` with the results of a call to lint can be done like so:
```javascript
const { "sync": markdownlintSync, applyFixes } = require("markdownlint");
function fixMarkdownlintViolations(content) {
const fixResults = markdownlintSync({ strings: { content } });
return applyFixes(content, fixResults.content);
}
```
## Usage
Invoke `markdownlint` and use the `result` object's `toString` method:
@ -934,14 +972,6 @@ bad.md: 1: MD041/first-line-heading/first-line-h1 First line in a file should be
Use --force to continue.
```
### Fixing
Rules that can be fixed automatically include a `fixInfo` property which is
outlined in the [documentation for custom rules](doc/CustomRules.md#authoring).
To apply those fixes more easily, the `applyFixes` method in
[markdownlint-rule-helpers](helpers/README.md#applying-recommended-fixes) may
be used.
## Browser
`markdownlint` also works in the browser.