Skip to content

Commit

Permalink
Disable Prettier for Markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
nzakas committed Aug 21, 2024
1 parent 905f9a7 commit 03ad9ab
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 124 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist
CHANGELOG.md
jsr.json
**/*.md
81 changes: 41 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ npm install --save-dev eslint @eslint/markdown

### Configurations

| **Configuration Name** | **Description** |
| ---------------------- | ---------------------------------------------------------------------------------------------------------- |
| `recommended` | Lints all `.md` files with the recommended rules and assumes [CommonMark](https://commonmark.org/) format. |
| `processor` | Enables extracting code blocks from all `.md` files so code blocks can be individually linted. |
| **Configuration Name** | **Description** |
|---------------|-----------------|
| `recommended` | Lints all `.md` files with the recommended rules and assumes [CommonMark](https://commonmark.org/) format. |
| `processor` | Enables extracting code blocks from all `.md` files so code blocks can be individually linted. |

In your `eslint.config.js` file, import `@eslint/markdown` and include the recommended config to enable the Markdown processor on all `.md` files:

Expand All @@ -37,23 +37,23 @@ In your `eslint.config.js` file, import `@eslint/markdown` and include the recom
import markdown from "@eslint/markdown";

export default [
...markdown.configs.recommended,
...markdown.configs.recommended

// your other configs here
// your other configs here
];
```

### Rules

| **Rule Name** | **Description** |
| ---------------------------------------------------------------- | ------------------------------------------------- |
| [`fenced-code-language`](./docs/rules/fenced-code-language.md) | Enforce fenced code blocks to specify a language. |
| [`heading-increment`](./docs/rules/heading-increment.md) | Enforce heading levels increment by one. |
| **Rule Name** | **Description** |
|---------------|-----------------|
| [`fenced-code-language`](./docs/rules/fenced-code-language.md) | Enforce fenced code blocks to specify a language. |
| [`heading-increment`](./docs/rules/heading-increment.md) | Enforce heading levels increment by one. |
| [`no-duplicate-headings`](./docs/rules/no-duplicate-headings.md) | Disallow duplicate headings in the same document. |
| [`no-empty-links`](./docs/rules/no-empty-links.md) | Disallow empty links. |
| [`no-html`](./docs/rules/no-html.md) | Enforce fenced code blocks to specify a language. |
| [`no-invalid-label-refs`](./docs/rules/no-invalid-label-refs.md) | Disallow invalid label references. |
| [`no-missing-label-refs`](./docs/rules/no-missing-label-refs.md) | Disallow missing label references. |
| [`no-empty-links`](./docs/rules/no-empty-links.md) | Disallow empty links. |
| [`no-html`](./docs/rules/no-html.md) | Enforce fenced code blocks to specify a language. |
| [`no-invalid-label-refs`](./docs/rules/no-invalid-label-refs.md) | Disallow invalid label references. |
| [`no-missing-label-refs`](./docs/rules/no-missing-label-refs.md) | Disallow missing label references. |

**Note:** This plugin does not provide formatting rules. We recommend using a source code formatter such as [Prettier](https://prettier.io) for that purpose.

Expand All @@ -64,24 +64,25 @@ In order to individually configure a rule in your `eslint.config.js` file, impor
import markdown from "@eslint/markdown";

export default [
{
files: ["**/*.md"],
plugins: {
markdown,
},
rules: {
"markdown/no-html": "error",
},
},
{
files: ["**/*.md"],
plugins: {
markdown
},
rules: {
"markdown/no-html": "error"
}
}
];
```

### Languages

| **Language Name** | **Description** |
| ----------------- | ----------------------------------------------------------------------------- |
| `commonmark` | Parse using [CommonMark](https://commonmark.org) Markdown format |
| `gfm` | Parse using [GitHub-Flavored Markdown](https://github.github.com/gfm/) format |
| **Language Name** | **Description** |
|---------------|-----------------|
| `commonmark` | Parse using [CommonMark](https://commonmark.org) Markdown format |
| `gfm` | Parse using [GitHub-Flavored Markdown](https://github.github.com/gfm/) format |


In order to individually configure a language in your `eslint.config.js` file, import `@eslint/markdown` and configure a `language`:

Expand All @@ -90,24 +91,24 @@ In order to individually configure a language in your `eslint.config.js` file, i
import markdown from "@eslint/markdown";

export default [
{
files: ["**/*.md"],
plugins: {
markdown,
},
language: "markdown/gfm",
rules: {
"markdown/no-html": "error",
},
},
{
files: ["**/*.md"],
plugins: {
markdown
},
language: "markdown/gfm",
rules: {
"markdown/no-html": "error"
}
}
];
```

### Processors

| **Processor Name** | **Description** |
| ------------------------------------------- | ----------------------------------------------------------------------------------- |
| [`markdown`](./docs/processors/markdown.md) | Extract fenced code blocks from the Markdown code so they can be linted separately. |
| **Processor Name** | **Description** |
|---------------|-----------------|
| [`markdown`](./docs/processors/markdown.md) | Extract fenced code blocks from the Markdown code so they can be linted separately. |

