Skip to content

Commit

Permalink
Add minDepth
Browse files Browse the repository at this point in the history
Closes GH-74.
Closes GH-75.

Reviewed-by: Titus Wormer <tituswormer@gmail.com>
  • Loading branch information
dmca-glasgow committed Jun 2, 2024
1 parent 6d5c0ce commit 623c332
Show file tree
Hide file tree
Showing 11 changed files with 348 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
*
* This is inclusive: when set to `3`, level three headings are included
* (those with three hashes, `###`).
* @property {Rank | null | undefined} [minDepth=1]
* Minimum heading depth to include in the table of contents (default: `1`).
*
* This is inclusive: when set to `3`, level three headings are included
* (those with three hashes, `###`).
* @property {string | null | undefined} [skip]
* Headings to skip, wrapped in `new RegExp('^(' + value + ')$', 'i')`
* (default: `undefined`).
Expand Down Expand Up @@ -125,6 +130,7 @@ export function search(root, expression, settings) {
// A heading after the closing (if we were looking for one).
if (
(endIndex || !expression) &&
(!settings.minDepth || node.depth >= settings.minDepth) &&
(!settings.maxDepth || node.depth <= settings.maxDepth) &&
(!skip || !skip.test(value))
) {
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ Configuration (TypeScript type).
— maximum heading depth to include in the table of contents.
This is inclusive: when set to `3`, level three headings are included
(those with three hashes, `###`)
* `minDepth` (`number`, default: `1`)
— minimum heading depth to include in the table of contents.
This is inclusive: when set to `3`, level three headings are included
(those with three hashes, `###`)
* `skip` (`string`, optional)
— headings to skip, wrapped in `new RegExp('^(' + value + ')$', 'i')`.
Any heading matching this expression will not be present in the table of
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/minimum-depth-1/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"minDepth": 1
}
11 changes: 11 additions & 0 deletions test/fixtures/minimum-depth-1/input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Alpha

## Bravo

### Charlie

#### Delta

##### Echo

###### Foxtrot
176 changes: 176 additions & 0 deletions test/fixtures/minimum-depth-1/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
{
"map": {
"type": "list",
"ordered": false,
"spread": true,
"children": [
{
"type": "listItem",
"spread": true,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "link",
"title": null,
"url": "#alpha",
"children": [
{
"type": "text",
"value": "Alpha"
}
]
}
]
},
{
"type": "list",
"ordered": false,
"spread": true,
"children": [
{
"type": "listItem",
"spread": true,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "link",
"title": null,
"url": "#bravo",
"children": [
{
"type": "text",
"value": "Bravo"
}
]
}
]
},
{
"type": "list",
"ordered": false,
"spread": true,
"children": [
{
"type": "listItem",
"spread": true,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "link",
"title": null,
"url": "#charlie",
"children": [
{
"type": "text",
"value": "Charlie"
}
]
}
]
},
{
"type": "list",
"ordered": false,
"spread": true,
"children": [
{
"type": "listItem",
"spread": true,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "link",
"title": null,
"url": "#delta",
"children": [
{
"type": "text",
"value": "Delta"
}
]
}
]
},
{
"type": "list",
"ordered": false,
"spread": true,
"children": [
{
"type": "listItem",
"spread": true,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "link",
"title": null,
"url": "#echo",
"children": [
{
"type": "text",
"value": "Echo"
}
]
}
]
},
{
"type": "list",
"ordered": false,
"spread": false,
"children": [
{
"type": "listItem",
"spread": false,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "link",
"title": null,
"url": "#foxtrot",
"children": [
{
"type": "text",
"value": "Foxtrot"
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
}
3 changes: 3 additions & 0 deletions test/fixtures/minimum-depth-4/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"minDepth": 4
}
11 changes: 11 additions & 0 deletions test/fixtures/minimum-depth-4/input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Alpha

## Bravo

### Charlie

#### Delta

##### Echo

###### Foxtrot
89 changes: 89 additions & 0 deletions test/fixtures/minimum-depth-4/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{
"map": {
"type": "list",
"ordered": false,
"spread": true,
"children": [
{
"type": "listItem",
"spread": true,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "link",
"title": null,
"url": "#delta",
"children": [
{
"type": "text",
"value": "Delta"
}
]
}
]
},
{
"type": "list",
"ordered": false,
"spread": true,
"children": [
{
"type": "listItem",
"spread": true,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "link",
"title": null,
"url": "#echo",
"children": [
{
"type": "text",
"value": "Echo"
}
]
}
]
},
{
"type": "list",
"ordered": false,
"spread": false,
"children": [
{
"type": "listItem",
"spread": false,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "link",
"title": null,
"url": "#foxtrot",
"children": [
{
"type": "text",
"value": "Foxtrot"
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
}
3 changes: 3 additions & 0 deletions test/fixtures/minimum-depth-6/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"minDepth": 6
}
11 changes: 11 additions & 0 deletions test/fixtures/minimum-depth-6/input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Alpha

## Bravo

### Charlie

#### Delta

##### Echo

###### Foxtrot
31 changes: 31 additions & 0 deletions test/fixtures/minimum-depth-6/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"map": {
"type": "list",
"ordered": false,
"spread": false,
"children": [
{
"type": "listItem",
"spread": false,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "link",
"title": null,
"url": "#foxtrot",
"children": [
{
"type": "text",
"value": "Foxtrot"
}
]
}
]
}
]
}
]
}
}

0 comments on commit 623c332

Please sign in to comment.