Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add minDepth #75

Merged
merged 4 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -89,7 +94,7 @@

// Visit all headings in `root`. We `slug` all headings (to account for
// duplicates), but only create a TOC from top-level headings (by default).
visit(root, 'heading', function (node, position, parent) {

Check warning on line 97 in lib/search.js

View workflow job for this annotation

GitHub Actions / lts/gallium

Function has a complexity of 21. Maximum allowed is 20.

Check warning on line 97 in lib/search.js

View workflow job for this annotation

GitHub Actions / lts/iron

Function has a complexity of 21. Maximum allowed is 20.
const value = toString(node, {includeImageAlt: false})
/** @type {string} */
// @ts-expect-error `hProperties` from <https://github.com/syntax-tree/mdast-util-to-hast>
Expand Down Expand Up @@ -125,6 +130,7 @@
// 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"
}
]
}
]
}
]
}
]
}
}
Loading