Skip to content

Commit 3edfeee

Browse files
committed
Update API to remove remark dependency
1 parent a74a9c5 commit 3edfeee

File tree

6 files changed

+114
-137
lines changed

6 files changed

+114
-137
lines changed

example.js

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
11
var heading = require('./index.js');
22
var remark = require('remark');
33

4-
// Callback invoked when a heading is found.
5-
function onrun(start, nodes, end) {
6-
return [
7-
start,
8-
{
9-
'type': 'paragraph',
10-
'children': [
11-
{
12-
'type': 'text',
13-
'value': 'Qux.'
14-
}
15-
]
16-
},
17-
end
18-
];
19-
}
20-
214
// Process a document.
22-
var doc = remark().use(heading('foo', onrun)).process(
23-
'# Foo\n' +
24-
'\n' +
25-
'Bar.\n' +
26-
'\n' +
27-
'# Baz\n'
28-
);
5+
var doc = remark()
6+
.use(function () {
7+
return function (node) {
8+
heading(node, 'foo', function (start, nodes, end) {
9+
return [
10+
start,
11+
{
12+
'type': 'paragraph',
13+
'children': [
14+
{
15+
'type': 'text',
16+
'value': 'Qux.'
17+
}
18+
]
19+
},
20+
end
21+
];
22+
});
23+
}
24+
}).process([
25+
'# Foo',
26+
'',
27+
'Bar.',
28+
'',
29+
'# Baz',
30+
''
31+
].join('\n'));
2932

3033
// Yields:
3134
console.log('markdown', doc);

index.js

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,15 @@ function search(root, test, callback) {
159159
}
160160

