diff --git a/doc/CustomRules.md b/doc/CustomRules.md index e5ff9917..3b538d97 100644 --- a/doc/CustomRules.md +++ b/doc/CustomRules.md @@ -38,6 +38,7 @@ A rule is implemented as an `Object` with four required properties: - `tags` is an `Array` of `String` values that groups related rules for easier customization. - `function` is a synchronous `Function` that implements the rule and is passed two parameters: - `params` is an `Object` with properties that describe the content being analyzed: + - `name` is a `String` that identifies the input file/string. - `tokens` is an `Array` of [`markdown-it` `Token` objects](https://markdown-it.github.io/markdown-it/#Token) with added `line` and `lineNumber` properties. - `lines` is an `Array` of `String` values corresponding to the lines of the input file/string. - `frontMatterLines` is an `Array` of `String` values corresponding to any front matter (not present in `lines`). @@ -73,6 +74,7 @@ Would create a `params` object like: ```json { + "name": "doc/example.md", "tokens": [ { "type": "heading_open", diff --git a/lib/markdownlint.js b/lib/markdownlint.js index ccd52521..83b30062 100644 --- a/lib/markdownlint.js +++ b/lib/markdownlint.js @@ -283,7 +283,7 @@ function uniqueFilterForSortedErrors(value, index, array) { // Lints a single string function lintContent( - ruleList, content, config, frontMatter, noInlineConfig, resultVersion, + ruleList, name, content, config, frontMatter, noInlineConfig, resultVersion, callback) { // Remove UTF-8 byte order marker (if present) content = content.replace(/^\ufeff/, ""); @@ -304,9 +304,10 @@ function lintContent( effectiveConfig, aliasToRuleNames); // Create parameters for rules const params = { - "tokens": tokens, - "lines": lines, - "frontMatterLines": frontMatterLines + name, + tokens, + lines, + frontMatterLines }; shared.makeTokenCache(params); // Function to run for each rule @@ -411,7 +412,7 @@ function lintFile( if (err) { return callback(err); } - lintContent(ruleList, content, config, frontMatter, noInlineConfig, + lintContent(ruleList, file, content, config, frontMatter, noInlineConfig, resultVersion, callback); } // Make a/synchronous call to read file @@ -465,6 +466,7 @@ function lintInput(options, synchronous, callback) { if ((item = stringsKeys.shift())) { lintContent( ruleList, + item, strings[item] || "", config, frontMatter, diff --git a/test/markdownlint-test.js b/test/markdownlint-test.js index 0ebe939c..92d67b56 100644 --- a/test/markdownlint-test.js +++ b/test/markdownlint-test.js @@ -2282,6 +2282,50 @@ module.exports.customRulesOnErrorLazy = function customRulesOnErrorLazy(test) { }); }; +module.exports.customRulesFileName = function customRulesFileName(test) { + test.expect(2); + const options = { + "customRules": [ + { + "names": [ "name" ], + "description": "description", + "tags": [ "tag" ], + "function": function stringName(params) { + test.equal(params.name, "doc/CustomRules.md", "Incorrect file name"); + } + } + ], + "files": "doc/CustomRules.md" + }; + markdownlint(options, function callback(err) { + test.ifError(err); + test.done(); + }); +}; + +module.exports.customRulesStringName = function customRulesStringName(test) { + test.expect(2); + const options = { + "customRules": [ + { + "names": [ "name" ], + "description": "description", + "tags": [ "tag" ], + "function": function stringName(params) { + test.equal(params.name, "string", "Incorrect string name"); + } + } + ], + "strings": { + "string": "# Heading" + } + }; + markdownlint(options, function callback(err) { + test.ifError(err); + test.done(); + }); +}; + module.exports.customRulesDoc = function customRulesDoc(test) { test.expect(2); markdownlint({