Skip to content

Commit

Permalink
Add simple/concrete example of custom rule package for publishing (fixes
Browse files Browse the repository at this point in the history
 #133).
  • Loading branch information
DavidAnson committed Jul 21, 2018
1 parent 183d9c5 commit 23d5be6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
9 changes: 5 additions & 4 deletions doc/CustomRules.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# 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).
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.

## Authoring
Expand Down Expand Up @@ -52,8 +52,9 @@ A rule is implemented as an `Object` with four required properties:
## Examples

- [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)
- [Package configuration for publishing to npm](../test/rules/npm)
- Each package should export a single rule object

## References

Expand All @@ -62,15 +63,15 @@ A rule is implemented as an `Object` with four required properties:

## Params

Linting the Markdown document:
The Markdown document:

```markdown
# Title

Text *text* text.
```

Would create a `params` object like:
Yields the `params` object:

```json
{
Expand Down
22 changes: 21 additions & 1 deletion test/markdownlint-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const tv4 = require("tv4");
const markdownlint = require("../lib/markdownlint");
const shared = require("../lib/shared");
const rules = require("../lib/rules");
const customRules = require("./rules");
const customRules = require("./rules/rules.js");
const defaultConfig = require("./markdownlint-test-default-config.json");
const configSchema = require("../schema/markdownlint-config-schema.json");

Expand Down Expand Up @@ -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) {
test.expect(76);
[
Expand Down
8 changes: 4 additions & 4 deletions test/rules/package.json → test/rules/npm/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "markdownlint-rules-test",
"name": "markdownlint-rule-sample",
"version": "0.0.1",
"description": "Package of markdownlint custom rules used for testing",
"main": "rules.js",
"description": "Package for markdownlint custom rule sample",
"main": "sample-rule.js",
"author": "David Anson (https://dlaa.me/)",
"homepage": "https://github.com/DavidAnson/markdownlint",
"license": "MIT",
"keywords": [
"markdownlint-rules"
"markdownlint-rule"
],
"private": true
}
19 changes: 19 additions & 0 deletions test/rules/npm/sample-rule.js
Original file line number Diff line number Diff line change
@@ -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"
});
}
});
}
};

0 comments on commit 23d5be6

Please sign in to comment.