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

@ -1 +1 @@
test.js
test.cjs

View file

@ -4,13 +4,17 @@
const micromark = require("./micromark-helpers.cjs");
const { newLineRe, nextLinesRe } = require("./shared.js");
const { newLineRe, nextLinesRe } = require("./shared.cjs");
module.exports.newLineRe = newLineRe;
module.exports.nextLinesRe = nextLinesRe;
/** @typedef {import("../lib/markdownlint.js").RuleOnError} RuleOnError */
/** @typedef {import("../lib/markdownlint.js").RuleOnErrorFixInfo} RuleOnErrorFixInfo */
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/52529
/** @typedef {import("../lib/markdownlint.mjs").RuleOnError} RuleOnError */
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/52529
/** @typedef {import("../lib/markdownlint.mjs").RuleOnErrorFixInfo} RuleOnErrorFixInfo */
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/52529
/** @typedef {import("../lib/markdownlint.mjs").MicromarkToken} MicromarkToken */
// Regular expression for matching common front matter (YAML and TOML)
module.exports.frontMatterRe =
@ -336,8 +340,8 @@ const positionLessThanOrEqual = (lineA, columnA, lineB, columnB) => (
/**
* Returns whether two ranges (or MicromarkTokens) overlap anywhere.
*
* @param {FileRange|import("../lib/markdownlint.js").MicromarkToken} rangeA Range A.
* @param {FileRange|import("../lib/markdownlint.js").MicromarkToken} rangeB Range B.
* @param {FileRange|MicromarkToken} rangeA Range A.
* @param {FileRange|MicromarkToken} rangeB Range B.
* @returns {boolean} True iff the two ranges overlap.
*/
module.exports.hasOverlap = function hasOverlap(rangeA, rangeB) {

View file

@ -2,10 +2,12 @@
"use strict";
const { flatTokensSymbol, htmlFlowSymbol } = require("./shared.js");
const { flatTokensSymbol, htmlFlowSymbol } = require("./shared.cjs");
/** @typedef {import("markdownlint-micromark").TokenType} TokenType */
/** @typedef {import("../lib/markdownlint.js").MicromarkToken} Token */
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/52529
/** @typedef {import("micromark-util-types").TokenType} TokenType */
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/52529
/** @typedef {import("../lib/markdownlint.mjs").MicromarkToken} Token */
/**
* Determines if a Micromark token is within an htmlFlow type.

View file

@ -1,18 +1,24 @@
// @ts-check
"use strict";
import { directive } from "micromark-extension-directive";
import { gfmAutolinkLiteral } from "micromark-extension-gfm-autolink-literal";
import { gfmFootnote } from "micromark-extension-gfm-footnote";
import { gfmTable } from "micromark-extension-gfm-table";
import { math } from "micromark-extension-math";
import { parse as micromarkParse, postprocess as micromarkPostprocess, preprocess as micromarkPreprocess } from "micromark";
// micromark-core-commonmark is not a dependency because this instance must match what's used by micromark
// eslint-disable-next-line n/no-extraneous-import
import { labelEnd } from "micromark-core-commonmark";
import { isHtmlFlowComment } from "./micromark-helpers.cjs";
import { flatTokensSymbol, htmlFlowSymbol, newLineRe } from "./shared.cjs";
const micromark = require("markdownlint-micromark");
const { isHtmlFlowComment } = require("./micromark-helpers.cjs");
const { flatTokensSymbol, htmlFlowSymbol, newLineRe } = require("./shared.js");
/** @typedef {import("markdownlint-micromark").Construct} Construct */
/** @typedef {import("markdownlint-micromark").Event} Event */
/** @typedef {import("markdownlint-micromark").ParseOptions} MicromarkParseOptions */
/** @typedef {import("markdownlint-micromark").State} State */
/** @typedef {import("markdownlint-micromark").Token} Token */
/** @typedef {import("markdownlint-micromark").Tokenizer} Tokenizer */
/** @typedef {import("../lib/markdownlint.js").MicromarkToken} MicromarkToken */
/** @typedef {import("micromark-util-types").Event} Event */
/** @typedef {import("micromark-util-types").ParseOptions} MicromarkParseOptions */
/** @typedef {import("micromark-util-types").State} State */
/** @typedef {import("micromark-util-types").Token} Token */
/** @typedef {import("micromark-util-types").Tokenizer} Tokenizer */
/** @typedef {import("./micromark-types.d.mts")} */
/** @typedef {import("../lib/markdownlint.mjs").MicromarkToken} MicromarkToken */
/**
* Parse options.
@ -28,27 +34,23 @@ const { flatTokensSymbol, htmlFlowSymbol, newLineRe } = require("./shared.js");
* @param {MicromarkParseOptions} [micromarkParseOptions] Options for micromark.
* @returns {Event[]} Micromark events.
*/
function getEvents(
export function getEvents(
markdown,
micromarkParseOptions = {}
) {
// Customize extensions list to add useful extensions
const extensions = [
micromark.directive(),
micromark.gfmAutolinkLiteral(),
micromark.gfmFootnote(),
micromark.gfmTable(),
micromark.math(),
directive(),
gfmAutolinkLiteral(),
gfmFootnote(),
gfmTable(),
math(),
...(micromarkParseOptions.extensions || [])
];
// // Shim labelEnd to identify undefined link labels
/** @type {Event[][]} */
const artificialEventLists = [];
/** @type {Construct} */
const labelEnd =
// @ts-ignore
micromark.labelEnd;
const tokenizeOriginal = labelEnd.tokenize;
/** @type {Tokenizer} */
@ -162,9 +164,9 @@ function getEvents(
// Use micromark to parse document into Events
const encoding = undefined;
const eol = true;
const parseContext = micromark.parse({ ...micromarkParseOptions, extensions });
const chunks = micromark.preprocess()(markdown, encoding, eol);
const events = micromark.postprocess(parseContext.document().write(chunks));
const parseContext = micromarkParse({ ...micromarkParseOptions, extensions });
const chunks = micromarkPreprocess()(markdown, encoding, eol);
const events = micromarkPostprocess(parseContext.document().write(chunks));
// Append artificial events and return all events
// eslint-disable-next-line unicorn/prefer-spread
@ -214,8 +216,7 @@ function parseInternal(
};
const history = [ root ];
let current = root;
// eslint-disable-next-line jsdoc/valid-types
/** @type MicromarkParseOptions | null */
/** @type {MicromarkParseOptions | null} */
let reparseOptions = null;
let lines = null;
let skipHtmlFlowChildren = false;
@ -303,11 +304,6 @@ function parseInternal(
* @param {ParseOptions} [parseOptions] Options.
* @returns {MicromarkToken[]} Micromark tokens.
*/
function parse(markdown, parseOptions) {
export function parse(markdown, parseOptions) {
return parseInternal(markdown, parseOptions);
}
module.exports = {
getEvents,
parse
};

View file

@ -0,0 +1,11 @@
export {};
// Augment TokenTypeMap with markdownlint-specific types.
declare module "micromark-util-types" {
export interface TokenTypeMap {
undefinedReference: "undefinedReference"
undefinedReferenceCollapsed: "undefinedReferenceCollapsed"
undefinedReferenceFull: "undefinedReferenceFull"
undefinedReferenceShortcut: "undefinedReferenceShortcut"
}
}

View file

@ -2,9 +2,9 @@
"name": "markdownlint-rule-helpers",
"version": "0.27.0",
"description": "A collection of markdownlint helper functions for custom rules",
"main": "./helpers.js",
"main": "./helpers.cjs",
"exports": {
".": "./helpers.js",
".": "./helpers.cjs",
"./micromark": "./micromark-helpers.cjs"
},
"author": "David Anson (https://dlaa.me/)",
@ -20,7 +20,12 @@
"node": ">=18"
},
"dependencies": {
"markdownlint-micromark": "0.1.2"
"micromark": "4.0.0",
"micromark-extension-directive": "3.0.2",
"micromark-extension-gfm-autolink-literal": "2.1.0",
"micromark-extension-gfm-footnote": "2.1.0",
"micromark-extension-gfm-table": "2.1.0",
"micromark-extension-math": "3.1.0"
},
"keywords": [
"markdownlint",

View file

@ -7,7 +7,7 @@ const test = require("ava").default;
const { "exports": packageExports, name } = require("../helpers/package.json");
const exportMappings = new Map([
[ ".", "../helpers/helpers.js" ],
[ ".", "../helpers/helpers.cjs" ],
[ "./micromark", "../helpers/micromark-helpers.cjs" ]
]);