Skip to content

Commit 8da43dd

Browse files
committed
Update to use named exports via / /async /promise /sync, simplify references via self-referencing, refine examples.
1 parent e41f034 commit 8da43dd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+627
-540
lines changed

Diff for: README.md

+39-37
Original file line numberDiff line numberDiff line change
@@ -312,42 +312,41 @@ alternate formats.
312312

313313
### Linting
314314

315-
Standard asynchronous API:
315+
Asynchronous API via `import { lint } from "markdownlint/async"`:
316316

317317
```javascript
318318
/**
319319
* Lint specified Markdown files.
320320
*
321-
* @param {Options} options Configuration options.
321+
* @param {Options | null} options Configuration options.
322322
* @param {LintCallback} callback Callback (err, result) function.
323323
* @returns {void}
324324
*/
325-
function markdownlint(options, callback) { ... }
325+
function lint(options, callback) { ... }
326326
```
327327

328-
Synchronous API (for build scripts, etc.):
328+
Synchronous API via `import { lint } from "markdownlint/sync"`:
329329

330330
```javascript
331331
/**
332-
* Lint specified Markdown files synchronously.
332+
* Lint specified Markdown files.
333333
*
334-
* @param {Options} options Configuration options.
334+
* @param {Options | null} options Configuration options.
335335
* @returns {LintResults} Results object.
336336
*/
337-
function markdownlint.sync(options) { ... }
337+
function lint(options) { ... }
338338
```
339339

