mdast-util-definitions

6.0.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates how to use `mdast-util-definitions` to find definition nodes by identifier in an mdast tree generated from Markdown input.

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)

view raw JSON →