diff --git a/doc/CustomRules.md b/doc/CustomRules.md index 94ab91f3..226aceb5 100644 --- a/doc/CustomRules.md +++ b/doc/CustomRules.md @@ -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 @@ -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 @@ -62,7 +63,7 @@ A rule is implemented as an `Object` with four required properties: ## Params -Linting the Markdown document: +The Markdown document: ```markdown # Title @@ -70,7 +71,7 @@ Linting the Markdown document: Text *text* text. ``` -Would create a `params` object like: +Yields the `params` object: ```json { diff --git a/test/markdownlint-test.js b/test/markdownlint-test.js index ceb39944..ad99ab83 100644 --- a/test/markdownlint-test.js +++ b/test/markdownlint-test.js @@ -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"); @@ -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); [ diff --git a/test/rules/package.json b/test/rules/npm/package.json similarity index 54% rename from test/rules/package.json rename to test/rules/npm/package.json index d486389d..bbbf5eef 100644 --- a/test/rules/package.json +++ b/test/rules/npm/package.json @@ -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 } diff --git a/test/rules/npm/sample-rule.js b/test/rules/npm/sample-rule.js new file mode 100644 index 00000000..624960f1 --- /dev/null +++ b/test/rules/npm/sample-rule.js @@ -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" + }); + } + }); + } +};