Skip to content

Commit ec03317

Browse files
authored
build: add prettier to update-rules-docs script (#510)
* build: add `prettier` to `update-rules-docs` script * wip * wip * wip: add prettier-ignore
1 parent 3b6248d commit ec03317

File tree

3 files changed

+98
-82
lines changed

3 files changed

+98
-82
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ dist
22
CHANGELOG.md
33
jsr.json
44
**/*.md
5+
!/README.md

README.md

Lines changed: 85 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Lint Markdown with ESLint, as well JS, JSX, TypeScript, and more inside Markdown
1717

1818
### Installing
1919

20-
Install the plugin alongside ESLint v9 or greater.
20+
Install the plugin alongside ESLint v9 or greater.
2121

2222
For Node.js and compatible runtimes:
2323

@@ -39,10 +39,10 @@ deno add jsr:@eslint/markdown
3939

4040
### Configurations
4141

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

4747
In your `eslint.config.js` file, import `@eslint/markdown` and include the recommended config to enable Markdown parsing and linting:
4848

@@ -52,9 +52,9 @@ import { defineConfig } from "eslint/config";
5252
import markdown from "@eslint/markdown";
5353

5454
export default defineConfig([
55-
markdown.configs.recommended
55+
markdown.configs.recommended,
5656

57-
// your other configs here
57+
// your other configs here
5858
]);
5959
```
6060

@@ -66,17 +66,17 @@ import { defineConfig } from "eslint/config";
6666
import markdown from "@eslint/markdown";
6767

6868
export default defineConfig([
69-
{
70-
plugins: {
71-
markdown
72-
},
73-
extends: ["markdown/recommended"],
74-
rules: {
75-
"markdown/no-html": "error"
76-
}
77-
}
78-
79-
// your other configs here
69+
{
70+
plugins: {
71+
markdown,
72+
},
73+
extends: ["markdown/recommended"],
74+
rules: {
75+
"markdown/no-html": "error",
76+
},
77+
},
78+
79+
// your other configs here
8080
]);
8181
```
8282

@@ -85,27 +85,29 @@ export default defineConfig([
8585
<!-- NOTE: The following table is autogenerated. Do not manually edit. -->
8686

8787
<!-- Rule Table Start -->
88-
| **Rule Name** | **Description** | **Recommended** |
89-
| :- | :- | :-: |
90-
| [`fenced-code-language`](./docs/rules/fenced-code-language.md) | Require languages for fenced code blocks | yes |
91-
| [`heading-increment`](./docs/rules/heading-increment.md) | Enforce heading levels increment by one | yes |
92-
| [`no-bare-urls`](./docs/rules/no-bare-urls.md) | Disallow bare URLs | no |
93-
| [`no-duplicate-definitions`](./docs/rules/no-duplicate-definitions.md) | Disallow duplicate definitions | yes |
94-
| [`no-duplicate-headings`](./docs/rules/no-duplicate-headings.md) | Disallow duplicate headings in the same document | no |
95-
| [`no-empty-definitions`](./docs/rules/no-empty-definitions.md) | Disallow empty definitions | yes |
96-
| [`no-empty-images`](./docs/rules/no-empty-images.md) | Disallow empty images | yes |
97-
| [`no-empty-links`](./docs/rules/no-empty-links.md) | Disallow empty links | yes |
98-
| [`no-html`](./docs/rules/no-html.md) | Disallow HTML tags | no |
99-
| [`no-invalid-label-refs`](./docs/rules/no-invalid-label-refs.md) | Disallow invalid label references | yes |
100-
| [`no-missing-atx-heading-space`](./docs/rules/no-missing-atx-heading-space.md) | Disallow headings without a space after the hash characters | yes |
101-
| [`no-missing-label-refs`](./docs/rules/no-missing-label-refs.md) | Disallow missing label references | yes |
102-
| [`no-missing-link-fragments`](./docs/rules/no-missing-link-fragments.md) | Disallow link fragments that do not reference valid headings | yes |
103-
| [`no-multiple-h1`](./docs/rules/no-multiple-h1.md) | Disallow multiple H1 headings in the same document | yes |
104-
| [`no-reversed-media-syntax`](./docs/rules/no-reversed-media-syntax.md) | Disallow reversed link and image syntax | yes |
105-
| [`no-space-in-emphasis`](./docs/rules/no-space-in-emphasis.md) | Disallow spaces around emphasis markers | yes |
106-
| [`no-unused-definitions`](./docs/rules/no-unused-definitions.md) | Disallow unused definitions | yes |
107-
| [`require-alt-text`](./docs/rules/require-alt-text.md) | Require alternative text for images | yes |
108-
| [`table-column-count`](./docs/rules/table-column-count.md) | Disallow data rows in a GitHub Flavored Markdown table from having more cells than the header row | yes |
88+
89+
| **Rule Name** | **Description** | **Recommended** |
90+
| :----------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------ | :-------------: |
91+
| [`fenced-code-language`](./docs/rules/fenced-code-language.md) | Require languages for fenced code blocks | yes |
92+
| [`heading-increment`](./docs/rules/heading-increment.md) | Enforce heading levels increment by one | yes |
93+
| [`no-bare-urls`](./docs/rules/no-bare-urls.md) | Disallow bare URLs | no |
94+
| [`no-duplicate-definitions`](./docs/rules/no-duplicate-definitions.md) | Disallow duplicate definitions | yes |
95+
| [`no-duplicate-headings`](./docs/rules/no-duplicate-headings.md) | Disallow duplicate headings in the same document | no |
96+
| [`no-empty-definitions`](./docs/rules/no-empty-definitions.md) | Disallow empty definitions | yes |
97+
| [`no-empty-images`](./docs/rules/no-empty-images.md) | Disallow empty images | yes |
98+
| [`no-empty-links`](./docs/rules/no-empty-links.md) | Disallow empty links | yes |
99+
| [`no-html`](./docs/rules/no-html.md) | Disallow HTML tags | no |
100+
| [`no-invalid-label-refs`](./docs/rules/no-invalid-label-refs.md) | Disallow invalid label references | yes |
101+
| [`no-missing-atx-heading-space`](./docs/rules/no-missing-atx-heading-space.md) | Disallow headings without a space after the hash characters | yes |
102+
| [`no-missing-label-refs`](./docs/rules/no-missing-label-refs.md) | Disallow missing label references | yes |
103+
| [`no-missing-link-fragments`](./docs/rules/no-missing-link-fragments.md) | Disallow link fragments that do not reference valid headings | yes |
104+
| [`no-multiple-h1`](./docs/rules/no-multiple-h1.md) | Disallow multiple H1 headings in the same document | yes |
105+
| [`no-reversed-media-syntax`](./docs/rules/no-reversed-media-syntax.md) | Disallow reversed link and image syntax | yes |
106+
| [`no-space-in-emphasis`](./docs/rules/no-space-in-emphasis.md) | Disallow spaces around emphasis markers | yes |
107+
| [`no-unused-definitions`](./docs/rules/no-unused-definitions.md) | Disallow unused definitions | yes |
108+
| [`require-alt-text`](./docs/rules/require-alt-text.md) | Require alternative text for images | yes |
109+
| [`table-column-count`](./docs/rules/table-column-count.md) | Disallow data rows in a GitHub Flavored Markdown table from having more cells than the header row | yes |
110+
109111
<!-- Rule Table End -->
110112

111113
**Note:** This plugin does not provide formatting rules. We recommend using a source code formatter such as [Prettier](https://prettier.io) for that purpose.
@@ -118,21 +120,22 @@ import { defineConfig } from "eslint/config";
118120
import markdown from "@eslint/markdown";
119121

120122
export default defineConfig([
121-
{
122-
files: ["**/*.md"],
123-
plugins: {
124-
markdown
125-
},
126-
language: "markdown/commonmark",
127-
rules: {
128-
"markdown/no-html": "error"
129-
}
130-
}
123+
{
124+
files: ["**/*.md"],
125+
plugins: {
126+
markdown,
127+
},
128+
language: "markdown/commonmark",
129+
rules: {
130+
"markdown/no-html": "error",
131+
},
132+
},
131133
]);
132134
```
133135

134136
You can individually disable rules in Markdown using HTML comments, such as:
135137

138+
<!-- prettier-ignore-start -->
136139
```markdown
137140
<!-- eslint-disable-next-line markdown/no-html -- I want to allow HTML here -->
138141
<custom-element>Hello world!</custom-element>
@@ -143,13 +146,14 @@ You can individually disable rules in Markdown using HTML comments, such as:
143146

