https://github.com/suin/refractor-flatten
A utility to transform Refractor ASTs to flat arrays.
https://github.com/suin/refractor-flatten
refractor
Last synced: 11 months ago
JSON representation
A utility to transform Refractor ASTs to flat arrays.
- Host: GitHub
- URL: https://github.com/suin/refractor-flatten
- Owner: suin
- License: mit
- Created: 2021-06-02T06:08:37.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-06-02T06:15:16.000Z (about 5 years ago)
- Last Synced: 2025-02-28T20:48:11.566Z (over 1 year ago)
- Topics: refractor
- Language: TypeScript
- Homepage:
- Size: 142 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# @suin/refractor-flatten
A utility to transform [Refractor](https://github.com/wooorm/refractor) ASTs to flat arrays.
## Installation
```bash
yarn add @suin/refractor-flatten
# or
npm install @suin/refractor-flatten
```
## Usage
### Basic Usage
```typescript
import refractor from "refractor";
import flatten from "@suin/refractor-flatten";
const tree = refractor.highlight("`${value}`", "js");
const flat = flatten(tree);
```
The structure of `tree` is:
```
root[1]
└─0 element[3]
│ properties: {"className":["token","template-string"]}
├─0 element[1]
│ │ properties: {"className":["token","template-punctuation","string"]}
│ └─0 text "`"
├─1 element[3]
│ │ properties: {"className":["token","interpolation"]}
│ ├─0 element[1]
│ │ │ properties: {"className":["token","interpolation-punctuation","punctuation"]}
│ │ └─0 text "${"
│ ├─1 text "value"
│ └─2 element[1]
│ │ properties: {"className":["token","interpolation-punctuation","punctuation"]}
│ └─0 text "}"
└─2 element[1]
│ properties: {"className":["token","template-punctuation","string"]}
└─0 text "`"
```
The structure of the `flat` above becomes:
```
root[5]
├─0 element[1]
│ │ properties: {"className":["token","template-string","template-punctuation","string"]}
│ └─0 text "`"
├─1 element[1]
│ │ properties: {"className":["token","template-string","interpolation","interpolation-punctuation","punctuation"]}
│ └─0 text "${"
├─2 element[1]
│ │ properties: {"className":["token","template-string","interpolation"]}
│ └─0 text "value"
├─3 element[1]
│ │ properties: {"className":["token","template-string","interpolation","interpolation-punctuation","punctuation"]}
│ └─0 text "}"
└─4 element[1]
│ properties: {"className":["token","template-string","template-punctuation","string"]}
└─0 text "`"
```
## API Reference
https://suin.github.io/refractor-flatten/