Include file/string name in params object for custom rules (fixes #119).

This commit is contained in:
David Anson 2018-05-25 17:28:56 -07:00
parent 8c87a20677
commit 1c816897c1
3 changed files with 53 additions and 5 deletions

View file

@ -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",

View file

@ -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,

View file

@ -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({