144147
[Object] <!-- eslint-disable-line markdown/no-missing-label-refs -- not meant to be a link ref -->
145148
```
149+
<!-- prettier-ignore-end -->
146150

147151
### Languages
148152

149-
| **Language Name** | **Description** |
150-
|---------------|-----------------|
151-
| `commonmark` | Parse using [CommonMark](https://commonmark.org) Markdown format |
152-
| `gfm` | Parse using [GitHub-Flavored Markdown](https://github.github.com/gfm/) format |
153+
| **Language Name** | **Description** |
154+
| ----------------- | ----------------------------------------------------------------------------- |
155+
| `commonmark` | Parse using [CommonMark](https://commonmark.org) Markdown format |
156+
| `gfm` | Parse using [GitHub-Flavored Markdown](https://github.github.com/gfm/) format |
153157

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

@@ -159,29 +163,29 @@ import { defineConfig } from "eslint/config";
159163
import markdown from "@eslint/markdown";
160164

161165
export default defineConfig([
162-
{
163-
files: ["**/*.md"],
164-
plugins: {
165-
markdown
166-
},
167-
language: "markdown/gfm",
168-
rules: {
169-
"markdown/no-html": "error"
170-
}
171-
}
166+
{
167+
files: ["**/*.md"],
168+
plugins: {
169+
markdown,
170+
},
171+
language: "markdown/gfm",
172+
rules: {
173+
"markdown/no-html": "error",
174+
},
175+
},
172176
]);
173177
```
174178

