/jsx-parser

A parser that translates strings into jsx objects

Primary LanguageJavaScript

jsx-parser

This is a lightweight string to jsx obj parser, Only 250 lines of code

Usage

parse function returns a jsx object

let JsxParser = require('./parse')

let str = `<div name="{{jsx-parse}}" class="{{fuck}}" id="1">
                Life is too difficult
                <span name="life" like="rape">
                    <p>Life is like rape</p>
                </span> 
                <div>
                    <span name="live" do="{{gofuck}}">
                        <p>Looking away, everything is sad</p>
                    </span> 
                    <Counter me="excellent">
                        I am awesome
                    </Counter>
                </div>  
            </div>
            `
let jp = new JsxParser(str)
let jsx = jp.parse()
log(JSON.stringify(jsx, null, 2))

Output

{
  "type": "div",
  "props": {
    "childrens": [
      {
        "type": "span",
        "props": {
          "childrens": [
            {
              "type": "p",
              "props": {
                "childrens": [],
                "text": "Life is like rape"
              }
            }
          ],
          "name": "life",
          "like": "rape"
        }
      },
      {
        "type": "div",
        "props": {
          "childrens": [
            {
              "type": "span",
              "props": {
                "childrens": [
                  {
                    "type": "p",
                    "props": {
                      "childrens": [],
                      "text": "Looking away, everything is sad"
                    }
                  }
                ],
                "name": "live",
                "do": "{{gofuck}}"
              }
            },
            {
              "type": "Counter",
              "props": {
                "childrens": [],
                "me": "excellent",
                "text": "I am awesome"
              }
            }
          ]
        }
      }
    ],
    "name": "{{jsx-parse}}",
    "class": "{{fuck}}",
    "id": "1",
    "text": "Life is too difficult"
  }
}