## Editor Integrations

Expand Down
122 changes: 61 additions & 61 deletions docs/processors/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Here is some regular Markdown text that will be ignored.
/* eslint quotes: [2, "double"] */

function hello() {
console.log("Hello, world!");
console.log("Hello, world!");
}
hello();
```
Expand Down Expand Up @@ -49,9 +49,9 @@ To enable the Markdown processor, use the `processor` configuration, which conta
import markdown from "@eslint/markdown";

export default [
...markdown.configs.processor,
...markdown.configs.processor

// your other configs here
// your other configs here
];
```

Expand All @@ -61,89 +61,89 @@ You can manually include the Markdown processor by setting the `processor` optio

Each fenced code block inside a Markdown document has a virtual filename appended to the Markdown file's path.

The virtual filename's extension will match the fenced code block's syntax tag, except for the following:
The virtual filename's extension will match the fenced code block's syntax tag, except for the following:

- `javascript` and `ecmascript` are mapped to `js`
- `typescript` is mapped to `ts`
- `markdown` is mapped to `md`
* `javascript` and `ecmascript` are mapped to `js`
* `typescript` is mapped to `ts`
* `markdown` is mapped to `md`

For example, ` ```js ` code blocks in `README.md` would match `README.md/*.js` and ` ```typescript ` in `CONTRIBUTING.md` would match `CONTRIBUTING.md/*.ts`.
For example, ```` ```js ```` code blocks in `README.md` would match `README.md/*.js` and ```` ```typescript ```` in `CONTRIBUTING.md` would match `CONTRIBUTING.md/*.ts`.

You can use glob patterns for these virtual filenames to customize configuration for code blocks without affecting regular code.
For more information on configuring processors, refer to the [ESLint documentation](https://eslint.org/docs/latest/use/configure/plugins#specify-a-processor).

Here's an example:

````js
```js
// eslint.config.js
import markdown from "@eslint/markdown";

export default [
{
// 1. Add the plugin
plugins: {
markdown,
},
},
{
// 2. Enable the Markdown processor for all .md files.
files: ["**/*.md"],
processor: "markdown/markdown",
},
{
// 3. Optionally, customize the configuration ESLint uses for ```js
// fenced code blocks inside .md files.
files: ["**/*.md/*.js"],
// ...
rules: {
// ...
},
},

// your other configs here
{
// 1. Add the plugin
plugins: {
markdown
}
},
{
// 2. Enable the Markdown processor for all .md files.
files: ["**/*.md"],
processor: "markdown/markdown"
},
{
// 3. Optionally, customize the configuration ESLint uses for ```js
// fenced code blocks inside .md files.
files: ["**/*.md/*.js"],
// ...
rules: {
// ...
}
}

// your other configs here
];
````
```

## Frequently-Disabled Rules

Some rules that catch mistakes in regular code are less helpful in documentation.
For example, `no-undef` would flag variables that are declared outside of a code snippet because they aren't relevant to the example.
The `markdown.configs.processor` config disables these rules in Markdown files:

- [`no-undef`](https://eslint.org/docs/rules/no-undef)
- [`no-unused-expressions`](https://eslint.org/docs/rules/no-unused-expressions)
- [`no-unused-vars`](https://eslint.org/docs/rules/no-unused-vars)
- [`padded-blocks`](https://eslint.org/docs/rules/padded-blocks)
- [`no-undef`](https://eslint.org/docs/rules/no-undef)
- [`no-unused-expressions`](https://eslint.org/docs/rules/no-unused-expressions)
- [`no-unused-vars`](https://eslint.org/docs/rules/no-unused-vars)
- [`padded-blocks`](https://eslint.org/docs/rules/padded-blocks)

Use glob patterns to disable more rules just for Markdown code blocks:

````js
```js
// / eslint.config.js
import markdown from "@eslint/markdown";

export default [
{
plugins: {
markdown,
},
},
{
files: ["**/*.md"],
processor: "markdown/markdown",
},
{
// 1. Target ```js code blocks in .md files.
files: ["**/*.md/*.js"],
rules: {
// 2. Disable other rules.
"no-console": "off",
"import/no-unresolved": "off",
},
},

// your other configs here
{
plugins: {
markdown
}
},
{
files: ["**/*.md"],
processor: "markdown/markdown"
},
{
// 1. Target ```js code blocks in .md files.
files: ["**/*.md/*.js"],
rules: {
// 2. Disable other rules.
"no-console": "off",
"import/no-unresolved": "off"
}
}

// your other configs here
];
````
```

## Additional Notes

Expand All @@ -160,8 +160,8 @@ This opts into strict mode parsing without repeated `"use strict"` directives.
Markdown code blocks are not real files, so ESLint's file-format rules do not apply.
The `markdown.configs.processor` config disables these rules in Markdown files:

- [`eol-last`](https://eslint.org/docs/rules/eol-last): The Markdown parser trims trailing newlines from code blocks.
- [`unicode-bom`](https://eslint.org/docs/rules/unicode-bom): Markdown code blocks do not have Unicode Byte Order Marks.
- [`eol-last`](https://eslint.org/docs/rules/eol-last): The Markdown parser trims trailing newlines from code blocks.
- [`unicode-bom`](https://eslint.org/docs/rules/unicode-bom): Markdown code blocks do not have Unicode Byte Order Marks.

### Autofixing

Expand All @@ -186,7 +186,7 @@ This example enables the `alert` global variable, disables the `no-alert` rule,
<!-- eslint quotes: ["error", "single"] -->

```js
alert("Hello, world!");
alert('Hello, world!');
```
````

Expand Down
6 changes: 3 additions & 3 deletions docs/rules/fenced-code-language.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ console.log(message);

The following options are available on this rule:

- `required: Array<string>` - when specified, fenced code blocks must use one of the languages specified in this array.
* `required: Array<string>` - when specified, fenced code blocks must use one of the languages specified in this array.

Examples of incorrect code when configured as `"fenced-code-language: ["error", { required: ["js"]}]`:

