@suin/refractor-flatten
A utility to transform Refractor ASTs to flat arrays.
Installation
yarn add @suin/refractor-flatten
npm install @suin/refractor-flatten
Usage
Basic Usage
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<span>[3]
β properties: {"className":["token","template-string"]}
ββ0 element<span>[1]
β β properties: {"className":["token","template-punctuation","string"]}
β ββ0 text "`"
ββ1 element<span>[3]
β β properties: {"className":["token","interpolation"]}
β ββ0 element<span>[1]
β β β properties: {"className":["token","interpolation-punctuation","punctuation"]}
β β ββ0 text "${"
β ββ1 text "value"
β ββ2 element<span>[1]
β β properties: {"className":["token","interpolation-punctuation","punctuation"]}
β ββ0 text "}"
ββ2 element<span>[1]
β properties: {"className":["token","template-punctuation","string"]}
ββ0 text "`"
The structure of the flat above becomes:
root[5]
ββ0 element<span>[1]
β β properties: {"className":["token","template-string","template-punctuation","string"]}
β ββ0 text "`"
ββ1 element<span>[1]
β β properties: {"className":["token","template-string","interpolation","interpolation-punctuation","punctuation"]}
β ββ0 text "${"
ββ2 element<span>[1]
β β properties: {"className":["token","template-string","interpolation"]}
β ββ0 text "value"
ββ3 element<span>[1]
β β properties: {"className":["token","template-string","interpolation","interpolation-punctuation","punctuation"]}
β ββ0 text "}"
ββ4 element<span>[1]
β properties: {"className":["token","template-string","template-punctuation","string"]}
ββ0 text "`"
API Reference
https://suin.github.io/refractor-flatten/