Skip to content

Commit 60c5c1b

Browse files
laysentBarryThePenguin
authored andcommitted
Add: option to allow headings under certain node types (#53)
1 parent b4645ca commit 60c5c1b

File tree

7 files changed

+154
-3
lines changed

7 files changed

+154
-3
lines changed

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var contents = require('./contents')
99
function toc(node, options) {
1010
var settings = options || {}
1111
var heading = settings.heading ? toExpression(settings.heading) : null
12-
var result = search(node, heading, settings.maxDepth || 6)
12+
var result = search(node, heading, settings)
1313
var map = result.map
1414

1515
result.map = map.length === 0 ? null : contents(map, settings.tight)

lib/search.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@ module.exports = search
44

55
var toString = require('mdast-util-to-string')
66
var visit = require('unist-util-visit')
7+
var is = require('unist-util-is')
78
var slugs = require('github-slugger')()
89

910
var HEADING = 'heading'
1011

1112
// Search a node for a location.
12-
function search(root, expression, maxDepth) {
13+
function search(root, expression, settings) {
1314
var length = root.children.length
1415
var depth = null
1516
var lookingForToc = expression !== null
17+
var maxDepth = settings.maxDepth || 6
18+
var parents = settings.parents || root
1619
var map = []
1720
var headingIndex
1821
var closingIndex
@@ -43,7 +46,7 @@ function search(root, expression, maxDepth) {
4346
var value = toString(child)
4447
var id = child.data && child.data.hProperties && child.data.hProperties.id
4548

46-
if (parent !== root) {
49+
if (!is(parents, parent)) {
4750
return
4851
}
4952

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"dependencies": {
2323
"github-slugger": "^1.1.1",
2424
"mdast-util-to-string": "^1.0.2",
25+
"unist-util-is": "^2.1.2",
2526
"unist-util-visit": "^1.1.0"
2627
},
2728
"devDependencies": {

readme.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ If no `heading` is specified, creates a table of contents for all headings in
6060

6161
Links to headings are based on GitHub’s style.
6262
Only top-level headings (those not in blockquotes or lists), are used.
63+
(Change this default behavior by using option `parents` as described below)
6364
The given node is not modified.
6465

6566
##### `options`
@@ -79,6 +80,28 @@ This is inclusive, thus, when set to `3`, level three headings, are included
7980

8081
Whether to compile list-items tightly (`boolean?`, default: `false`).
8182

83+
###### `options.parents`
84+
85+
Allows headings to be children of certain node types.
86+
Internally, it uses
87+
[unist-util-is](https://github.com/syntax-tree/unist-util-is) to check.
88+
Hence all types that can be passed in as first parameter can be used here,
89+
including `Function`, `string`, `Object` and `Array.<Test>`.
90+
Check
91+
[documentation](https://github.com/syntax-tree/unist-util-is#readme)
92+
for details.
93+
(default: the first parameter `node`, which only allows top-level headings)
94+
95+
Example:
96+
97+
```json
98+
{
99+
"parents": ["root", "blockquote"]
100+
}
101+
```
102+
103+
This would allow headings under either `root` or `blockquote` to be used.
104+
82105
##### Returns
83106

84107
An object representing the table of contents.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"parents": ["root", "blockquote"]
3+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Something if
2+
3+
## Something else
4+
5+
Text.
6+
7+
> ## heading in a blockquote
8+
9+
* ## heading in a list
10+
11+
# Something iffi
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
{
2+
"index": null,
3+
"endIndex": null,
4+
"map": {
5+
"type": "list",
6+
"ordered": false,
7+
"spread": true,
8+
"children": [
9+
{
10+
"type": "listItem",
11+
"loose": true,
12+
"spread": true,
13+
"children": [
14+
{
15+
"type": "paragraph",
16+
"children": [
17+
{
18+
"type": "link",
19+
"title": null,
20+
"url": "#something-if",
21+
"children": [
22+
{
23+
"type": "text",
24+
"value": "Something if"
25+
}
26+
]
27+
}
28+
]
29+
},
30+
{
31+
"type": "list",
32+
"ordered": false,
33+
"spread": false,
34+
"children": [
35+
{
36+
"type": "listItem",
37+
"loose": false,
38+
"spread": false,
39+
"children": [
40+
{
41+
"type": "paragraph",
42+
"children": [
43+
{
44+
"type": "link",
45+
"title": null,
46+
"url": "#something-else",
47+
"children": [
48+
{
49+
"type": "text",
50+
"value": "Something else"
51+
}
52+
]
53+
}
54+
]
55+
}
56+
]
57+
},
58+
{
59+
"type": "listItem",
60+
"loose": false,
61+
"spread": false,
62+
"children": [
63+
{
64+
"type": "paragraph",
65+
"children": [
66+
{
67+
"type": "link",
68+
"title": null,
69+
"url": "#heading-in-a-blockquote",
70+
"children": [
71+
{
72+
"type": "text",
73+
"value": "heading in a blockquote"
74+
}
75+
]
76+
}
77+
]
78+
}
79+
]
80+
}
81+
]
82+
}
83+
]
84+
},
85+
{
86+
"type": "listItem",
87+
"loose": false,
88+
"spread": false,
89+
"children": [
90+
{
91+
"type": "paragraph",
92+
"children": [
93+
{
94+
"type": "link",
95+
"title": null,
96+
"url": "#something-iffi",
97+
"children": [
98+
{
99+
"type": "text",
100+
"value": "Something iffi"
101+
}
102+
]
103+
}
104+
]
105+
}
106+
]
107+
}
108+
]
109+
}
110+
}

0 commit comments

Comments
 (0)