You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/create-a-custom-rule.md
+65-39Lines changed: 65 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,7 @@
1
1
# Create a custom `remark-lint` rule
2
2
3
-
This guide is part of [a step-by-step tutorial](https://dev.to/floroz/how-to-create-a-custom-lint-rule-for-markdown-and-mdx-using-remark-and-eslint-2jim), and will help you getting started to create your first linting plugin for `remark`.
3
+
This guide is part of [a step-by-step tutorial][tutorial], and will help you
4
+
getting started to create your first linting plugin for `remark`.
4
5
5
6
## Contents
6
7
@@ -31,8 +32,10 @@ Now we can start installing our dependencies:
These will help us creating and managing our custom rules.
44
47
45
-
[Back to Top](#contents)
46
-
47
48
## Set up remark
48
49
49
-
With everything installed, we can now create a `.remarkrc.js` that will contain the plugins we’ll use.
50
+
With everything installed, we can now create a `.remarkrc.js` that will contain
51
+
the plugins we’ll use.
50
52
51
-
For more info on configuration, see [Configuring `remark-lint`](https://github.com/remarkjs/remark-lint#configuring-remark-lint).
53
+
For more info on configuration, see [Examples in `remark-lint`][examples].
52
54
53
55
```sh
54
56
touch .remarkrc.js
@@ -61,7 +63,8 @@ module.exports = {
61
63
}
62
64
```
63
65
64
-
Then, in our `package.json`, add the following script to process all the markdown files in our project:
66
+
Then, in our `package.json`, add the following script to process all the
67
+
markdown files in our project:
65
68
66
69
```json
67
70
"scripts": {
@@ -87,34 +90,35 @@ Some funny images of our favorite pets
87
90

88
91
```
89
92
90
-
At this point, we have a working `remark` configuration and a markdown file in the project.
93
+
At this point, we have a working `remark` configuration and a markdown file in
94
+
the project.
91
95
92
96
If we run `npm run lint` we should expect to see in our terminal:
93
97
94
98
```sh
95
99
doc.md: no issues found
96
100
```
97
101
98
-
All good, the file has been processed, and because we haven’t specified any plugins nor lint rules, no issues are found.
99
-
100
-
[Back to Top](#contents)
102
+
All good, the file has been processed, and because we haven’t specified any
103
+
plugins nor lint rules, no issues are found.
101
104
102
105
## The `no-invalid-gif` rule
103
106
104
-
Let’s imagine we want to write a rule that checks whether a `.gif` file is used as an image.
105
-
Given the content of our `doc.md` file declared above, we would expect an *error* or *warning* pointing to:
107
+
Let’s imagine we want to write a rule that checks whether a `.gif` file is used
108
+
as an image.
109
+
Given the content of our `doc.md` file declared above, we would expect an
110
+
*error* or *warning* pointing to:
106
111
107
112
```markdown
108
113

109
114
```
110
115
111
116
Because the file extension `.gif` in the image violates our rule.
112
117
113
-
[Back to Top](#contents)
114
-
115
118
## Create the custom rule
116
119
117
-
Let’s create a new folder `rules` under the root directory, where we will place all of our custom rules, and create a new file in it named `no-gif-allowed.js`.
120
+
Let’s create a new folder `rules` under the root directory, where we will place
121
+
all of our custom rules, and create a new file in it named `no-gif-allowed.js`.
118
122
119
123
```sh
120
124
mkdir rules
@@ -123,11 +127,14 @@ touch no-gif-allowed.js
123
127
cd .. # return to project root
124
128
```
125
129
126
-
*Note*: the name of folders and files, and where to place them within your project, is up to you.
130
+
*Note*: the name of folders and files, and where to place them within your
131
+
project, is up to you.
127
132
128
133
In `./rules/no-gif-allowed.js`, let’s import `unified-lint-rule`.
129
134
130
-
We then export the result of calling `rule` by providing the *namespace and rule name* (`remark-lint:no-gif-allowed`) as the first argument, and our implementation of the rule (`noGifAllowed`) as the second argument.
135
+
We then export the result of calling `rule` by providing the *namespace and rule
136
+
name* (`remark-lint:no-gif-allowed`) as the first argument, and our
137
+
implementation of the rule (`noGifAllowed`) as the second argument.
*`options` (*optional*): additional information passed to the rule by users
170
-
171
-
[Back to Top](#contents)
174
+
*`tree` (*required*): [mdast][]
175
+
*`file` (*required*): [virtual file][vfile]
176
+
*`options` (*optional*): additional info passed to the rule by users
172
177
173
178
## Rule implementation
174
179
175
-
Because we will be inspecting [mdast](https://github.com/syntax-tree/mdast), which is a markdown abstract syntax tree built upon [unist](https://github.com/syntax-tree/unist), we can take advantage of the many existing [unist utilities](https://github.com/syntax-tree/unist#utilities) to inspect our tree’s nodes.
180
+
Because we will be inspecting [mdast][], which is a markdown abstract syntax
181
+
tree built upon [unist][], we can take advantage of the many existing
182
+
[unist utilities][unist-util] to inspect our tree’s nodes.
176
183
177
-
For this example, we will use [`unist-util-visit`](https://github.com/syntax-tree/unist-util-visit) to recursively inspect all the image nodes, and [`unist-util-generated`](https://github.com/syntax-tree/unist-util-generated) to ensure we are not inspecting nodes that we have generated ourselves and do not belong to the `doc.md`.
184
+
For this example, we will use [`unist-util-visit`][unist-util-visit] to
185
+
recursively inspect all the image nodes, and
186
+
[`unist-util-generated`][unist-util-generated] to ensure we are not inspecting
187
+
nodes that we have generated ourselves and do not belong to the `doc.md`.
0 commit comments