Expand All @@ -49,5 +49,5 @@ If you don't mind omitting the language for fenced code blocks, you can safely d

## Prior Art

- [MD040 - Fenced code blocks should have a language specified](https://github.com/markdownlint/markdownlint/blob/main/docs/RULES.md#md040---fenced-code-blocks-should-have-a-language-specified)
- [MD040 fenced-code-language](https://github.com/DavidAnson/markdownlint/blob/main/doc/md040.md)
* [MD040 - Fenced code blocks should have a language specified](https://github.com/markdownlint/markdownlint/blob/main/docs/RULES.md#md040---fenced-code-blocks-should-have-a-language-specified)
* [MD040 fenced-code-language](https://github.com/DavidAnson/markdownlint/blob/main/doc/md040.md)
7 changes: 4 additions & 3 deletions docs/rules/heading-increment.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ Examples of incorrect code:

### Hello world!

## Goodbye World!
Goodbye World!
--------------

#EEE Goodbye World!
```
Expand All @@ -28,5 +29,5 @@ If you aren't concerned with enforcing heading levels increment by one, you can

## Prior Art

- [MD001 - Header levels should only increment by one level at a time](https://github.com/markdownlint/markdownlint/blob/main/docs/RULES.md#md001---header-levels-should-only-increment-by-one-level-at-a-time)
- [MD001 - heading-increment](https://github.com/DavidAnson/markdownlint/blob/main/doc/md001.md)
* [MD001 - Header levels should only increment by one level at a time](https://github.com/markdownlint/markdownlint/blob/main/docs/RULES.md#md001---header-levels-should-only-increment-by-one-level-at-a-time)
* [MD001 - heading-increment](https://github.com/DavidAnson/markdownlint/blob/main/doc/md001.md)
7 changes: 4 additions & 3 deletions docs/rules/no-duplicate-headings.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Examples of incorrect code:

## Hello world!

## Goodbye World!
Goodbye World!
--------------

# Goodbye World!
```
Expand All @@ -33,5 +34,5 @@ If you aren't concerned with autolinking heading or autogenerating a table of co

## Prior Art

- [MD024 - Multiple headers with the same content](https://github.com/markdownlint/markdownlint/blob/main/docs/RULES.md#md024---multiple-headers-with-the-same-content)
- [MD024 - no-duplicate-heading](https://github.com/DavidAnson/markdownlint/blob/main/doc/md024.md)
* [MD024 - Multiple headers with the same content](https://github.com/markdownlint/markdownlint/blob/main/docs/RULES.md#md024---multiple-headers-with-the-same-content)
* [MD024 - no-duplicate-heading](https://github.com/DavidAnson/markdownlint/blob/main/doc/md024.md)
2 changes: 1 addition & 1 deletion docs/rules/no-empty-links.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ If you aren't concerned with empty links, you can safely disable this rule.

## Prior Art

- [MD042 - no-empty-links](https://github.com/DavidAnson/markdownlint/blob/main/doc/md042.md)
* [MD042 - no-empty-links](https://github.com/DavidAnson/markdownlint/blob/main/doc/md042.md)
6 changes: 3 additions & 3 deletions docs/rules/no-html.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Hello <b>world!</b>

The following options are available on this rule:

- `allowed: Array<string>` - when specified, HTML tags are allowed only if they match one of the tags in this array..
* `allowed: Array<string>` - when specified, HTML tags are allowed only if they match one of the tags in this array..

Examples of incorrect code when configured as `"no-html: ["error", { allowed: ["b"]}]`:

Expand All @@ -46,5 +46,5 @@ If you aren't concerned with empty links, you can safely disable this rule.

## Prior Art

- [MD033 - Inline HTML](https://github.com/markdownlint/markdownlint/blob/main/docs/RULES.md#md033---inline-html)
- [MD033 - no-inline-html](https://github.com/DavidAnson/markdownlint/blob/main/doc/md033.md)
* [MD033 - Inline HTML](https://github.com/markdownlint/markdownlint/blob/main/docs/RULES.md#md033---inline-html)
* [MD033 - no-inline-html](https://github.com/DavidAnson/markdownlint/blob/main/doc/md033.md)
Loading

0 comments on commit 03ad9ab

Please sign in to comment.