|
8 | 8 | [![Backers][backers-badge]][collective]
|
9 | 9 | [![Chat][chat-badge]][chat]
|
10 | 10 |
|
11 |
| -[**unist**][unist] utility to remove [`position`][position]s from tree. |
| 11 | +[unist][] utility to create a remove positional info from a tree. |
12 | 12 |
|
13 |
| -## Install |
| 13 | +## Contents |
| 14 | + |
| 15 | +* [What is this?](#what-is-this) |
| 16 | +* [When should I use this?](#when-should-i-use-this) |
| 17 | +* [Install](#install) |
| 18 | +* [Use](#use) |
| 19 | +* [API](#api) |
| 20 | + * [`removePosition(node[, force])`](#removepositionnode-force) |
| 21 | +* [Types](#types) |
| 22 | +* [Compatibility](#compatibility) |
| 23 | +* [Contribute](#contribute) |
| 24 | +* [License](#license) |
| 25 | + |
| 26 | +## What is this? |
| 27 | + |
| 28 | +This is a small utility that helps you remove the `position` field from nodes in |
| 29 | +a unist tree. |
| 30 | + |
| 31 | +## When should I use this? |
| 32 | + |
| 33 | +Often, positional info is the whole reason, or an important reason, for using |
| 34 | +ASTs. |
| 35 | +Sometimes, especially when comparing trees, or when inserting one tree into |
| 36 | +another, the positional info is at best useless and at worst harmful. |
| 37 | +In those cases, you can use this utility to remove `position` fields from a |
| 38 | +tree. |
| 39 | + |
| 40 | +You might find the utility [`unist-util-position`][unist-util-position] |
| 41 | +useful to instead get clean position info from a tree, or |
| 42 | +[`unist-util-generated`][unist-util-generated] useful to check whether a node is |
| 43 | +considered to be generated (not in the original input file). |
14 | 44 |
|
15 |
| -This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): |
16 |
| -Node 12+ is needed to use it and it must be `import`ed instead of `require`d. |
| 45 | +You might also enjoy |
| 46 | +[`unist-util-stringify-position`][unist-util-stringify-position] when you want |
| 47 | +to display positional info to users. |
17 | 48 |
|
18 |
| -[npm][]: |
| 49 | +## Install |
| 50 | + |
| 51 | +This package is [ESM only][esm]. |
| 52 | +In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with [npm][]: |
19 | 53 |
|
20 | 54 | ```sh
|
21 | 55 | npm install unist-util-remove-position
|
22 | 56 | ```
|
23 | 57 |
|
| 58 | +In Deno with [`esm.sh`][esmsh]: |
| 59 | + |
| 60 | +```js |
| 61 | +import {removePosition} from "https://esm.sh/unist-util-remove-position@4" |
| 62 | +``` |
| 63 | + |
| 64 | +In browsers with [`esm.sh`][esmsh]: |
| 65 | + |
| 66 | +```html |
| 67 | +<script type="module"> |
| 68 | + import {removePosition} from "https://esm.sh/unist-util-remove-position@4?bundle" |
| 69 | +</script> |
| 70 | +``` |
| 71 | + |
24 | 72 | ## Use
|
25 | 73 |
|
26 | 74 | ```js
|
27 |
| -import remark from 'remark' |
| 75 | +import {fromMarkdown} from 'mdast-util-from-markdown' |
28 | 76 | import {removePosition} from 'unist-util-remove-position'
|
29 | 77 |
|
30 |
| -const tree = remark().parse('Some _emphasis_, **importance**, and `code`.') |
| 78 | +const tree = fromMarkdown('Some _emphasis_, **importance**, and `code`.') |
31 | 79 |
|
32 | 80 | removePosition(tree, true)
|
33 | 81 |
|
@@ -58,22 +106,35 @@ Yields:
|
58 | 106 |
|
59 | 107 | ## API
|
60 | 108 |
|
61 |
| -This package exports the following identifiers: `removePosition`. |
| 109 | +This package exports the identifier `removePosition`. |
62 | 110 | There is no default export.
|
63 | 111 |
|
64 | 112 | ### `removePosition(node[, force])`
|
65 | 113 |
|
66 |
| -Remove [`position`][position] fields from [`node`][node]. |
67 |
| -If `force` is given, uses `delete`, otherwise, sets `position`s to `undefined`. |
| 114 | +Remove the `position` field from a tree ([`Node`][node]). |
| 115 | +If `force` is given (`boolean`, default: `false`), uses `delete` to remove the |
| 116 | +field entirely, otherwise it’s set to `undefined`. |
68 | 117 |
|
69 | 118 | ###### Returns
|
70 | 119 |
|
71 |
| -The given `node`. |
| 120 | +The given, modified, `node` ([`Node`][node]). |
| 121 | + |
| 122 | +## Types |
| 123 | + |
| 124 | +This package is fully typed with [TypeScript][]. |
| 125 | +It exports no additional types. |
| 126 | + |
| 127 | +## Compatibility |
| 128 | + |
| 129 | +Projects maintained by the unified collective are compatible with all maintained |
| 130 | +versions of Node.js. |
| 131 | +As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+. |
| 132 | +Our projects sometimes work with older versions, but this is not guaranteed. |
72 | 133 |
|
73 | 134 | ## Contribute
|
74 | 135 |
|
75 |
| -See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get |
76 |
| -started. |
| 136 | +See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for |
| 137 | +ways to get started. |
77 | 138 | See [`support.md`][support] for ways to get help.
|
78 | 139 |
|
79 | 140 | This project has a [code of conduct][coc].
|
@@ -114,18 +175,30 @@ abide by its terms.
|
114 | 175 |
|
115 | 176 | [npm]: https://docs.npmjs.com/cli/install
|
116 | 177 |
|
| 178 | +[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c |
| 179 | + |
| 180 | +[esmsh]: https://esm.sh |
| 181 | + |
| 182 | +[typescript]: https://www.typescriptlang.org |
| 183 | + |
117 | 184 | [license]: license
|
118 | 185 |
|
119 | 186 | [author]: https://wooorm.com
|
120 | 187 |
|
121 |
| -[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md |
| 188 | +[health]: https://github.com/syntax-tree/.github |
122 | 189 |
|
123 |
| -[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md |
| 190 | +[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md |
124 | 191 |
|
125 |
| -[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md |
| 192 | +[support]: https://github.com/syntax-tree/.github/blob/main/support.md |
126 | 193 |
|
127 |
| -[unist]: https://github.com/syntax-tree/unist |
| 194 | +[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md |
128 | 195 |
|
129 |
| -[position]: https://github.com/syntax-tree/unist#position |
| 196 | +[unist]: https://github.com/syntax-tree/unist |
130 | 197 |
|
131 | 198 | [node]: https://github.com/syntax-tree/unist#node
|
| 199 | + |
| 200 | +[unist-util-position]: https://github.com/syntax-tree/unist-util-position |
| 201 | + |
| 202 | +[unist-util-generated]: https://github.com/syntax-tree/unist-util-generated |
| 203 | + |
| 204 | +[unist-util-stringify-position]: https://github.com/syntax-tree/unist-util-stringify-position |
0 commit comments