Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Dec 25, 2024
1 parent a20a50a commit dc2a277
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 30 deletions.
1 change: 1 addition & 0 deletions lib/exports.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type LintCallback = import("./markdownlint.mjs").LintCallback;
export type LintContentCallback = import("./markdownlint.mjs").LintContentCallback;
export type LintError = import("./markdownlint.mjs").LintError;
export type LintResults = import("./markdownlint.mjs").LintResults;
export type MarkdownItFactory = import("./markdownlint.mjs").MarkdownItFactory;
export type MarkdownItToken = import("./markdownlint.mjs").MarkdownItToken;
export type MarkdownParsers = import("./markdownlint.mjs").MarkdownParsers;
export type MicromarkToken = import("./markdownlint.mjs").MicromarkToken;
Expand Down
1 change: 1 addition & 0 deletions lib/exports.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export { applyFix, applyFixes, getVersion } from "./markdownlint.mjs";
/** @typedef {import("./markdownlint.mjs").LintContentCallback} LintContentCallback */
/** @typedef {import("./markdownlint.mjs").LintError} LintError */
/** @typedef {import("./markdownlint.mjs").LintResults} LintResults */
/** @typedef {import("./markdownlint.mjs").MarkdownItFactory} MarkdownItFactory */
/** @typedef {import("./markdownlint.mjs").MarkdownItToken} MarkdownItToken */
/** @typedef {import("./markdownlint.mjs").MarkdownParsers} MarkdownParsers */
/** @typedef {import("./markdownlint.mjs").MicromarkToken} MicromarkToken */
Expand Down
8 changes: 5 additions & 3 deletions lib/markdownit.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const { newLineRe } = require("../helpers");
/** @typedef {import("markdownlint").MarkdownItToken} MarkdownItToken */
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/52529
/** @typedef {import("markdownlint").Plugin} Plugin */
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/52529
/** @typedef {import("markdownlint").MarkdownItFactory} MarkdownItFactory */

