Gathering detailed insights and metrics for markdownlint-micromark
Gathering detailed insights and metrics for markdownlint-micromark
Gathering detailed insights and metrics for markdownlint-micromark
Gathering detailed insights and metrics for markdownlint-micromark
markdownlint
A Node.js style checker and lint tool for Markdown/CommonMark files.
micromark-util-symbol
micromark utility with symbols
micromark-factory-destination
micromark factory to parse destinations (found in resources, definitions)
micromark
small commonmark compliant markdown parser with positional info and concrete tokens
A Node.js style checker and lint tool for Markdown/CommonMark files.
npm install markdownlint-micromark
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
4,841 Stars
1,838 Commits
738 Forks
65 Watching
4 Branches
51 Contributors
Updated on 27 Nov 2024
JavaScript (88.51%)
TypeScript (11.49%)
Cumulative downloads
Total Downloads
Last day
-10.8%
136,757
Compared to previous day
Last week
-0.4%
691,067
Compared to previous week
Last month
32.8%
2,859,435
Compared to previous month
Last year
248.4%
22,340,093
Compared to previous year
A Node.js style checker and lint tool for Markdown/CommonMark files.
1npm install markdownlint --save-dev
The Markdown markup language is designed to be easy to read, write, and understand. It succeeds - and its flexibility is both a benefit and a drawback. Many styles are possible, so formatting can be inconsistent; some constructs don't work well in all parsers and should be avoided.
markdownlint
is a static analysis tool for
Node.js with a library of rules to enforce standards and consistency
for Markdown files. It was inspired by - and heavily influenced by - Mark
Harrison's markdownlint for Ruby. The initial rules, rule
documentation, and test cases came from that project.
markdownlint
uses the micromark
parser and honors the
CommonMark specification for Markdown. It additionally supports
popular GitHub Flavored Markdown (GFM) syntax like autolinks and tables
as well as directives, footnotes, and math syntax - all implemented by
micromark
extensions.
The following specifications are considered authoritative in cases of ambiguity:
markdownlint
demo, an interactive, in-browser
playground for learning and exploring.
See Rules.md for more details.
In addition to built-in rules, custom rules can be used to address
project-specific requirements. To find community-developed rules use
keyword markdownlint-rule
on npm.
To implement your own rules, refer to CustomRules.md.
Tags group related rules and can be used to enable/disable multiple rules at once.
accessibility
- MD045
atx
- MD018
, MD019
atx_closed
- MD020
, MD021
blank_lines
- MD012
, MD022
, MD031
, MD032
, MD047
blockquote
- MD027
, MD028
bullet
- MD004
, MD005
, MD007
, MD032
code
- MD014
, MD031
, MD038
, MD040
, MD046
, MD048
emphasis
- MD036
, MD037
, MD049
, MD050
hard_tab
- MD010
headings
- MD001
, MD003
, MD018
, MD019
, MD020
, MD021
,
MD022
, MD023
, MD024
, MD025
, MD026
, MD036
, MD041
, MD043
hr
- MD035
html
- MD033
images
- MD045
, MD052
, MD053
, MD054
indentation
- MD005
, MD007
, MD027
language
- MD040
line_length
- MD013
links
- MD011
, MD034
, MD039
, MD042
, MD051
, MD052
, MD053
,
MD054
ol
- MD029
, MD030
, MD032
spaces
- MD018
, MD019
, MD020
, MD021
, MD023
spelling
- MD044
table
- MD055
, MD056
, MD058
ul
- MD004
, MD005
, MD007
, MD030
, MD032
url
- MD034
whitespace
- MD009
, MD010
, MD012
, MD027
, MD028
, MD030
,
MD037
, MD038
, MD039
Text passed to markdownlint
is parsed as Markdown, analyzed, and any
issues reported. Two kinds of text are ignored by most rules:
options.frontMatter
below)Rules can be enabled, disabled, and configured via options.config
(described below) to define the expected behavior for a set of inputs.
To enable or disable rules at a particular location within a file, add
one of these markers to the appropriate place (HTML comments don't
appear in the final markup):
<!-- markdownlint-disable -->
<!-- markdownlint-enable -->
<!-- markdownlint-disable-line -->
<!-- markdownlint-disable-next-line -->
<!-- markdownlint-disable MD001 MD005 -->
<!-- markdownlint-enable MD001 MD005 -->
<!-- markdownlint-disable-line MD001 MD005 -->
<!-- markdownlint-disable-next-line MD001 MD005 -->
<!-- markdownlint-capture -->
<!-- markdownlint-restore -->
For example:
1<!-- markdownlint-disable-next-line no-space-in-emphasis --> 2space * in * emphasis
Or:
1space * in * emphasis <!-- markdownlint-disable-line no-space-in-emphasis -->
Or:
1<!-- markdownlint-disable no-space-in-emphasis --> 2space * in * emphasis 3<!-- markdownlint-enable no-space-in-emphasis -->
To temporarily disable rule(s), then restore the former configuration:
1<!-- markdownlint-capture --> 2<!-- markdownlint-disable --> 3any violations you want 4<!-- markdownlint-restore -->
The initial configuration is captured by default (as if every document
began with <!-- markdownlint-capture -->
), so the pattern above can
be expressed more simply:
1<!-- markdownlint-disable --> 2any violations you want 3<!-- markdownlint-restore -->
Changes take effect starting with the line a comment is on, so the following has no effect:
1space * in * emphasis <!-- markdownlint-disable --> <!-- markdownlint-enable -->
To apply changes to an entire file regardless of where the comment is located, the following syntax is supported:
<!-- markdownlint-disable-file -->
<!-- markdownlint-enable-file -->
<!-- markdownlint-disable-file MD001 -->
<!-- markdownlint-enable-file MD001 -->
This can be used to "hide" markdownlint
comments at the bottom of a file.
In cases where it is desirable to change the configuration of one or more rules for a file, the following more advanced syntax is supported:
<!-- markdownlint-configure-file { options.config JSON } -->
For example:
1<!-- markdownlint-configure-file { "hr-style": { "style": "---" } } -->
or
1<!-- markdownlint-configure-file
2{
3 "hr-style": {
4 "style": "---"
5 },
6 "no-trailing-spaces": false
7}
8-->
These changes apply to the entire file regardless of where the comment is
located. Multiple such comments (if present) are applied top-to-bottom. By
default, content of markdownlint-configure-file
is assumed to be JSON, but
options.configParsers
can be used to support
alternate formats.
Standard asynchronous API:
1/** 2 * Lint specified Markdown files. 3 * 4 * @param {Options} options Configuration options. 5 * @param {LintCallback} callback Callback (err, result) function. 6 * @returns {void} 7 */ 8function markdownlint(options, callback) { ... }
Synchronous API (for build scripts, etc.):
1/** 2 * Lint specified Markdown files synchronously. 3 * 4 * @param {Options} options Configuration options. 5 * @returns {LintResults} Results object. 6 */ 7function markdownlint.sync(options) { ... }
Promise API (in the promises
namespace like Node.js's
fs
Promises API):
1/** 2 * Lint specified Markdown files. 3 * 4 * @param {Options} options Configuration options. 5 * @returns {Promise<LintResults>} Results object. 6 */ 7function markdownlint(options) { ... }
Type: Object
Configures the function. All properties are optional, but at least one
of files
or strings
should be set to provide input.
Type: Object
mapping String
to Boolean | Object
Configures the rules to use.
Object keys are rule names/aliases; object values are the rule's configuration.
The value false
disables a rule, true
enables its default configuration,
and passing an object value customizes that rule. Setting the special default
rule to true
or false
includes/excludes all rules by default. In the absence
of a configuration object, all rules are enabled. Enabling or disabling a tag
name (ex: whitespace
) affects all rules having that tag.
The default
rule is applied first, then keys are processed in order from top
to bottom with later values overriding earlier ones. Keys (including rule names,
aliases, tags, and default
) are not case-sensitive.
Example:
1{ 2 "default": true, 3 "MD003": { "style": "atx_closed" }, 4 "MD007": { "indent": 4 }, 5 "no-hard-tabs": false, 6 "whitespace": false 7}
See .markdownlint.jsonc and/or .markdownlint.yaml for an example configuration object with all properties set to the default value.
Sets of rules (known as a "style") can be stored separately and loaded as JSON.
Example of referencing a built-in style from JavaScript:
1const options = { 2 "files": [ "..." ], 3 "config": require("style/relaxed.json") 4};
Example doing so from .markdownlint.json
via extends
(more on this below):
1{ 2 "extends": "markdownlint/style/relaxed" 3}
See the style directory for more samples.
See markdownlint-config-schema.json
for the JSON Schema of the options.config
object.
See ValidatingConfiguration.md for ways to use the JSON Schema to validate configuration.
For more advanced scenarios, styles can reference and extend other styles.
The readConfig
and readConfigSync
functions can be used to read such
styles.
For example, assuming a base.json
configuration file:
1{ 2 "default": true 3}
And a custom.json
configuration file:
1{ 2 "extends": "base.json", 3 "line-length": false 4}
Then code like the following:
1const options = { 2 "config": markdownlint.readConfigSync("./custom.json") 3};
Merges custom.json
and base.json
and is equivalent to:
1const options = { 2 "config": { 3 "default": true, 4 "line-length": false 5 } 6};
Type: Optional Array
of Function
taking (String
) and returning Object
Array of functions to parse the content of markdownlint-configure-file
blocks.
As shown in the Configuration section, inline comments can be
used to customize the configuration object for a document. By
default, the JSON.parse
built-in is used, but custom parsers can be specified.
Content is passed to each parser function until one returns a value (vs.
throwing an exception). As such, strict parsers should come before flexible
ones.
For example:
1[ JSON.parse, require("toml").parse, require("js-yaml").load ]
Type: Array
of Object
List of custom rules to include with the default rule set for linting.
Each array element should define a rule. Rules are typically exported by another package, but can be defined locally.
Example:
1const extraRules = require("extraRules"); 2const options = { 3 "customRules": [ extraRules.one, extraRules.two ] 4};
See CustomRules.md for details about authoring custom rules.
Type: Array
of String
List of files to lint.
Each array element should be a single file (via relative or absolute path); globbing is the caller's responsibility.
Example: [ "one.md", "dir/two.md" ]
Type: RegExp
Matches any front matter found at the beginning of a file.
Some Markdown content begins with metadata; the default RegExp
for
this option ignores common forms of "front matter". To match differently,
specify a custom RegExp
or use the value null
to disable the feature.
The default value:
1/((^---[^\S\r\n\u2028\u2029]*$[\s\S]+?^---\s*)|(^\+\+\+[^\S\r\n\u2028\u2029]*$[\s\S]+?^(\+\+\+|\.\.\.)\s*)|(^\{[^\S\r\n\u2028\u2029]*$[\s\S]+?^\}\s*))(\r\n|\r|\n|$)/m
Ignores YAML, TOML, and JSON front matter such as:
1--- 2layout: post 3title: Title 4---
Note: Matches must occur at the start of the file.
Type: Object
implementing the file system API
In advanced scenarios, it may be desirable to bypass the default file system
API. If a custom file system implementation is provided, markdownlint
will use
that instead of invoking require("fs")
.
Note: The only methods called are readFile
and readFileSync
.
Type: Boolean
Catches exceptions thrown during rule processing and reports the problem as a rule violation.
By default, exceptions thrown by rules (or the library itself) are unhandled
and bubble up the stack to the caller in the conventional manner. By setting
handleRuleFailures
to true
, exceptions thrown by failing rules will
be handled by the library and the exception message logged as a rule violation.
This setting can be useful in the presence of (custom) rules that encounter
unexpected syntax and fail. By enabling this option, the linting process
is allowed to continue and report any violations that were found.
Type: Array
of Array
of Function
and plugin parameters
Specifies additional markdown-it
plugins to use when
parsing input. Plugins can be used to support additional syntax and features for
advanced scenarios. Deprecated.
Each item in the top-level Array
should be of the form:
1[ require("markdown-it-plugin"), plugin_param_0, plugin_param_1, ... ]
Note that
markdown-it
plugins are only called when themarkdown-it
parser is invoked. None of the built-in rules use themarkdown-it
parser, somarkdown-it
plugins will only be invoked when one or more custom rules that use themarkdown-it
parser are present.
Type: Boolean
Disables the use of HTML comments like <!-- markdownlint-enable -->
to toggle
rules within the body of Markdown content.
By default, properly-formatted inline comments can be used to create exceptions
for parts of a document. Setting noInlineConfig
to true
ignores all such
comments.
Type: Number
Specifies which version of the result
object to return (see the "Usage"
section below for examples).
Passing a resultVersion
of 0
corresponds to the original, simple format
where each error is identified by rule name and line number. Deprecated
Passing a resultVersion
of 1
corresponds to a detailed format where each
error includes information about the line number, rule name, alias, description,
as well as any additional detail or context that is available. Deprecated
Passing a resultVersion
of 2
corresponds to a detailed format where each
error includes information about the line number, rule names, description, as
well as any additional detail or context that is available. Deprecated
Passing a resultVersion
of 3
corresponds to the detailed version 2
format
with additional information about how to fix automatically-fixable errors. In
this mode, all errors that occur on each line are reported (other versions
report only the first error for each rule). This is the default behavior.
Type: Object
mapping String
to String
Map of identifiers to strings for linting.
When Markdown content is not available as files, it can be passed as
strings. The keys of the strings
object are used to identify each
input value in the result
summary.
Example:
1{ 2 "readme": "# README\n...", 3 "changelog": "# CHANGELOG\n..." 4}
Type: Function
taking (Error
, Object
)
Standard completion callback.
Type: Object
Call result.toString()
for convenience or see below for an example of the
structure of the result
object. Passing the value true
to toString()
uses rule aliases (ex: no-hard-tabs
) instead of names (ex: MD010
).
The options.config
configuration object is simple and can be stored in a file
for readability and easy reuse. The readConfig
and readConfigSync
functions
load configuration settings and support the extends
keyword for referencing
other files (see above).
By default, configuration files are parsed as JSON (and named
.markdownlint.json
). Custom parsers can be provided to handle other formats
like JSONC, YAML, and TOML.
Asynchronous API:
1/** 2 * Read specified configuration file. 3 * 4 * @param {string} file Configuration file name. 5 * @param {ConfigurationParser[] | ReadConfigCallback} parsers Parsing function. 6 * @param {Object} [fs] File system implementation. 7 * @param {ReadConfigCallback} [callback] Callback (err, result) function. 8 * @returns {void} 9 */ 10function readConfig(file, parsers, fs, callback) { ... }
Synchronous API:
1/** 2 * Read specified configuration file synchronously. 3 * 4 * @param {string} file Configuration file name. 5 * @param {ConfigurationParser[]} [parsers] Parsing function(s). 6 * @param {Object} [fs] File system implementation. 7 * @returns {Configuration} Configuration object. 8 */ 9function readConfigSync(file, parsers, fs) { ... }
Promise API (in the promises
namespace like Node.js's
fs
Promises API):
1/** 2 * Read specified configuration file. 3 * 4 * @param {string} file Configuration file name. 5 * @param {ConfigurationParser[]} [parsers] Parsing function(s). 6 * @param {Object} [fs] File system implementation. 7 * @returns {Promise<Configuration>} Configuration object. 8 */ 9function readConfig(file, parsers, fs) { ... }
Type: String
Location of configuration file to read.
The file
is resolved relative to the current working directory. If an
extends
key is present once read, its value will be resolved as a path
relative to file
and loaded recursively. Settings from a file referenced by
extends
are applied first, then those of file
are applied on top (overriding
any of the same keys appearing in the referenced file). If either the file
or
extends
path begins with the ~
directory, it will act as a placeholder for
the home directory.
Type: Optional Array
of Function
taking (String
) and returning Object
Array of functions to parse configuration files.
The contents of a configuration file are passed to each parser function until one of them returns a value (vs. throwing an exception). Consequently, strict parsers should come before flexible parsers.
For example:
1[ JSON.parse, require("toml").parse, require("js-yaml").load ]
Type: Optional Object
implementing the file system API
In advanced scenarios, it may be desirable to bypass the default file system
API. If a custom file system implementation is provided, markdownlint
will use
that instead of invoking require("fs")
.
Note: The only methods called are readFile
, readFileSync
, access
, and
accessSync
.
Type: Function
taking (Error
, Object
)
Standard completion callback.
Type: Object
Configuration object.
Rules that can be fixed automatically include a fixInfo
property which is
outlined in the documentation for custom rules.
To apply fixes consistently, the applyFix
/applyFixes
methods may be used:
1/** 2 * Applies the specified fix to a Markdown content line. 3 * 4 * @param {string} line Line of Markdown content. 5 * @param {RuleOnErrorFixInfo} fixInfo RuleOnErrorFixInfo instance. 6 * @param {string} [lineEnding] Line ending to use. 7 * @returns {string | null} Fixed content or null if deleted. 8 */ 9function applyFix(line, fixInfo, lineEnding = "\n") { ... } 10 11/** 12 * Applies as many of the specified fixes as possible to Markdown content. 13 * 14 * @param {string} input Lines of Markdown content. 15 * @param {RuleOnErrorInfo[]} errors RuleOnErrorInfo instances. 16 * @returns {string} Fixed content. 17 */ 18function applyFixes(input, errors) { ... }
Invoking applyFixes
with the results of a call to lint can be done like so:
1const { "sync": markdownlintSync, applyFixes } = require("markdownlint"); 2 3function fixMarkdownlintViolations(content) { 4 const fixResults = markdownlintSync({ strings: { content } }); 5 return applyFixes(content, fixResults.content); 6}
Invoke markdownlint
and use the result
object's toString
method:
1const markdownlint = require("markdownlint"); 2 3const options = { 4 "files": [ "good.md", "bad.md" ], 5 "strings": { 6 "good.string": "# good.string\n\nThis string passes all rules.", 7 "bad.string": "#bad.string\n\n#This string fails\tsome rules." 8 } 9}; 10 11markdownlint(options, function callback(err, result) { 12 if (!err) { 13 console.log(result.toString()); 14 } 15});
Output:
1bad.string: 3: MD010/no-hard-tabs Hard tabs [Column: 19] 2bad.string: 1: MD018/no-missing-space-atx No space after hash on atx style heading [Context: "#bad.string"] 3bad.string: 3: MD018/no-missing-space-atx No space after hash on atx style heading [Context: "#This string fails some rules."] 4bad.string: 1: MD041/first-line-heading/first-line-h1 First line in a file should be a top-level heading [Context: "#bad.string"] 5bad.md: 3: MD010/no-hard-tabs Hard tabs [Column: 17] 6bad.md: 1: MD018/no-missing-space-atx No space after hash on atx style heading [Context: "#bad.md"] 7bad.md: 3: MD018/no-missing-space-atx No space after hash on atx style heading [Context: "#This file fails some rules."] 8bad.md: 1: MD041/first-line-heading/first-line-h1 First line in a file should be a top-level heading [Context: "#bad.md"]
Or invoke markdownlint.sync
for a synchronous call:
1const result = markdownlint.sync(options); 2console.log(result.toString());
To examine the result
object directly:
1markdownlint(options, function callback(err, result) { 2 if (!err) { 3 console.dir(result, { "colors": true, "depth": null }); 4 } 5});
Output:
1{ 2 "good.md": [], 3 "bad.md": [ 4 { "lineNumber": 3, 5 "ruleNames": [ "MD010", "no-hard-tabs" ], 6 "ruleDescription": "Hard tabs", 7 "ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md010.md", 8 "errorDetail": "Column: 17", 9 "errorContext": null, 10 "errorRange": [ 17, 1 ] }, 11 { "lineNumber": 1, 12 "ruleNames": [ "MD018", "no-missing-space-atx" ], 13 "ruleDescription": "No space after hash on atx style heading", 14 "ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md018.md", 15 "errorDetail": null, 16 "errorContext": "#bad.md", 17 "errorRange": [ 1, 2 ] }, 18 { "lineNumber": 3, 19 "ruleNames": [ "MD018", "no-missing-space-atx" ], 20 "ruleDescription": "No space after hash on atx style heading", 21 "ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md018.md", 22 "errorDetail": null, 23 "errorContext": "#This file fails\tsome rules.", 24 "errorRange": [ 1, 2 ] }, 25 { "lineNumber": 1, 26 "ruleNames": [ "MD041", "first-line-heading", "first-line-h1" ], 27 "ruleDescription": "First line in a file should be a top-level heading", 28 "ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md041.md", 29 "errorDetail": null, 30 "errorContext": "#bad.md", 31 "errorRange": null } 32 ] 33}
Integration with the gulp build system is straightforward:
1const gulp = require("gulp"); 2const through2 = require("through2"); 3const markdownlint = require("markdownlint"); 4 5gulp.task("markdownlint", function task() { 6 return gulp.src("*.md", { "read": false }) 7 .pipe(through2.obj(function obj(file, enc, next) { 8 markdownlint( 9 { "files": [ file.relative ] }, 10 function callback(err, result) { 11 const resultString = (result || "").toString(); 12 if (resultString) { 13 console.log(resultString); 14 } 15 next(err, file); 16 }); 17 })); 18});
Output:
1[00:00:00] Starting 'markdownlint'... 2bad.md: 3: MD010/no-hard-tabs Hard tabs [Column: 17] 3bad.md: 1: MD018/no-missing-space-atx No space after hash on atx style heading [Context: "#bad.md"] 4bad.md: 3: MD018/no-missing-space-atx No space after hash on atx style heading [Context: "#This file fails some rules."] 5bad.md: 1: MD041/first-line-heading/first-line-h1 First line in a file should be a top-level heading [Context: "#bad.md"] 6[00:00:00] Finished 'markdownlint' after 10 ms
Integration with the Grunt build system is similar:
1const markdownlint = require("markdownlint"); 2 3module.exports = function wrapper(grunt) { 4 grunt.initConfig({ 5 "markdownlint": { 6 "example": { 7 "src": [ "*.md" ] 8 } 9 } 10 }); 11 12 grunt.registerMultiTask("markdownlint", function task() { 13 const done = this.async(); 14 markdownlint( 15 { "files": this.filesSrc }, 16 function callback(err, result) { 17 const resultString = err || ((result || "").toString()); 18 if (resultString) { 19 grunt.fail.warn("\n" + resultString + "\n"); 20 } 21 done(!err || !resultString); 22 }); 23 }); 24};
Output:
1Running "markdownlint:example" (markdownlint) task 2Warning: 3bad.md: 3: MD010/no-hard-tabs Hard tabs [Column: 17] 4bad.md: 1: MD018/no-missing-space-atx No space after hash on atx style heading [Context: "#bad.md"] 5bad.md: 3: MD018/no-missing-space-atx No space after hash on atx style heading [Context: "#This file fails some rules."] 6bad.md: 1: MD041/first-line-heading/first-line-h1 First line in a file should be a top-level heading [Context: "#bad.md"] 7 Use --force to continue.
markdownlint
also works in the browser.
Generate normal and minified scripts with:
1npm run build-demo
Then reference markdownlint
and micromark
scripts:
1<script src="demo/micromark-browser.js"></script> 2<script src="demo/micromark-html-browser.js"></script> 3<script src="demo/markdownlint-browser.min.js"></script>
And call it like so:
1const options = { 2 "strings": { 3 "content": "Some Markdown to lint." 4 } 5}; 6const results = window.markdownlint.sync(options).toString();
For ideas how to integrate markdownlint
into your workflow, refer to the
following projects or one of the tools in the Related section:
For more advanced integration scenarios:
See CONTRIBUTING.md for more information.
See ReleaseProcess.md for more information.
See CHANGELOG.md.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
30 commit(s) and 17 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
SAST tool is run on all commits
Details
Reason
dependency not pinned by hash detected -- score normalized to 1
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
Found 0/17 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More