|
1 |
| -# mdast-util-heading-range [![Build Status][travis-badge]][travis] [![Coverage Status][coverage-badge]][coverage] |
| 1 | +# mdast-util-heading-range [![Build Status][build-badge]][build-status] [![Coverage Status][coverage-badge]][coverage-status] [![Chat][chat-badge]][chat] |
2 | 2 |
|
3 |
| -Markdown heading as ranges in [**mdast**][mdast]. |
| 3 | +<!--lint disable list-item-spacing heading-increment no-duplicate-headings--> |
| 4 | + |
| 5 | +Markdown heading as ranges in [**MDAST**][mdast]. |
4 | 6 |
|
5 | 7 | ## Installation
|
6 | 8 |
|
7 |
| -[npm][npm-install]: |
| 9 | +[npm][]: |
8 | 10 |
|
9 | 11 | ```bash
|
10 | 12 | npm install mdast-util-heading-range
|
11 | 13 | ```
|
12 | 14 |
|
13 |
| -**mdast-util-heading-range** is also as an AMD, CommonJS, and globals |
14 |
| -module, [uncompressed and compressed][releases]. |
| 15 | +**mdast-util-heading-range** is also available as an AMD, CommonJS, and |
| 16 | +globals module, [uncompressed and compressed][releases]. |
15 | 17 |
|
16 | 18 | ## Usage
|
17 | 19 |
|
| 20 | +Dependencies: |
| 21 | + |
18 | 22 | ```javascript
|
19 | 23 | var heading = require('mdast-util-heading-range');
|
20 | 24 | var remark = require('remark');
|
21 | 25 | ```
|
22 | 26 |
|
23 |
| -Process a document. |
| 27 | +Plug-in: |
24 | 28 |
|
25 | 29 | ```javascript
|
26 |
| -var doc = remark() |
27 |
| - .use(function () { |
28 |
| - return function (node) { |
29 |
| - heading(node, 'foo', function (start, nodes, end) { |
30 |
| - return [ |
31 |
| - start, |
32 |
| - { |
33 |
| - 'type': 'paragraph', |
34 |
| - 'children': [ |
35 |
| - { |
36 |
| - 'type': 'text', |
37 |
| - 'value': 'Qux.' |
38 |
| - } |
39 |
| - ] |
40 |
| - }, |
41 |
| - end |
42 |
| - ]; |
43 |
| - }); |
44 |
| - } |
45 |
| - }).process([ |
46 |
| - '# Foo', |
47 |
| - '', |
48 |
| - 'Bar.', |
49 |
| - '', |
50 |
| - '# Baz', |
51 |
| - '' |
52 |
| - ].join('\n')); |
| 30 | +function plugin() { |
| 31 | + return function (node) { |
| 32 | + heading(node, 'foo', function (start, nodes, end) { |
| 33 | + return [ |
| 34 | + start, |
| 35 | + { |
| 36 | + 'type': 'paragraph', |
| 37 | + 'children': [{ |
| 38 | + 'type': 'text', |
| 39 | + 'value': 'Qux.' |
| 40 | + }] |
| 41 | + }, |
| 42 | + end |
| 43 | + ]; |
| 44 | + }); |
| 45 | + } |
| 46 | +} |
53 | 47 | ```
|
54 | 48 |
|
55 |
| -Yields: |
| 49 | +Process a document. |
56 | 50 |
|
57 | 51 | ```javascript
|
58 |
| -console.log('markdown', doc); |
| 52 | +var file = remark().use(plugin).process([ |
| 53 | + '# Foo', |
| 54 | + '', |
| 55 | + 'Bar.', |
| 56 | + '', |
| 57 | + '# Baz', |
| 58 | + '' |
| 59 | +].join('\n')); |
59 | 60 | ```
|
60 | 61 |
|
61 |
| -## API |
| 62 | +Yields: |
62 | 63 |
|
63 |
| -### `heading(node, test, onrun)` |
| 64 | +```markdown |
| 65 | +# Foo |
64 | 66 |
|
65 |
| -Transform part of a document without affecting other parts, by changing a |
66 |
| -section: a heading which passes `test`, until the next heading of the same |
67 |
| -or lower depth, or the end of the document. |
| 67 | +Qux. |
68 | 68 |
|
69 |
| -**Parameters** |
| 69 | +# Baz |
| 70 | +``` |
70 | 71 |
|
71 |
| -* `node` ([`Node`][mdast-node]) — Node to search; |
| 72 | +## API |
72 | 73 |
|
73 |
| -* `test` (`string`, `RegExp`, `function(string, Node): boolean`) |
74 |
| - — Heading to look for: |
| 74 | +### `heading(node, test, onrun)` |
75 | 75 |
|
76 |
| - * When `string`, wrapped in |
77 |
| - `new RegExp('^(' + value + ')$', 'i')`; |
| 76 | +Transform part of a document without affecting other parts, by changing |
| 77 | +a section: a heading which passes `test`, until the next heading of the |
| 78 | +same or lower depth, or the end of the document. |
78 | 79 |
|
79 |
| - * Then, when `RegExp`, wrapped in |
80 |
| - `function (value) {expression.test(value)}`. |
| 80 | +###### Parameters |
81 | 81 |
|
82 |
| -* [`onrun`](#function-onrunstart-nodes-end-scope) |
83 |
| - (`Array.<Node>? = function (start, nodes, end)`) |
84 |
| - — Callback invoked when a range is found. |
| 82 | +* `node` ([`Node`][node]) — Node to search; |
| 83 | +* `test` (`string`, `RegExp`, `function(string, Node): boolean`) |
| 84 | + — Heading to look for. When `string`, wrapped in |
| 85 | + `new RegExp('^(' + value + ')$', 'i')`; when `RegExp`, wrapped |
| 86 | + in `function (value) {expression.test(value)}`. |
| 87 | +* `onrun` ([`Function`][onrun]). |
85 | 88 |
|
86 | 89 | #### `function onrun(start, nodes, end?, scope)`
|
87 | 90 |
|
88 |
| -**Parameters** |
| 91 | +Callback invoked when a range is found. |
89 | 92 |
|
90 |
| -* `start` (`Heading`) — Start of range; |
| 93 | +###### Parameters |
91 | 94 |
|
| 95 | +* `start` (`Heading`) — Start of range; |
92 | 96 | * `nodes` (`Array.<Node>`) — Nodes between `start` and `end`;
|
93 |
| - |
94 | 97 | * `end` (`Heading?`) — End of range, if any.
|
95 |
| - |
96 | 98 | * `scope` (`Object`):
|
97 | 99 |
|
98 | 100 | * `parent` (`Node`) — Parent of the range;
|
99 | 101 | * `start` (`number`) — Index of `start` in `parent`;
|
100 | 102 | * `end` (`number?`) — Index of `end` in `parent`.
|
101 | 103 |
|
102 |
| -**Returns** |
103 |
| - |
104 |
| -`Array.<Node>?` — Zero or more nodes to replace the range (including |
105 |
| -`start`, and `end`) with. |
106 |
| - |
107 | 104 | ## License
|
108 | 105 |
|
109 |
| -[MIT][license] © [Titus Wormer][home] |
| 106 | +[MIT][license] © [Titus Wormer][author] |
110 | 107 |
|
111 | 108 | <!-- Definitions -->
|
112 | 109 |
|
113 |
| -[travis-badge]: https://img.shields.io/travis/wooorm/mdast-util-heading-range.svg |
| 110 | +[build-badge]: https://img.shields.io/travis/wooorm/mdast-util-heading-range.svg |
114 | 111 |
|
115 |
| -[travis]: https://travis-ci.org/wooorm/mdast-util-heading-range |
| 112 | +[build-status]: https://travis-ci.org/wooorm/mdast-util-heading-range |
116 | 113 |
|
117 | 114 | [coverage-badge]: https://img.shields.io/codecov/c/github/wooorm/mdast-util-heading-range.svg
|
118 | 115 |
|
119 |
| -[coverage]: https://codecov.io/github/wooorm/mdast-util-heading-range |
| 116 | +[coverage-status]: https://codecov.io/github/wooorm/mdast-util-heading-range |
120 | 117 |
|
121 |
| -[mdast]: https://github.com/wooorm/mdast |
| 118 | +[chat-badge]: https://img.shields.io/gitter/room/wooorm/remark.svg |
122 | 119 |
|
123 |
| -[mdast-node]: https://github.com/wooorm/mdast#node |
124 |
| - |
125 |
| -[npm-install]: https://docs.npmjs.com/cli/install |
| 120 | +[chat]: https://gitter.im/wooorm/remark |
126 | 121 |
|
127 | 122 | [releases]: https://github.com/wooorm/mdast-util-heading-range/releases
|
128 | 123 |
|
129 | 124 | [license]: LICENSE
|
130 | 125 |
|
131 |
| -[home]: http://wooorm.com |
| 126 | +[author]: http://wooorm.com |
| 127 | + |
| 128 | +[npm]: https://docs.npmjs.com/cli/install |
| 129 | + |
| 130 | +[mdast]: https://github.com/wooorm/mdast |
| 131 | + |
| 132 | +[node]: https://github.com/wooorm/mdast#node |
| 133 | + |
| 134 | +[onrun]: #function-onrunstart-nodes-end-scope |
0 commit comments