mdast-util-definitions
Advanced tools
Comparing version 5.1.0 to 5.1.1
/** | ||
* Find definitions in `node`. | ||
* Uses CommonMark precedence, which means that earlier definitions are | ||
* preferred over duplicate later definitions. | ||
* | ||
@@ -10,2 +13,1 @@ * @param {Node} node | ||
export type Definition = import('mdast').Definition | ||
export type DefinitionVisitor = import('unist-util-visit').Visitor<Definition> |
21
index.js
/** | ||
* @typedef {import('mdast').Root|import('mdast').Content} Node | ||
* @typedef {import('mdast').Definition} Definition | ||
* @typedef {import('unist-util-visit').Visitor<Definition>} DefinitionVisitor | ||
*/ | ||
@@ -12,2 +11,5 @@ | ||
/** | ||
* Find definitions in `node`. | ||
* Uses CommonMark precedence, which means that earlier definitions are | ||
* preferred over duplicate later definitions. | ||
* | ||
@@ -17,3 +19,3 @@ * @param {Node} node | ||
export function definitions(node) { | ||
/** @type {Object.<string, Definition>} */ | ||
/** @type {Record<string, Definition>} */ | ||
const cache = Object.create(null) | ||
@@ -25,8 +27,3 @@ | ||
visit(node, 'definition', ondefinition) | ||
return getDefinition | ||
/** @type {DefinitionVisitor} */ | ||
function ondefinition(definition) { | ||
visit(node, 'definition', (definition) => { | ||
const id = clean(definition.identifier) | ||
@@ -36,6 +33,8 @@ if (id && !own.call(cache, id)) { | ||
} | ||
} | ||
}) | ||
return definition | ||
/** | ||
* Get a node from the bound definition-cache. | ||
* Get a node from the bound definition cache. | ||
* | ||
@@ -45,3 +44,3 @@ * @param {string} identifier | ||
*/ | ||
function getDefinition(identifier) { | ||
function definition(identifier) { | ||
const id = clean(identifier) | ||
@@ -48,0 +47,0 @@ return id && own.call(cache, id) ? cache[id] : null |
{ | ||
"name": "mdast-util-definitions", | ||
"version": "5.1.0", | ||
"version": "5.1.1", | ||
"description": "mdast utility to find definition nodes in a tree", | ||
@@ -40,3 +40,3 @@ "license": "MIT", | ||
"@types/unist": "^2.0.0", | ||
"unist-util-visit": "^3.0.0" | ||
"unist-util-visit": "^4.0.0" | ||
}, | ||
@@ -47,5 +47,5 @@ "devDependencies": { | ||
"prettier": "^2.0.0", | ||
"remark": "^13.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"remark": "^14.0.0", | ||
"remark-cli": "^10.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"rimraf": "^3.0.0", | ||
@@ -55,3 +55,3 @@ "tape": "^5.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.42.0" | ||
"xo": "^0.50.0" | ||
}, | ||
@@ -58,0 +58,0 @@ "scripts": { |
108
readme.md
@@ -11,13 +11,35 @@ # mdast-util-definitions | ||
[**mdast**][mdast] utility to get definitions by `identifier`. | ||
[mdast][] utility to find definitions by `identifier`. | ||
Supports funky keys, like `__proto__` or `toString`. | ||
## Contents | ||
* [What is this?](#what-is-this) | ||
* [When should I use this?](#when-should-i-use-this) | ||
* [Install](#install) | ||
* [Use](#use) | ||
* [API](#api) | ||
* [`definitions(node)`](#definitionsnode) | ||
* [`definition(identifier)`](#definitionidentifier) | ||
* [Types](#types) | ||
* [Compatibility](#compatibility) | ||
* [Security](#security) | ||
* [Related](#related) | ||
* [Contribute](#contribute) | ||
* [License](#license) | ||
## What is this? | ||
This package is a tiny utility that lets you find definitions. | ||
## When should I use this? | ||
This utility can be useful because definitions can occur after the things that | ||
reference them. | ||
It’s small and protects against prototype pollution. | ||
## Install | ||
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): | ||
Node 12+ is needed to use it and it must be `import`ed instead of `require`d. | ||
This package is [ESM only][esm]. | ||
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]: | ||
[npm][]: | ||
```sh | ||
@@ -27,9 +49,23 @@ npm install mdast-util-definitions | ||
In Deno with [`esm.sh`][esmsh]: | ||
```js | ||
import {definitions} from 'https://esm.sh/mdast-util-definitions@5' | ||
``` | ||
In browsers with [`esm.sh`][esmsh]: | ||
```html | ||
<script type="module"> | ||
import {definitions} from 'https://esm.sh/mdast-util-definitions@5?bundle' | ||
</script> | ||
``` | ||
## Use | ||
```js | ||
import {remark} from 'remark' | ||
import {fromMarkdown} from 'mdast-util-from-markdown' | ||
import {definitions} from 'mdast-util-definitions' | ||
const tree = remark().parse('[example]: https://example.com "Example"') | ||
const tree = fromMarkdown('[example]: https://example.com "Example"') | ||
@@ -47,31 +83,39 @@ const definition = definitions(tree) | ||
This package exports the following identifiers: `definitions`. | ||
This package exports the identifier `definitions`. | ||
There is no default export. | ||
### `definitions(tree)` | ||
### `definitions(node)` | ||
Create a cache of all [definition][]s in [`tree`][node]. | ||
Find [definition][]s in `node` ([`Node`][node]). | ||
Uses CommonMark precedence, which means that earlier definitions are preferred | ||
over duplicate later definitions. | ||
Uses CommonMark precedence: prefers the first definitions for duplicate | ||
definitions. | ||
###### Returns | ||
[`Function`][fn-definition] | ||
`definition` ([`Function`][fn-definition]). | ||
### `definition(identifier)` | ||
###### Parameters | ||
Get a node from the bound definition cache by its `identifier` (`string`). | ||
* `identifier` (`string`) — [Identifier][] of [definition][]. | ||
###### Returns | ||
[`Node?`][node] — [Definition][], if found. | ||
[Definition][], if found ([`Node?`][node]) | ||
## Types | ||
This package is fully typed with [TypeScript][]. | ||
There are no additional exported types. | ||
## Compatibility | ||
Projects maintained by the unified collective are compatible with all maintained | ||
versions of Node.js. | ||
As of now, that is Node.js 12.20+, 14.14+, and 16.0+. | ||
Our projects sometimes work with older versions, but this is not guaranteed. | ||
## Security | ||
Use of `mdast-util-definitions` does not involve [**hast**][hast] or user | ||
content so there are no openings for [cross-site scripting (XSS)][xss] attacks. | ||
Use of `mdast-util-definitions` does not involve **[hast][]** or user content so | ||
there are no openings for [cross-site scripting (XSS)][xss] attacks. | ||
Additionally, safe guards are in place to protect against prototype poisoning. | ||
@@ -86,4 +130,4 @@ | ||
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get | ||
started. | ||
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for | ||
ways to get started. | ||
See [`support.md`][support] for ways to get help. | ||
@@ -133,8 +177,16 @@ | ||
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md | ||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c | ||
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md | ||
[esmsh]: https://esm.sh | ||
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md | ||
[typescript]: https://www.typescriptlang.org | ||
[health]: https://github.com/syntax-tree/.github | ||
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md | ||
[support]: https://github.com/syntax-tree/.github/blob/main/support.md | ||
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md | ||
[mdast]: https://github.com/syntax-tree/mdast | ||
@@ -148,6 +200,4 @@ | ||
[identifier]: https://github.com/syntax-tree/mdast#association | ||
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting | ||
[hast]: https://github.com/syntax-tree/hast |
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
9944
56
198
+ Addedunist-util-visit@4.1.2(transitive)
+ Addedunist-util-visit-parents@5.1.3(transitive)
- Removedunist-util-visit@3.1.0(transitive)
- Removedunist-util-visit-parents@4.1.1(transitive)
Updatedunist-util-visit@^4.0.0