diff --git a/lib/search.js b/lib/search.js index 2f07218..604ec69 100644 --- a/lib/search.js +++ b/lib/search.js @@ -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`). @@ -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)) ) { diff --git a/readme.md b/readme.md index a181642..3d6ec12 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/test/fixtures/minimum-depth-1/config.json b/test/fixtures/minimum-depth-1/config.json new file mode 100644 index 0000000..fbdc162 --- /dev/null +++ b/test/fixtures/minimum-depth-1/config.json @@ -0,0 +1,3 @@ +{ + "minDepth": 1 +} diff --git a/test/fixtures/minimum-depth-1/input.md b/test/fixtures/minimum-depth-1/input.md new file mode 100644 index 0000000..8c0c38a --- /dev/null +++ b/test/fixtures/minimum-depth-1/input.md @@ -0,0 +1,11 @@ +# Alpha + +## Bravo + +### Charlie + +#### Delta + +##### Echo + +###### Foxtrot diff --git a/test/fixtures/minimum-depth-1/output.json b/test/fixtures/minimum-depth-1/output.json new file mode 100644 index 0000000..edfaebd --- /dev/null +++ b/test/fixtures/minimum-depth-1/output.json @@ -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" + } + ] + } + ] + } + ] + } + ] + } + ] + } + ] + } + ] + } + ] + } + ] + } + ] + } + ] + } + ] + } + ] + } + ] + } +} diff --git a/test/fixtures/minimum-depth-4/config.json b/test/fixtures/minimum-depth-4/config.json new file mode 100644 index 0000000..f61182d --- /dev/null +++ b/test/fixtures/minimum-depth-4/config.json @@ -0,0 +1,3 @@ +{ + "minDepth": 4 +} diff --git a/test/fixtures/minimum-depth-4/input.md b/test/fixtures/minimum-depth-4/input.md new file mode 100644 index 0000000..8c0c38a --- /dev/null +++ b/test/fixtures/minimum-depth-4/input.md @@ -0,0 +1,11 @@ +# Alpha + +## Bravo + +### Charlie + +#### Delta + +##### Echo + +###### Foxtrot diff --git a/test/fixtures/minimum-depth-4/output.json b/test/fixtures/minimum-depth-4/output.json new file mode 100644 index 0000000..7817dd0 --- /dev/null +++ b/test/fixtures/minimum-depth-4/output.json @@ -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" + } + ] + } + ] + } + ] + } + ] + } + ] + } + ] + } + ] + } + ] + } +} diff --git a/test/fixtures/minimum-depth-6/config.json b/test/fixtures/minimum-depth-6/config.json new file mode 100644 index 0000000..f79c7fa --- /dev/null +++ b/test/fixtures/minimum-depth-6/config.json @@ -0,0 +1,3 @@ +{ + "minDepth": 6 +} diff --git a/test/fixtures/minimum-depth-6/input.md b/test/fixtures/minimum-depth-6/input.md new file mode 100644 index 0000000..8c0c38a --- /dev/null +++ b/test/fixtures/minimum-depth-6/input.md @@ -0,0 +1,11 @@ +# Alpha + +## Bravo + +### Charlie + +#### Delta + +##### Echo + +###### Foxtrot diff --git a/test/fixtures/minimum-depth-6/output.json b/test/fixtures/minimum-depth-6/output.json new file mode 100644 index 0000000..562f984 --- /dev/null +++ b/test/fixtures/minimum-depth-6/output.json @@ -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" + } + ] + } + ] + } + ] + } + ] + } +}