@@ -65,25 +65,59 @@ In the next step we’ll write the code to use our plugin.
65
65
66
66
### Setting up
67
67
68
+ Let’s set up a project.
69
+ Create a folder, ` example ` , enter it, and initialize a new project:
70
+
71
+ ``` sh
72
+ mkdir example
73
+ cd example
74
+ npm init -y
75
+ ```
76
+
77
+ Then make sure the project is a module, so that ` import ` and ` export ` work,
78
+ by changing ` package.json ` :
79
+
80
+ ``` diff
81
+ --- a/package.json
82
+ +++ b/package.json
83
+ @@ -2,6 +2,7 @@
84
+ "name": "example",
85
+ "version": "1.0.0",
86
+ "description": "",
87
+ + "type": "module",
88
+ "main": "index.js",
89
+ "scripts": {
90
+ "test": "echo \"Error: no test specified\" && exit 1"
91
+ ```
92
+
93
+ Make sure ` example.md ` exists with:
94
+
95
+ ``` markdown
96
+ One sentence. Two sentences.
97
+
98
+ One sentence. Two sentences.
99
+ ```
100
+
68
101
Now, let’s create an ` example.js ` file that will process our text file and
69
102
report any found problems.
70
103
71
104
``` javascript
72
- var fs = require ( ' fs' )
73
- var retext = require ( ' retext' )
74
- var report = require ( ' vfile-reporter' )
75
- var spacing = require ( ' . ' )
105
+ import fs from ' fs'
106
+ import { retext } from ' retext'
107
+ import { reporter } from ' vfile-reporter'
108
+ import retextSentenceSpacing from ' ./index.js '
76
109
77
- var doc = fs .readFileSync (' example.md' )
110
+ const buffer = fs .readFileSync (' example.md' )
78
111
79
112
retext ()
80
- .use (spacing)
81
- .process (doc, function (err , file ) {
82
- console .error (report (err || file))
113
+ .use (retextSentenceSpacing)
114
+ .process (buffer)
115
+ .then ((file ) => {
116
+ console .error (reporter (file))
83
117
})
84
118
```
85
119
86
- > Don’t forget to ` npm install ` dependencies!
120
+ > Don’t forget to ` npm install ` dependencies ( ` retext ` , ` vfile-reporter ` ) !
87
121
88
122
If you read the guide on [ using unified] [ use ] , you’ll see some familiar
89
123
statements.
@@ -104,17 +138,13 @@ We’ll do that in the next section.
104
138
105
139
### Plugin
106
140
107
- As we read in Plugin Basics, we’ll need an attacher , and for our case also a
141
+ As we read in Plugin Basics, we’ll need a plugin , and for our case also a
108
142
transformer.
109
143
Let’s create them in our plugin file ` index.js ` :
110
144
111
145
``` javascript
112
- module .exports = attacher
113
-
114
- function attacher () {
115
- return transformer
116
-
117
- function transformer (tree , file ) {
146
+ export default function retextSentenceSpacing () {
147
+ return (tree , file ) => {
118
148
}
119
149
}
120
150
```
@@ -125,21 +155,18 @@ We can use a utility to help us to recursively walk our tree, namely
125
155
Let’s add that.
126
156
127
157
``` diff
128
- + var visit = require('unist-util-visit')
129
- +
130
- module.exports = attacher
131
-
132
- function attacher() {
133
- return transformer
134
-
135
- function transformer(tree, file) {
136
- + visit(tree, 'ParagraphNode', visitor)
158
+ --- a/index.js
159
+ +++ b/index.js
160
+ @@ -1,4 +1,9 @@
161
+ + import {visit} from 'unist-util-visit'
137
162
+
138
- + function visitor(node) {
163
+ export default function retextSentenceSpacing() {
164
+ return (tree, file) => {
165
+ + visit(tree, 'ParagraphNode', (node) => {
139
166
+ console.log(node)
140
- + }
141
- }
142
- }
167
+ + })
168
+ }
169
+ }
143
170
```
144
171
145
172
> Don’t forget to ` npm install ` the utility!
@@ -148,17 +175,34 @@ If we now run our example with Node.js, as follows, we’ll see that visitor is
148
175
invoked with both paragraphs in our example:
149
176
150
177
``` sh
151
- $ node example.js
152
- { type: ' ParagraphNode' ,
153
- children:
154
- [ { type: ' SentenceNode' , children: [Object] },
155
- { type: ' WhiteSpaceNode' , value: ' ' },
156
- { type: ' SentenceNode' , children: [Object] } ] }
157
- { type: ' ParagraphNode' ,
158
- children:
159
- [ { type: ' SentenceNode' , children: [Object] },
160
- { type: ' WhiteSpaceNode' , value: ' ' },
161
- { type: ' SentenceNode' , children: [Object] } ] }
178
+ node example.js
179
+ ```
180
+
181
+ ``` txt
182
+ {
183
+ type: 'ParagraphNode',
184
+ children: [
185
+ { type: 'SentenceNode', children: [Array], position: [Object] },
186
+ { type: 'WhiteSpaceNode', value: ' ', position: [Position] },
187
+ { type: 'SentenceNode', children: [Array], position: [Object] }
188
+ ],
189
+ position: {
190
+ start: { line: 1, column: 1, offset: 0 },
191
+ end: { line: 1, column: 29, offset: 28 }
192
+ }
193
+ }
194
+ {
195
+ type: 'ParagraphNode',
196
+ children: [
197
+ { type: 'SentenceNode', children: [Array], position: [Object] },
198
+ { type: 'WhiteSpaceNode', value: ' ', position: [Position] },
199
+ { type: 'SentenceNode', children: [Array], position: [Object] }
200
+ ],
201
+ position: {
202
+ start: { line: 3, column: 1, offset: 30 },
203
+ end: { line: 3, column: 30, offset: 59 }
204
+ }
205
+ }
162
206
no issues found
163
207
```
164
208
@@ -173,22 +217,19 @@ Only checking white-space between sentences.
173
217
We use a small utility for checking node types: [ ` unist-util-is ` ] [ is ] .
174
218
175
219
``` diff
176
- var visit = require(' unist-util-visit' )
177
- +var is = require(' unist-util-is' )
178
-
179
- module.exports = attacher
180
-
181
- function attacher() {
182
- return transformer
183
-
184
- function transformer(tree, file) {
185
- visit(tree, ' ParagraphNode' , visitor)
186
-
187
- function visitor(node) {
220
+ --- a/index.js
221
+ +++ b/index.js
222
+ @@ -1,9 +1,20 @@
223
+ import {visit} from 'unist-util-visit'
224
+ + import {is} from 'unist-util-is'
225
+
226
+ export default function retextSentenceSpacing() {
227
+ return (tree, file) => {
228
+ visit(tree, 'ParagraphNode', (node) => {
188
229
- console.log(node)
189
- + var children = node.children
230
+ + const children = node.children
190
231
+
191
- + children.forEach(function (child, index) {
232
+ + children.forEach((child, index) => {
192
233
+ if (
193
234
+ is(children[index - 1], 'SentenceNode') &&
194
235
+ is(child, 'WhiteSpaceNode') &&
@@ -197,9 +238,9 @@ function attacher() {
197
238
+ console.log(child)
198
239
+ }
199
240
+ })
200
- }
201
- }
202
- }
241
+ })
242
+ }
243
+ }
203
244
```
204
245
205
246
> Don’t forget to ` npm install ` the utility!
@@ -208,9 +249,26 @@ If we now run our example with Node, as follows, we’ll see that only white-spa
208
249
between sentences is logged.
209
250
210
251
``` sh
211
- $ node example.js
212
- { type: ' WhiteSpaceNode' , value: ' ' }
213
- { type: ' WhiteSpaceNode' , value: ' ' }
252
+ node example.js
253
+ ```
254
+
255
+ ``` txt
256
+ {
257
+ type: 'WhiteSpaceNode',
258
+ value: ' ',
259
+ position: Position {
260
+ start: { line: 1, column: 14, offset: 13 },
261
+ end: { line: 1, column: 15, offset: 14 }
262
+ }
263
+ }
264
+ {
265
+ type: 'WhiteSpaceNode',
266
+ value: ' ',
267
+ position: Position {
268
+ start: { line: 3, column: 14, offset: 43 },
269
+ end: { line: 3, column: 16, offset: 45 }
270
+ }
271
+ }
214
272
no issues found
215
273
```
216
274
@@ -219,38 +277,22 @@ characters than needed.
219
277
We can use [ ` file.message() ` ] [ message ] to associate a message with the file.
220
278
221
279
``` diff
222
- var visit = require(' unist-util-visit' )
223
- var is = require(' unist-util-is' )
224
-
225
- module.exports = attacher
226
-
227
- function attacher() {
228
- return transformer
229
-
230
- function transformer(tree, file) {
231
- visit(tree, ' ParagraphNode' , visitor)
232
-
233
- function visitor(node) {
234
- var children = node.children
235
-
236
- children.forEach(function(child, index) {
237
- if (
238
- is(children[index - 1], ' SentenceNode' ) &&
239
- is(child, ' WhiteSpaceNode' ) &&
240
- is(children[index + 1], ' SentenceNode' )
241
- ) {
280
+ --- a/index.js
281
+ +++ b/index.js
282
+ @@ -12,7 +12,12 @@ export default function retextSentenceSpacing() {
283
+ is(child, 'WhiteSpaceNode') &&
284
+ is(children[index + 1], 'SentenceNode')
285
+ ) {
242
286
- console.log(child)
243
287
+ if (child.value.length !== 1) {
244
288
+ file.message(
245
289
+ 'Expected 1 space between sentences, not ' + child.value.length,
246
290
+ child
247
291
+ )
248
292
+ }
249
- }
250
- })
251
- }
252
- }
253
- }
293
+ }
294
+ })
295
+ })
254
296
```
255
297
256
298
If we now run our example one final time, we’ll see a message for our problem!
0 commit comments