Add simple/concrete example of custom rule package for publishing (fixes #133).

This commit is contained in:
David Anson 2018-07-20 22:31:41 -07:00
parent 183d9c5cb1
commit 23d5be6015
4 changed files with 49 additions and 9 deletions

View file

@ -1,7 +1,7 @@
# Custom Rules # Custom Rules
In addition to its built-in rules, `markdownlint` lets you enhance the linting experience by passing a list of custom rules using the [`options.customRules` property](../README.md#optionscustomrules). In addition to its built-in rules, `markdownlint` lets you enhance the linting experience by passing a list of custom rules using the [`options.customRules` property](../README.md#optionscustomrules).
Custom rules can do everything the built-in rules can and are defined inline or imported from another package ([keyword `markdownlint-rules` on npm](https://www.npmjs.com/search?q=keywords:markdownlint-rules)). Custom rules can do everything the built-in rules can and are defined inline or imported from another package ([keyword `markdownlint-rule` on npm](https://www.npmjs.com/search?q=keywords:markdownlint-rule)).
Custom rules can be disabled, enabled, and customized using the same syntax as built-in rules. Custom rules can be disabled, enabled, and customized using the same syntax as built-in rules.
## Authoring ## Authoring
@ -52,8 +52,9 @@ A rule is implemented as an `Object` with four required properties:
## Examples ## Examples
- [Simple rules used by the project's test cases](../test/rules) - [Simple rules used by the project's test cases](../test/rules)
- Includes [sample package configuration for npm](../test/rules/package.json)
- [Code for all `markdownlint` built-in rules](../lib) - [Code for all `markdownlint` built-in rules](../lib)
- [Package configuration for publishing to npm](../test/rules/npm)
- Each package should export a single rule object
## References ## References
@ -62,7 +63,7 @@ A rule is implemented as an `Object` with four required properties:
## Params ## Params
Linting the Markdown document: The Markdown document:
```markdown ```markdown
# Title # Title
@ -70,7 +71,7 @@ Linting the Markdown document:
Text *text* text. Text *text* text.
``` ```
Would create a `params` object like: Yields the `params` object:
```json ```json
{ {

View file

@ -7,7 +7,7 @@ const tv4 = require("tv4");
const markdownlint = require("../lib/markdownlint"); const markdownlint = require("../lib/markdownlint");
const shared = require("../lib/shared"); const shared = require("../lib/shared");
const rules = require("../lib/rules"); const rules = require("../lib/rules");
const customRules = require("./rules"); const customRules = require("./rules/rules.js");
const defaultConfig = require("./markdownlint-test-default-config.json"); const defaultConfig = require("./markdownlint-test-default-config.json");
const configSchema = require("../schema/markdownlint-config-schema.json"); const configSchema = require("../schema/markdownlint-config-schema.json");
@ -1984,6 +1984,26 @@ module.exports.customRulesConfig = function customRulesConfig(test) {
}); });
}; };
module.exports.customRulesNpmPackage = function customRulesNpmPackage(test) {
test.expect(2);
const options = {
"customRules": [ require("./rules/npm") ],
"strings": {
"string": "# Text\n\n---\n\nText"
},
"resultVersion": 0
};
markdownlint(options, function callback(err, actualResult) {
test.ifError(err);
const expectedResult = {};
expectedResult.string = {
"sample-rule": [ 3 ]
};
test.deepEqual(actualResult, expectedResult, "Undetected issues.");
test.done();
});
};
module.exports.customRulesBadProperty = function customRulesBadProperty(test) { module.exports.customRulesBadProperty = function customRulesBadProperty(test) {
test.expect(76); test.expect(76);
[ [

View file

@ -1,13 +1,13 @@
{ {
"name": "markdownlint-rules-test", "name": "markdownlint-rule-sample",
"version": "0.0.1", "version": "0.0.1",
"description": "Package of markdownlint custom rules used for testing", "description": "Package for markdownlint custom rule sample",
"main": "rules.js", "main": "sample-rule.js",
"author": "David Anson (https://dlaa.me/)", "author": "David Anson (https://dlaa.me/)",
"homepage": "https://github.com/DavidAnson/markdownlint", "homepage": "https://github.com/DavidAnson/markdownlint",
"license": "MIT", "license": "MIT",
"keywords": [ "keywords": [
"markdownlint-rules" "markdownlint-rule"
], ],
"private": true "private": true
} }

View file

@ -0,0 +1,19 @@
// @ts-check
"use strict";
module.exports = {
"names": [ "sample-rule" ],
"description": "Sample rule",
"tags": [ "sample" ],
"function": function rule(params, onError) {
params.tokens.forEach((token) => {
if (token.type === "hr") {
onError({
"lineNumber": token.lineNumber,
"detail": "Sample error for hr"
});
}
});
}
};