From 37560ad93b85da0365bbce44a668114be9d16ead Mon Sep 17 00:00:00 2001 From: LaySent Date: Mon, 18 Feb 2019 21:59:03 +0800 Subject: [PATCH 1/6] add: option to allow headings under certain node type --- lib/index.js | 8 +- lib/search.js | 4 +- readme.md | 16 +++ .../deep-headings-with-config/config.json | 3 + .../deep-headings-with-config/input.md | 11 ++ .../deep-headings-with-config/output.json | 110 ++++++++++++++++++ 6 files changed, 149 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/deep-headings-with-config/config.json create mode 100644 test/fixtures/deep-headings-with-config/input.md create mode 100644 test/fixtures/deep-headings-with-config/output.json diff --git a/lib/index.js b/lib/index.js index a409e61..edbda0d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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) diff --git a/lib/search.js b/lib/search.js index 99a7158..14cf2fc 100644 --- a/lib/search.js +++ b/lib/search.js @@ -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 @@ -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 } diff --git a/readme.md b/readme.md index 23d43ca..3e39f32 100644 --- a/readme.md +++ b/readme.md @@ -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` @@ -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. diff --git a/test/fixtures/deep-headings-with-config/config.json b/test/fixtures/deep-headings-with-config/config.json new file mode 100644 index 0000000..bf41369 --- /dev/null +++ b/test/fixtures/deep-headings-with-config/config.json @@ -0,0 +1,3 @@ +{ + "parents": ["blockquote"] +} diff --git a/test/fixtures/deep-headings-with-config/input.md b/test/fixtures/deep-headings-with-config/input.md new file mode 100644 index 0000000..e41f57a --- /dev/null +++ b/test/fixtures/deep-headings-with-config/input.md @@ -0,0 +1,11 @@ +# Something if + +## Something else + +Text. + +> ## heading in a blockquote + +* ## heading in a list + +# Something iffi diff --git a/test/fixtures/deep-headings-with-config/output.json b/test/fixtures/deep-headings-with-config/output.json new file mode 100644 index 0000000..e64a6cf --- /dev/null +++ b/test/fixtures/deep-headings-with-config/output.json @@ -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" + } + ] + } + ] + } + ] + } + ] + } +} From 7487fe2d34850b386ddbde73735253b5dc55414e Mon Sep 17 00:00:00 2001 From: LaySent Date: Mon, 19 Feb 2018 12:03:33 +0800 Subject: [PATCH 2/6] refact: function signature pass settings as a whole, instead of options one by one. --- lib/index.js | 8 +------- lib/search.js | 4 +++- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/index.js b/lib/index.js index edbda0d..6b09044 100644 --- a/lib/index.js +++ b/lib/index.js @@ -9,13 +9,7 @@ var contents = require('./contents') function toc(node, options) { var settings = options || {} var heading = settings.heading ? toExpression(settings.heading) : null - var parentTypeWhiteList = settings.parents || [] - var result = search( - node, - heading, - settings.maxDepth || 6, - parentTypeWhiteList - ) + var result = search(node, heading, settings) var map = result.map result.map = map.length === 0 ? null : contents(map, settings.tight) diff --git a/lib/search.js b/lib/search.js index 14cf2fc..866dd04 100644 --- a/lib/search.js +++ b/lib/search.js @@ -9,10 +9,12 @@ var slugs = require('github-slugger')() var HEADING = 'heading' // Search a node for a location. -function search(root, expression, maxDepth, parentTypeWhiteList) { +function search(root, expression, settings) { var length = root.children.length var depth = null var lookingForToc = expression !== null + var maxDepth = settings.maxDepth || 6 + var parentTypeWhiteList = settings.parents || [] var map = [] var headingIndex var closingIndex From 5ef02373000b17a7053f20aefe7201fe7e77113a Mon Sep 17 00:00:00 2001 From: LaySent Date: Mon, 19 Feb 2018 12:48:22 +0800 Subject: [PATCH 3/6] modify: use unist-util-is to validate parent type --- lib/search.js | 5 +++-- package.json | 1 + readme.md | 9 ++++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/search.js b/lib/search.js index 866dd04..7736e42 100644 --- a/lib/search.js +++ b/lib/search.js @@ -4,6 +4,7 @@ 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' @@ -14,7 +15,7 @@ function search(root, expression, settings) { var depth = null var lookingForToc = expression !== null var maxDepth = settings.maxDepth || 6 - var parentTypeWhiteList = settings.parents || [] + var parents = settings.parents || [] var map = [] var headingIndex var closingIndex @@ -45,7 +46,7 @@ function search(root, expression, settings) { var value = toString(child) var id = child.data && child.data.hProperties && child.data.hProperties.id - if (parent !== root && parentTypeWhiteList.indexOf(parent.type) < 0) { + if (parent !== root && !is(parents, parent)) { return } diff --git a/package.json b/package.json index fd23cc1..ec81b7c 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/readme.md b/readme.md index 3e39f32..0071e4c 100644 --- a/readme.md +++ b/readme.md @@ -83,7 +83,14 @@ 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) +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.`. +Check +[documentation](https://github.com/syntax-tree/unist-util-is#readme) +for details. +(default: `[]`, i.e. only top-level headings are used) Example: From cabd83ab0769a993f72c8ece905eea4055387ea7 Mon Sep 17 00:00:00 2001 From: LaySent Date: Wed, 20 Feb 2019 05:51:05 +0800 Subject: [PATCH 4/6] modify: use blockquote as example type --- readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 0071e4c..5cb40b3 100644 --- a/readme.md +++ b/readme.md @@ -96,11 +96,11 @@ Example: ```json { - "parents": ["section"] + "parents": ["blockquote"] } ``` -This would allow headings both on top-level and under `section` node to be used. +This would allow headings both on top-level and under `blockquote` to be used. ##### Returns From 698a548bbc9c653f94e12ffb2a989f53d730e40b Mon Sep 17 00:00:00 2001 From: LaySent Date: Wed, 20 Feb 2019 06:06:21 +0800 Subject: [PATCH 5/6] modify: allows to exclude top-level headings using config --- lib/search.js | 5 +++-- readme.md | 4 ++-- test/fixtures/deep-headings-with-config/config.json | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/search.js b/lib/search.js index 7736e42..a7000d5 100644 --- a/lib/search.js +++ b/lib/search.js @@ -15,7 +15,8 @@ function search(root, expression, settings) { var depth = null var lookingForToc = expression !== null var maxDepth = settings.maxDepth || 6 - var parents = settings.parents || [] + var parents = + typeof settings.parents === 'undefined' ? 'root' : settings.parents var map = [] var headingIndex var closingIndex @@ -46,7 +47,7 @@ function search(root, expression, settings) { var value = toString(child) var id = child.data && child.data.hProperties && child.data.hProperties.id - if (parent !== root && !is(parents, parent)) { + if (!is(parents, parent)) { return } diff --git a/readme.md b/readme.md index 5cb40b3..ec133b3 100644 --- a/readme.md +++ b/readme.md @@ -90,13 +90,13 @@ including `Function`, `string`, `Object` and `Array.`. Check [documentation](https://github.com/syntax-tree/unist-util-is#readme) for details. -(default: `[]`, i.e. only top-level headings are used) +(default: `"root"`, which only allows top-level headings) Example: ```json { - "parents": ["blockquote"] + "parents": ["root", "blockquote"] } ``` diff --git a/test/fixtures/deep-headings-with-config/config.json b/test/fixtures/deep-headings-with-config/config.json index bf41369..4f9487e 100644 --- a/test/fixtures/deep-headings-with-config/config.json +++ b/test/fixtures/deep-headings-with-config/config.json @@ -1,3 +1,3 @@ { - "parents": ["blockquote"] + "parents": ["root", "blockquote"] } From 5ecd174a1d873e7c4203d65e3aaecc70ffdcbb6f Mon Sep 17 00:00:00 2001 From: LaySent Date: Wed, 20 Feb 2019 08:04:39 +0800 Subject: [PATCH 6/6] modify: keep same api behavior when selecting headings by default only headings on second layer of tree will be included --- lib/search.js | 3 +-- readme.md | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/search.js b/lib/search.js index a7000d5..17c1b7b 100644 --- a/lib/search.js +++ b/lib/search.js @@ -15,8 +15,7 @@ function search(root, expression, settings) { var depth = null var lookingForToc = expression !== null var maxDepth = settings.maxDepth || 6 - var parents = - typeof settings.parents === 'undefined' ? 'root' : settings.parents + var parents = settings.parents || root var map = [] var headingIndex var closingIndex diff --git a/readme.md b/readme.md index ec133b3..894b1d1 100644 --- a/readme.md +++ b/readme.md @@ -90,7 +90,7 @@ including `Function`, `string`, `Object` and `Array.`. Check [documentation](https://github.com/syntax-tree/unist-util-is#readme) for details. -(default: `"root"`, which only allows top-level headings) +(default: the first parameter `node`, which only allows top-level headings) Example: @@ -100,7 +100,7 @@ Example: } ``` -This would allow headings both on top-level and under `blockquote` to be used. +This would allow headings under either `root` or `blockquote` to be used. ##### Returns