diff --git a/demo/markdownlint-browser.js b/demo/markdownlint-browser.js index d1eb6438..cab12d80 100644 --- a/demo/markdownlint-browser.js +++ b/demo/markdownlint-browser.js @@ -1657,7 +1657,7 @@ module.exports = { const path = __webpack_require__(/*! node:path */ "?9a52"); const { promisify } = __webpack_require__(/*! node:util */ "?39e5"); const micromark = __webpack_require__(/*! ../helpers/micromark-parse.cjs */ "../helpers/micromark-parse.cjs"); -// const { deprecatedRuleNames } = require("./constants"); +const { version } = __webpack_require__(/*! ./constants */ "../lib/constants.js"); const rules = __webpack_require__(/*! ./rules */ "../lib/rules.js"); const helpers = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"); const cache = __webpack_require__(/*! ./cache */ "../lib/cache.js"); @@ -2177,6 +2177,7 @@ function lintContent( const parsersNone = Object.freeze({}); const paramsBase = { name, + version, "lines": Object.freeze(lines), "frontMatterLines": Object.freeze(frontMatterLines) }; @@ -2861,7 +2862,7 @@ function readConfigSync(file, parsers, fs) { * @returns {string} SemVer string. */ function getVersion() { - return (__webpack_require__(/*! ./constants */ "../lib/constants.js").version); + return version; } // Export a/synchronous/Promise APIs @@ -2898,6 +2899,7 @@ module.exports = markdownlint; * @property {readonly string[]} lines File/string lines. * @property {readonly string[]} frontMatterLines Front matter lines. * @property {RuleConfiguration} config Rule configuration. + * @property {string} version Version of the markdownlint library. */ /* eslint-enable jsdoc/valid-types */ diff --git a/doc/CustomRules.md b/doc/CustomRules.md index 36616066..3921f518 100644 --- a/doc/CustomRules.md +++ b/doc/CustomRules.md @@ -85,6 +85,7 @@ A rule is implemented as an `Object`: front matter (not present in `lines`). - `config` is an `Object` corresponding to the rule's entry in `options.config` (if present). + - `version` is a `String` that corresponds to the version of `markdownlint` - `onError` is a function that takes a single `Object` parameter with one required and four optional properties: - `lineNumber` is a required `Number` specifying the 1-based line number of diff --git a/example/typescript/type-check.ts b/example/typescript/type-check.ts index f99c7f7f..b33351da 100644 --- a/example/typescript/type-check.ts +++ b/example/typescript/type-check.ts @@ -136,7 +136,8 @@ const testRule: markdownlint.Rule = { "frontMatterLines": [ "three" ], - "config": options.config + "config": options.config, + "version": "1.2.3" }; assert(ruleParams); let ruleOnErrorInfo: markdownlint.RuleOnErrorInfo; diff --git a/lib/markdownlint.d.ts b/lib/markdownlint.d.ts index f3c98fe4..f7bbb380 100644 --- a/lib/markdownlint.d.ts +++ b/lib/markdownlint.d.ts @@ -77,6 +77,10 @@ type RuleParams = { * Rule configuration. */ config: RuleConfiguration; + /** + * Version of the markdownlint library. + */ + version: string; }; /** * Markdown parser data. diff --git a/lib/markdownlint.js b/lib/markdownlint.js index ff51c07f..cfdf50b8 100644 --- a/lib/markdownlint.js +++ b/lib/markdownlint.js @@ -5,7 +5,7 @@ const path = require("node:path"); const { promisify } = require("node:util"); const micromark = require("../helpers/micromark-parse.cjs"); -// const { deprecatedRuleNames } = require("./constants"); +const { version } = require("./constants"); const rules = require("./rules"); const helpers = require("../helpers"); const cache = require("./cache"); @@ -525,6 +525,7 @@ function lintContent( const parsersNone = Object.freeze({}); const paramsBase = { name, + version, "lines": Object.freeze(lines), "frontMatterLines": Object.freeze(frontMatterLines) }; @@ -1209,7 +1210,7 @@ function readConfigSync(file, parsers, fs) { * @returns {string} SemVer string. */ function getVersion() { - return require("./constants").version; + return version; } // Export a/synchronous/Promise APIs @@ -1246,6 +1247,7 @@ module.exports = markdownlint; * @property {readonly string[]} lines File/string lines. * @property {readonly string[]} frontMatterLines Front matter lines. * @property {RuleConfiguration} config Rule configuration. + * @property {string} version Version of the markdownlint library. */ /* eslint-enable jsdoc/valid-types */ diff --git a/test/markdownlint-test-custom-rules.js b/test/markdownlint-test-custom-rules.js index f42b3321..8f4a8a20 100644 --- a/test/markdownlint-test-custom-rules.js +++ b/test/markdownlint-test-custom-rules.js @@ -1338,6 +1338,30 @@ test("customRulesOnErrorInvalidHandledSync", (t) => { t.deepEqual(actualResult, expectedResult, "Undetected issues."); }); +test("customRulesVersion", (t) => new Promise((resolve) => { + t.plan(2); + // eslint-disable-next-line jsdoc/valid-types + /** @type import("../lib/markdownlint").Options */ + const options = { + "customRules": [ + { + "names": [ "name" ], + "description": "description", + "tags": [ "tag" ], + "parser": "none", + "function": function stringName(params) { + t.is(params.version, version, "Incorrect version"); + } + } + ], + "files": "doc/CustomRules.md" + }; + markdownlint(options, function callback(err) { + t.falsy(err); + resolve(); + }); +})); + test("customRulesFileName", (t) => new Promise((resolve) => { t.plan(2); // eslint-disable-next-line jsdoc/valid-types