Skip to content

Commit 28b9a34

Browse files
committed
Update example in readme.md
1 parent 34dc7ff commit 28b9a34

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

readme.md

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,46 @@ npm install mdast-util-heading-range
1212

1313
## Usage
1414

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+
1527
```javascript
16-
var heading = require('mdast-util-heading-range');
28+
var vfile = require('to-vfile');
1729
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+
});
1838

1939
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+
];
3150
}
3251
}
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));
4452
```
4553

46-
Yields:
54+
Now, running `node example` yields:
4755

4856
```markdown
4957
# Foo

0 commit comments

Comments
 (0)