175179
### Language Options
176180

177181
#### Enabling Front Matter in both `commonmark` and `gfm`
178182

179-
By default, Markdown parsers do not support [front matter](https://jekyllrb.com/docs/front-matter/). To enable front matter in both `commonmark` and `gfm`, you can use the `frontmatter` option in `languageOptions`.
183+
By default, Markdown parsers do not support [front matter](https://jekyllrb.com/docs/front-matter/). To enable front matter in both `commonmark` and `gfm`, you can use the `frontmatter` option in `languageOptions`.
180184

181185
> `@eslint/markdown` internally uses [`micromark-extension-frontmatter`](https://github.com/micromark/micromark-extension-frontmatter) and [`mdast-util-frontmatter`](https://github.com/syntax-tree/mdast-util-frontmatter) to parse front matter.
182186
183187
| **Option Value** | **Description** |
184-
|------------------|------------------------------------------------------------|
188+
| ---------------- | ---------------------------------------------------------- |
185189
| `false` | Disables front matter parsing in Markdown files. (Default) |
186190
| `"yaml"` | Enables YAML front matter parsing in Markdown files. |
187191
| `"toml"` | Enables TOML front matter parsing in Markdown files. |
@@ -193,27 +197,27 @@ import { defineConfig } from "eslint/config";
193197
import markdown from "@eslint/markdown";
194198

195199
export default defineConfig([
196-
{
197-
files: ["**/*.md"],
198-
plugins: {
199-
markdown
200-
},
201-
language: "markdown/gfm",
202-
languageOptions: {
203-
frontmatter: "yaml", // Or pass `"toml"` or `"json"` to enable TOML or JSON front matter parsing.
204-
},
205-
rules: {
206-
"markdown/no-html": "error"
207-
}
208-
}
200+
{
201+
files: ["**/*.md"],
202+
plugins: {
203+
markdown,
204+
},
205+
language: "markdown/gfm",
206+
languageOptions: {
207+
frontmatter: "yaml", // Or pass `"toml"` or `"json"` to enable TOML or JSON front matter parsing.
208+
},
209+
rules: {
210+
"markdown/no-html": "error",
211+
},
212+
},
209213
]);
210214
```
211215

212216
### Processors
213217

214-
| **Processor Name** | **Description** |
215-
|---------------|-----------------|
216-
| [`markdown`](./docs/processors/markdown.md) | Extract fenced code blocks from the Markdown code so they can be linted separately. |
218+
| **Processor Name** | **Description** |
219+
| ------------------------------------------- | ----------------------------------------------------------------------------------- |
220+
| [`markdown`](./docs/processors/markdown.md) | Extract fenced code blocks from the Markdown code so they can be linted separately. |
217221

218222
## Editor Integrations
219223

tools/update-rules-docs.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import { fromMarkdown } from "mdast-util-from-markdown";
1616
import fs from "node:fs/promises";
1717
import path from "node:path";
18+
import prettier from "prettier";
1819

1920
//-----------------------------------------------------------------------------
2021
// Type Definitions
@@ -99,4 +100,14 @@ const tableText = await createRulesTableText();
99100

100101
docsText = `${docsText.slice(0, rulesTableRange[0])}\n${tableText}\n${docsText.slice(rulesTableRange[1])}`;
101102

102-
await fs.writeFile(docsFileURL, docsText);
103+
// load prettier config
104+
const prettierConfig = await prettier.resolveConfig(docsFileURL);
105+
106+
// format and write the file
107+
await fs.writeFile(
108+
docsFileURL,
109+
await prettier.format(docsText, {
110+
filepath: docsFileURL.pathname,
111+
...prettierConfig,
112+
}),
113+
);

0 commit comments

Comments
 (0)