seikichi / restructured

Pure JavaScript reStructuredText parser
58 stars 15 forks source link
javascript restructuredtext restructuredtext-parser

restructured

NPM Build Status

A reStructuredText parser for JavaScript.

Online Demo

demo.

Example

> npm install -g restructured
> echo 'Hello, world' | restructured | jq
{
  "type": "document",
  "children": [
    {
      "type": "paragraph",
      "children": [
        {
          "type": "text",
          "value": "Hello, world\n"
        }
      ]
    }
  ]
}

Consider the following script.

import restructured from 'restructured';

const parsed = restructured.parse(`
=====
Title
=====

Subtitle
--------

This is a paragraph.

- This is item 1
- This is item 2
`);

console.log(JSON.stringify(parsed));

The above script outputs the following JSON.

{
  "type": "document",
  "children": [
    {
      "type": "section",
      "depth": 1,
      "children": [
        {
          "type": "title",
          "children": [
            {
              "type": "text",
              "value": "Title"
            }
          ]
        },
        {
          "type": "section",
          "depth": 2,
          "children": [
            {
              "type": "title",
              "children": [
                {
                  "type": "text",
                  "value": "Subtitle"
                }
              ]
            },
            {
              "type": "paragraph",
              "children": [
                {
                  "type": "text",
                  "value": "This is a paragraph.\n"
                }
              ]
            },
            {
              "type": "bullet_list",
              "bullet": "-",
              "children": [
                {
                  "type": "list_item",
                  "children": [
                    {
                      "type": "paragraph",
                      "children": [
                        {
                          "type": "text",
                          "value": "This is item 1\n"
                        }
                      ]
                    }
                  ]
                },
                {
                  "type": "list_item",
                  "children": [
                    {
                      "type": "paragraph",
                      "children": [
                        {
                          "type": "text",
                          "value": "This is item 2\n"
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

Progress

License

MIT