What is unist-util-remove-position?
The unist-util-remove-position npm package is designed to work with Unist syntax trees. It allows users to remove the position information from nodes in the tree. This can be particularly useful when you want to compare nodes based on their content rather than their position in the source file, or when you want to minimize the size of the tree for serialization.
What are unist-util-remove-position's main functionalities?
Remove position from a single node
This feature allows you to remove the position information from a single node in the syntax tree. The code sample shows a node before the position information has been removed.
{"type": "text", "value": "Hello, world!", "position": {"start": {"line": 1, "column": 1, "offset": 0}, "end": {"line": 1, "column": 13, "offset": 12}}}
Remove position from an entire tree
This feature allows you to remove the position information from all nodes in a syntax tree, effectively stripping out all position data. The code sample shows a simple tree before the position information has been removed.
{"type": "root", "children": [{"type": "paragraph", "children": [{"type": "text", "value": "Hello, world!", "position": {"start": {"line": 1, "column": 1, "offset": 0}, "end": {"line": 1, "column": 13, "offset": 12}}}], "position": {"start": {"line": 1, "column": 1, "offset": 0}, "end": {"line": 1, "column": 13, "offset": 12}}}]
Other packages similar to unist-util-remove-position
unist-util-visit
While unist-util-visit is designed for traversing Unist nodes and performing operations on them, it can be used in conjunction with other utilities to modify nodes, including removing position information. However, unist-util-remove-position is specifically tailored for removing position data, making it more straightforward for this particular task.
unist-util-map
unist-util-map can be used to create a new tree by applying a function to each node in the input tree. This function could theoretically remove position information from each node, similar to unist-util-remove-position. However, unist-util-remove-position provides a more direct and optimized approach for this specific purpose.
unist-util-remove-position
unist utility to remove position
s from tree.
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-remove-position
Use
import remark from 'remark'
import {removePosition} from 'unist-util-remove-position'
var tree = remark().parse('Some _emphasis_, **importance**, and `code`.')
removePosition(tree, true)
console.dir(tree, {depth: null})
Yields:
{
type: 'root',
children: [
{
type: 'paragraph',
children: [
{type: 'text', value: 'Some '},
{type: 'emphasis', children: [{type: 'text', value: 'emphasis'}]},
{type: 'text', value: ', '},
{type: 'strong', children: [{type: 'text', value: 'importance'}]},
{type: 'text', value: ', and '},
{type: 'inlineCode', value: 'code'},
{type: 'text', value: '.'}
]
}
]
}
API
This package exports the following identifiers: removePosition
.
There is no default export.
removePosition(node[, force])
Remove position
fields from node
.
If force
is given, uses delete
, otherwise, sets position
s to undefined
.
Returns
The given node
.
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