Giter Club home page Giter Club logo

refractor-flatten's Introduction

@suin/refractor-flatten

A utility to transform Refractor ASTs to flat arrays.

Installation

yarn add @suin/refractor-flatten
# or
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/

refractor-flatten's People

Contributors

semantic-release-bot avatar suin avatar

Watchers

 avatar  avatar

Forkers

scm-manager

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.