Skip to content

Commit 37560ad

Browse files
committed
add: option to allow headings under certain node type
1 parent b4645ca commit 37560ad

File tree

6 files changed

+149
-3
lines changed

6 files changed

+149
-3
lines changed

lib/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ 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 parentTypeWhiteList = settings.parents || []
13+
var result = search(
14+
node,
15+
heading,
16+
settings.maxDepth || 6,
17+
parentTypeWhiteList
18+
)
1319
var map = result.map
1420

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

lib/search.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var slugs = require('github-slugger')()
99
var HEADING = 'heading'
1010

1111
// Search a node for a location.
12-
function search(root, expression, maxDepth) {
12+
function search(root, expression, maxDepth, parentTypeWhiteList) {
1313
var length = root.children.length
1414
var depth = null
1515
var lookingForToc = expression !== null
@@ -43,7 +43,7 @@ function search(root, expression, maxDepth) {
4343
var value = toString(child)
4444
var id = child.data && child.data.hProperties && child.data.hProperties.id
4545

46-
if (parent !== root) {
46+
if (parent !== root && parentTypeWhiteList.indexOf(parent.type) < 0) {
4747
return
4848
}
4949

readme.md

Lines changed: 16 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,21 @@ 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+
(`string[]`, default: `[]`, i.e. only top-level headings are used)
87+
88+
Example:
89+
90+
```json
91+
{
92+
"parents": ["section"]
93+
}
94+
```
95+
96+
This would allow headings both on top-level and under `section` node to be used.
97+
8298
##### Returns
8399

84100
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": ["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)