3.0.0-alpha.1 • Published 2 years ago

idyll-ast v3.0.0-alpha.1

Weekly downloads
864
License
MIT
Repository
github
Last release
2 years ago

What is idyll-ast?

idyll-ast is a library that defines the Abstract Syntax Tree used for Idyll. It is a JSON based AST, and the structure is defined by this schema.

Getting Started

You can install idyll-ast by both npm and yarn.

//using npm
npm install --save idyll-ast

//using yarn
yarn add idyll-ast

We recommend using --save to add all the dependencies required for idyll-ast to your package.json file.

Structure

The ast structure used, is defined by JSON schema (Draft-6), and the current schema is at src/ast.schema.json.

Let's take a look at an example:

## This is a header
And this is a normal paragraph. This is # not a header.

The above Idyll syntax would look like the following when in ast form:

{ id: 0,
  type: 'component',
  name: 'div',
  children:
   [ { id: 2,
       type: 'component',
       name: 'TextContainer',
       children:
        [ { id: 3,
            type: 'component',
            name: 'h2',
            children: [ { id: 4, type: 'textnode', value: 'This is a header' } ] },
          { id: 5,
            type: 'component',
            name: 'p',
            children:
             [ { id: 6,
                 type: 'textnode',
                 value: 'And this is a normal paragraph. This is # not a header.' } ] } ] } ] }

All the data in the tree is encapsulated by the node called root. All the top-level components in the document are considered the children of the root.

Type of Nodes

There can be 5 different types of nodes in the AST.

  1. component : Represents an Idyll component.
  2. textnode: Represents an Idyll textnode.
  3. var: Represents a variable declaration in Idyll.
  4. derive: Represents a derived variable. In Idyll, it represents a variable whose value is derived from other variables.
  5. data: Represents a dataset in Idyll. In Idyll, datasets act like variables, but instead of value, they have a source field.

Properties

The properties field for each component represent, its attributes or value. The general structure for a properties field is as following:

"properties" : {
    "prop1": {
        "type": "type1",
        "value": "value1"
    },
    "prop2": {
        "type": "type2",
        "value" : "value2"
    }
}

The type field can take 3 different values.

  1. value: This type field will evaluate the value field as the given property's value.
  2. variable: This type field will evaluate the value field as the given property's variables declaration.
  3. expression: This type field will evaluate the value field as the given property's expression syntax.

Also, the property key should match the pattern: /[^+\-0-9:\s\/\]"'`\.][^:\s\/\]"'`\.]*/

Children

The children field is an array that contains all the child nodes of a node. Only component nodes can have any children. textnodes, var, derive and data nodes should not have any children.

API

idyll-ast~appendNode ⇒ object

Function to append a top-level child to the root element.

Kind: inner property of idyll-ast
Returns: object - Modifed ast node

ParamTypeDescription
astobjectJSON-object
nodeobjectJSON-object

idyll-ast~appendNodes ⇒ object

Function to append multiple top-level children to the root element.

Kind: inner property of idyll-ast
Returns: object - modified ast

ParamTypeDescription
astojectJSON-object
nodeArray.<object>an array of JSON-objects

idyll-ast~createNode ⇒ object

Function to create a new AST node following the schema.

Kind: inner property of idyll-ast
Returns: object - New component node.

ParamTypeDescription
idintegerId of the node
namestringName of the node.
typestringType of the node.
valuestringValue evaluation of the node
propsArray.<object>Properties of the node.
childrenArray.<object>Children of the node.

idyll-ast~createTextNode ⇒

Function to create a new textnode

Kind: inner property of idyll-ast
Returns: New textnode

ParamType
id*
value*

idyll-ast~getChildren ⇒ Array.<object>

Function to return the children of the passed node.

Kind: inner property of idyll-ast
Returns: Array.<object> - children of the node

ParamTypeDescription
nodeobjectAST node

idyll-ast~setChildren ⇒ object

Function to set children of the passed node.

Kind: inner property of idyll-ast
Returns: object - modified node

ParamType
nodeobject
childrenobject

idyll-ast~getNodesByName ⇒ Array.<object>

Function to get all the nodes with the passed name in the passed AST.

Kind: inner property of idyll-ast
Returns: Array.<object> - Array of nodes matching the name

ParamTypeDescription
astobjectAST object
namestringname of the nodes

idyll-ast~getNodesByType ⇒ Array.<object>

Function to get all the nodes with the passed type in the passed AST.

Kind: inner property of idyll-ast
Returns: Array.<object> - Array of nodes matching the type

ParamTypeDescription
astobjectAST object
typestringtype of the nodes

idyll-ast~hasType ⇒ boolean

Function to check if a node has type attribute or not

Kind: inner property of idyll-ast
Returns: boolean - true if type exists, false otherwise

ParamType
nodeobject

idyll-ast~getType ⇒ string

Function to get the type information of a node

Kind: inner property of idyll-ast
Returns: string - type of the node

ParamTypeDescription
astobjectAST object

idyll-ast~getText ⇒ string

