What is mdast-util-definitions?
The mdast-util-definitions npm package is a utility for working with Markdown Abstract Syntax Tree (MDAST) data structures. It provides a way to look up and manage definitions in a MDAST, which is useful when working with markdown documents in a programmatic way. This can include tasks like parsing, manipulating, or inspecting markdown content.
What are mdast-util-definitions's main functionalities?
Definition lookup
This feature allows you to look up definitions in a MDAST by their identifier. It's useful for resolving references in markdown documents, such as links or images, to their corresponding definitions.
const definitions = require('mdast-util-definitions');
const ast = { /* some MDAST */ };
const definition = definitions(ast);
console.log(definition('referenceId'));
Other packages similar to mdast-util-definitions
unist-util-visit
While unist-util-visit is not specifically designed for handling definitions within markdown documents, it is a utility for traversing Unist syntax trees, which includes MDASTs. It can be used in conjunction with mdast-util-definitions for more complex operations involving visiting nodes and acting upon them, offering a broader scope of manipulation than mdast-util-definitions.
remark-parse
remark-parse is a part of the remark ecosystem, which is built on top of MDAST. It is used for parsing markdown into MDASTs. While it doesn't provide definition lookup functionality directly, it is a foundational tool for creating MDASTs from markdown content, which can then be used with mdast-util-definitions for definition management.
mdast-util-definitions
Get a definition in node
by identifier
. Supports weird keys (like
__proto__
or toString
).
Installation
npm:
npm install mdast-util-definitions
mdast-util-definitions is also available for bower,
component, duo,
and for AMD, CommonJS, and globals (uncompressed
and compressed).
Usage
var mdast = require('mdast');
var definitions = require('mdast-util-definitions');
var ast = mdast.parse('[example]: http://example.com "Example"');
var getDefinition = definitions(ast);
getDefinition('example');
getDefinition('foo');
API
getDefinitionFactory(node): getDefinition
Create a cache of all definition
s in node
.
Parameters:
node
(Node
) — (Grand)parent of definitions.
Returns: Function
— See getDefinition
getDefinition(identifier)
Get a definition by identifier
.
Parameters:
identifier
(string
) — Identifier of definition.
Returns: Node?
— Definition, if found.
License
MIT @ Titus Wormer