Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Dec 24, 2024
1 parent c767af1 commit a20a50a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 24 deletions.
4 changes: 3 additions & 1 deletion example/typescript/type-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { lint as lintPromise, readConfig as readConfigPromise } from "../../lib/
import { lint as lintSync, readConfig as readConfigSync } from "../../lib/exports-sync.mjs";

import assert from "assert";
// @ts-expect-error TS7016: Could not find a declaration file for module 'markdown-it'.
import markdownIt from "markdown-it";
// @ts-expect-error TS7016: Could not find a declaration file for module 'markdown-it-sub'.
import markdownItSub from "markdown-it-sub";
const markdownlintJsonPath = "../../.markdownlint.json";
Expand Down Expand Up @@ -99,7 +101,7 @@ options = {
"frontMatter": /---/,
"handleRuleFailures": false,
"noInlineConfig": false,
"markdownItFactory": null
"markdownItFactory": () => new markdownIt()
};

assertLintResults(lintSync(options));
Expand Down
4 changes: 2 additions & 2 deletions lib/markdownlint.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,9 @@ export type Rule = {
function: RuleFunction;
};
/**
* ...
* Gets an instance of the markdown-it parser. Any plugins should already have been loaded.
*/
export type MarkdownItFactory = () => any;
export type MarkdownItFactory = () => Function;
/**
* Configuration options.
*/
Expand Down
8 changes: 5 additions & 3 deletions lib/markdownlint.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,9 @@ function lintInput(options, synchronous, callback) {
const resultVersion = (options.resultVersion === undefined) ?
3 :
options.resultVersion;
const markdownItFactory = options.markdownItFactory || (() => { throw new Error("BAD"); });
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."); });
const fs = options.fs || nodeFs;
const aliasToRuleNames = mapAliasToRuleNames(ruleList);
const results = newResults(ruleList);
Expand Down Expand Up @@ -1475,10 +1477,10 @@ export function getVersion() {
*/

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

/**
Expand Down
23 changes: 15 additions & 8 deletions test/markdownlint-test-custom-rules.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ test("customRulesV0", (t) => new Promise((resolve) => {
const options = {
"customRules": customRules.all,
"files": [ customRulesMd ],
"markdownItFactory": () => markdownIt({ "html": true }),
"resultVersion": 0
};
lintAsync(options, function callback(err, actualResult) {
Expand Down Expand Up @@ -93,6 +94,7 @@ test("customRulesV1", (t) => new Promise((resolve) => {
const options = {
"customRules": customRules.all,
"files": [ customRulesMd ],
"markdownItFactory": () => markdownIt({ "html": true }),
"resultVersion": 1
};
lintAsync(options, function callback(err, actualResult) {
Expand Down Expand Up @@ -224,6 +226,7 @@ test("customRulesV2", (t) => new Promise((resolve) => {
const options = {
"customRules": customRules.all,
"files": [ customRulesMd ],
"markdownItFactory": () => markdownIt({ "html": true }),
"resultVersion": 2
};
lintAsync(options, function callback(err, actualResult) {
Expand Down Expand Up @@ -352,6 +355,7 @@ test("customRulesConfig", (t) => new Promise((resolve) => {
},
"letters-e-x": false
},
"markdownItFactory": () => markdownIt({ "html": true }),
"resultVersion": 0
};
lintAsync(options, function callback(err, actualResult) {
Expand Down Expand Up @@ -557,11 +561,10 @@ test("customRulesParserUndefined", (t) => {
}
}
],
"markdownItFactory": () => markdownIt({ "html": true }),
"strings": {
"string": "# Heading\n"
},
"markdownItFactory": () => markdownIt({ "html": true })

}
};
return lintPromise(options).then(() => null);
});
Expand Down Expand Up @@ -610,10 +613,10 @@ test("customRulesParserMarkdownIt", (t) => {
}
}
],
"markdownItFactory": () => markdownIt({ "html": true }),
"strings": {
"string": "# Heading\n"
},
"markdownItFactory": () => markdownIt({ "html": true })
}
};
return lintPromise(options).then(() => null);
});
Expand Down Expand Up @@ -686,6 +689,7 @@ test("customRulesMarkdownItTokensSnapshot", (t) => {
}
}
],
"markdownItFactory": () => markdownIt({ "html": true }),
"noInlineConfig": true
};
return fs
Expand Down Expand Up @@ -1671,7 +1675,8 @@ test("customRulesLintJavaScript", (t) => new Promise((resolve) => {
/** @type {import("markdownlint").Options} */
const options = {
"customRules": customRules.lintJavaScript,
"files": "test/lint-javascript.md"
"files": "test/lint-javascript.md",
"markdownItFactory": () => markdownIt({ "html": true })
};
lintAsync(options, (err, actual) => {
t.falsy(err);
Expand Down Expand Up @@ -1699,7 +1704,8 @@ test("customRulesValidateJson", (t) => new Promise((resolve) => {
/** @type {import("markdownlint").Options} */
const options = {
"customRules": customRules.validateJson,
"files": "test/validate-json.md"
"files": "test/validate-json.md",
"markdownItFactory": () => markdownIt({ "html": true })
};
lintAsync(options, (err, actual) => {
t.falsy(err);
Expand Down Expand Up @@ -1798,7 +1804,8 @@ test("customRulesParamsAreFrozen", (t) => {
"function": assertParamsFrozen
}
],
"files": [ "README.md" ]
"files": [ "README.md" ],
"markdownItFactory": () => markdownIt({ "html": true })
};
return lintPromise(options).then(() => null);
});
Expand Down
27 changes: 17 additions & 10 deletions test/markdownlint-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ const ajvOptions = {
"allowUnionTypes": true
};

/**
* 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.
*/
function getMarkdownItFactory(markdownItPlugins) {
return () => {
const md = markdownIt({ "html": true });
for (const markdownItPlugin of markdownItPlugins) {
// @ts-ignore
md.use(...markdownItPlugin);
}
return md;
};
}

test("simpleAsync", (t) => new Promise((resolve) => {
t.plan(2);
const options = {
Expand Down Expand Up @@ -1104,16 +1121,6 @@ test("someCustomRulesHaveValidUrl", (t) => {
}
});

function getMarkdownItFactory(markdownItPlugins) {
return () => {
const md = markdownIt({ "html": true });
for (const markdownItPlugin of markdownItPlugins) {
// @ts-ignore
md.use(...markdownItPlugin);
}
return md;
}
}
test("markdownItPluginsSingle", (t) => new Promise((resolve) => {
t.plan(4);
lintAsync({
Expand Down

0 comments on commit a20a50a

Please sign in to comment.