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 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
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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 result = search(node, heading, settings)
var map = result.map

result.map = map.length === 0 ? null : contents(map, settings.tight)
Expand Down
7 changes: 5 additions & 2 deletions lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ module.exports = search

var toString = require('mdast-util-to-string')
var visit = require('unist-util-visit')
var is = require('unist-util-is')
var slugs = require('github-slugger')()

var HEADING = 'heading'

// Search a node for a location.
function search(root, expression, maxDepth) {
function search(root, expression, settings) {
var length = root.children.length
var depth = null
var lookingForToc = expression !== null
var maxDepth = settings.maxDepth || 6
var parents = settings.parents || root
var map = []
var headingIndex
var closingIndex
Expand Down Expand Up @@ -43,7 +46,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 (!is(parents, parent)) {
return
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dependencies": {
"github-slugger": "^1.1.1",
"mdast-util-to-string": "^1.0.2",
"unist-util-is": "^2.1.2",
"unist-util-visit": "^1.1.0"
},
"devDependencies": {
Expand Down
23 changes: 23 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,28 @@ 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.
Internally, it uses
[unist-util-is](https://github.com/syntax-tree/unist-util-is) to check.
Hence all types that can be passed in as first parameter can be used here,
including `Function`, `string`, `Object` and `Array.<Test>`.
Check
[documentation](https://github.com/syntax-tree/unist-util-is#readme)
for details.
(default: the first parameter `node`, which only allows top-level headings)

Example:

```json
{
"parents": ["root", "blockquote"]
}
```

This would allow headings under either `root` or `blockquote` 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": ["root", "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"
}
]
}
]
}
]
}
]
}
}