UNPKG

4.34 kBMarkdownView Raw
1# @textlint/markdown-to-ast
2
3Parse Markdown to AST with location info.
4
5This library is not parser itself, it dependent on [wooorm/remark](https://github.com/wooorm/remark).
6
7> Markdown -> remark -> markdown-to-ast -> `TxtNode`s
8
9The AST consists of `TxtNode`s.
10A `TxtNode` of the AST has following properties:
11
12- `loc` - Nodes have line and column-based location info.
13- `range` - Nodes have an index-based location range (array).
14- `raw` - Node have a `raw` text.
15- `value` - Node have a `value` of text.
16
17The interface are defined in [textlint/txtnode.md](https://github.com/textlint/textlint/blob/master/docs/txtnode.md "textlint/txtnode.md at master · textlint/textlint")
18
19This library is a part of [textlint/textlint](https://github.com/textlint/textlint "textlint/textlint").
20
21**If you need to markdown parser, please use [wooorm/remark](https://github.com/wooorm/remark) directly.**
22
23## DEMO
24
25- [textlint AST explorer](https://textlint.github.io/astexplorer/ "textlint AST explorer")
26
27## Installation
28
29```
30npm install @textlint/markdown-to-ast
31```
32
33## Usage
34
35```sh
36var parse = require("@textlint/markdown-to-ast").parse;
37var markdown = "It's a *text*";
38var AST = parse(markdown);
39/*
40{
41 "type": "Document",
42 "children": [
43 {
44 "type": "Paragraph",
45 "children": [
46 {
47 "type": "Str",
48 "value": "It's a ",
49 "loc": {
50 "start": {
51 "line": 1,
52 "column": 0
53 },
54 "end": {
55 "line": 1,
56 "column": 7
57 }
58 },
59 "range": [
60 0,
61 7
62 ],
63 "raw": "It's a "
64 },
65 {
66 "type": "Emphasis",
67 "children": [
68 {
69 "type": "Str",
70 "value": "text",
71 "loc": {
72 "start": {
73 "line": 1,
74 "column": 8
75 },
76 "end": {
77 "line": 1,
78 "column": 12
79 }
80 },
81 "range": [
82 8,
83 12
84 ],
85 "raw": "text"
86 }
87 ],
88 "loc": {
89 "start": {
90 "line": 1,
91 "column": 7
92 },
93 "end": {
94 "line": 1,
95 "column": 13
96 }
97 },
98 "range": [
99 7,
100 13
101 ],
102 "raw": "*text*"
103 }
104 ],
105 "loc": {
106 "start": {
107 "line": 1,
108 "column": 0
109 },
110 "end": {
111 "line": 1,
112 "column": 13
113 }
114 },
115 "range": [
116 0,
117 13
118 ],
119 "raw": "It's a *text*"
120 }
121 ],
122 "loc": {
123 "start": {
124 "line": 1,
125 "column": 0
126 },
127 "end": {
128 "line": 1,
129 "column": 13
130 }
131 },
132 "range": [
133 0,
134 13
135 ],
136 "raw": "It's a *text*"
137}
138*/
139```
140
141If you want to know real use-case, please see [textlint/textlint](https://github.com/textlint/textlint "textlint/textlint").
142
143## Tests
144
145```
146npm test
147```
148
149### Create fixtures
150
151See [tools/](tools/) directory.
152
153## Contributing
154
1551. Fork it!
1562. Create your feature branch: `git checkout -b my-new-feature`
1573. Commit your changes: `git commit -am 'Add some feature'`
1584. Push to the branch: `git push origin my-new-feature`
1595. Submit a pull request :D
160
161## License
162
163MIT