0.1.5 • Published 3 years ago

microhtml v0.1.5

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

Introduction

Github Downloads (total) Github Downloads (total) Github Downloads (total) Github Downloads (total)

Let's connect library

const { createElement } = require('microhtml');

Firstly you must have AST

The syntax is pretty simple.

const AST = {
   tagName: 'h1', // Any
   type: 'paired', // 'single' or 'paired'
   attr: {
      variant: 'bold'
   },
   children: [
      {...},
      {...},
   ],
   body: 'Hello world!'
};

And now we can create our element, then transform it to HTML code.

const element = createElement(AST).transform();
console.log(element);

// output: <h1 variant="bold">Hello world!</h1>

A few words about AST

AST is an object that can contain tagName type attr children and body fields and nothing more! Elements created as the "single" type cannot have "children" and "body", only attributes, so you can leave these fields as empty object and array respectively. children field must be an array of the same objects as above.

Classes

Functions

HTMLElement

Kind: global class
Summary: Create an instance of HTMLElement
Access: public

new HTMLElement(tagName, type, props, body)

Returns: HTMLElement - HTMLElement instance

ParamTypeDescription
tagNameStringtag definition
typeStringtag type (can be 'single' or 'paired')
propsObjectexpects object with two fields: children (ARRAY), attribute (OBJECT)
bodyStringinner body text.

Example

const element = new HTMLElement(
      'h1',
      'paired',
      { attributes: {...}, children: [...] },
      'Hello world!',
    );

htmlElement.transform()

Kind: instance method of HTMLElement
Summary: transform
Access: public
Example

const element = new HTMLElement(
      'h1',
      'paired',
      { attributes: {...}, children: [...] },
      'Hello world!',
    );
element.transform();
> '<h1>Hello world!</h1>'

createDocument() ⇒ True

Creates document from certain content, like HTML tags.

Kind: global function
Returns: True - - if operation succeed.

TypeDescription
StringHTML document file name.
StringHTML document content.

createElement() ⇒ HTMLElement

Recursive function that generates "HTMLElement" object from given AST.

Kind: global function

TypeDescription
ObjectAST

Example

const AST = {
  tagName: 'html',
  type: 'paired',
  attr: {
    lang: 'ru',
  },
  children: [
    {
      tagName: 'body',
      type: 'paired',
      attr: {},
      children: [],
      body: ''
    },
    {
      tagName: 'img',
      type: 'single',
      attr: {},
      children: [],
      body: ''
    }
  ],
  body: '',
};
const elem = createElement(AST).transform();
createDocument('index', elem);
0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.1

3 years ago