diff --git a/lib/index.js b/lib/index.js index f374586..741f9da 100644 --- a/lib/index.js +++ b/lib/index.js @@ -4,6 +4,7 @@ module.exports = toc var search = require('./search') var contents = require('./contents') +var toExpression = require('./to-expression') // Get a TOC representation of `node`. function toc(node, options) { @@ -23,8 +24,3 @@ function toc(node, options) { return result } - -// Transform a string into an applicable expression. -function toExpression(value) { - return new RegExp('^(' + value + ')$', 'i') -} diff --git a/lib/search.js b/lib/search.js index b743bb3..93613c1 100644 --- a/lib/search.js +++ b/lib/search.js @@ -6,6 +6,7 @@ var toString = require('mdast-util-to-string') var visit = require('unist-util-visit') var convert = require('unist-util-is/convert') var slugs = require('github-slugger')() +var toExpression = require('./to-expression') var heading = convert('heading') @@ -15,6 +16,7 @@ function search(root, expression, settings) { var depth = null var lookingForToc = expression !== null var maxDepth = settings.maxDepth || 6 + var skip = settings.skip ? toExpression(settings.skip) : null var parents = convert(settings.parents || root) var map = [] var headingIndex @@ -62,7 +64,12 @@ function search(root, expression, settings) { } } - if (!lookingForToc && value && child.depth <= maxDepth) { + if ( + !lookingForToc && + value && + child.depth <= maxDepth && + (!skip || !skip.test(value)) + ) { map.push({ depth: child.depth, children: child.children, diff --git a/lib/to-expression.js b/lib/to-expression.js new file mode 100644 index 0000000..6c4b9d6 --- /dev/null +++ b/lib/to-expression.js @@ -0,0 +1,8 @@ +'use strict' + +module.exports = toExpression + +// Transform a string into an applicable expression. +function toExpression(value) { + return new RegExp('^(' + value + ')$', 'i') +} diff --git a/readme.md b/readme.md index ded4f24..d6b7ace 100644 --- a/readme.md +++ b/readme.md @@ -93,6 +93,13 @@ Maximum heading depth to include in the table of contents (`number`, default: This is inclusive: when set to `3`, level three headings are included (those with three hashes, `###`). +###### `options.skip` + +Headings to skip (`string`, optional), wrapped in +`new RegExp('^(' + value + ')$', 'i')`. +Any heading matching this expression will not be present in the table of +contents. + ###### `options.tight` Whether to compile list-items tightly (`boolean?`, default: `false`). diff --git a/test/fixtures/skip/config.json b/test/fixtures/skip/config.json new file mode 100644 index 0000000..92719f5 --- /dev/null +++ b/test/fixtures/skip/config.json @@ -0,0 +1,3 @@ +{ + "skip": "example|notes?" +} diff --git a/test/fixtures/skip/input.md b/test/fixtures/skip/input.md new file mode 100644 index 0000000..ee081a7 --- /dev/null +++ b/test/fixtures/skip/input.md @@ -0,0 +1,23 @@ +# Alpha + +## Bravo + +### Example + +### Note + +## Charlie + +### Note + +## Delta + +### Examples + +## Echo + +### Notes + +## Foxtrot + +## Example diff --git a/test/fixtures/skip/output.json b/test/fixtures/skip/output.json new file mode 100644 index 0000000..eac38fc --- /dev/null +++ b/test/fixtures/skip/output.json @@ -0,0 +1,186 @@ +{ + "index": null, + "endIndex": null, + "map": { + "type": "list", + "ordered": false, + "spread": true, + "children": [ + { + "type": "listItem", + "loose": true, + "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", + "loose": true, + "spread": false, + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "link", + "title": null, + "url": "#bravo", + "children": [ + { + "type": "text", + "value": "Bravo" + } + ] + } + ] + } + ] + }, + { + "type": "listItem", + "loose": true, + "spread": false, + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "link", + "title": null, + "url": "#charlie", + "children": [ + { + "type": "text", + "value": "Charlie" + } + ] + } + ] + } + ] + }, + { + "type": "listItem", + "loose": true, + "spread": true, + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "link", + "title": null, + "url": "#delta", + "children": [ + { + "type": "text", + "value": "Delta" + } + ] + } + ] + }, + { + "type": "list", + "ordered": false, + "spread": false, + "children": [ + { + "type": "listItem", + "loose": false, + "spread": false, + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "link", + "title": null, + "url": "#examples", + "children": [ + { + "type": "text", + "value": "Examples" + } + ] + } + ] + } + ] + } + ] + } + ] + }, + { + "type": "listItem", + "loose": true, + "spread": false, + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "link", + "title": null, + "url": "#echo", + "children": [ + { + "type": "text", + "value": "Echo" + } + ] + } + ] + } + ] + }, + { + "type": "listItem", + "loose": false, + "spread": false, + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "link", + "title": null, + "url": "#foxtrot", + "children": [ + { + "type": "text", + "value": "Foxtrot" + } + ] + } + ] + } + ] + } + ] + } + ] + } + ] + } +}