Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
eslint-plugin-markdown
Advanced tools
The eslint-plugin-markdown package allows you to lint JavaScript code blocks within Markdown files using ESLint. This is particularly useful for ensuring that code snippets in documentation are up to standard and free of errors.
Linting JavaScript Code Blocks in Markdown
This configuration allows ESLint to process JavaScript code blocks within Markdown files. By specifying the `markdown/markdown` processor, ESLint will recognize and lint the JavaScript code embedded in Markdown.
```json
{
"overrides": [
{
"files": ["**/*.md"],
"processor": "markdown/markdown"
}
]
}
```
Custom ESLint Rules for Markdown
This configuration applies custom ESLint rules specifically to JavaScript code blocks within Markdown files. In this example, the `no-console` rule is enforced, which will trigger an error if `console` statements are found in the code blocks.
```json
{
"overrides": [
{
"files": ["**/*.md/*.js"],
"rules": {
"no-console": "error"
}
}
]
}
```
Linting Specific Code Blocks
This configuration allows you to lint only specific code blocks within a Markdown file. By defining the `start` and `end` markers, you can control which parts of the Markdown file are subject to linting.
```json
{
"overrides": [
{
"files": ["**/*.md"],
"processor": "markdown/markdown",
"settings": {
"markdown/code-blocks": [
{
"language": "js",
"start": "<!-- eslint-enable -->",
"end": "<!-- eslint-disable -->"
}
]
}
}
]
}
```
remark-lint is a plugin for remark, a Markdown processor powered by plugins. It provides a set of rules to lint Markdown files, ensuring consistency and quality. Unlike eslint-plugin-markdown, which focuses on linting JavaScript code within Markdown, remark-lint is designed to lint the Markdown content itself.
markdownlint is a Node.js style checker and lint tool for Markdown files. It provides a set of rules to enforce consistent Markdown style and formatting. While eslint-plugin-markdown focuses on JavaScript code within Markdown, markdownlint is dedicated to the Markdown syntax and structure.
markdown-it is a Markdown parser that can be extended with plugins to add custom functionality. While it is not a linter, it can be used in conjunction with other tools to process and validate Markdown content. It is more flexible and general-purpose compared to eslint-plugin-markdown, which is specifically for linting JavaScript code in Markdown.
An ESLint plugin to lint JavaScript in Markdown.
Supported extensions are .markdown
, .mdown
, .mkdn
, and .md
.
Install the plugin:
npm install --save-dev eslint eslint-plugin-markdown
Add it to your .eslintrc
:
{
"plugins": [
"markdown"
]
}
Run ESLint on .md
files:
eslint --ext md .
It will lint js
, javascript
, jsx
, or node
fenced code blocks in your Markdown documents:
```js
// This gets linted
var answer = 6 * 7;
console.log(answer);
```
```JavaScript
// This also gets linted
/* eslint quotes: [2, "double"] */
function hello() {
console.log("Hello, world!");
}
hello();
```
```jsx
// This gets linted too
var div = <div className="jsx"></div>;
```
```node
// And this
console.log(process.version);
```
Blocks that don't specify either js
, javascript
, jsx
, or node
syntax are ignored:
```
This is plain text and doesn't get linted.
```
```python
print("This doesn't get linted either.")
```
The processor will convert HTML comments immediately preceding a code block into JavaScript block comments and insert them at the beginning of the source code that it passes to ESLint. This permits configuring ESLint via configuration comments while keeping the configuration comments themselves hidden when the markdown is rendered. Comment bodies are passed through unmodified, so the plugin supports any configuration comments supported by ESLint itself.
This example enables the browser
environment, disables the no-alert
rule, and configures the quotes
rule to prefer single quotes:
<!-- eslint-env browser -->
<!-- eslint-disable no-alert -->
<!-- eslint quotes: ["error", "single"] -->
```js
alert('Hello, world!');
```
Each code block in a file is linted separately, so configuration comments apply only to the code block that immediately follows.
Assuming `no-alert` is enabled in `.eslintrc`, the first code block will have no error from `no-alert`:
<!-- eslint-env browser -->
<!-- eslint-disable no-alert -->
```js
alert("Hello, world!");
```
But the next code block will have an error from `no-alert`:
<!-- eslint-env browser -->
```js
alert("Hello, world!");
```
Sometimes it can be useful to have code blocks marked with js
even though they don't contain valid JavaScript syntax, such as commented JSON blobs that need js
syntax highlighting. Standard eslint-disable
comments only silence rule reporting, but ESLint still reports any syntax errors it finds. In cases where a code block should not even be parsed, insert a non-standard <!-- eslint-skip -->
comment before the block, and this plugin will hide the following block from ESLint. Neither rule nor syntax errors will be reported.
There are comments in this JSON, so we use `js` syntax for better
highlighting. Skip the block to prevent warnings about invalid syntax.
<!-- eslint-skip -->
```js
{
// This code block is hidden from ESLint.
"hello": "world"
}
```
```js
console.log("This code block is linted normally.");
```
This plugin can attempt to fix some of the issues automatically using fix
ESLint option. This option instructs ESLint to try to fix as many issues as possible. To enable this option you can add --fix
to your ESLint call, for example:
eslint --fix --ext md .
Since code blocks are not files themselves but embedded inside a Markdown document, some rules do not apply to Markdown code blocks, and messages from these rules are automatically suppressed:
eol-last
unicode-bom
Given that code snippets often lack full context, and adding full context through configuration comments may be too cumbersome to apply for each snippet, one may wish to instead set defaults for all one's JavaScript snippets in a manner that applies to all Markdown files within your project (or a specific directory).
ESLint allows a configuration property overrides
which has a files
property which accepts a
glob pattern, allowing you to designate files (such as all md
files) whose rules will
be overridden.
The following example shows the disabling of a few commonly problematic rules
for code snippets. It also points to the fact that some rules
(e.g., padded-blocks
) may be more appealing for disabling given that
one may wish for documentation to be more liberal in providing padding for
readability.
// .eslintrc.json
{
// ...
"overrides": [{
"files": ["**/*.md"],
"rules": {
"no-undef": "off",
"no-unused-vars": "off",
"no-console": "off",
"padded-blocks": "off"
}
}]
}
strict
The strict
rule is technically satisfiable inside of Markdown code blocks, but writing a "use strict"
directive at the top of every code block is tedious and distracting. We recommend a glob pattern for .md
files to disable strict
and enable the impliedStrict
parser option so the code blocks still parse in strict mode:
// .eslintrc.json
{
// ...
"overrides": [{
"files": ["**/*.md"],
"parserOptions": {
"ecmaFeatures": {
"impliedStrict": true
}
},
"rules": {
"strict": "off"
}
}]
}
The linter-eslint package allows for linting within the Atom IDE.
In order to see eslint-plugin-markdown
work its magic within Markdown code
blocks in your Atom editor, you can go to linter-eslint
's settings and
within "List of scopes to run ESLint on...", add the cursor scope "source.gfm".
However, this reports a problem when viewing Markdown which does not have
configuration, so you may wish to use the cursor scope "source.embedded.js",
but note that eslint-plugin-markdown
configuration comments and skip
directives won't work in this context.
$ git clone https://github.com/eslint/eslint-plugin-markdown.git
$ cd eslint-plugin-markdown
$ npm install
$ npm test
This project follows the ESLint contribution guidelines.
FAQs
An ESLint plugin to lint JavaScript in Markdown code fences.
The npm package eslint-plugin-markdown receives a total of 439,386 weekly downloads. As such, eslint-plugin-markdown popularity was classified as popular.
We found that eslint-plugin-markdown demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.