# markdown-to-ast [![Build Status](https://travis-ci.org/azu/markdown-to-ast.svg)](https://travis-ci.org/azu/markdown-to-ast)

Parse Markdown to AST with location info.

This library has not parser in itself, it is  dependent on [jgm/CommonMark](https://github.com/jgm/CommonMark "jgm/CommonMark")'s parser.
(We may use other parser library in the future.)

However, The AST generated by CommonMark has not `loc` object.

The AST consists of `TxtNode`s.
A `TxtNode` of the AST has following properties:

- `loc` - Nodes have line and column-based location info.
- `range` - Nodes have an index-based location range (array).
- `raw` - Node have a `raw` text.

The interface defined as [txtnode.d.ts](typing/txtnode.d.ts).

This library is a part of [azu/textlint](https://github.com/azu/textlint "azu/textlint").


## DEMO

[![screenshot](http://monosnap.com/image/0fqi1UF7yOv89nxJPaDWtvyqERaM49.png)](http://azu.github.io/markdown-to-ast/example/)

[Online Parsing Demo](http://azu.github.io/markdown-to-ast/example/) provide markdown to AST on-the-fly.

## Installation

```
npm install markdown-to-ast
```

## Usage

```sh
var parse = require("markdown-to-ast").parse;
var markdown = "It's a *text*";
var AST = parse(markdown);
/*
{
    "children": [
        {
            "inline_content": [
                {
                    "c": "It's a",
                    "raw": "It's a",
                    "loc": {
                        "start": {
                            "line": 1,
                            "column": 0
                        },
                        "end": {
                            "line": 1,
                            "column": 6
                        }
                    },
                    "range": [
                        0,
                        6
                    ],
                    "type": "Str"
                },
                {
                    "c": " ",
                    "raw": " ",
                    "loc": {
                        "start": {
                            "line": 1,
                            "column": 6
                        },
                        "end": {
                            "line": 1,
                            "column": 7
                        }
                    },
                    "range": [
                        6,
                        7
                    ],
                    "type": "Str"
                },
                {
                    "c": [
                        {
                            "c": "text",
                            "raw": "text",
                            "loc": {
                                "start": {
                                    "line": 1,
                                    "column": 0
                                },
                                "end": {
                                    "line": 1,
                                    "column": 4
                                }
                            },
                            "range": [
                                0,
                                4
                            ],
                            "type": "Str"
                        }
                    ],
                    "raw": "*text*",
                    "loc": {
                        "start": {
                            "line": 1,
                            "column": 7
                        },
                        "end": {
                            "line": 1,
                            "column": 13
                        }
                    },
                    "range": [
                        7,
                        13
                    ],
                    "type": "Emphasis"
                }
            ],
            "children": [],
            "raw": "It's a *text*",
            "loc": {
                "start": {
                    "line": 1,
                    "column": 0
                },
                "end": {
                    "line": 1,
                    "column": 13
                }
            },
            "range": [
                0,
                13
            ],
            "type": "Paragraph"
        }
    ],
    "raw": "It's a *text*",
    "loc": {
        "start": {
            "line": 1,
            "column": 0
        },
        "end": {
            "line": 1,
            "column": 13
        }
    },
    "range": [
        0,
        13
    ],
    "type": "Document"
}
*/
```

The interface of a node on AST is defined as [txtnode.d.ts](typing/txtnode.d.ts).

If you want to know real use-case, please see [azu/textlint](https://github.com/azu/textlint "azu/textlint").

## Tests

```
npm test
```

## Contributing

1. Fork it!
2. Create your feature branch: `git checkout -b my-new-feature`
3. Commit your changes: `git commit -am 'Add some feature'`
4. Push to the branch: `git push origin my-new-feature`
5. Submit a pull request :D

## License

MIT