unist-util-remove-position

unist utility to remove positions from a tree

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
unist-util-remove-position
1104.0.29 days ago7 years agoMinified + gzip package size for unist-util-remove-position in KB

Readme

unist-util-remove-position
!Buildbuild-badgebuild !Coveragecoverage-badgecoverage !Downloadsdownloads-badgedownloads !Sizesize-badgesize !Sponsorssponsors-badgecollective !Backersbackers-badgecollective !Chatchat-badgechat
unist utility to remove positional info from a tree.

Contents

*   [`removePosition(node[, force|options])`](#removepositionnode-forceoptions)
*   [`Options`](#options)

What is this?

This is a small utility that helps you remove the position field from nodes in a unist tree.

When should I use this?

Often, positional info is the whole reason, or an important reason, for using ASTs. Sometimes, especially when comparing trees, or when inserting one tree into another, the positional info is at best useless and at worst harmful. In those cases, you can use this utility to remove position fields from a tree.
You might find the utility unist-util-positionunist-util-position useful to instead get clean position info from a tree, or unist-util-generatedunist-util-generated useful to check whether a node is considered to be generated (not in the original input file).
You might also enjoy unist-util-stringify-positionunist-util-stringify-position when you want to display positional info to users.

Install

This package is ESM onlyesm. In Node.js (version 14.14+ and 16.0+), install with npm:
npm install unist-util-remove-position

In Deno with esm.shesmsh:
import {removePosition} from 'https://esm.sh/unist-util-remove-position@4'

In browsers with esm.shesmsh:
<script type="module">
  import {removePosition} from 'https://esm.sh/unist-util-remove-position@4?bundle'
</script>

Use

import {fromMarkdown} from 'mdast-util-from-markdown'
import {removePosition} from 'unist-util-remove-position'

const tree = fromMarkdown('Some _emphasis_, **importance**, and `code`.')

removePosition(tree, {force: 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 identifier removePositionremoveposition. There is no default export.

removePosition(node[, force|options])

Remove the position field from a tree.
Parameters
— tree to clean
  • force (boolean)
— equivalent to `{force: boolean}`
— configuration
Returns
The given, modified, tree (Nodenode).

Options

Configuration (TypeScript type).
Fields
  • force (boolean, default: false)
— whether to use `delete` to remove `position` fields, the default is to
set them to `undefined`

Types

This package is fully typed with TypeScript. It exports the additional type Optionsoptions.

Compatibility

Projects maintained by the unified collective are compatible with all maintained versions of Node.js. As of now, that is Node.js 14.14+ and 16.0+. Our projects sometimes work with older versions, but this is not guaranteed.

Contribute

See contributing.mdcontributing in syntax-tree/.githubhealth for ways to get started. See support.mdsupport for ways to get help.
This project has a code of conductcoc. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MITlicense © Titus Wormerauthor