markdown-parser 0.0.8 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{MIT-LICENSE.txt → LICENSE} +21 -20
- package/README.md +95 -175
- package/dist/index.d.ts +115 -0
- package/dist/index.js +2764 -0
- package/package.json +38 -36
- package/.npmignore +0 -21
- package/.travis.yml +0 -4
- package/Gruntfile.js +0 -33
- package/src/markdown-grammar.pegjs +0 -289
- package/src/markdown-parser.js +0 -35
- package/src/parser-browser.js +0 -3564
- package/src/parser.js +0 -3564
- package/test/main.js +0 -82
|
@@ -1,20 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
SOFTWARE
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nimesh Nayaju
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,175 +1,95 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
##
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
//
|
|
41
|
-
//
|
|
42
|
-
|
|
43
|
-
//
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
"`callback` : function triggering current value, can be useful"
|
|
97
|
-
],
|
|
98
|
-
[
|
|
99
|
-
"2.1.3: bug fixes, refactoring, inline options param",
|
|
100
|
-
"2.1.2: bug fixes, changes in z-index via CSS and not js",
|
|
101
|
-
"2.1.1: override css, colors...",
|
|
102
|
-
"2.1.0: bug fixes",
|
|
103
|
-
"2.0.0: no JQuery"
|
|
104
|
-
]
|
|
105
|
-
],
|
|
106
|
-
"listsOrdered": [],
|
|
107
|
-
"sections": [
|
|
108
|
-
"Why",
|
|
109
|
-
"Screenshot",
|
|
110
|
-
"Demo",
|
|
111
|
-
"How to use it",
|
|
112
|
-
"Installation",
|
|
113
|
-
"RELEASE"
|
|
114
|
-
],
|
|
115
|
-
"strikethroughs": [],
|
|
116
|
-
"tasks": []
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
*/
|
|
120
|
-
|
|
121
|
-
});
|
|
122
|
-
```
|
|
123
|
-
|
|
124
|
-
## Return
|
|
125
|
-
```json
|
|
126
|
-
{
|
|
127
|
-
"bolds": [],
|
|
128
|
-
"codes": [],
|
|
129
|
-
"headings": [],
|
|
130
|
-
"italics": [],
|
|
131
|
-
"references": [],
|
|
132
|
-
"lists": [],
|
|
133
|
-
"listsOrdered": [],
|
|
134
|
-
"sections": [],
|
|
135
|
-
"strikethroughs": [],
|
|
136
|
-
"tasks": []
|
|
137
|
-
}
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
### Options
|
|
141
|
-
|
|
142
|
-
* `html_url`: repository url, used for link references, ex: "https://github.com/darul75/markdown-parser"
|
|
143
|
-
|
|
144
|
-
## TODO
|
|
145
|
-
- [ ] Finish tests
|
|
146
|
-
- [ ] Browserify
|
|
147
|
-
- [ ] Missing markdown
|
|
148
|
-
|
|
149
|
-
## Metrics
|
|
150
|
-
|
|
151
|
-
[](https://nodei.co/npm/markdown-parser/)
|
|
152
|
-
|
|
153
|
-
## License
|
|
154
|
-
|
|
155
|
-
The MIT License (MIT)
|
|
156
|
-
|
|
157
|
-
Copyright (c) 2015 Julien Valéry
|
|
158
|
-
|
|
159
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
160
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
161
|
-
in the Software without restriction, including without limitation the rights
|
|
162
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
163
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
164
|
-
furnished to do so, subject to the following conditions:
|
|
165
|
-
|
|
166
|
-
The above copyright notice and this permission notice shall be included in
|
|
167
|
-
all copies or substantial portions of the Software.
|
|
168
|
-
|
|
169
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
170
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
171
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
172
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
173
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
174
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
175
|
-
THE SOFTWARE.
|
|
1
|
+
# `markdown-parser`
|
|
2
|
+
|
|
3
|
+
A markdown parser with streaming support, suitable for incrementally parsing LLM markdown streams. Parses markdown into a structured fully typed tree of nodes, following the [CommonMark](https://commonmark.org/) specification. It supports streaming/incremental parsing, so you can feed it growing input and emit only the blocks that have become finalized.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install markdown-parser
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { MarkdownParser } from "markdown-parser";
|
|
15
|
+
|
|
16
|
+
const parser = new MarkdownParser();
|
|
17
|
+
|
|
18
|
+
// Parse complete markdown
|
|
19
|
+
const nodes = parser.parse("# Hello World\nThis is a paragraph.");
|
|
20
|
+
// [
|
|
21
|
+
// { type: "heading", level: 1, children: [{ type: "text", text: "Hello World" }] },
|
|
22
|
+
// { type: "paragraph", children: [{ type: "text", text: "This is a paragraph." }] }
|
|
23
|
+
// ]
|
|
24
|
+
|
|
25
|
+
// Parse with streaming mode (for incremental content)
|
|
26
|
+
const partialNodes = parser.parse("# Hello World\nThis", { stream: true });
|
|
27
|
+
// Emits heading, but not the paragraph (still open)
|
|
28
|
+
// [
|
|
29
|
+
// { type: "heading", level: 1, children: [{ type: "text", text: "Hello World" }] },
|
|
30
|
+
// ]
|
|
31
|
+
|
|
32
|
+
// Continue parsing as more content arrives
|
|
33
|
+
const moreNodes = parser.parse(" is a paragraph\n\nThis is another paragraph.", { stream: true });
|
|
34
|
+
// Emits the paragraph
|
|
35
|
+
// [
|
|
36
|
+
// { type: "paragraph", children: [{ type: "text", text: "This is a paragraph." }] }
|
|
37
|
+
// ]
|
|
38
|
+
|
|
39
|
+
const finalNodes = parse.parse("", { stream: false })
|
|
40
|
+
// Closes anything still open and emits remaining blocks
|
|
41
|
+
// [
|
|
42
|
+
// { type: "paragraph", children: [{ type: "text", text: "This is another paragraph." }] }
|
|
43
|
+
// ]
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
When stream is false (default), the parser finalizes all open blocks at the end of the input and returns the full set of blocks (for that input). When you parse in streaming mode, the parser keeps internal state across calls and returns only blocks that have become closed and stable since the last call.
|
|
47
|
+
|
|
48
|
+
## API
|
|
49
|
+
|
|
50
|
+
### `MarkdownParser`
|
|
51
|
+
|
|
52
|
+
The main parser class that converts markdown text into a structured block AST (headings, paragraphs, lists, etc.).
|
|
53
|
+
|
|
54
|
+
#### `parse(text: string, options?: { stream: boolean }): BlockNode[]`
|
|
55
|
+
|
|
56
|
+
Parses markdown text and returns an array of block nodes.
|
|
57
|
+
|
|
58
|
+
- `text` - The markdown text to parse
|
|
59
|
+
- `options.stream` - When `true`, enables streaming mode which buffers incomplete blocks until they can be fully parsed. Defaults to `false`.
|
|
60
|
+
|
|
61
|
+
## Supported Nodes
|
|
62
|
+
|
|
63
|
+
The parser provides 100% support for the CommonMark specification, and includes full support for GitHub Flavored Markdown (GFM) tables.
|
|
64
|
+
|
|
65
|
+
### Block nodes
|
|
66
|
+
|
|
67
|
+
- [x] Heading (ATX and setext style)
|
|
68
|
+
- [x] Paragraph
|
|
69
|
+
- [x] Code block (fenced and indented)
|
|
70
|
+
- [x] Thematic break (horizontal rule)
|
|
71
|
+
- [x] HTML block
|
|
72
|
+
- [x] Blockquote
|
|
73
|
+
- [x] List (ordered and unordered)
|
|
74
|
+
- [x] Link reference definitions
|
|
75
|
+
- [x] Table (GFM)
|
|
76
|
+
|
|
77
|
+
### Inline nodes
|
|
78
|
+
|
|
79
|
+
- [x] Text
|
|
80
|
+
- [x] Code span
|
|
81
|
+
- [x] Hard break
|
|
82
|
+
- [x] Soft break
|
|
83
|
+
- [x] HTML (inline)
|
|
84
|
+
- [x] Autolink
|
|
85
|
+
- [x] Link
|
|
86
|
+
- [x] Image
|
|
87
|
+
- [x] Emphasis
|
|
88
|
+
- [x] Strong
|
|
89
|
+
|
|
90
|
+
## Some notes on the implementation
|
|
91
|
+
|
|
92
|
+
The implementation is inspired by various other markdown parsers, including [commonmark.js](https://github.com/commonmark/commonmark.js), [markdown-it](https://github.com/markdown-it/markdown-it), and [marked.js](https://github.com/markedjs/marked). In fact, the implementation is structurally very similar to how commonmark.js goes about parsing; the only major difference is how we decide which lines to parse when streaming is set to true. I started with a much simpler and a lot more readable implementation for the parser, but it became complex when adding block container (blockquote and lists) support, so I ended up going for a slightly complex solution but a more robust and extensible one.
|
|
93
|
+
|
|
94
|
+
Commonmark specification allows link reference definitions to appear after the links that use them. Therefore, when streaming is enabled, it is important to consider that a link reference might not resolve, since its definition could arrive in a later chunk of the input.
|
|
95
|
+
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
interface CodeSpanNode {
|
|
2
|
+
type: "code-span";
|
|
3
|
+
text: string;
|
|
4
|
+
}
|
|
5
|
+
interface TextNode {
|
|
6
|
+
type: "text";
|
|
7
|
+
text: string;
|
|
8
|
+
}
|
|
9
|
+
interface HardBreakNode {
|
|
10
|
+
type: "hardbreak";
|
|
11
|
+
}
|
|
12
|
+
interface SoftBreakNode {
|
|
13
|
+
type: "softbreak";
|
|
14
|
+
}
|
|
15
|
+
interface StrongNode {
|
|
16
|
+
type: "strong";
|
|
17
|
+
children: Array<InlineNode>;
|
|
18
|
+
}
|
|
19
|
+
interface EmphasisNode {
|
|
20
|
+
type: "emphasis";
|
|
21
|
+
children: Array<InlineNode>;
|
|
22
|
+
}
|
|
23
|
+
interface LinkNode {
|
|
24
|
+
type: "link";
|
|
25
|
+
href: string;
|
|
26
|
+
title?: string;
|
|
27
|
+
children: Array<InlineNode>;
|
|
28
|
+
}
|
|
29
|
+
interface ImageNode {
|
|
30
|
+
type: "image";
|
|
31
|
+
href: string;
|
|
32
|
+
title?: string;
|
|
33
|
+
children: Array<InlineNode>;
|
|
34
|
+
}
|
|
35
|
+
interface HtmlTagNode {
|
|
36
|
+
type: "html";
|
|
37
|
+
content: string;
|
|
38
|
+
}
|
|
39
|
+
type InlineNode = TextNode | CodeSpanNode | HardBreakNode | SoftBreakNode | StrongNode | EmphasisNode | LinkNode | ImageNode | HtmlTagNode;
|
|
40
|
+
|
|
41
|
+
declare class MarkdownParser {
|
|
42
|
+
private splitter;
|
|
43
|
+
private root;
|
|
44
|
+
private nextNodeIndex;
|
|
45
|
+
private referenceDefinitions;
|
|
46
|
+
parse(input: string, options?: {
|
|
47
|
+
stream?: boolean;
|
|
48
|
+
}): BlockNode[];
|
|
49
|
+
private parseLine;
|
|
50
|
+
private parseReferenceLinkDefinitions;
|
|
51
|
+
private convertInternalBlockToPublicBlock;
|
|
52
|
+
get experimental_partialNodes(): BlockNode[];
|
|
53
|
+
private convertInternalBlockToPartialBlock;
|
|
54
|
+
}
|
|
55
|
+
interface TableNode {
|
|
56
|
+
type: "table";
|
|
57
|
+
head: {
|
|
58
|
+
cells: Array<{
|
|
59
|
+
align: "left" | "right" | "center" | undefined;
|
|
60
|
+
children: Array<InlineNode>;
|
|
61
|
+
}>;
|
|
62
|
+
};
|
|
63
|
+
body: {
|
|
64
|
+
rows: Array<{
|
|
65
|
+
cells: Array<{
|
|
66
|
+
align: "left" | "right" | "center" | undefined;
|
|
67
|
+
children: Array<InlineNode>;
|
|
68
|
+
}>;
|
|
69
|
+
}>;
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
interface ThematicBreakNode {
|
|
73
|
+
type: "thematic-break";
|
|
74
|
+
}
|
|
75
|
+
interface CodeBlockNode {
|
|
76
|
+
type: "code-block";
|
|
77
|
+
info?: string;
|
|
78
|
+
content: string;
|
|
79
|
+
}
|
|
80
|
+
interface BlockquoteNode {
|
|
81
|
+
type: "blockquote";
|
|
82
|
+
children: Array<BlockNode>;
|
|
83
|
+
}
|
|
84
|
+
type ListNode = {
|
|
85
|
+
type: "list";
|
|
86
|
+
tight: boolean;
|
|
87
|
+
items: Array<ListItemNode>;
|
|
88
|
+
} & ({
|
|
89
|
+
kind: "ordered";
|
|
90
|
+
start: number;
|
|
91
|
+
} | {
|
|
92
|
+
kind: "unordered";
|
|
93
|
+
marker: string;
|
|
94
|
+
});
|
|
95
|
+
type ListItemNode = {
|
|
96
|
+
type: "list-item";
|
|
97
|
+
children: Array<BlockNode>;
|
|
98
|
+
};
|
|
99
|
+
interface HtmlBlockNode {
|
|
100
|
+
type: "html-block";
|
|
101
|
+
content: string;
|
|
102
|
+
}
|
|
103
|
+
interface HeadingNode {
|
|
104
|
+
type: "heading";
|
|
105
|
+
level: 1 | 2 | 3 | 4 | 5 | 6;
|
|
106
|
+
children: Array<InlineNode>;
|
|
107
|
+
}
|
|
108
|
+
interface ParagraphNode {
|
|
109
|
+
type: "paragraph";
|
|
110
|
+
children: Array<InlineNode>;
|
|
111
|
+
}
|
|
112
|
+
type BlockNode = TableNode | ThematicBreakNode | CodeBlockNode | HeadingNode | ParagraphNode | BlockquoteNode | ListNode | HtmlBlockNode;
|
|
113
|
+
|
|
114
|
+
export { MarkdownParser };
|
|
115
|
+
export type { BlockNode, InlineNode };
|