Skip to content

Commit 7312e50

Browse files
expose map and index in result
1 parent 218a26f commit 7312e50

File tree

6 files changed

+36
-11
lines changed

6 files changed

+36
-11
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ deploy:
2020
api_key:
2121
secure: bx7oTgpt1rSzisE0H60XTqVnwjBxJRBG60yWeoayCPgvSXXQGFw0oNv6nXiklF7CAvIg7aQWLUKMPvDn1W6+ICYPPfaWu+SgF+IkPoXfd+hT616QtEgSekTa1/5YEyc21D7J98EWG2CTmGUYDfjWDGBMoysPZuFGcyEAVLES8143uUdc5ybB9qoBhGi9dTULsCUd0wmuGnchGgqDfkqtQPPnt5wb09w+1mNcZ76nQ1bi2UWuIzeHt+fms3TNqfsNodcJX358V7EHo/V/vN3Y+br7XKrVH+TVgs01By+Ugo4mWeE3BPQqjM/vO4JtwcDSnDht8EJ5MzrHkrDtGnRuflD2rNu4IOMDOU9IjraKChgqFg+8ZRXXJWbjB13x4oUZYVRR23fs1gCley0wq2t5up6myP77xleFwPFfCECt5+tG0YH5kl6zKaPoDUZSxqLmbDAYw2vZDvEMBEZmWG0p7FfuZQY3lypBUyKYpR6ojB+b/b5VXF7WUExXYjSG+QokVzQ2SW6W6Pp6p/FvqwEuzXGVzMMq8Z2pvmqw8LcOVypu+1M9ktczrMOrZ1TmGhqUVfnfiHWQem8E8KX6U6PD84mZud33zFBigl21Eznjc7eLrf6sjj4HVNrR9vCsVhvTTV+9KqhVUnwZbzRl7BeDMg+udo4hmdYQSlSr31ube18=
2222
file:
23-
- remark-toc.js
24-
- remark-toc.min.js
23+
- mdast-util-toc.js
24+
- mdast-util-toc.min.js
2525
skip_cleanup: true
2626
on:
2727
tags: true

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ var input = remark().parse([
3434
''
3535
].join('\n'));
3636

37-
toc(input);
38-
// [ { type: 'list', ordered: false, children: [ { ... } ] } ]
37+
toc(input));
38+
39+
// { map: [ { type: 'list', ordered: false, children: [{...}] } ],
40+
// index: -1 }
3941
```
4042

4143
## API

example.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Dependencies:
2+
var remark = require('remark');
3+
var toc = require('./index.js');
4+
5+
// Transform:
6+
var input = remark().parse([
7+
'# Alpha',
8+
'',
9+
'## Bravo',
10+
'',
11+
'### Charlie',
12+
'',
13+
'## Delta',
14+
''
15+
].join('\n'));
16+
17+
// Yields:
18+
console.log('markdown', toc(input));

lib/index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
module.exports = attacher;
1111

1212
/* Dependencies */
13+
var remark = require('remark');
14+
var slug = require('remark-slug');
1315
var toExpression = require('./to-expression');
1416
var search = require('./search');
1517
var contents = require('./contents');
@@ -27,11 +29,16 @@ function attacher(node, options) {
2729
var depth = settings.maxDepth || 6;
2830
var tight = settings.tight;
2931

30-
var result = search(node, heading, depth);
32+
var processor = remark().use(slug);
33+
var tree = processor.run(node);
34+
var result = search(tree, heading, depth);
3135

3236
if (result.index === null || !result.map.length) {
33-
return [];
37+
return result;
3438
}
3539

36-
return [contents(result.map, tight)];
40+
return {
41+
map: [contents(result.map, tight)],
42+
index: result.index
43+
};
3744
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mdast-util-toc",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "Generate a Table of Contents (TOC) from a given Markdown file",
55
"main": "index.js",
66
"files": [

test/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ var test = require('tape');
33
var fs = require('fs');
44
var path = require('path');
55
var remark = require('remark');
6-
var slug = require('remark-slug');
76
var toc = require('..');
87

98
var read = fs.readFileSync;
@@ -37,10 +36,9 @@ test('Fixtures', function (t) {
3736
var result;
3837

3938
config = exists(config) ? JSON.parse(read(config, 'utf-8')) : {};
40-
slug()(input);
4139
result = compiler.compile({
4240
type: 'root',
43-
children: toc(input, config)
41+
children: toc(input, config).map
4442
});
4543

4644
t.is(result, output, 'should work on `' + fixture + '`');

0 commit comments

Comments
 (0)