Convert markdownlint library to an ECMAScript module, replace markdownlint-micromark with micromark, stop publishing (large) markdownlint-browser.js, see https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c for guidance.

This commit is contained in:
David Anson 2024-11-28 20:36:44 -08:00
parent 191226f070
commit 1e71f6f44e
140 changed files with 1087 additions and 10428 deletions

View file

@ -0,0 +1,64 @@
// @ts-check
import path from "node:path";
const { join } = path.posix;
import { globby } from "globby";
import jsoncParser from "jsonc-parser";
import jsYaml from "js-yaml";
import library from "../lib/markdownlint.mjs";
const { markdownlint, readConfig } = library.promises;
import { markdownlintParallel } from "./markdownlint-test-parallel.mjs";
/**
* Lints a test repository.
*
* @param {Object} t Test instance.
* @param {string[]} globPatterns Array of files to in/exclude.
* @param {string} configPath Path to config file.
* @param {boolean} [parallel] True to lint in parallel.
* @returns {Promise} Test result.
*/
export function lintTestRepo(t, globPatterns, configPath, parallel) {
t.plan(1);
const jsoncParse = (json) => {
const config = jsoncParser.parse(json, [], { "allowTrailingComma": true });
return config.config || config;
};
const yamlParse = (yaml) => jsYaml.load(yaml);
return Promise.all([
globby(globPatterns),
readConfig(configPath, [ jsoncParse, yamlParse ])
]).then((globbyAndReadConfigResults) => {
const [ files, rawConfig ] = globbyAndReadConfigResults;
// eslint-disable-next-line no-console
console.log(`${t.title}: Linting ${files.length} files...`);
const config = Object.fromEntries(
Object.entries(rawConfig)
.map(([ k, v ]) => [
k.replace(/header/, "heading"),
v
])
);
return (parallel ? markdownlintParallel : markdownlint)({
files,
config
}).then((results) => {
const resultsString = results.toString();
t.snapshot(
resultsString,
"Expected linting violations"
);
});
});
}
/**
* Excludes a list of globs.
*
* @param {string} rootDir Root directory for globs.
* @param {...string} globs Globs to exclude.
* @returns {string[]} Array of excluded globs.
*/
export function excludeGlobs(rootDir, ...globs) {
return globs.map((glob) => "!" + join(rootDir, glob));
}