What is unist-util-position?
The unist-util-position package is a utility for working with positional information in unist syntax trees. It provides functions to get and manipulate positions of nodes within these trees, which is useful for tasks such as error reporting, syntax highlighting, and more.
What are unist-util-position's main functionalities?
position.start
This feature retrieves the starting position of a node. The code sample demonstrates how to use the `position.start` function to get the starting line and column of a node.
const position = require('unist-util-position');
const node = { position: { start: { line: 1, column: 1 }, end: { line: 1, column: 5 } } };
console.log(position.start(node)); // { line: 1, column: 1 }
position.end
This feature retrieves the ending position of a node. The code sample shows how to use the `position.end` function to get the ending line and column of a node.
const position = require('unist-util-position');
const node = { position: { start: { line: 1, column: 1 }, end: { line: 1, column: 5 } } };
console.log(position.end(node)); // { line: 1, column: 5 }
position.point
This feature normalizes a point object. The code sample demonstrates how to use the `position.point` function to ensure a point object has the correct structure.
const position = require('unist-util-position');
const point = { line: 1, column: 1 };
console.log(position.point(point)); // { line: 1, column: 1 }
Other packages similar to unist-util-position
unist-util-visit
The unist-util-visit package is used to recursively walk through unist syntax trees. While it doesn't specifically handle positional information, it can be used in conjunction with unist-util-position to traverse nodes and then retrieve their positions.
unist-util-find
The unist-util-find package helps in finding nodes in unist syntax trees based on certain criteria. It can be used to locate nodes and then unist-util-position can be used to get their positional information.
unist-util-select
The unist-util-select package allows for selecting nodes in unist syntax trees using CSS-like selectors. Once nodes are selected, unist-util-position can be used to get their positions. This package is useful for more complex queries within the tree.
unist-util-position
unist utility to get the positional info of nodes.
Install
This package is ESM only:
Node 12+ is needed to use it and it must be import
ed instead of require
d.
npm:
npm install unist-util-position
Use
import remark from 'remark'
import {position, pointStart, pointEnd} from 'unist-util-position'
var tree = remark().parse('# foo\n\n* bar\n')
console.log(position(tree))
console.log(pointStart(tree))
console.log(pointEnd(tree))
console.log(position())
console.log(pointStart())
console.log(pointEnd())
Yields:
{start: {line: 1, column: 1, offset: 0}, end: {line: 4, column: 1, offset: 13}}
{line: 1, column: 1, offset: 0}
{line: 4, column: 1, offset: 13}
{start: {line: null, column: null, offset: null}, end: {line: null, column: null, offset: null}}
{line: null, column: null, offset: null}
{line: null, column: null, offset: null}
API
This package exports the following identifiers: position
, pointStart
, and
pointEnd
.
There is no default export.
position(node?)
Get the positional info of node
(Node?
).
Returns Position
.
pointStart(node?)
pointEnd(node?)
Get the start or end points in the positional info of node
(Node?
).
Returns Point
.
Contribute
See contributing.md
in syntax-tree/.github
for ways to get
started.
See support.md
for ways to get help.
This project has a code of conduct.
By interacting with this repository, organization, or community you agree to
abide by its terms.
License
MIT © Titus Wormer