/**
* @callback InlineCodeSpanCallback
Expand Down Expand Up @@ -97,7 +99,7 @@ function freezeToken(token) {
/**
* Annotate tokens with line/lineNumber and freeze them.
*
* @param {Object[]} tokens Array of markdown-it tokens.
* @param {MarkdownItToken[]} tokens Array of markdown-it tokens.
* @param {string[]} lines Lines of Markdown content.
* @returns {void}
*/
Expand Down Expand Up @@ -152,10 +154,10 @@ function annotateAndFreezeTokens(tokens, lines) {
/**
* Gets an array of markdown-it tokens for the input.
*
* @param {Function} markdownItFactory ...
* @param {MarkdownItFactory} markdownItFactory ...
* @param {string} content Markdown content.
* @param {string[]} lines Lines of Markdown content.
* @returns {MarkdownItToken} Array of markdown-it tokens.
* @returns {MarkdownItToken[]} Array of markdown-it tokens.
*/
function getMarkdownItTokens(markdownItFactory, content, lines) {
const markdownIt = markdownItFactory();
Expand Down
15 changes: 14 additions & 1 deletion lib/markdownlint.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,23 @@ export type Rule = {
*/
function: RuleFunction;
};
/**
* Method used by the markdown-it parser to parse input.
*/
export type MarkdownItParse = (src: string, env: any) => any[];
/**
* Instance of the markdown-it parser.
*/
export type MarkdownIt = {
/**
* Method to parse input.
*/
parse: MarkdownItParse;
};
/**
* Gets an instance of the markdown-it parser. Any plugins should already have been loaded.
*/
export type MarkdownItFactory = () => Function;
export type MarkdownItFactory = () => MarkdownIt;
/**
* Configuration options.
*/
Expand Down
22 changes: 19 additions & 3 deletions lib/markdownlint.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ function lintInput(options, synchronous, callback) {
options.resultVersion;
const markdownItFactory =
options.markdownItFactory ||
(() => { throw new Error("The option 'markdownItFactory' needed to be set (due to the option 'customRules' including a rule requiring the 'markdown-it' parser), but 'markdownItFactory' was not set."); });
(() => { throw new Error("The option 'markdownItFactory' was required (due to the option 'customRules' including a rule requiring the 'markdown-it' parser), but 'markdownItFactory' was not set."); });
const fs = options.fs || nodeFs;
const aliasToRuleNames = mapAliasToRuleNames(ruleList);
const results = newResults(ruleList);
Expand Down Expand Up @@ -1476,11 +1476,27 @@ export function getVersion() {
* @property {RuleFunction} function Rule implementation.
*/

/**
* Method used by the markdown-it parser to parse input.
*
* @callback MarkdownItParse
* @param {string} src Source string.
* @param {Object} env Environment sandbox.
* @returns {import("markdown-it").Token[]} Tokens.
*/

/**
* Instance of the markdown-it parser.
*
* @typedef MarkdownIt
* @property {MarkdownItParse} parse Method to parse input.
*/

/**
* Gets an instance of the markdown-it parser. Any plugins should already have been loaded.
*
* @callback MarkdownItFactory
* @returns {Function} Instance of the markdown-it parser.
* @returns {MarkdownIt} Instance of the markdown-it parser.
*/

/**
Expand Down Expand Up @@ -1511,7 +1527,7 @@ export function getVersion() {
*
* @callback ToStringCallback
* @param {boolean} [ruleAliases] True to use rule aliases.
* @returns {string}
* @returns {string} Pretty-printed results.
*/

/**
Expand Down
70 changes: 49 additions & 21 deletions test/markdownlint-test-custom-rules.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ import { __filename, importWithTypeJson } from "./esm-helpers.mjs";
const packageJson = await importWithTypeJson(import.meta, "../package.json");
const { homepage, version } = packageJson;

const markdownItFactory = () => markdownIt({ "html": true });

test("customRulesV0", (t) => new Promise((resolve) => {
t.plan(4);
const customRulesMd = "./test/custom-rules.md";
/** @type {import("markdownlint").Options} */
const options = {
"customRules": customRules.all,
"files": [ customRulesMd ],
"markdownItFactory": () => markdownIt({ "html": true }),
markdownItFactory,
"resultVersion": 0
};
lintAsync(options, function callback(err, actualResult) {
Expand Down Expand Up @@ -94,7 +96,7 @@ test("customRulesV1", (t) => new Promise((resolve) => {
const options = {
"customRules": customRules.all,
"files": [ customRulesMd ],
"markdownItFactory": () => markdownIt({ "html": true }),
markdownItFactory,
"resultVersion": 1
};
lintAsync(options, function callback(err, actualResult) {
Expand Down Expand Up @@ -226,7 +228,7 @@ test("customRulesV2", (t) => new Promise((resolve) => {
const options = {
"customRules": customRules.all,
"files": [ customRulesMd ],
"markdownItFactory": () => markdownIt({ "html": true }),
markdownItFactory,
"resultVersion": 2
};
lintAsync(options, function callback(err, actualResult) {
Expand Down Expand Up @@ -355,7 +357,7 @@ test("customRulesConfig", (t) => new Promise((resolve) => {
},
"letters-e-x": false
},
"markdownItFactory": () => markdownIt({ "html": true }),
markdownItFactory,
"resultVersion": 0
};
lintAsync(options, function callback(err, actualResult) {
Expand All @@ -381,7 +383,7 @@ test("customRulesNpmPackage", (t) => new Promise((resolve) => {
require("./rules/npm"),
require("markdownlint-rule-extended-ascii")
],
"markdownItFactory": () => markdownIt({ "html": true }),
markdownItFactory,
"strings": {
"string": "# Text\n\n---\n\nText ✅\n"
},
Expand Down Expand Up @@ -561,12 +563,12 @@ test("customRulesParserUndefined", (t) => {
}
}
],
"markdownItFactory": () => markdownIt({ "html": true }),
markdownItFactory,
"strings": {
"string": "# Heading\n"
}
};
return lintPromise(options).then(() => null);
return lintPromise(options);
});

test("customRulesParserNone", (t) => {
Expand All @@ -590,7 +592,7 @@ test("customRulesParserNone", (t) => {
"string": "# Heading\n"
}
};
return lintPromise(options).then(() => null);
return lintPromise(options);
});

test("customRulesParserMarkdownIt", (t) => {
Expand All @@ -613,12 +615,12 @@ test("customRulesParserMarkdownIt", (t) => {
}
}
],
"markdownItFactory": () => markdownIt({ "html": true }),
markdownItFactory,
"strings": {
"string": "# Heading\n"
}
};
return lintPromise(options).then(() => null);
return lintPromise(options);
});

test("customRulesParserMicromark", (t) => {
Expand All @@ -645,7 +647,33 @@ test("customRulesParserMicromark", (t) => {
"string": "# Heading\n"
}
};
return lintPromise(options).then(() => null);
return lintPromise(options);
});

test("customRulesMarkdownItFactoryUndefined", (t) => {
t.plan(1);
/** @type {import("markdownlint").Options} */
const options = {
"customRules": [
{
"names": [ "name" ],
"description": "description",
"tags": [ "tag" ],
"parser": "markdownit",
"function": () => {}
}
],
"strings": {
"string": "# Heading\n"
}
};
t.throws(
() => lintSync(options),
{
"message": "The option 'markdownItFactory' was required (due to the option 'customRules' including a rule requiring the 'markdown-it' parser), but 'markdownItFactory' was not set."
},
"No exception when markdownItFactory is undefined."
);
});

test("customRulesMarkdownItParamsTokensSameObject", (t) => {
Expand All @@ -665,12 +693,12 @@ test("customRulesMarkdownItParamsTokensSameObject", (t) => {
}
}
],
"markdownItFactory": () => markdownIt({ "html": true }),
markdownItFactory,
"strings": {
"string": "# Heading\n"
}
};
return lintPromise(options).then(() => null);
return lintPromise(options);
});

test("customRulesMarkdownItTokensSnapshot", (t) => {
Expand All @@ -689,14 +717,14 @@ test("customRulesMarkdownItTokensSnapshot", (t) => {
}
}
],
"markdownItFactory": () => markdownIt({ "html": true }),
markdownItFactory,
"noInlineConfig": true
};
return fs
.readFile("./test/every-markdown-syntax.md", "utf8")
.then((content) => {
options.strings = { "content": content.split(newLineRe).join("\n") };
return lintPromise(options).then(() => null);
return lintPromise(options);
});
});

Expand All @@ -722,7 +750,7 @@ test("customRulesMicromarkTokensSnapshot", (t) => {
.readFile("./test/every-markdown-syntax.md", "utf8")
.then((content) => {
options.strings = { "content": content.split(newLineRe).join("\n") };
return lintPromise(options).then(() => null);
return lintPromise(options);
});
});

Expand Down Expand Up @@ -1676,7 +1704,7 @@ test("customRulesLintJavaScript", (t) => new Promise((resolve) => {
const options = {
"customRules": customRules.lintJavaScript,
"files": "test/lint-javascript.md",
"markdownItFactory": () => markdownIt({ "html": true })
markdownItFactory
};
lintAsync(options, (err, actual) => {
t.falsy(err);
Expand Down Expand Up @@ -1705,7 +1733,7 @@ test("customRulesValidateJson", (t) => new Promise((resolve) => {
const options = {
"customRules": customRules.validateJson,
"files": "test/validate-json.md",
"markdownItFactory": () => markdownIt({ "html": true })
markdownItFactory
};
lintAsync(options, (err, actual) => {
t.falsy(err);
Expand Down Expand Up @@ -1805,9 +1833,9 @@ test("customRulesParamsAreFrozen", (t) => {
}
],
"files": [ "README.md" ],
"markdownItFactory": () => markdownIt({ "html": true })
markdownItFactory
};
return lintPromise(options).then(() => null);
return lintPromise(options);
});

test("customRulesParamsAreStable", (t) => {
Expand Down Expand Up @@ -1875,7 +1903,7 @@ test("customRulesParamsAreStable", (t) => {
"string": "# Heading"
}
};
return lintPromise(options).then(() => null);
return lintPromise(options);
});

test("customRulesAsyncReadFiles", (t) => {
Expand Down
4 changes: 2 additions & 2 deletions test/markdownlint-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const ajvOptions = {
* Gets an instance of a markdown-it factory, suitable for use with options.markdownItFactory.
*
* @param {import("../lib/markdownlint.mjs").Plugin[]} markdownItPlugins Additional markdown-it plugins.
* @returns {Function} Instance of the markdown-it parser.
* @returns {import("../lib/markdownlint.mjs").MarkdownItFactory} Instance of the markdown-it parser.
*/
function getMarkdownItFactory(markdownItPlugins) {
return () => {
Expand Down Expand Up @@ -1260,7 +1260,7 @@ test("token-map-spans", (t) => {
}
],
"files": [ "./test/token-map-spans.md" ],
"markdownItFactory": () => markdownIt({ "html": true })
"markdownItFactory": getMarkdownItFactory([])
};
lintSync(options);
});
Expand Down

0 comments on commit dc2a277

Please sign in to comment.