mdast-util-toc
Advanced tools
Comparing version 6.0.0 to 6.1.0
@@ -11,3 +11,3 @@ /** | ||
): import('mdast').List | ||
export type Node = import('unist').Node | ||
export type Node = import('mdast').Root | import('mdast').Content | ||
export type List = import('mdast').List | ||
@@ -22,11 +22,11 @@ export type ListItem = import('mdast').ListItem | ||
*/ | ||
tight?: boolean | ||
tight?: boolean | undefined | ||
/** | ||
* Whether to compile list-items as an ordered list, otherwise they are unordered. | ||
*/ | ||
ordered?: boolean | ||
ordered?: boolean | undefined | ||
/** | ||
* Add a prefix to links to headings in the table of contents. Useful for example when later going from mdast to hast and sanitizing with `hast-util-sanitize`. | ||
*/ | ||
prefix?: string | ||
prefix?: string | null | undefined | ||
} |
/** | ||
* @typedef {import('unist').Node} Node | ||
* @typedef {import('mdast').Root|import('mdast').Content} Node | ||
* @typedef {import('mdast').List} List | ||
@@ -12,3 +12,3 @@ * @typedef {import('mdast').ListItem} ListItem | ||
* @property {boolean} [ordered=false] Whether to compile list-items as an ordered list, otherwise they are unordered. | ||
* @property {string} [prefix=null] Add a prefix to links to headings in the table of contents. Useful for example when later going from mdast to hast and sanitizing with `hast-util-sanitize`. | ||
* @property {string|null} [prefix=null] Add a prefix to links to headings in the table of contents. Useful for example when later going from mdast to hast and sanitizing with `hast-util-sanitize`. | ||
*/ | ||
@@ -101,3 +101,3 @@ | ||
entry, | ||
// @ts-ignore It’s a `list`, we just checked. | ||
// @ts-expect-error It’s a `list`, we just checked. | ||
parent.children[parent.children.length - 1], | ||
@@ -162,18 +162,13 @@ settings | ||
) { | ||
// @ts-ignore Looks like a parent. | ||
// @ts-expect-error Looks like a parent. | ||
return all(node.children) | ||
} | ||
let copy = extend({}, node) | ||
delete copy.children | ||
delete copy.position | ||
copy = extend(true, {}, copy) | ||
if (node.children) { | ||
// @ts-ignore Looks like a parent. | ||
copy.children = all(node.children) | ||
if ('children' in node) { | ||
const {children, position, ...copy} = node | ||
return Object.assign(extend(true, {}, copy), {children: all(node.children)}) | ||
} | ||
return copy | ||
const {position, ...copy} = node | ||
return extend(true, {}, copy) | ||
} |
@@ -8,4 +8,4 @@ /** | ||
*/ | ||
export function toc(node: Node, options?: Options): Result | ||
export type Node = import('unist').Node | ||
export function toc(node: Node, options?: Options | undefined): Result | ||
export type Node = import('mdast').Root | import('mdast').Content | ||
export type List = import('mdast').List | ||
@@ -19,8 +19,8 @@ export type SearchOptions = import('./search.js').SearchOptions | ||
*/ | ||
heading?: string | ||
heading?: string | undefined | ||
} | ||
export type Result = { | ||
index: number | ||
endIndex: number | ||
map: List | ||
index: number | null | ||
endIndex: number | null | ||
map: List | null | ||
} |
/** | ||
* @typedef {import('unist').Node} Node | ||
* @typedef {import('mdast').Root|import('mdast').Content} Node | ||
* @typedef {import('mdast').List} List | ||
@@ -12,5 +12,5 @@ * @typedef {import('./search.js').SearchOptions} SearchOptions | ||
* @typedef Result | ||
* @property {number} index | ||
* @property {number} endIndex | ||
* @property {List} map | ||
* @property {number|null} index | ||
* @property {number|null} endIndex | ||
* @property {List|null} map | ||
*/ | ||
@@ -17,0 +17,0 @@ |
@@ -5,3 +5,3 @@ /** | ||
* @param {Node} root | ||
* @param {RegExp} expression | ||
* @param {RegExp|null} expression | ||
* @param {SearchOptions} settings | ||
@@ -12,6 +12,6 @@ * @returns {SearchResult} | ||
root: Node, | ||
expression: RegExp, | ||
expression: RegExp | null, | ||
settings: SearchOptions | ||
): SearchResult | ||
export type Node = import('unist').Node | ||
export type Node = import('mdast').Root | import('mdast').Content | ||
export type Heading = import('mdast').Heading | ||
@@ -22,3 +22,4 @@ export type PhrasingContent = import('mdast').PhrasingContent | ||
export type IsProps = import('unist-util-is').Props | ||
export type IsTestFunctionAnything = import('unist-util-is').TestFunctionAnything | ||
export type IsTestFunctionAnything = | ||
import('unist-util-is').TestFunctionAnything | ||
export type SearchOptions = { | ||
@@ -28,12 +29,17 @@ /** | ||
*/ | ||
skip?: string | ||
skip?: string | undefined | ||
parents?: | ||
| IsType | ||
| IsProps | ||
| IsTestFunctionAnything | ||
| Array<IsType | IsProps | IsTestFunctionAnything> | ||
| string | ||
| import('unist-util-is').Props | ||
| import('unist-util-is').TestFunctionAnything | ||
| ( | ||
| string | ||
| import('unist-util-is').Props | ||
| import('unist-util-is').TestFunctionAnything | ||
)[] | ||
| undefined | ||
/** | ||
* Maximum heading depth to include in the table of contents. This is inclusive: when set to `3`, level three headings are included (those with three hashes, `###`). | ||
*/ | ||
maxDepth?: Heading['depth'] | ||
maxDepth?: 1 | 2 | 3 | 4 | 5 | 6 | undefined | ||
} | ||
@@ -40,0 +46,0 @@ export type SearchEntry = { |
/** | ||
* @typedef {import('unist').Node} Node | ||
* @typedef {import('mdast').Root|import('mdast').Content} Node | ||
* @typedef {import('mdast').Heading} Heading | ||
@@ -38,3 +38,3 @@ * @typedef {import('mdast').PhrasingContent} PhrasingContent | ||
* @param {Node} root | ||
* @param {RegExp} expression | ||
* @param {RegExp|null} expression | ||
* @param {SearchOptions} settings | ||
@@ -45,6 +45,6 @@ * @returns {SearchResult} | ||
const skip = settings.skip && toExpression(settings.skip) | ||
const parents = convert(settings.parents || root) | ||
const parents = convert(settings.parents || ((d) => d === root)) | ||
/** @type {Array.<SearchEntry>} */ | ||
const map = [] | ||
/** @type {number} */ | ||
/** @type {number|undefined} */ | ||
let index | ||
@@ -65,3 +65,3 @@ /** @type {number} */ | ||
// <sindresorhus/eslint-plugin-unicorn#980> | ||
// @ts-ignore Looks like a parent. | ||
// @ts-expect-error Looks like a parent. | ||
endIndex: index ? endIndex || root.children.length : -1, // eslint-disable-line unicorn/explicit-length-check | ||
@@ -75,3 +75,3 @@ map | ||
/** @type {string} */ | ||
// @ts-ignore `hProperties` from <https://github.com/syntax-tree/mdast-util-to-hast> | ||
// @ts-expect-error `hProperties` from <https://github.com/syntax-tree/mdast-util-to-hast> | ||
const id = node.data && node.data.hProperties && node.data.hProperties.id | ||
@@ -85,3 +85,3 @@ const slug = slugs.slug(id || value) | ||
// Our opening heading. | ||
if (expression && !index && expression.test(value)) { | ||
if (position !== null && expression && !index && expression.test(value)) { | ||
index = position + 1 | ||
@@ -93,3 +93,8 @@ opening = node | ||
// Our closing heading. | ||
if (opening && !endIndex && node.depth <= opening.depth) { | ||
if ( | ||
position !== null && | ||
opening && | ||
!endIndex && | ||
node.depth <= opening.depth | ||
) { | ||
endIndex = position | ||
@@ -96,0 +101,0 @@ } |
{ | ||
"name": "mdast-util-toc", | ||
"version": "6.0.0", | ||
"version": "6.1.0", | ||
"description": "mdast utility to generate a table of contents from a tree", | ||
@@ -41,3 +41,2 @@ "license": "MIT", | ||
"@types/mdast": "^3.0.0", | ||
"@types/unist": "^2.0.0", | ||
"extend": "^3.0.0", | ||
@@ -64,5 +63,5 @@ "github-slugger": "^1.0.0", | ||
"typescript": "^4.0.0", | ||
"unified": "^9.0.0", | ||
"unified": "^10.0.0", | ||
"unist-builder": "^3.0.0", | ||
"xo": "^0.39.0" | ||
"xo": "^0.42.0" | ||
}, | ||
@@ -69,0 +68,0 @@ "scripts": { |
@@ -29,2 +29,3 @@ # mdast-util-toc | ||
```javascript | ||
/** @typedef {import('mdast').Root} Root */ | ||
import {u} from 'unist-builder' | ||
@@ -37,8 +38,10 @@ import {toc} from 'mdast-util-toc' | ||
```javascript | ||
const tree = u('root', [ | ||
u('heading', {depth: 1}, [u('text', 'Alpha')]), | ||
u('heading', {depth: 2}, [u('text', 'Bravo')]), | ||
u('heading', {depth: 3}, [u('text', 'Charlie')]), | ||
u('heading', {depth: 2}, [u('text', 'Delta')]) | ||
]) | ||
const tree = /** @type {Root} */ ( | ||
u('root', [ | ||
u('heading', {depth: 1}, [u('text', 'Alpha')]), | ||
u('heading', {depth: 2}, [u('text', 'Bravo')]), | ||
u('heading', {depth: 3}, [u('text', 'Charlie')]), | ||
u('heading', {depth: 2}, [u('text', 'Delta')]) | ||
]) | ||
) | ||
@@ -45,0 +48,0 @@ const table = toc(tree) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
24352
8
412
272
0
- Removed@types/unist@^2.0.0