wip
Some checks are pending
Checkers / linkcheck (push) Waiting to run
Checkers / spellcheck (push) Waiting to run
CI / build (20, macos-latest) (push) Waiting to run
CI / build (20, ubuntu-latest) (push) Waiting to run
CI / build (20, windows-latest) (push) Waiting to run
CI / build (22, macos-latest) (push) Waiting to run
CI / build (22, ubuntu-latest) (push) Waiting to run
CI / build (22, windows-latest) (push) Waiting to run
CI / build (24, macos-latest) (push) Waiting to run
CI / build (24, ubuntu-latest) (push) Waiting to run
CI / build (24, windows-latest) (push) Waiting to run
CI / pnpm (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
TestRepos / build (latest, ubuntu-latest) (push) Waiting to run
UpdateTestRepos / update (push) Waiting to run

This commit is contained in:
David Anson 2025-09-05 17:51:13 -07:00
parent cd4d9f2722
commit 93a42ae891
7 changed files with 74 additions and 86 deletions

View file

@ -659,9 +659,9 @@ Standard completion callback.
Type: `Object` Type: `Object`
Call `result.toString()` for convenience or see below for an example of the Map of input file/string to errors within.
structure of the `result` object. Passing the value `true` to `toString()`
uses rule aliases (ex: `no-hard-tabs`) instead of names (ex: `MD010`). See [the Usage section](#usage) for an example of the structure of this object.
### Config ### Config
@ -837,7 +837,7 @@ console.log(getVersion());
## Usage ## Usage
Invoke `lint` and use the `result` object's `toString` method: Invoke `lint` as an asynchronous call:
```javascript ```javascript
import { lint as lintAsync } from "markdownlint/async"; import { lint as lintAsync } from "markdownlint/async";
@ -852,34 +852,21 @@ const options = {
lintAsync(options, function callback(error, results) { lintAsync(options, function callback(error, results) {
if (!error && results) { if (!error && results) {
console.log(results.toString()); console.dir(results, { "colors": true, "depth": null });
} }
}); });
``` ```
Output:
```text
bad.string: 3: MD010/no-hard-tabs Hard tabs [Column: 19]
bad.string: 1: MD018/no-missing-space-atx No space after hash on atx style heading [Context: "#bad.string"]
bad.string: 3: MD018/no-missing-space-atx No space after hash on atx style heading [Context: "#This string fails some rules."]
bad.string: 1: MD041/first-line-heading/first-line-h1 First line in a file should be a top-level heading [Context: "#bad.string"]
bad.md: 3: MD010/no-hard-tabs Hard tabs [Column: 17]
bad.md: 1: MD018/no-missing-space-atx No space after hash on atx style heading [Context: "#bad.md"]
bad.md: 3: MD018/no-missing-space-atx No space after hash on atx style heading [Context: "#This file fails some rules."]
bad.md: 1: MD041/first-line-heading/first-line-h1 First line in a file should be a top-level heading [Context: "#bad.md"]
```
Or as a synchronous call: Or as a synchronous call:
```javascript ```javascript
import { lint as lintSync } from "markdownlint/sync"; import { lint as lintSync } from "markdownlint/sync";
const results = lintSync(options); const results = lintSync(options);
console.log(results.toString()); console.dir(results, { "colors": true, "depth": null });
``` ```
To examine the `result` object directly via a `Promise`-based call: Or as a `Promise`-based call:
```javascript ```javascript
import { lint as lintPromise } from "markdownlint/promise"; import { lint as lintPromise } from "markdownlint/promise";
@ -888,7 +875,7 @@ const results = await lintPromise(options);
console.dir(results, { "colors": true, "depth": null }); console.dir(results, { "colors": true, "depth": null });
``` ```
Output: All of which output something like:
```json ```json
{ {
@ -900,28 +887,32 @@ Output:
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md010.md", "ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md010.md",
"errorDetail": "Column: 17", "errorDetail": "Column: 17",
"errorContext": null, "errorContext": null,
"errorRange": [ 17, 1 ] }, "errorRange": [ 17, 1 ],
"fixInfo": { "editColumn": 17, "deleteCount": 1, "insertText": ' ' } }
{ "lineNumber": 1, { "lineNumber": 1,
"ruleNames": [ "MD018", "no-missing-space-atx" ], "ruleNames": [ "MD018", "no-missing-space-atx" ],
"ruleDescription": "No space after hash on atx style heading", "ruleDescription": "No space after hash on atx style heading",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md018.md", "ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md018.md",
"errorDetail": null, "errorDetail": null,
"errorContext": "#bad.md", "errorContext": "#bad.md",
"errorRange": [ 1, 2 ] }, "errorRange": [ 1, 2 ],
"fixInfo": { "editColumn": 2, "insertText": ' ' } }
{ "lineNumber": 3, { "lineNumber": 3,
"ruleNames": [ "MD018", "no-missing-space-atx" ], "ruleNames": [ "MD018", "no-missing-space-atx" ],
"ruleDescription": "No space after hash on atx style heading", "ruleDescription": "No space after hash on atx style heading",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md018.md", "ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md018.md",
"errorDetail": null, "errorDetail": null,
"errorContext": "#This file fails\tsome rules.", "errorContext": "#This file fails\tsome rules.",
"errorRange": [ 1, 2 ] }, "errorRange": [ 1, 2 ],
"fixInfo": { "editColumn": 2, "insertText": ' ' } }
{ "lineNumber": 1, { "lineNumber": 1,
"ruleNames": [ "MD041", "first-line-heading", "first-line-h1" ], "ruleNames": [ "MD041", "first-line-heading", "first-line-h1" ],
"ruleDescription": "First line in a file should be a top-level heading", "ruleDescription": "First line in a file should be a top-level heading",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md041.md", "ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md041.md",
"errorDetail": null, "errorDetail": null,
"errorContext": "#bad.md", "errorContext": "#bad.md",
"errorRange": null } "errorRange": null,
"fixInfo": null }
] ]
} }
``` ```
@ -957,7 +948,7 @@ const options = {
} }
}; };
const results = globalThis.markdownlint.lintSync(options).toString(); const results = globalThis.markdownlint.lintSync(options);
``` ```
## Examples ## Examples

View file

@ -18,18 +18,18 @@ const options = {
if (true) { if (true) {
// Makes a synchronous call, uses result.toString for pretty formatting // Makes a synchronous call
const results = lintSync(options); const results = lintSync(options);
console.log(results.toString()); console.dir(results, { "colors": true, "depth": null });
} }
if (true) { if (true) {
// Makes an asynchronous call, uses result.toString for pretty formatting // Makes an asynchronous call
lintAsync(options, function callback(error, results) { lintAsync(options, function callback(error, results) {
if (!error && results) { if (!error && results) {
console.log(results.toString()); console.dir(results, { "colors": true, "depth": null });
} }
}); });
@ -37,7 +37,7 @@ if (true) {
if (true) { if (true) {
// Makes a Promise-based asynchronous call, displays the result object directly // Makes a Promise-based asynchronous call
const results = await lintPromise(options); const results = await lintPromise(options);
console.dir(results, { "colors": true, "depth": null }); console.dir(results, { "colors": true, "depth": null });

View file

@ -171,13 +171,15 @@ lintAsync(options, assertLintResultsCallback);
assertLintResultsCallback(null, await lintPromise(options)); assertLintResultsCallback(null, await lintPromise(options));
})(); })();
const needsFixing = "# Heading\n";
assert.equal( assert.equal(
applyFix( applyFix(
"# Fixing\n", needsFixing,
{ {
"insertText": "Head", "insertText": " ",
"editColumn": 3, "editColumn": 2,
"deleteCount": 3 "deleteCount": 2
}, },
"\n" "\n"
), ),
@ -186,17 +188,12 @@ assert.equal(
assert.equal( assert.equal(
applyFixes( applyFixes(
"# Fixing\n", needsFixing,
[ lintSync({
{ "strings": {
"lineNumber": 1, needsFixing
"fixInfo": {
"insertText": "Head",
"editColumn": 3,
"deleteCount": 3
}
} }
] })["needsFixing"]
), ),
"# Heading\n" "# Heading\n"
); );

View file

@ -3,6 +3,7 @@ export type Configuration = import("./markdownlint.mjs").Configuration;
export type ConfigurationParser = import("./markdownlint.mjs").ConfigurationParser; export type ConfigurationParser = import("./markdownlint.mjs").ConfigurationParser;
export type ConfigurationStrict = import("./markdownlint.mjs").ConfigurationStrict; export type ConfigurationStrict = import("./markdownlint.mjs").ConfigurationStrict;
export type FixInfo = import("./markdownlint.mjs").FixInfo; export type FixInfo = import("./markdownlint.mjs").FixInfo;
export type FixInfoNormalized = import("./markdownlint.mjs").FixInfoNormalized;
export type LintCallback = import("./markdownlint.mjs").LintCallback; export type LintCallback = import("./markdownlint.mjs").LintCallback;
export type LintContentCallback = import("./markdownlint.mjs").LintContentCallback; export type LintContentCallback = import("./markdownlint.mjs").LintContentCallback;
export type LintError = import("./markdownlint.mjs").LintError; export type LintError = import("./markdownlint.mjs").LintError;
@ -23,7 +24,6 @@ export type RuleConfiguration = import("./markdownlint.mjs").RuleConfiguration;
export type RuleFunction = import("./markdownlint.mjs").RuleFunction; export type RuleFunction = import("./markdownlint.mjs").RuleFunction;
export type RuleOnError = import("./markdownlint.mjs").RuleOnError; export type RuleOnError = import("./markdownlint.mjs").RuleOnError;
export type RuleOnErrorFixInfo = import("./markdownlint.mjs").RuleOnErrorFixInfo; export type RuleOnErrorFixInfo = import("./markdownlint.mjs").RuleOnErrorFixInfo;
export type RuleOnErrorFixInfoNormalized = import("./markdownlint.mjs").RuleOnErrorFixInfoNormalized;
export type RuleOnErrorInfo = import("./markdownlint.mjs").RuleOnErrorInfo; export type RuleOnErrorInfo = import("./markdownlint.mjs").RuleOnErrorInfo;
export type RuleParams = import("./markdownlint.mjs").RuleParams; export type RuleParams = import("./markdownlint.mjs").RuleParams;
export { applyFix, applyFixes, formatLintError, getVersion, parseLintErrorString } from "./markdownlint.mjs"; export { applyFix, applyFixes, formatLintError, getVersion, parseLintErrorString } from "./markdownlint.mjs";

View file

@ -7,6 +7,7 @@ export { resolveModule } from "./resolve-module.cjs";
/** @typedef {import("./markdownlint.mjs").ConfigurationParser} ConfigurationParser */ /** @typedef {import("./markdownlint.mjs").ConfigurationParser} ConfigurationParser */
/** @typedef {import("./markdownlint.mjs").ConfigurationStrict} ConfigurationStrict */ /** @typedef {import("./markdownlint.mjs").ConfigurationStrict} ConfigurationStrict */
/** @typedef {import("./markdownlint.mjs").FixInfo} FixInfo */ /** @typedef {import("./markdownlint.mjs").FixInfo} FixInfo */
/** @typedef {import("./markdownlint.mjs").FixInfoNormalized} FixInfoNormalized */
/** @typedef {import("./markdownlint.mjs").LintCallback} LintCallback */ /** @typedef {import("./markdownlint.mjs").LintCallback} LintCallback */
/** @typedef {import("./markdownlint.mjs").LintContentCallback} LintContentCallback */ /** @typedef {import("./markdownlint.mjs").LintContentCallback} LintContentCallback */
/** @typedef {import("./markdownlint.mjs").LintError} LintError */ /** @typedef {import("./markdownlint.mjs").LintError} LintError */
@ -27,6 +28,5 @@ export { resolveModule } from "./resolve-module.cjs";
/** @typedef {import("./markdownlint.mjs").RuleFunction} RuleFunction */ /** @typedef {import("./markdownlint.mjs").RuleFunction} RuleFunction */
/** @typedef {import("./markdownlint.mjs").RuleOnError} RuleOnError */ /** @typedef {import("./markdownlint.mjs").RuleOnError} RuleOnError */
/** @typedef {import("./markdownlint.mjs").RuleOnErrorFixInfo} RuleOnErrorFixInfo */ /** @typedef {import("./markdownlint.mjs").RuleOnErrorFixInfo} RuleOnErrorFixInfo */
/** @typedef {import("./markdownlint.mjs").RuleOnErrorFixInfoNormalized} RuleOnErrorFixInfoNormalized */
/** @typedef {import("./markdownlint.mjs").RuleOnErrorInfo} RuleOnErrorInfo */ /** @typedef {import("./markdownlint.mjs").RuleOnErrorInfo} RuleOnErrorInfo */
/** @typedef {import("./markdownlint.mjs").RuleParams} RuleParams */ /** @typedef {import("./markdownlint.mjs").RuleParams} RuleParams */

View file

@ -63,19 +63,19 @@ export function readConfigSync(file: string, parsers?: ConfigurationParser[], fs
* Applies the specified fix to a Markdown content line. * Applies the specified fix to a Markdown content line.
* *
* @param {string} line Line of Markdown content. * @param {string} line Line of Markdown content.
* @param {RuleOnErrorFixInfo} fixInfo RuleOnErrorFixInfo instance. * @param {FixInfo} fixInfo FixInfo instance.
* @param {string} [lineEnding] Line ending to use. * @param {string} [lineEnding] Line ending to use.
* @returns {string | null} Fixed content or null if deleted. * @returns {string | null} Fixed content or null if deleted.
*/ */
export function applyFix(line: string, fixInfo: RuleOnErrorFixInfo, lineEnding?: string): string | null; export function applyFix(line: string, fixInfo: FixInfo, lineEnding?: string): string | null;
/** /**
* Applies as many of the specified fixes as possible to Markdown content. * Applies as many of the specified fixes as possible to Markdown content.
* *
* @param {string} input Lines of Markdown content. * @param {string} input Lines of Markdown content.
* @param {RuleOnErrorInfo[]} errors RuleOnErrorInfo instances. * @param {LintError[]} errors LintError instances.
* @returns {string} Fixed content. * @returns {string} Fixed content.
*/ */
export function applyFixes(input: string, errors: RuleOnErrorInfo[]): string; export function applyFixes(input: string, errors: LintError[]): string;
/** /**
* TBD * TBD
* *
@ -335,27 +335,6 @@ export type RuleOnErrorFixInfo = {
*/ */
insertText?: string; insertText?: string;
}; };
/**
* RuleOnErrorInfo with all optional properties present.
*/
export type RuleOnErrorFixInfoNormalized = {
/**
* Line number (1-based).
*/
lineNumber: number;
/**
* Column of the fix (1-based).
*/
editColumn: number;
/**
* Count of characters to delete.
*/
deleteCount: number;
/**
* Text to insert (after deleting).
*/
insertText: string;
};
/** /**
* Rule definition. * Rule definition.
*/ */
@ -521,6 +500,27 @@ export type FixInfo = {
*/ */
insertText?: string; insertText?: string;
}; };
/**
* FixInfo with all optional properties present.
*/
export type FixInfoNormalized = {
/**
* Line number (1-based).
*/
lineNumber: number;
/**
* Column of the fix (1-based).
*/
editColumn: number;
/**
* Count of characters to delete.
*/
deleteCount: number;
/**
* Text to insert (after deleting).
*/
insertText: string;
};
/** /**
* Called with the result of linting a string or document. * Called with the result of linting a string or document.
*/ */

View file

@ -1195,9 +1195,9 @@ export function readConfigSync(file, parsers, fs) {
/** /**
* Normalizes the fields of a RuleOnErrorFixInfo instance. * Normalizes the fields of a RuleOnErrorFixInfo instance.
* *
* @param {RuleOnErrorFixInfo} fixInfo RuleOnErrorFixInfo instance. * @param {FixInfo} fixInfo RuleOnErrorFixInfo instance.
* @param {number} [lineNumber] Line number. * @param {number} [lineNumber] Line number.
* @returns {RuleOnErrorFixInfoNormalized} Normalized RuleOnErrorFixInfo instance. * @returns {FixInfoNormalized} Normalized RuleOnErrorFixInfo instance.
*/ */
function normalizeFixInfo(fixInfo, lineNumber = 0) { function normalizeFixInfo(fixInfo, lineNumber = 0) {
return { return {
@ -1212,7 +1212,7 @@ function normalizeFixInfo(fixInfo, lineNumber = 0) {
* Applies the specified fix to a Markdown content line. * Applies the specified fix to a Markdown content line.
* *
* @param {string} line Line of Markdown content. * @param {string} line Line of Markdown content.
* @param {RuleOnErrorFixInfo} fixInfo RuleOnErrorFixInfo instance. * @param {FixInfo} fixInfo FixInfo instance.
* @param {string} [lineEnding] Line ending to use. * @param {string} [lineEnding] Line ending to use.
* @returns {string | null} Fixed content or null if deleted. * @returns {string | null} Fixed content or null if deleted.
*/ */
@ -1228,7 +1228,7 @@ export function applyFix(line, fixInfo, lineEnding = "\n") {
* Applies as many of the specified fixes as possible to Markdown content. * Applies as many of the specified fixes as possible to Markdown content.
* *
* @param {string} input Lines of Markdown content. * @param {string} input Lines of Markdown content.
* @param {RuleOnErrorInfo[]} errors RuleOnErrorInfo instances. * @param {LintError[]} errors LintError instances.
* @returns {string} Fixed content. * @returns {string} Fixed content.
*/ */
export function applyFixes(input, errors) { export function applyFixes(input, errors) {
@ -1463,16 +1463,6 @@ export function getVersion() {
* @property {string} [insertText] Text to insert (after deleting). * @property {string} [insertText] Text to insert (after deleting).
*/ */
/**
* RuleOnErrorInfo with all optional properties present.
*
* @typedef {Object} RuleOnErrorFixInfoNormalized
* @property {number} lineNumber Line number (1-based).
* @property {number} editColumn Column of the fix (1-based).
* @property {number} deleteCount Count of characters to delete.
* @property {string} insertText Text to insert (after deleting).
*/
/** /**
* Rule definition. * Rule definition.
* *
@ -1561,6 +1551,16 @@ export function getVersion() {
* @property {string} [insertText] Text to insert (after deleting). * @property {string} [insertText] Text to insert (after deleting).
*/ */
/**
* FixInfo with all optional properties present.
*
* @typedef {Object} FixInfoNormalized
* @property {number} lineNumber Line number (1-based).
* @property {number} editColumn Column of the fix (1-based).
* @property {number} deleteCount Count of characters to delete.
* @property {string} insertText Text to insert (after deleting).
*/
/** /**
* Called with the result of linting a string or document. * Called with the result of linting a string or document.
* *