mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Include file/string name in params object for custom rules (fixes #119).
This commit is contained in:
parent
8c87a20677
commit
1c816897c1
3 changed files with 53 additions and 5 deletions
|
@ -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.
|
- `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:
|
- `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:
|
- `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.
|
- `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.
|
- `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`).
|
- `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
|
```json
|
||||||
{
|
{
|
||||||
|
"name": "doc/example.md",
|
||||||
"tokens": [
|
"tokens": [
|
||||||
{
|
{
|
||||||
"type": "heading_open",
|
"type": "heading_open",
|
||||||
|
|
|
@ -283,7 +283,7 @@ function uniqueFilterForSortedErrors(value, index, array) {
|
||||||
|
|
||||||
// Lints a single string
|
// Lints a single string
|
||||||
function lintContent(
|
function lintContent(
|
||||||
ruleList, content, config, frontMatter, noInlineConfig, resultVersion,
|
ruleList, name, content, config, frontMatter, noInlineConfig, resultVersion,
|
||||||
callback) {
|
callback) {
|
||||||
// Remove UTF-8 byte order marker (if present)
|
// Remove UTF-8 byte order marker (if present)
|
||||||
content = content.replace(/^\ufeff/, "");
|
content = content.replace(/^\ufeff/, "");
|
||||||
|
@ -304,9 +304,10 @@ function lintContent(
|
||||||
effectiveConfig, aliasToRuleNames);
|
effectiveConfig, aliasToRuleNames);
|
||||||
// Create parameters for rules
|
// Create parameters for rules
|
||||||
const params = {
|
const params = {
|
||||||
"tokens": tokens,
|
name,
|
||||||
"lines": lines,
|
tokens,
|
||||||
"frontMatterLines": frontMatterLines
|
lines,
|
||||||
|
frontMatterLines
|
||||||
};
|
};
|
||||||
shared.makeTokenCache(params);
|
shared.makeTokenCache(params);
|
||||||
// Function to run for each rule
|
// Function to run for each rule
|
||||||
|
@ -411,7 +412,7 @@ function lintFile(
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
lintContent(ruleList, content, config, frontMatter, noInlineConfig,
|
lintContent(ruleList, file, content, config, frontMatter, noInlineConfig,
|
||||||
resultVersion, callback);
|
resultVersion, callback);
|
||||||
}
|
}
|
||||||
// Make a/synchronous call to read file
|
// Make a/synchronous call to read file
|
||||||
|
@ -465,6 +466,7 @@ function lintInput(options, synchronous, callback) {
|
||||||
if ((item = stringsKeys.shift())) {
|
if ((item = stringsKeys.shift())) {
|
||||||
lintContent(
|
lintContent(
|
||||||
ruleList,
|
ruleList,
|
||||||
|
item,
|
||||||
strings[item] || "",
|
strings[item] || "",
|
||||||
config,
|
config,
|
||||||
frontMatter,
|
frontMatter,
|
||||||
|
|
|
@ -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) {
|
module.exports.customRulesDoc = function customRulesDoc(test) {
|
||||||
test.expect(2);
|
test.expect(2);
|
||||||
markdownlint({
|
markdownlint({
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue