Skip to content

add: option to allow headings under certain node type #53

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 6 commits into from
Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 7 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ var contents = require('./contents')
function toc(node, options) {
var settings = options || {}
var heading = settings.heading ? toExpression(settings.heading) : null
var result = search(node, heading, settings.maxDepth || 6)
var parentTypeWhiteList = settings.parents || []
var result = search(
node,
heading,
settings.maxDepth || 6,
parentTypeWhiteList
)
var map = result.map

result.map = map.length === 0 ? null : contents(map, settings.tight)
Expand Down
4 changes: 2 additions & 2 deletions lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var slugs = require('github-slugger')()
var HEADING = 'heading'

// Search a node for a location.
function search(root, expression, maxDepth) {
function search(root, expression, maxDepth, parentTypeWhiteList) {
var length = root.children.length
var depth = null
var lookingForToc = expression !== null
Expand Down Expand Up @@ -43,7 +43,7 @@ function search(root, expression, maxDepth) {
var value = toString(child)
var id = child.data && child.data.hProperties && child.data.hProperties.id

if (parent !== root) {
if (parent !== root && parentTypeWhiteList.indexOf(parent.type) < 0) {
return
}

Expand Down
16 changes: 16 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ If no `heading` is specified, creates a table of contents for all headings in

Links to headings are based on GitHub’s style.
Only top-level headings (those not in blockquotes or lists), are used.
(Change this default behavior by using option `parents` as described below)
The given node is not modified.

##### `options`
Expand All @@ -79,6 +80,21 @@ This is inclusive, thus, when set to `3`, level three headings, are included

Whether to compile list-items tightly (`boolean?`, default: `false`).

###### `options.parents`

Allows headings to be children of certain node types.
(`string[]`, default: `[]`, i.e. only top-level headings are used)

Example:

```json
{
"parents": ["section"]
}
```

This would allow headings both on top-level and under `section` node to be used.

##### Returns

An object representing the table of contents.
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/deep-headings-with-config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"parents": ["blockquote"]
}
11 changes: 11 additions & 0 deletions test/fixtures/deep-headings-with-config/input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Something if

## Something else

Text.

> ## heading in a blockquote

* ## heading in a list

# Something iffi
110 changes: 110 additions & 0 deletions test/fixtures/deep-headings-with-config/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
{
"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": "#something-if",
"children": [
{
"type": "text",
"value": "Something if"
}
]
}
]
},
{
"type": "list",
"ordered": false,
"spread": false,
"children": [
{
"type": "listItem",
"loose": false,
"spread": false,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "link",
"title": null,
"url": "#something-else",
"children": [
{
"type": "text",
"value": "Something else"
}
]
}
]
}
]
},
{
"type": "listItem",
"loose": false,
"spread": false,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "link",
"title": null,
"url": "#heading-in-a-blockquote",
"children": [
{
"type": "text",
"value": "heading in a blockquote"
}
]
}
]
}
]
}
]
}
]
},
{
"type": "listItem",
"loose": false,
"spread": false,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "link",
"title": null,
"url": "#something-iffi",
"children": [
{
"type": "text",
"value": "Something iffi"
}
]
}
]
}
]
}
]
}
}