Giter Club home page Giter Club logo

unist-util-remove-position's Introduction

unist-util-remove-position

Build Coverage Downloads Size Sponsors Backers Chat

unist utility to remove positional info from a tree.

Contents

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-position useful to instead get clean position info from a tree, or unist-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-position when you want to display positional info to users.

Install

This package is ESM only. In Node.js (version 16+), install with npm:

npm install unist-util-remove-position

In Deno with esm.sh:

import {removePosition} from 'https://esm.sh/unist-util-remove-position@5'

In browsers with esm.sh:

<script type="module">
  import {removePosition} from 'https://esm.sh/unist-util-remove-position@5?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 removePosition. There is no default export.

removePosition(node[, options])

Remove the position field from a tree.

Parameters
  • node (Node) — tree to clean
  • options (Options, optional) — configuration
Returns

Nothing (undefined).

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 Options.

Compatibility

Projects maintained by the unified collective are compatible with maintained versions of Node.js.

When we cut a new major release, we drop support for unmaintained versions of Node. This means we try to keep the current release line, unist-util-remove-position@^5, compatible with Node.js 16.

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

unist-util-remove-position's People

Contributors

christianmurphy avatar greenkeeperio-bot avatar murderlon avatar wooorm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

christianmurphy

unist-util-remove-position's Issues

Typescript types

Would be great to have typescript types. I know there's a lot of packages in the queue, so sharing what's working for me right now in case it's helpful to anybody else.

declare module 'unist-util-remove-position' {
  import { Node } from 'unist';
  const removePosition: <T extends Node>(node: T, force?: boolean) => T;
  export default removePosition;
}

I've got that in my module.d.ts file. Don't have time to create a proper PR with types and tests right now.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.