Socket
Socket
Sign inDemoInstall

mdast-util-toc

Package Overview
Dependencies
Maintainers
2
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mdast-util-toc - npm Package Compare versions

Comparing version 5.0.3 to 5.0.4

109

lib/contents.js

@@ -7,19 +7,10 @@ 'use strict'

var LIST = 'list'
var LIST_ITEM = 'listItem'
var PARAGRAPH = 'paragraph'
var LINK = 'link'
var LINK_REFERENCE = 'linkReference'
var FOOTNOTE = 'footnote'
var FOOTNOTE_REFERENCE = 'footnoteReference'
// Transform a list of heading objects to a markdown list.
function contents(map, tight, prefix) {
var table = {type: 'list', ordered: false, spread: false, children: []}
var minDepth = Infinity
var index = -1
var length = map.length
var table
// Find minimum depth.
while (++index < length) {
while (++index < map.length) {
if (map[index].depth < minDepth) {

@@ -33,13 +24,10 @@ minDepth = map[index].depth

while (++index < length) {
while (++index < map.length) {
map[index].depth -= minDepth - 1
}
// Construct the main list.
table = list()
// Add TOC to list.
index = -1
while (++index < length) {
while (++index < map.length) {
insert(map[index], table, tight, prefix)

@@ -53,54 +41,46 @@ }

function insert(entry, parent, tight, prefix) {
var children = parent.children
var length = children.length
var last = children[length - 1]
var index
var siblings = parent.children
var tail = siblings[siblings.length - 1]
var index = -1
var item
if (entry.depth === 1) {
item = listItem()
item.children.push({
type: PARAGRAPH,
siblings.push({
type: 'listItem',
spread: false,
children: [
{
type: LINK,
title: null,
url: '#' + (prefix || '') + entry.id,
children: all(entry.children)
type: 'paragraph',
children: [
{
type: 'link',
title: null,
url: '#' + (prefix || '') + entry.id,
children: all(entry.children)
}
]
}
]
})
children.push(item)
} else if (last && last.type === LIST_ITEM) {
insert(entry, last, tight, prefix)
} else if (last && last.type === LIST) {
} else if (tail && tail.type === 'listItem') {
insert(entry, siblings[siblings.length - 1], tight, prefix)
} else if (tail && tail.type === 'list') {
entry.depth--
insert(entry, last, tight, prefix)
} else if (parent.type === LIST) {
item = listItem()
insert(entry, tail, tight, prefix)
} else if (parent.type === 'list') {
item = {type: 'listItem', spread: false, children: []}
siblings.push(item)
insert(entry, item, tight, prefix)
children.push(item)
} else {
item = list()
item = {type: 'list', ordered: false, spread: false, children: []}
siblings.push(item)
entry.depth--
insert(entry, item, tight, prefix)
children.push(item)
}
// Properly style list-items with new lines.
parent.spread = !tight
if (parent.type === LIST && parent.spread) {
if (parent.type === 'list' && !tight) {
parent.spread = false
index = -1
while (++index < length) {
if (children[index].children.length > 1) {
while (++index < siblings.length) {
if (siblings[index].children.length > 1) {
parent.spread = true

@@ -110,2 +90,4 @@ break

}
} else {
parent.spread = !tight
}

@@ -116,7 +98,8 @@ }

var result = []
var length = children ? children.length : 0
var index = -1
while (++index < length) {
result = result.concat(one(children[index]))
if (children) {
while (++index < children.length) {
result = result.concat(one(children[index]))
}
}

@@ -131,6 +114,6 @@

if (
node.type === LINK ||
node.type === LINK_REFERENCE ||
node.type === FOOTNOTE ||
node.type === FOOTNOTE_REFERENCE
node.type === 'link' ||
node.type === 'linkReference' ||
node.type === 'footnote' ||
node.type === 'footnoteReference'
) {

@@ -153,11 +136,1 @@ return all(node.children)

}
// Create a list.
function list() {
return {type: LIST, ordered: false, spread: false, children: []}
}
// Create a list item.
function listItem() {
return {type: LIST_ITEM, spread: false, children: []}
}

@@ -14,11 +14,10 @@ 'use strict'

var result = search(node, heading, settings)
var map = result.map
result.map =
map.length === 0 ? null : contents(map, settings.tight, settings.prefix)
result.map = result.map.length
? contents(result.map, settings.tight, settings.prefix)
: null
// No given heading.
if (!heading) {
result.index = null
result.endIndex = null
result.endIndex = result.index = null
}

@@ -25,0 +24,0 @@

@@ -11,20 +11,11 @@ 'use strict'

var heading = convert('heading')
// Search a node for a location.
function search(root, expression, settings) {
var length = root.children.length
var depth = null
var lookingForToc = expression !== null
var maxDepth = settings.maxDepth || 6
var skip = settings.skip ? toExpression(settings.skip) : null
var skip = settings.skip && toExpression(settings.skip)
var parents = convert(settings.parents || root)
var map = []
var headingIndex
var closingIndex
var index
var endIndex
var opening
if (!lookingForToc) {
headingIndex = -1
}
slugs.reset()

@@ -36,17 +27,13 @@

if (headingIndex && !closingIndex) {
closingIndex = length + 1
return {
index: index || -1,
endIndex: index ? endIndex || root.children.length : -1,
map: map
}
if (headingIndex === undefined) {
headingIndex = -1
closingIndex = -1
map = []
}
return {index: headingIndex, endIndex: closingIndex, map: map}
function onheading(child, index, parent) {
var value = toString(child)
var id = child.data && child.data.hProperties && child.data.hProperties.id
function onheading(node, position, parent) {
var value = toString(node)
/* istanbul ignore next - to do: remove this when `remark-attr` is up to
* date w/ micromark. */
var id = node.data && node.data.hProperties && node.data.hProperties.id
var slug = slugs.slug(id || value)

@@ -58,37 +45,24 @@

if (lookingForToc) {
if (isClosingHeading(child, depth)) {
closingIndex = index
lookingForToc = false
}
// Our opening heading.
if (expression && !index && expression.test(value)) {
index = position + 1
opening = node
return
}
if (isOpeningHeading(child, depth, expression)) {
headingIndex = index + 1
depth = child.depth
}
// Our closing heading.
if (opening && !endIndex && node.depth <= opening.depth) {
endIndex = position
}
// A non-empty heading after the closing (if we were looking for one).
if (
!lookingForToc &&
value &&
child.depth <= maxDepth &&
(endIndex || !expression) &&
(!settings.maxDepth || node.depth <= settings.maxDepth) &&
(!skip || !skip.test(value))
) {
map.push({
depth: child.depth,
children: child.children,
id: slug
})
map.push({depth: node.depth, children: node.children, id: slug})
}
}
}
// Check if `node` is the main heading.
function isOpeningHeading(node, depth, expression) {
return depth === null && heading(node) && expression.test(toString(node))
}
// Check if `node` is the next heading.
function isClosingHeading(node, depth) {
return depth && heading(node) && node.depth <= depth
}
{
"name": "mdast-util-toc",
"version": "5.0.3",
"version": "5.0.4",
"description": "mdast utility to generate a table of contents from a tree",

@@ -44,22 +44,23 @@ "license": "MIT",

"devDependencies": {
"browserify": "^16.0.0",
"dtslint": "^3.0.0",
"browserify": "^17.0.0",
"dtslint": "^4.0.0",
"nyc": "^15.0.0",
"prettier": "^2.0.0",
"remark": "^12.0.0",
"remark-attr": "^0.10.0",
"remark-cli": "^8.0.0",
"remark-footnotes": "^1.0.0",
"remark-parse": "^8.0.0",
"remark-preset-wooorm": "^7.0.0",
"remark-usage": "^8.0.0",
"tape": "^4.0.0",
"tinyify": "^2.0.0",
"typescript": "^3.0.0",
"remark": "^13.0.0",
"remark-attr": "^0.11.0",
"remark-cli": "^9.0.0",
"remark-footnotes": "^3.0.0",
"remark-gfm": "^1.0.0",
"remark-parse": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"remark-usage": "^9.0.0",
"tape": "^5.0.0",
"tinyify": "^3.0.0",
"typescript": "^4.0.0",
"unified": "^9.0.0",
"unist-builder": "^2.0.0",
"xo": "^0.29.0"
"xo": "^0.34.0"
},
"scripts": {
"format": "remark . -qfo && prettier --write \"**/*.{js,ts}\" && xo --fix",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"build-bundle": "browserify . -s mdastUtilTOC > mdast-util-toc.js",

@@ -90,4 +91,9 @@ "build-mangle": "browserify . -s mdastUtilTOC -p tinyify > mdast-util-toc.min.js",

"esnext": false,
"rules": {
"no-multi-assign": "off",
"unicorn/explicit-length-check": "off",
"unicorn/prefer-optional-catch-binding": "off"
},
"ignores": [
"types",
"types/",
"mdast-util-toc.js"

@@ -94,0 +100,0 @@ ]

@@ -128,6 +128,7 @@ # mdast-util-toc

* `index` (`number?`)
— [Index][] of the found table of contents [heading][] in `tree`.
— [Index][] of the node right after the table of contents [heading][].
`-1` if no heading was found, `null` if no `heading` was given
* `endIndex` (`number?`)
— [Index][] of the last node after `heading` before the TOC starts.
— [Index][] of the first node after `heading` that is not part of its
section.
`-1` if no heading was found, `null` if no `heading` was given,

@@ -221,5 +222,5 @@ same as `index` if there are no nodes between `heading` and the

[chat-badge]: https://img.shields.io/badge/chat-spectrum-7b16ff.svg
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg
[chat]: https://spectrum.chat/unified/syntax-tree
[chat]: https://github.com/syntax-tree/unist/discussions

@@ -232,7 +233,7 @@ [npm]: https://docs.npmjs.com/cli/install

[contributing]: https://github.com/syntax-tree/.github/blob/master/contributing.md
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
[support]: https://github.com/syntax-tree/.github/blob/master/support.md
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
[coc]: https://github.com/syntax-tree/.github/blob/master/code-of-conduct.md
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md

@@ -239,0 +240,0 @@ [mdast]: https://github.com/syntax-tree/mdast

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc