@@ -12,38 +12,46 @@ npm install mdast-util-heading-range
12
12
13
13
## Usage
14
14
15
+ Say we have the following file, ` example.md ` :
16
+
17
+ ``` markdown
18
+ # Foo
19
+
20
+ Bar.
21
+
22
+ # Baz
23
+ ```
24
+
25
+ And our script, ` example.js ` , looks as follows:
26
+
15
27
``` javascript
16
- var heading = require (' mdast-util-heading-range ' );
28
+ var vfile = require (' to-vfile ' );
17
29
var remark = require (' remark' );
30
+ var heading = require (' mdast-util-heading-range' );
31
+
32
+ remark ()
33
+ .use (plugin)
34
+ .process (vfile .readSync (' example.md' ), function (err , file ) {
35
+ if (err) throw err;
36
+ console .log (String (file));
37
+ });
18
38
19
39
function plugin () {
20
- return function (node ) {
21
- heading (node, ' foo' , function (start , nodes , end ) {
22
- return [
23
- start,
24
- {
25
- type: ' paragraph' ,
26
- children: [{type: ' text' , value: ' Qux.' }]
27
- },
28
- end
29
- ];
30
- });
40
+ return transformer;
41
+ function transformer (tree ) {
42
+ heading (tree, ' foo' , mutate);
43
+ }
44
+ function mutate (start , nodes , end ) {
45
+ return [
46
+ start,
47
+ {type: ' paragraph' , children: [{type: ' text' , value: ' Qux.' }]},
48
+ end
49
+ ];
31
50
}
32
51
}
33
-
34
- var file = remark ().use (plugin).processSync ([
35
- ' # Foo' ,
36
- ' ' ,
37
- ' Bar.' ,
38
- ' ' ,
39
- ' # Baz' ,
40
- ' '
41
- ].join (' \n ' ));
42
-
43
- console .log (String (file));
44
52
```
45
53
46
- Yields :
54
+ Now, running ` node example ` yields :
47
55
48
56
``` markdown
49
57
# Foo
0 commit comments