Skip to content

Commit 38aaca5

Browse files
authored
Add support for skipping headings
Closes remarkjs/remark-toc#22. Closes GH-61. Reviewed-by: Titus Wormer Jonathan Haines <jonno.haines@gmail.com>
1 parent 8351a6a commit 38aaca5

File tree

7 files changed

+236
-6
lines changed

7 files changed

+236
-6
lines changed

lib/index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module.exports = toc
44

55
var search = require('./search')
66
var contents = require('./contents')
7+
var toExpression = require('./to-expression')
78

89
// Get a TOC representation of `node`.
910
function toc(node, options) {
@@ -23,8 +24,3 @@ function toc(node, options) {
2324

2425
return result
2526
}
26-
27-
// Transform a string into an applicable expression.
28-
function toExpression(value) {
29-
return new RegExp('^(' + value + ')$', 'i')
30-
}

lib/search.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var toString = require('mdast-util-to-string')
66
var visit = require('unist-util-visit')
77
var convert = require('unist-util-is/convert')
88
var slugs = require('github-slugger')()
9+
var toExpression = require('./to-expression')
910

1011
var heading = convert('heading')
1112

@@ -15,6 +16,7 @@ function search(root, expression, settings) {
1516
var depth = null
1617
var lookingForToc = expression !== null
1718
var maxDepth = settings.maxDepth || 6
19+
var skip = settings.skip ? toExpression(settings.skip) : null
1820
var parents = convert(settings.parents || root)
1921
var map = []
2022
var headingIndex
@@ -62,7 +64,12 @@ function search(root, expression, settings) {
6264
}
6365
}
6466

65-
if (!lookingForToc && value && child.depth <= maxDepth) {
67+
if (
68+
!lookingForToc &&
69+
value &&
70+
child.depth <= maxDepth &&
71+
(!skip || !skip.test(value))
72+
) {
6673
map.push({
6774
depth: child.depth,
6875
children: child.children,

lib/to-expression.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict'
2+
3+
module.exports = toExpression
4+
5+
// Transform a string into an applicable expression.
6+
function toExpression(value) {
7+
return new RegExp('^(' + value + ')$', 'i')
8+
}

readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ Maximum heading depth to include in the table of contents (`number`, default:
9393
This is inclusive: when set to `3`, level three headings are included (those
9494
with three hashes, `###`).
9595

96+
###### `options.skip`
97+
98+
Headings to skip (`string`, optional), wrapped in
99+
`new RegExp('^(' + value + ')$', 'i')`.
100+
Any heading matching this expression will not be present in the table of
101+
contents.
102+
96103
###### `options.tight`
97104

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

test/fixtures/skip/config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"skip": "example|notes?"
3+
}

test/fixtures/skip/input.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Alpha
2+
3+
## Bravo
4+
5+
### Example
6+
7+
### Note
8+
9+
## Charlie
10+
11+
### Note
12+
13+
## Delta
14+
15+
### Examples
16+
17+
## Echo
18+
19+
### Notes
20+
21+
## Foxtrot
22+
23+
## Example

test/fixtures/skip/output.json

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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": "#alpha",
21+
"children": [
22+
{
23+
"type": "text",
24+
"value": "Alpha"
25+
}
26+
]
27+
}
28+
]
29+
},
30+
{
31+
"type": "list",
32+
"ordered": false,
33+
"spread": true,
34+
"children": [
35+
{
36+
"type": "listItem",
37+
"loose": true,
38+
"spread": false,
39+
"children": [
40+
{
41+
"type": "paragraph",
42+
"children": [
43+
{
44+
"type": "link",
45+
"title": null,
46+
"url": "#bravo",
47+
"children": [
48+
{
49+
"type": "text",
50+
"value": "Bravo"
51+
}
52+
]
53+
}
54+
]
55+
}
56+
]
57+
},
58+
{
59+
"type": "listItem",
60+
"loose": true,
61+
"spread": false,
62+
"children": [
63+
{
64+
"type": "paragraph",
65+
"children": [
66+
{
67+
"type": "link",
68+
"title": null,
69+
"url": "#charlie",
70+
"children": [
71+
{
72+
"type": "text",
73+
"value": "Charlie"
74+
}
75+
]
76+
}
77+
]
78+
}
79+
]
80+
},
81+
{
82+
"type": "listItem",
83+
"loose": true,
84+
"spread": true,
85+
"children": [
86+
{
87+
"type": "paragraph",
88+
"children": [
89+
{
90+
"type": "link",
91+
"title": null,
92+
"url": "#delta",
93+
"children": [
94+
{
95+
"type": "text",
96+
"value": "Delta"
97+
}
98+
]
99+
}
100+
]
101+
},
102+
{
103+
"type": "list",
104+
"ordered": false,
105+
"spread": false,
106+
"children": [
107+
{
108+
"type": "listItem",
109+
"loose": false,
110+
"spread": false,
111+
"children": [
112+
{
113+
"type": "paragraph",
114+
"children": [
115+
{
116+
"type": "link",
117+
"title": null,
118+
"url": "#examples",
119+
"children": [
120+
{
121+
"type": "text",
122+
"value": "Examples"
123+
}
124+
]
125+
}
126+
]
127+
}
128+
]
129+
}
130+
]
131+
}
132+
]
133+
},
134+
{
135+
"type": "listItem",
136+
"loose": true,
137+
"spread": false,
138+
"children": [
139+
{
140+
"type": "paragraph",
141+
"children": [
142+
{
143+
"type": "link",
144+
"title": null,
145+
"url": "#echo",
146+
"children": [
147+
{
148+
"type": "text",
149+
"value": "Echo"
150+
}
151+
]
152+
}
153+
]
154+
}
155+
]
156+
},
157+
{
158+
"type": "listItem",
159+
"loose": false,
160+
"spread": false,
161+
"children": [
162+
{
163+
"type": "paragraph",
164+
"children": [
165+
{
166+
"type": "link",
167+
"title": null,
168+
"url": "#foxtrot",
169+
"children": [
170+
{
171+
"type": "text",
172+
"value": "Foxtrot"
173+
}
174+
]
175+
}
176+
]
177+
}
178+
]
179+
}
180+
]
181+
}
182+
]
183+
}
184+
]
185+
}
186+
}

0 commit comments

Comments
 (0)