Skip to content
A parser that translates strings into jsx objects
JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
test
.babelrc
.gitignore
README.md
package-lock.json
package.json

README.md

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"
  }
}
You can’t perform that action at this time.