mdast-util-definitions
Advanced tools
Comparing version 5.1.1 to 5.1.2
@@ -1,12 +0,2 @@ | ||
/** | ||
* Find definitions in `node`. | ||
* Uses CommonMark precedence, which means that earlier definitions are | ||
* preferred over duplicate later definitions. | ||
* | ||
* @param {Node} node | ||
*/ | ||
export function definitions( | ||
node: Node | ||
): (identifier: string) => Definition | null | ||
export type Node = import('mdast').Root | import('mdast').Content | ||
export type Definition = import('mdast').Definition | ||
export {definitions} from './lib/index.js' | ||
export type GetDefinition = import('./lib/index.js').GetDefinition |
51
index.js
/** | ||
* @typedef {import('mdast').Root|import('mdast').Content} Node | ||
* @typedef {import('mdast').Definition} Definition | ||
* @typedef {import('./lib/index.js').GetDefinition} GetDefinition | ||
*/ | ||
import {visit} from 'unist-util-visit' | ||
const own = {}.hasOwnProperty | ||
/** | ||
* Find definitions in `node`. | ||
* Uses CommonMark precedence, which means that earlier definitions are | ||
* preferred over duplicate later definitions. | ||
* | ||
* @param {Node} node | ||
*/ | ||
export function definitions(node) { | ||
/** @type {Record<string, Definition>} */ | ||
const cache = Object.create(null) | ||
if (!node || !node.type) { | ||
throw new Error('mdast-util-definitions expected node') | ||
} | ||
visit(node, 'definition', (definition) => { | ||
const id = clean(definition.identifier) | ||
if (id && !own.call(cache, id)) { | ||
cache[id] = definition | ||
} | ||
}) | ||
return definition | ||
/** | ||
* Get a node from the bound definition cache. | ||
* | ||
* @param {string} identifier | ||
* @returns {Definition|null} | ||
*/ | ||
function definition(identifier) { | ||
const id = clean(identifier) | ||
return id && own.call(cache, id) ? cache[id] : null | ||
} | ||
} | ||
/** | ||
* @param {string} [value] | ||
* @returns {string} | ||
*/ | ||
function clean(value) { | ||
return String(value || '').toUpperCase() | ||
} | ||
export {definitions} from './lib/index.js' |
{ | ||
"name": "mdast-util-definitions", | ||
"version": "5.1.1", | ||
"version": "5.1.2", | ||
"description": "mdast utility to find definition nodes in a tree", | ||
@@ -34,2 +34,3 @@ "license": "MIT", | ||
"files": [ | ||
"lib/", | ||
"index.d.ts", | ||
@@ -44,20 +45,18 @@ "index.js" | ||
"devDependencies": { | ||
"@types/tape": "^4.0.0", | ||
"@types/node": "^18.0.0", | ||
"c8": "^7.0.0", | ||
"mdast-util-from-markdown": "^1.0.0", | ||
"prettier": "^2.0.0", | ||
"remark": "^14.0.0", | ||
"remark-cli": "^10.0.0", | ||
"remark-cli": "^11.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"rimraf": "^3.0.0", | ||
"tape": "^5.0.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.50.0" | ||
"xo": "^0.53.0" | ||
}, | ||
"scripts": { | ||
"prepack": "npm run build && npm run format", | ||
"build": "rimraf \"*.d.ts\" && tsc && type-coverage", | ||
"build": "tsc --build --clean && tsc --build && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"test-api": "node test.js", | ||
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js", | ||
"test-api": "node --conditions development test.js", | ||
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
@@ -64,0 +63,0 @@ }, |
@@ -20,4 +20,4 @@ # mdast-util-definitions | ||
* [API](#api) | ||
* [`definitions(node)`](#definitionsnode) | ||
* [`definition(identifier)`](#definitionidentifier) | ||
* [`definitions(tree)`](#definitionstree) | ||
* [`GetDefinition`](#getdefinition) | ||
* [Types](#types) | ||
@@ -43,3 +43,3 @@ * [Compatibility](#compatibility) | ||
This package is [ESM only][esm]. | ||
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]: | ||
In Node.js (version 14.14+ and 16.0+), install with [npm][]: | ||
@@ -83,22 +83,34 @@ ```sh | ||
This package exports the identifier `definitions`. | ||
This package exports the identifier [`definitions`][api-definitions]. | ||
There is no default export. | ||
### `definitions(node)` | ||
### `definitions(tree)` | ||
Find [definition][]s in `node` ([`Node`][node]). | ||
Uses CommonMark precedence, which means that earlier definitions are preferred | ||
over duplicate later definitions. | ||
Find definitions in `tree`. | ||
Uses CommonMark precedence, which means that earlier definitions are | ||
preferred over duplicate later definitions. | ||
###### Parameters | ||
* `tree` ([`Node`][node]) | ||
— tree to check | ||
###### Returns | ||
`definition` ([`Function`][fn-definition]). | ||
Getter ([`GetDefinition`][api-getdefinition]). | ||
### `definition(identifier)` | ||
### `GetDefinition` | ||
Get a node from the bound definition cache by its `identifier` (`string`). | ||
Get a definition by identifier (TypeScript type). | ||
###### Parameters | ||
* `identifier` (`string`, optional) | ||
— identifier of definition | ||
###### Returns | ||
[Definition][], if found ([`Node?`][node]) | ||
Definition corresponding to `identifier` ([`Definition`][definition]) or | ||
`null`. | ||
@@ -108,3 +120,3 @@ ## Types | ||
This package is fully typed with [TypeScript][]. | ||
There are no additional exported types. | ||
It exports the additional type [`GetDefinition`][api-getdefinition]. | ||
@@ -115,3 +127,3 @@ ## Compatibility | ||
versions of Node.js. | ||
As of now, that is Node.js 12.20+, 14.14+, and 16.0+. | ||
As of now, that is Node.js 14.14+ and 16.0+. | ||
Our projects sometimes work with older versions, but this is not guaranteed. | ||
@@ -196,4 +208,2 @@ | ||
[fn-definition]: #definitionidentifier | ||
[definition]: https://github.com/syntax-tree/mdast#definition | ||
@@ -204,1 +214,5 @@ | ||
[hast]: https://github.com/syntax-tree/hast | ||
[api-definitions]: #definitionstree | ||
[api-getdefinition]: #getdefinition |
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
10911
9
7
83
212