161161
/**
162-
* Wrapper.
162+
* Search `node` for `heading` and invoke `callback`.
163163
*
164+
* @param {Node} node - Node to search in.
164165
* @param {string|RegExp|Function} heading - Heading to
165166
* search for.
166167
* @param {Function} callback - Callback invoked when
167168
* found.
168-
* @return {function(node)} - Attacher.
169169
*/
170-
function wrapper(heading, callback) {
170+
function headingRange(node, heading, callback) {
171171
var test = heading;
172172

173173
if (typeof test === 'string') {
@@ -178,29 +178,11 @@ function wrapper(heading, callback) {
178178
test = wrapExpression(test);
179179
}
180180

181-
/**
182-
* Find a range based on a starting heading matching
183-
* `test`, up until the following closing with
184-
* equal or higher depth.
185-
*
186-
* @param {Node} node - Node to search in.
187-
*/
188-
function transformer(node) {
189-
search(node, test, callback);
190-
}
191-
192-
/**
193-
* `Attacher`.
194-
*/
195-
function attacher() {
196-
return transformer;
197-
}
198-
199-
return attacher;
181+
search(node, test, callback);
200182
}
201183

202184
/*
203185
* Expose.
204186
*/
205187

206-
module.exports = wrapper;
188+
module.exports = headingRange;

mdast-util-heading-range.js

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,15 @@ function search(root, test, callback) {
160160
}
161161

162162
/**
163-
* Wrapper.
163+
* Search `node` for `heading` and invoke `callback`.
164164
*
165+
* @param {Node} node - Node to search in.
165166
* @param {string|RegExp|Function} heading - Heading to
166167
* search for.
167168
* @param {Function} callback - Callback invoked when
168169
* found.
169-
* @return {function(node)} - Attacher.
170170
*/
171-
function wrapper(heading, callback) {
171+
function headingRange(node, heading, callback) {
172172
var test = heading;
173173

174174
if (typeof test === 'string') {
@@ -179,32 +179,14 @@ function wrapper(heading, callback) {
179179
test = wrapExpression(test);
180180
}
181181

182-
/**
183-
* Find a range based on a starting heading matching
184-
* `test`, up until the following closing with
185-
* equal or higher depth.
186-
*
187-
* @param {Node} node - Node to search in.
188-
*/
189-
function transformer(node) {
190-
search(node, test, callback);
191-
}
192-
193-
/**
194-
* `Attacher`.
195-
*/
196-
function attacher() {
197-
return transformer;
198-
}
199-
200-
return attacher;
182+
search(node, test, callback);
201183
}
202184

203185
/*
204186
* Expose.
205187
*/
206188

207-
module.exports = wrapper;
189+
module.exports = headingRange;
208190

209191
},{"mdast-util-to-string":2}],2:[function(require,module,exports){
210192
/**

mdast-util-heading-range.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readme.md

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,36 @@ var heading = require('mdast-util-heading-range');
2121
var remark = require('remark');
2222
```
2323

24-
Callback invoked when a heading is found.
25-
26-
```javascript
27-
function onrun(start, nodes, end) {
28-
return [
29-
start,
30-
{
31-
'type': 'paragraph',
32-
'children': [
33-
{
34-
'type': 'text',
35-
'value': 'Qux.'
36-
}
37-
]
38-
},
39-
end
40-
];
41-
}
42-
```
43-
4424
Process a document.
4525

4626
```javascript
47-
var doc = remark().use(heading('foo', onrun)).process(
48-
'# Foo\n' +
49-
'\n' +
50-
'Bar.\n' +
51-
'\n' +
52-
'# Baz\n'
53-
);
27+
var doc = remark()
28+
.use(function () {
29+
return function (node) {
30+
heading(node, 'foo', function (start, nodes, end) {
31+
return [
32+
start,
33+
{
34+
'type': 'paragraph',
35+
'children': [
36+
{
37+
'type': 'text',
38+
'value': 'Qux.'
39+
}
40+
]
41+
},
42+
end
43+
];
44+
});
45+
}
46+
}).process([
47+
'# Foo',
48+
'',
49+
'Bar.',
50+
'',
51+
'# Baz',
52+
''
53+
].join('\n'));
5454
```
5555

5656
Yields:
@@ -65,14 +65,16 @@ Qux.
6565

6666
## API
6767

68-
### heading(test, onrun)
68+
### `heading(node, test, onrun)`
6969

7070
Transform part of a document without affecting other parts, by changing a
7171
section: a heading which passes `test`, until the next heading of the same
7272
or lower depth, or the end of the document.
7373

7474
**Parameters**
7575

76+
* `node` ([`Node`][mdast-node]) — Node to search;
77+
7678
* `test` (`string`, `RegExp`, `function(string, Node): boolean`)
7779
— Heading to look for:
7880

@@ -86,11 +88,7 @@ or lower depth, or the end of the document.
8688
(`Array.<Node>? = function (start, nodes, end)`)
8789
— Callback invoked when a range is found.
8890

89-
**Returns**
90-
91-
`Function` — Should be passed to [`mdast.use()`](https://github.com/wooorm/mdast#mdastuseplugin-options).
92-
93-
#### function onrun(start, nodes, end?, scope)
91+
#### `function onrun(start, nodes, end?, scope)`
9492

9593
**Parameters**
9694

@@ -127,6 +125,8 @@ or lower depth, or the end of the document.
127125

128126
[mdast]: https://github.com/wooorm/mdast
129127

128+
[mdast-node]: https://github.com/wooorm/mdast#node
129+
130130
[npm-install]: https://docs.npmjs.com/cli/install
131131

132132
[duo]: http://duojs.org/#getting-started

test.js

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,21 @@ var heading = require('./');
2323
*
2424
* @param {Object} t - Test.
2525
* @param {string} value - Value to process.
26-
* @param {*} options - configuration.
26+
* @param {*} name - Configuration.
2727
* @return {string} - Processed value.
2828
*/
29-
function process(t, value, options) {
30-
return remark().use(function (processor, name) {
31-
processor.use(heading(name, function (start, nodes, end, scope) {
32-
t.equal(typeof scope.start, 'number');
33-
t.assert(typeof scope.end === 'number' || scope.end === null);
34-
t.equal(scope.parent.type, 'root');
29+
function process(t, value, name) {
30+
return remark().use(function () {
31+
return function (node) {
32+
heading(node, name, function (start, nodes, end, scope) {
33+
t.equal(typeof scope.start, 'number');
34+
t.assert(typeof scope.end === 'number' || scope.end === null);
35+
t.equal(scope.parent.type, 'root');
3536

36-
return [start].concat(end ? [end] : []);
37-
}));
38-
}, options).process(value);
37+
return [start].concat(end ? [end] : []);
38+
});
39+
};
40+
}).process(value);
3941
}
4042

4143
/*
@@ -241,10 +243,12 @@ test('mdast-util-heading-range()', function (t) {
241243
'should not fail with empty headings'
242244
);
243245

244-
remark().use(function (processor) {
245-
processor.use(heading('foo', function () {
246-
return null;
247-
}));
246+
remark().use(function () {
247+
return function (node) {
248+
heading(node, 'foo', function () {
249+
return null;
250+
});
251+
};
248252
}).process([
249253
'Foo',
250254
'',
@@ -265,10 +269,12 @@ test('mdast-util-heading-range()', function (t) {
265269
].join('\n'), 'should not remove anything when `null` is given');
266270
});
267271

268-
remark().use(function (processor) {
269-
processor.use(heading('foo', function () {
270-
return [];
271-
}));
272+
remark().use(function () {
273+
return function (node) {
274+
heading(node, 'foo', function () {
275+
return [];
276+
});
277+
};
272278
}).process([
273279
'Foo',
274280
'',
@@ -285,16 +291,18 @@ test('mdast-util-heading-range()', function (t) {
285291
].join('\n'), 'should replace all previous nodes otherwise');
286292
});
287293

288-
remark().use(function (processor) {
289-
processor.use(heading('foo', function (start, nodes, end) {
290-
return [
291-
start,
292-
{
293-
'type': 'horizontalRule'
294-
},
295-
end
296-
];
297-
}));
294+
remark().use(function () {
295+
return function (node) {
296+
heading(node, 'foo', function (start, nodes, end) {
297+
return [
298+
start,
299+
{
300+
'type': 'horizontalRule'
301+
},
302+
end
303+
];
304+
});
305+
};
298306
}).process([
299307
'Foo',
300308
'',
@@ -319,12 +327,14 @@ test('mdast-util-heading-range()', function (t) {
319327
].join('\n'), 'should insert all returned nodes');
320328
});
321329

322-
remark().use(function (processor) {
323-
processor.use(heading('foo', function (start, nodes, end) {
324-
t.equal(nodes.length, 3);
330+
remark().use(function () {
331+
return function (node) {
332+
heading(node, 'foo', function (start, nodes, end) {
333+
t.equal(nodes.length, 3);
325334

326-
return [start].concat(nodes, end);
327-
}));
335+
return [start].concat(nodes, end);
336+
});
337+
};
328338
}).process([
329339
'# Alpha',
330340
'',

0 commit comments

Comments
 (0)