340-
Promise API (in the `promises` namespace like Node.js's
341-
[`fs` Promises API](https://nodejs.org/api/fs.html#fs_fs_promises_api)):
340+
Promise API via `import { lint } from "markdownlint/promise"`:
342341

343342
```javascript
344343
/**
345344
* Lint specified Markdown files.
346345
*
347-
* @param {Options} options Configuration options.
346+
* @param {Options | null} options Configuration options.
348347
* @returns {Promise<LintResults>} Results object.
349348
*/
350-
function markdownlint(options) { ... }
349+
function lint(options) { ... }
351350
```
352351

353352
#### options
@@ -669,7 +668,7 @@ By default, configuration files are parsed as JSON (and named
669668
`.markdownlint.json`). Custom parsers can be provided to handle other formats
670669
like JSONC, YAML, and TOML.
671670

672-
Asynchronous API:
671+
Asynchronous API via `import { readConfig } from "markdownlint/async"`:
673672

674673
```javascript
675674
/**
@@ -684,22 +683,21 @@ Asynchronous API:
684683
function readConfig(file, parsers, fs, callback) { ... }
685684
```
686685

687-
Synchronous API:
686+
Synchronous API via `import { readConfig } from "markdownlint/sync"`:
688687

689688
```javascript
690689
/**
691-
* Read specified configuration file synchronously.
690+
* Read specified configuration file.
692691
*
693692
* @param {string} file Configuration file name.
694693
* @param {ConfigurationParser[]} [parsers] Parsing function(s).
695694
* @param {Object} [fs] File system implementation.
696695
* @returns {Configuration} Configuration object.
697696
*/
698-
function readConfigSync(file, parsers, fs) { ... }
697+
function readConfig(file, parsers, fs) { ... }
699698
```
700699

701-
Promise API (in the `promises` namespace like Node.js's
702-
[`fs` Promises API](https://nodejs.org/api/fs.html#fs_promises_api)):
700+
Promise API via `import { readConfig } from "markdownlint/promise"`:
703701

704702
```javascript
705703
/**
@@ -772,7 +770,8 @@ Configuration object.
772770

773771
Rules that can be fixed automatically include a `fixInfo` property which is
774772
outlined in the [documentation for custom rules](doc/CustomRules.md#authoring).
775-
To apply fixes consistently, the `applyFix`/`applyFixes` methods may be used:
773+
To apply fixes consistently, the `applyFix`/`applyFixes` methods may be used via
774+
`import { applyFix, applyFixes } from "markdownlint"`:
776775

777776
```javascript
778777
/**
@@ -798,18 +797,19 @@ function applyFixes(input, errors) { ... }
798797
Invoking `applyFixes` with the results of a call to lint can be done like so:
799798

800799
```javascript
801-
import markdownlint from "markdownlint";
800+
import { applyFixes } from "markdownlint";
801+
import { lint as lintSync } from "markdownlint/sync";
802802

803-
const fixResults = markdownlint.sync({ "strings": { "content": original } });
804-
const fixed = markdownlint.applyFixes(original, fixResults.content);
803+
const results = lintSync({ "strings": { "content": original } });
804+
const fixed = applyFixes(original, results.content);
805805
```
806806

807807
## Usage
808808

809-
Invoke `markdownlint` and use the `result` object's `toString` method:
809+
Invoke `lint` and use the `result` object's `toString` method:
810810

811811
```javascript
812-
import markdownlint from "markdownlint";
812+
import { lint as lintAsync } from "markdownlint/async";
813813

814814
const options = {
815815
"files": [ "good.md", "bad.md" ],
@@ -819,9 +819,9 @@ const options = {
819819
}
820820
};
821821

822-
markdownlint(options, function callback(err, result) {
823-
if (!err) {
824-
console.log(result.toString());
822+
lintAsync(options, function callback(error, results) {
823+
if (!error && results) {
824+
console.log(results.toString());
825825
}
826826
});
827827
```
@@ -839,21 +839,22 @@ bad.md: 3: MD018/no-missing-space-atx No space after hash on atx style heading [
839839
bad.md: 1: MD041/first-line-heading/first-line-h1 First line in a file should be a top-level heading [Context: "#bad.md"]
840840
```
841841

842-
Or invoke `markdownlint.sync` for a synchronous call:
842+
Or as a synchronous call:
843843

844844
```javascript
845-
const result = markdownlint.sync(options);
846-
console.log(result.toString());
845+
import { lint as lintSync } from "markdownlint/sync";
846+
847+
const results = lintSync(options);
848+
console.log(results.toString());
847849
```
848850

849-
To examine the `result` object directly:
851+
To examine the `result` object directly via a `Promise`-based call:
850852

851853
```javascript
852-
markdownlint(options, function callback(err, result) {
853-
if (!err) {
854-
console.dir(result, { "colors": true, "depth": null });
855-
}
856-
});
854+
import { lint as lintPromise } from "markdownlint/promise";
855+
856+
const results = await lintPromise(options);
857+
console.dir(results, { "colors": true, "depth": null });
857858
```
858859

859860
Output:
@@ -910,7 +911,7 @@ Generate normal and minified scripts with:
910911
npm run build-demo
911912
```
912913

913-
Then reference the `markdownlint` script:
914+
Then reference the `markdownlint-browser` script:
914915

915916
```html
916917
<script src="demo/markdownlint-browser.min.js"></script>
@@ -924,7 +925,8 @@ const options = {
924925
"content": "Some Markdown to lint."
925926
}
926927
};
927-
const results = window.markdownlint.markdownlint.sync(options).toString();
928+
929+
const results = globalThis.markdownlint.lintSync(options).toString();
928930
```
929931

930932
## Examples

Diff for: demo/browser-exports.mjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @ts-check
22

3-
export { default as markdownlint } from "../lib/markdownlint.mjs";
3+
export { applyFixes, getVersion } from "markdownlint";
4+
export { lint as lintSync } from "markdownlint/sync";
45
export { compile, parse, postprocess, preprocess } from "micromark";
56
export { directive, directiveHtml } from "micromark-extension-directive";
67
export { gfmAutolinkLiteral, gfmAutolinkLiteralHtml } from "micromark-extension-gfm-autolink-literal";

Diff for: demo/default.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
(function main() {
44
// Dependencies
55
var markdownit = globalThis.markdownit;
6-
var markdownlint = globalThis.markdownlint.markdownlint;
7-
var micromark = globalThis.markdownlint;
6+
var markdownlint = globalThis.markdownlint;
7+
var micromark = markdownlint;
88

99
// DOM elements
1010
var markdown = document.getElementById("markdown");
@@ -112,7 +112,7 @@
112112
},
113113
"handleRuleFailures": true
114114
};
115-
allLintErrors = markdownlint.sync(options).content;
115+
allLintErrors = markdownlint.lintSync(options).content;
116116
violations.innerHTML = allLintErrors.map(function mapResult(result) {
117117
var ruleName = result.ruleNames.slice(0, 2).join(" / ");
118118
return "<em><a href='#line' target='" + result.lineNumber + "'>" +

Diff for: doc/CustomRules.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ implementation that is resolved when the rule completes. (The value passed to
160160
reported via the `onError` function just like for synchronous rules.
161161

162162
**Note**: Asynchronous rules cannot be referenced in a synchronous calling
163-
context (i.e., `markdownlint.sync(...)`). Attempting to do so throws an
164-
exception.
163+
context (i.e., `import { lint } from "markdownlint/sync"`). Attempting to do so
164+
throws an exception.
165165

166166
## Examples
167167

Diff for: eslint.config.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export default [
149149
],
150150
"rules": {
151151
"no-console": "off",
152-
"no-shadow": "off"
152+
"no-constant-condition": "off"
153153
}
154154
},
155155
{

Diff for: example/Gruntfile.cjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ module.exports = function wrapper(grunt) {
1313

1414
grunt.registerMultiTask("markdownlint", function task() {
1515
const done = this.async();
16-
import("markdownlint").then(({ "default": markdownlint }) => {
17-
markdownlint(
16+
import("markdownlint/async").then(({ lint }) => {
17+
lint(
1818
{ "files": this.filesSrc },
1919
function callback(err, result) {
2020
const resultString = err || ((result || "").toString());

Diff for: example/gulpfile.cjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ const through2 = require("through2");
99
gulp.task("markdownlint", function task() {
1010
return gulp.src("*.md", { "read": false })
1111
.pipe(through2.obj(function obj(file, enc, next) {
12-
import("markdownlint").then(({ "default": markdownlint }) => {
13-
markdownlint(
12+
import("markdownlint/async").then(({ lint }) => {
13+
lint(
1414
{ "files": [ file.relative ] },
1515
function callback(err, result) {
1616
const resultString = (result || "").toString();

Diff for: example/standalone.mjs

+37-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// @ts-check
22

3-
import markdownlint from "markdownlint";
3+
import { applyFixes } from "markdownlint";
4+
import { lint as lintAsync } from "markdownlint/async";
5+
import { lint as lintPromise } from "markdownlint/promise";
6+
import { lint as lintSync } from "markdownlint/sync";
47

58
const options = {
69
"files": [ "good.md", "bad.md" ],
@@ -10,27 +13,39 @@ const options = {
1013
}
1114
};
1215

13-
// Makes a synchronous call, using result.toString for pretty formatting
14-
const result = markdownlint.sync(options);
15-
console.log(result.toString());
16+
if (true) {
1617

17-
// Makes an asynchronous call
18-
markdownlint(options, function callback(err, result) {
19-
if (!err) {
20-
// @ts-ignore
21-
console.log(result.toString());
22-
}
23-
});
18+
// Makes a synchronous call, uses result.toString for pretty formatting
19+
const results = lintSync(options);
20+
console.log(results.toString());
2421

25-
// Displays the result object directly
26-
markdownlint(options, function callback(err, result) {
27-
if (!err) {
28-
console.dir(result, { "colors": true, "depth": null });
29-
}
30-
});
22+
}
23+
24+
if (true) {
25+
26+
// Makes an asynchronous call, uses result.toString for pretty formatting
27+
lintAsync(options, function callback(error, results) {
28+
if (!error && results) {
29+
console.log(results.toString());
30+
}
31+
});
32+
33+
}
34+
35+
if (true) {
36+
37+
// Makes a Promise-based asynchronous call, displays the result object directly
38+
const results = await lintPromise(options);
39+
console.dir(results, { "colors": true, "depth": null });
40+
41+
}
42+
43+
if (true) {
44+
45+
// Fixes all supported violations in Markdown content
46+
const original = "# Heading";
47+
const results = lintSync({ "strings": { "content": original } });
48+
const fixed = applyFixes(original, results.content);
49+
console.log(fixed);
3150

32-
// Fixes all supported violations in Markdown content
33-
const original = "# Heading";
34-
const fixResults = markdownlint.sync({ "strings": { "content": original } });
35-
const fixed = markdownlint.applyFixes(original, fixResults.content);
36-
console.log(fixed);
51+
}

0 commit comments

Comments
 (0)