Skip to content

Add support for skipping headings #61

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

Merged
merged 1 commit into from
Jun 18, 2019
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: 1 addition & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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')
}
9 changes: 8 additions & 1 deletion lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions lib/to-expression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'

module.exports = toExpression

// Transform a string into an applicable expression.
function toExpression(value) {
return new RegExp('^(' + value + ')$', 'i')
}
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/skip/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"skip": "example|notes?"
}
23 changes: 23 additions & 0 deletions test/fixtures/skip/input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Alpha

## Bravo

### Example

### Note

## Charlie

### Note

## Delta

### Examples

## Echo

### Notes

## Foxtrot

## Example
186 changes: 186 additions & 0 deletions test/fixtures/skip/output.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
]
}
]
}
]
}
]
}
]
}
}