Function to get all the text from textnodes from the passed AST node

Kind: inner property of idyll-ast

ParamTypeDescription
astobjectAST node

idyll-ast~filterNodes ⇒ Array.<object>

Function to find certain nodes based on a filter passed.

Kind: inner property of idyll-ast
Returns: Array.<object> - Array of all the nodes found

ParamTypeDescription
astobjectAST node
filterfunctionFilter function to find nodes

idyll-ast~modifyChildren ⇒ object

Function to modify children of a passed AST node using a passed modifier.

Kind: inner property of idyll-ast
Returns: object - node with modified children.

ParamType
nodeobject
modifierfunction

idyll-ast~filterChildren ⇒ object

Function to pass in a filter function to the children.

Kind: inner property of idyll-ast
Returns: object - node with modified children

ParamTypeDescription
nodeobjectAST node
filterfunctionFilter function

idyll-ast~modifyNodesByName ⇒ object

Function to modiy nodes based on the name property.

Kind: inner property of idyll-ast
Returns: object - ast

ParamType
astobject
namestring
modifierfunction

idyll-ast~handleNodeByName ⇒ object

Function to modify a single node using a modifier and name property.

Kind: inner property of idyll-ast
Returns: object - if node.name = name then modifier(node), else node.

ParamType
nodeObject
namestring
modifierfunction

idyll-ast~getNodeName ⇒ string

Function to get the name of a component

Kind: inner property of idyll-ast
Returns: string - name of the passed node

ParamType
nodeobject

idyll-ast~getPropertyKeys ⇒ Array.<string>

Function to return a the list of property keys of a node

Kind: inner property of idyll-ast
Returns: Array.<string> - keys

ParamType
nodeobject

idyll-ast~getProperty ⇒

Getter function to a return a specific property of a node based on a key.

Kind: inner property of idyll-ast
Returns: null, if the property does not exist, else property.data.

ParamType
nodeobject
keystring

idyll-ast~getProperties ⇒ object

Function to return all the properties of a given node.

Kind: inner property of idyll-ast
Returns: object - properties of the node, or null if none exists,

ParamType
node*

idyll-ast~getPropertiesByType ⇒ Array.<object>

Function to get properties of a particular type of a given node.

Kind: inner property of idyll-ast
Returns: Array.<object> - Array of properties if they exists, or an empty array of no properties of the given type exists.

ParamType
nodeobject
typestring

idyll-ast~prependNode ⇒ object

Function to prepend a node in the children array of root.

Kind: inner property of idyll-ast
Returns: object - modfied ast.

ParamType
astobject
nodeobject

idyll-ast~prependNodes ⇒ object

Function to prepend multiple nodes in the children array of root.

Kind: inner property of idyll-ast
Returns: object - modfied ast.

ParamType
astobject
nodesArray.<object>

idyll-ast~removeNodesByName

Function remove node with a particular name from the ast

Kind: inner property of idyll-ast

ParamType
ast*
name*

idyll-ast~removeNodesByType

Function remove node with a particular name from the ast

Kind: inner property of idyll-ast

ParamType
ast*
type*

idyll-ast~removeProperties ⇒ object

Function to remove a property from a node

Kind: inner property of idyll-ast
Returns: object - Modified node

ParamType
nodeobject
keystring

idyll-ast~setProperty ⇒ object

Function to add a property to a node or change the value if the property already exists.

Kind: inner property of idyll-ast
Returns: object - Modfied Node

ParamType
node*
name*
data*

idyll-ast~setProperties ⇒ object

Function to add multiple properties to a node

Kind: inner property of idyll-ast
Returns: object - Modified node

ParamType
nodeobject
propertiesobject

idyll-ast~walkNodes

Function to do a depth-first traversal of the AST.

Kind: inner property of idyll-ast

ParamTypeDescription
astobjectAST node
ffunctioncallback function for each node.

idyll-ast~walkNodeBreadthFirst

Function to breadth-first traversal on the AST.

Kind: inner property of idyll-ast

ParamType
astobject
ffunction

idyll-ast~toMarkup ⇒ string

Function to convert AST back to idyll markup

Kind: inner property of idyll-ast
Returns: string - Markup string

ParamTypeDescription
astobjectAST node
3.0.0-alpha.1

2 years ago

3.0.0-alpha.0

2 years ago

2.2.6

3 years ago

2.2.5

3 years ago

2.2.4

3 years ago

2.2.3

3 years ago

2.2.2

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago

2.0.12

3 years ago

2.0.11

3 years ago

2.0.10

3 years ago

2.0.9

5 years ago

2.0.8

5 years ago

2.0.7

5 years ago

2.0.6

5 years ago

2.0.6-alpha.0

5 years ago

2.0.5-alpha.0

5 years ago

2.0.4-alpha.0

5 years ago

2.0.3-alpha.0

5 years ago

2.0.2-alpha.0

5 years ago

2.0.1-alpha.0

5 years ago

2.0.0-alpha.0

5 years ago

1.5.2

5 years ago

1.5.1

6 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago