Skip to content

Commit 4d8f697

Browse files
committed
Refactor example in readme.md
1 parent 64893f0 commit 4d8f697

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

readme.md

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,46 @@ npm install mdast-zone
1313

1414
## Usage
1515

16+
Say we have the following file, `example.md`:
17+
18+
```markdown
19+
<!--foo start-->
20+
21+
Foo
22+
23+
<!--foo end-->
24+
```
25+
26+
And our script, `example.js`, looks as follows:
27+
1628
```javascript
17-
var zone = require('mdast-zone');
29+
var vfile = require('to-vfile');
1830
var remark = require('remark');
31+
var zone = require('mdast-zone');
1932

2033
remark()
21-
.use(function () {
22-
return transformer;
23-
24-
function transformer(tree) {
25-
zone(tree, 'foo', function (start, nodes, end) {
26-
return [
27-
start,
28-
{type: 'paragraph', children: [{type: 'text', value: 'Bar'}]},
29-
end
30-
]
31-
})
32-
}
33-
})
34-
.process([
35-
'<!--foo start-->',
36-
'',
37-
'Foo',
38-
'',
39-
'<!--foo end-->'
40-
].join('\n'), function (err, file) {
34+
.use(plugin)
35+
.process(vfile.readSync('example.md'), function (err, file) {
4136
if (err) throw err;
4237
console.log(String(file));
4338
});
39+
40+
function plugin() {
41+
return transformer;
42+
function transformer(tree) {
43+
zone(tree, 'foo', mutate);
44+
}
45+
function mutate(start, nodes, end) {
46+
return [
47+
start,
48+
{type: 'paragraph', children: [{type: 'text', value: 'Bar'}]},
49+
end
50+
];
51+
}
52+
}
4453
```
4554

46-
Yields:
55+
Now, running `node example` yields:
4756

4857
```markdown
4958
<!--foo start-->

0 commit comments

Comments
 (0)