mdast-util-definitions
mdast-util-definitions is a small, focused utility within the unified ecosystem designed to efficiently locate definition nodes within an mdast (Markdown Abstract Syntax Tree) based on their `identifier`. Currently at stable version 6.0.0, the package primarily sees updates related to Node.js version compatibility, ESM adoption, and type improvements, consistent with the unified collective's release cadence. A key differentiator is its ability to find definitions regardless of their position in the tree (even after references), its robustness against prototype pollution, and its adherence to CommonMark precedence rules (favoring earlier definitions in case of duplicates). Since version 5.0.0, it is an ESM-only package, requiring Node.js 16+ from version 6.0.0. It provides TypeScript types for enhanced developer experience.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to `require()` this package in a CommonJS environment, but it is ESM-only since v5.0.0.fixConvert your project or specific file to ESM and use `import { definitions } from 'mdast-util-definitions'`. -
TypeError: Cannot read properties of null (reading '...')
cause Your code is expecting `definitions(tree)('identifier')` to return `null` when a definition is not found, but it now returns `undefined` since v6.0.0.fixUpdate your code to check for `undefined` instead of `null`. -
SyntaxError: The requested module 'mdast-util-definitions' does not provide an export named 'default'
cause Attempting to use a default import, e.g., `import definitions from 'mdast-util-definitions'`, when there is no default export.fixUse a named import: `import { definitions } from 'mdast-util-definitions'`. -
TypeError: definitions is not a function
cause This error can occur if you're trying to call `definitions()` directly without first passing the `tree` to it, or if there was a problem with the import.fixRemember to call `definitions(tree)` first, which returns another function (a getter). Then call the returned getter with your identifier: `const getDefinition = definitions(tree); getDefinition('my-id');`
Warnings
- breaking Version 6.0.0 changed to require Node.js 16 or newer. Older Node.js versions are no longer supported.
- breaking Since version 5.0.0, this package is ESM-only. CommonJS `require()` statements are no longer supported and will result in an `ERR_REQUIRE_ESM` error.
- breaking As of v6.0.0, `definitions(tree)('identifier')` now returns `undefined` when a definition is not found, instead of `null`.
- breaking Version 6.0.0 changed to use `export` map, preventing direct imports of private APIs (e.g., `mdast-util-definitions/index.js`).
- breaking Type definitions were added in v3.0.0, and updated in v5.1.0 to use `mdast` types, then again in v6.0.0 with `@types/mdast`. This might cause type conflicts if your project uses older or incompatible `@types/mdast` versions.
Install
-
npm install mdast-util-definitions -
yarn add mdast-util-definitions -
pnpm add mdast-util-definitions
Imports
- definitions
const definitions = require('mdast-util-definitions')import { definitions } from 'mdast-util-definitions' - GetDefinition
import type { GetDefinition } from 'mdast-util-definitions' - Default export
import definitions from 'mdast-util-definitions'
/* No default export available */
Quickstart
import {definitions} from 'mdast-util-definitions'
import {fromMarkdown} from 'mdast-util-from-markdown'
const markdownInput = `
[example]: https://example.com "Example"
[another]: /path/to/another "Another Link"
This is some text with a reference to [example].
And another one to [another].
A non-existent [foo] reference.
`
const tree = fromMarkdown(markdownInput)
const getDefinition = definitions(tree)
console.log('Definition for "example":', getDefinition('example'))
// Expected: {type: 'definition', title: 'Example', url: 'https://example.com', identifier: 'example', label: 'example', ...}
console.log('Definition for "another":', getDefinition('another'))
// Expected: {type: 'definition', title: 'Another Link', url: '/path/to/another', identifier: 'another', label: 'another', ...}
console.log('Definition for "foo":', getDefinition('foo'))
// Expected: undefined
// To illustrate how one might access all definitions, though 'definitions' returns a getter function
const allDefinitions = {}
tree.children.forEach(node => {
if (node.type === 'definition') {
allDefinitions[node.identifier] = node
}
})
console.log('All definitions found in tree:', allDefinitions)