Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Nov 24, 2024
1 parent 1814962 commit ba955be
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 68 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ function fixMarkdownlintViolations(content) {
Invoke `markdownlint` and use the `result` object's `toString` method:

```javascript
const markdownlint = require("markdownlint");
import markdownlint from "../lib/markdownlint.mjs";

const options = {
"files": [ "good.md", "bad.md" ],
Expand Down Expand Up @@ -897,10 +897,10 @@ Output:
```

Integration with the [gulp](https://gulpjs.com/) build system is
straightforward: [`gulpfile.js`](example/gulpfile.js).
straightforward: [`gulpfile.cjs`](example/gulpfile.cjs).

Integration with the [Grunt](https://gruntjs.com/) build system is similar:
[`Gruntfile.js`](example/Gruntfile.js).
[`Gruntfile.cjs`](example/Gruntfile.cjs).

## Browser

Expand Down
28 changes: 28 additions & 0 deletions example/Gruntfile.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// @ts-check

"use strict";

module.exports = function wrapper(grunt) {
grunt.initConfig({
"markdownlint": {
"example": {
"src": [ "*.md" ]
}
}
});

grunt.registerMultiTask("markdownlint", function task() {
const done = this.async();
import("../lib/markdownlint.mjs").then(({ "default": markdownlint}) => {
markdownlint(
{ "files": this.filesSrc },
function callback(err, result) {
const resultString = err || ((result || "").toString());
if (resultString) {
grunt.fail.warn("\n" + resultString + "\n");
}
done(!err || !resultString);
});
}).catch(done);
});
};
28 changes: 0 additions & 28 deletions example/Gruntfile.js

This file was deleted.

24 changes: 24 additions & 0 deletions example/gulpfile.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// @ts-check

"use strict";

const gulp = require("gulp");
const through2 = require("through2");

// Simple task wrapper
gulp.task("markdownlint", function task() {
return gulp.src("*.md", { "read": false })
.pipe(through2.obj(function obj(file, enc, next) {
import("../lib/markdownlint.mjs").then(({ "default": markdownlint}) => {
markdownlint(
{ "files": [ file.relative ] },
function callback(err, result) {
const resultString = (result || "").toString();
if (resultString) {
console.log(resultString);
}
next(err, file);
});
}).catch(next);
}));
});
23 changes: 0 additions & 23 deletions example/gulpfile.js

This file was deleted.

5 changes: 2 additions & 3 deletions example/standalone.js → example/standalone.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// @ts-check

"use strict";

const markdownlint = require("../lib/markdownlint");
import markdownlint from "../lib/markdownlint.mjs";

const options = {
"files": [ "good.md", "bad.md" ],
Expand All @@ -19,6 +17,7 @@ console.log(result.toString());
// Makes an asynchronous call
markdownlint(options, function callback(err, result) {
if (!err) {
// @ts-ignore
console.log(result.toString());
}
});
Expand Down
18 changes: 10 additions & 8 deletions lib/markdownlint.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// @ts-check

import nodeFs from "node:fs";
import { createRequire } from "node:module";
import os from "node:os";
const dynamicRequire = createRequire(import.meta.url);
import path from "node:path";
import { promisify } from "node:util";
import { getMarkdownItTokens } from "./markdownit.cjs";
import { parse as micromarkParse } from "../helpers/micromark-parse.mjs";
import { version } from "./constants.mjs";
import rules from "./rules.mjs";
Expand Down Expand Up @@ -502,7 +505,7 @@ function lintContent(
// Parse content into lines and get markdown-it tokens
const lines = content.split(helpers.newLineRe);
const markdownitTokens = needMarkdownItTokens ?
require("./markdownit.cjs").getMarkdownItTokens(markdownItPlugins, preClearedContent, lines) :
getMarkdownItTokens(markdownItPlugins, preClearedContent, lines) :
[];
// Create (frozen) parameters for rules
/** @type {MarkdownParsers} */
Expand Down Expand Up @@ -861,7 +864,7 @@ function lintInput(options, synchronous, callback) {
3 :
options.resultVersion;
const markdownItPlugins = options.markdownItPlugins || [];
const fs = options.fs || require("node:fs");
const fs = options.fs || nodeFs;
const aliasToRuleNames = mapAliasToRuleNames(ruleList);
const results = newResults(ruleList);
let done = false;
Expand Down Expand Up @@ -1063,7 +1066,7 @@ function extendConfig(config, file, parsers, fs, callback) {
if (configExtends) {
return resolveConfigExtends(
file,
helpers.expandTildePath(configExtends, require("node:os")),
helpers.expandTildePath(configExtends, os),
fs,
// eslint-disable-next-line no-use-before-define
(_, resolvedExtends) => readConfig(
Expand Down Expand Up @@ -1127,10 +1130,10 @@ function readConfig(file, parsers, fs, callback) {
}
}
if (!fs) {
fs = require("node:fs");
fs = nodeFs;
}
// Read file
file = helpers.expandTildePath(file, require("node:os"));
file = helpers.expandTildePath(file, os);
fs.readFile(file, "utf8", (err, content) => {
if (err) {
// @ts-ignore
Expand Down Expand Up @@ -1175,10 +1178,9 @@ function readConfigPromise(file, parsers, fs) {
*/
function readConfigSync(file, parsers, fs) {
if (!fs) {
fs = require("node:fs");
fs = nodeFs;
}
// Read file
const os = require("node:os");
file = helpers.expandTildePath(file, os);
const content = fs.readFileSync(file, "utf8");
// Try to parse file
Expand Down Expand Up @@ -1243,7 +1245,7 @@ function applyFix(line, fixInfo, lineEnding = "\n") {
* @returns {string} Fixed content.
*/
function applyFixes(input, errors) {
const lineEnding = helpers.getPreferredLineEnding(input, require("node:os"));
const lineEnding = helpers.getPreferredLineEnding(input, os);
const lines = input.split(helpers.newLineRe);
// Normalize fixInfo objects
let fixInfos = errors
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@
"clone-test-repos-webpack-webpack-js-org": "cd test-repos && git clone https://github.com/webpack/webpack.js.org webpack-webpack-js-org --depth 1 --no-tags --quiet",
"clone-test-repos": "mkdir test-repos && cd test-repos && npm run clone-test-repos-apache-airflow && npm run clone-test-repos-dotnet-docs && npm run clone-test-repos-electron-electron && npm run clone-test-repos-eslint-eslint && npm run clone-test-repos-mdn-content && npm run clone-test-repos-mkdocs-mkdocs && npm run clone-test-repos-mochajs-mocha && npm run clone-test-repos-pi-hole-docs && npm run clone-test-repos-v8-v8-dev && npm run clone-test-repos-webhintio-hint && npm run clone-test-repos-webpack-webpack-js-org",
"declaration": "npm run build-declaration && npm run test-declaration",
"example": "cd example && node standalone.js && grunt markdownlint --force && gulp markdownlint",
"docker-npm-install": "docker run --rm --tty --name npm-install --volume $PWD:/home/workdir --workdir /home/workdir --user node node:latest npm install",
"docker-npm-run-upgrade": "docker run --rm --tty --name npm-run-upgrade --volume $PWD:/home/workdir --workdir /home/workdir --user node node:latest npm run upgrade",
"example": "cd example && node standalone.mjs && grunt markdownlint --force && gulp markdownlint",
"install-micromark": "cd micromark && npm install",
"lint": "eslint --max-warnings 0",
"lint-test-repos": "ava --timeout=10m test/markdownlint-test-repos-*.mjs",
Expand Down

0 comments on commit ba955be

Please sign in to comment.