emdp 1.0.0
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/LICENSE +21 -0
- package/README.md +124 -0
- package/dist/cjs/cli.js +36 -0
- package/dist/cjs/gfm.js +26 -0
- package/dist/cjs/index.js +26 -0
- package/dist/cjs/parser/block-parser.js +644 -0
- package/dist/cjs/parser/blocks/blockquote.js +28 -0
- package/dist/cjs/parser/blocks/code-block-fenced.js +58 -0
- package/dist/cjs/parser/blocks/code-block-indented.js +47 -0
- package/dist/cjs/parser/blocks/heading-atx.js +29 -0
- package/dist/cjs/parser/blocks/heading-setext.js +24 -0
- package/dist/cjs/parser/blocks/html-block.js +83 -0
- package/dist/cjs/parser/blocks/link-reference.js +109 -0
- package/dist/cjs/parser/blocks/list.js +155 -0
- package/dist/cjs/parser/blocks/paragraph.js +20 -0
- package/dist/cjs/parser/blocks/table.js +163 -0
- package/dist/cjs/parser/blocks/task-list.js +66 -0
- package/dist/cjs/parser/blocks/thematic-break.js +17 -0
- package/dist/cjs/parser/entities.js +2133 -0
- package/dist/cjs/parser/gfm/block-parser.js +773 -0
- package/dist/cjs/parser/gfm/index.js +125 -0
- package/dist/cjs/parser/gfm/inline-parser.js +813 -0
- package/dist/cjs/parser/gfm/renderer.js +513 -0
- package/dist/cjs/parser/index.js +104 -0
- package/dist/cjs/parser/inline-parser.js +564 -0
- package/dist/cjs/parser/inlines/autolink-extended.js +364 -0
- package/dist/cjs/parser/inlines/autolink.js +44 -0
- package/dist/cjs/parser/inlines/code-span.js +48 -0
- package/dist/cjs/parser/inlines/emphasis.js +64 -0
- package/dist/cjs/parser/inlines/entity.js +25 -0
- package/dist/cjs/parser/inlines/escape.js +25 -0
- package/dist/cjs/parser/inlines/footnote.js +41 -0
- package/dist/cjs/parser/inlines/hard-break.js +45 -0
- package/dist/cjs/parser/inlines/image.js +9 -0
- package/dist/cjs/parser/inlines/link.js +166 -0
- package/dist/cjs/parser/inlines/soft-break.js +18 -0
- package/dist/cjs/parser/inlines/strikethrough.js +48 -0
- package/dist/cjs/parser/inlines/text.js +20 -0
- package/dist/cjs/parser/renderer.js +345 -0
- package/dist/cjs/parser/types.js +5 -0
- package/dist/cjs/parser/utils.js +277 -0
- package/dist/cli.d.ts +6 -0
- package/dist/cli.js +36 -0
- package/dist/esm/cli.js +34 -0
- package/dist/esm/gfm.js +5 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/parser/block-parser.js +640 -0
- package/dist/esm/parser/blocks/blockquote.js +22 -0
- package/dist/esm/parser/blocks/code-block-fenced.js +52 -0
- package/dist/esm/parser/blocks/code-block-indented.js +42 -0
- package/dist/esm/parser/blocks/heading-atx.js +24 -0
- package/dist/esm/parser/blocks/heading-setext.js +19 -0
- package/dist/esm/parser/blocks/html-block.js +77 -0
- package/dist/esm/parser/blocks/link-reference.js +105 -0
- package/dist/esm/parser/blocks/list.js +145 -0
- package/dist/esm/parser/blocks/paragraph.js +15 -0
- package/dist/esm/parser/blocks/table.js +152 -0
- package/dist/esm/parser/blocks/task-list.js +61 -0
- package/dist/esm/parser/blocks/thematic-break.js +13 -0
- package/dist/esm/parser/entities.js +2130 -0
- package/dist/esm/parser/gfm/block-parser.js +769 -0
- package/dist/esm/parser/gfm/index.js +115 -0
- package/dist/esm/parser/gfm/inline-parser.js +809 -0
- package/dist/esm/parser/gfm/renderer.js +509 -0
- package/dist/esm/parser/index.js +80 -0
- package/dist/esm/parser/inline-parser.js +560 -0
- package/dist/esm/parser/inlines/autolink-extended.js +357 -0
- package/dist/esm/parser/inlines/autolink.js +40 -0
- package/dist/esm/parser/inlines/code-span.js +44 -0
- package/dist/esm/parser/inlines/emphasis.js +59 -0
- package/dist/esm/parser/inlines/entity.js +21 -0
- package/dist/esm/parser/inlines/escape.js +21 -0
- package/dist/esm/parser/inlines/footnote.js +38 -0
- package/dist/esm/parser/inlines/hard-break.js +41 -0
- package/dist/esm/parser/inlines/image.js +4 -0
- package/dist/esm/parser/inlines/link.js +156 -0
- package/dist/esm/parser/inlines/soft-break.js +14 -0
- package/dist/esm/parser/inlines/strikethrough.js +42 -0
- package/dist/esm/parser/inlines/text.js +16 -0
- package/dist/esm/parser/renderer.js +341 -0
- package/dist/esm/parser/types.js +4 -0
- package/dist/esm/parser/utils.js +254 -0
- package/dist/gfm.d.ts +6 -0
- package/dist/gfm.js +26 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +26 -0
- package/dist/parser/block-parser.d.ts +25 -0
- package/dist/parser/block-parser.js +644 -0
- package/dist/parser/blocks/blockquote.d.ts +8 -0
- package/dist/parser/blocks/blockquote.js +28 -0
- package/dist/parser/blocks/code-block-fenced.d.ts +14 -0
- package/dist/parser/blocks/code-block-fenced.js +58 -0
- package/dist/parser/blocks/code-block-indented.d.ts +7 -0
- package/dist/parser/blocks/code-block-indented.js +47 -0
- package/dist/parser/blocks/heading-atx.d.ts +10 -0
- package/dist/parser/blocks/heading-atx.js +29 -0
- package/dist/parser/blocks/heading-setext.d.ts +8 -0
- package/dist/parser/blocks/heading-setext.js +24 -0
- package/dist/parser/blocks/html-block.d.ts +9 -0
- package/dist/parser/blocks/html-block.js +83 -0
- package/dist/parser/blocks/link-reference.d.ts +11 -0
- package/dist/parser/blocks/link-reference.js +109 -0
- package/dist/parser/blocks/list.d.ts +25 -0
- package/dist/parser/blocks/list.js +155 -0
- package/dist/parser/blocks/paragraph.d.ts +7 -0
- package/dist/parser/blocks/paragraph.js +20 -0
- package/dist/parser/blocks/table.d.ts +13 -0
- package/dist/parser/blocks/table.js +163 -0
- package/dist/parser/blocks/task-list.d.ts +10 -0
- package/dist/parser/blocks/task-list.js +66 -0
- package/dist/parser/blocks/thematic-break.d.ts +6 -0
- package/dist/parser/blocks/thematic-break.js +17 -0
- package/dist/parser/entities.d.ts +4 -0
- package/dist/parser/entities.js +2133 -0
- package/dist/parser/gfm/block-parser.d.ts +32 -0
- package/dist/parser/gfm/block-parser.js +773 -0
- package/dist/parser/gfm/index.d.ts +31 -0
- package/dist/parser/gfm/index.js +125 -0
- package/dist/parser/gfm/inline-parser.d.ts +25 -0
- package/dist/parser/gfm/inline-parser.js +813 -0
- package/dist/parser/gfm/renderer.d.ts +43 -0
- package/dist/parser/gfm/renderer.js +513 -0
- package/dist/parser/index.d.ts +33 -0
- package/dist/parser/index.js +104 -0
- package/dist/parser/inline-parser.d.ts +16 -0
- package/dist/parser/inline-parser.js +564 -0
- package/dist/parser/inlines/autolink-extended.d.ts +24 -0
- package/dist/parser/inlines/autolink-extended.js +364 -0
- package/dist/parser/inlines/autolink.d.ts +9 -0
- package/dist/parser/inlines/autolink.js +44 -0
- package/dist/parser/inlines/code-span.d.ts +9 -0
- package/dist/parser/inlines/code-span.js +48 -0
- package/dist/parser/inlines/emphasis.d.ts +14 -0
- package/dist/parser/inlines/emphasis.js +64 -0
- package/dist/parser/inlines/entity.d.ts +8 -0
- package/dist/parser/inlines/entity.js +25 -0
- package/dist/parser/inlines/escape.d.ts +8 -0
- package/dist/parser/inlines/escape.js +25 -0
- package/dist/parser/inlines/footnote.d.ts +9 -0
- package/dist/parser/inlines/footnote.js +41 -0
- package/dist/parser/inlines/hard-break.d.ts +9 -0
- package/dist/parser/inlines/hard-break.js +45 -0
- package/dist/parser/inlines/image.d.ts +4 -0
- package/dist/parser/inlines/image.js +9 -0
- package/dist/parser/inlines/link.d.ts +33 -0
- package/dist/parser/inlines/link.js +166 -0
- package/dist/parser/inlines/soft-break.d.ts +9 -0
- package/dist/parser/inlines/soft-break.js +18 -0
- package/dist/parser/inlines/strikethrough.d.ts +16 -0
- package/dist/parser/inlines/strikethrough.js +48 -0
- package/dist/parser/inlines/text.d.ts +6 -0
- package/dist/parser/inlines/text.js +20 -0
- package/dist/parser/renderer.d.ts +33 -0
- package/dist/parser/renderer.js +345 -0
- package/dist/parser/types.d.ts +152 -0
- package/dist/parser/types.js +5 -0
- package/dist/parser/utils.d.ts +32 -0
- package/dist/parser/utils.js +277 -0
- package/dist/types/cli.d.ts +6 -0
- package/dist/types/gfm.d.ts +6 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/parser/block-parser.d.ts +25 -0
- package/dist/types/parser/blocks/blockquote.d.ts +8 -0
- package/dist/types/parser/blocks/code-block-fenced.d.ts +14 -0
- package/dist/types/parser/blocks/code-block-indented.d.ts +7 -0
- package/dist/types/parser/blocks/heading-atx.d.ts +10 -0
- package/dist/types/parser/blocks/heading-setext.d.ts +8 -0
- package/dist/types/parser/blocks/html-block.d.ts +9 -0
- package/dist/types/parser/blocks/link-reference.d.ts +11 -0
- package/dist/types/parser/blocks/list.d.ts +25 -0
- package/dist/types/parser/blocks/paragraph.d.ts +7 -0
- package/dist/types/parser/blocks/table.d.ts +13 -0
- package/dist/types/parser/blocks/task-list.d.ts +10 -0
- package/dist/types/parser/blocks/thematic-break.d.ts +6 -0
- package/dist/types/parser/entities.d.ts +4 -0
- package/dist/types/parser/gfm/block-parser.d.ts +32 -0
- package/dist/types/parser/gfm/index.d.ts +31 -0
- package/dist/types/parser/gfm/inline-parser.d.ts +25 -0
- package/dist/types/parser/gfm/renderer.d.ts +43 -0
- package/dist/types/parser/index.d.ts +33 -0
- package/dist/types/parser/inline-parser.d.ts +16 -0
- package/dist/types/parser/inlines/autolink-extended.d.ts +24 -0
- package/dist/types/parser/inlines/autolink.d.ts +9 -0
- package/dist/types/parser/inlines/code-span.d.ts +9 -0
- package/dist/types/parser/inlines/emphasis.d.ts +14 -0
- package/dist/types/parser/inlines/entity.d.ts +8 -0
- package/dist/types/parser/inlines/escape.d.ts +8 -0
- package/dist/types/parser/inlines/footnote.d.ts +9 -0
- package/dist/types/parser/inlines/hard-break.d.ts +9 -0
- package/dist/types/parser/inlines/image.d.ts +4 -0
- package/dist/types/parser/inlines/link.d.ts +33 -0
- package/dist/types/parser/inlines/soft-break.d.ts +9 -0
- package/dist/types/parser/inlines/strikethrough.d.ts +16 -0
- package/dist/types/parser/inlines/text.d.ts +6 -0
- package/dist/types/parser/renderer.d.ts +33 -0
- package/dist/types/parser/types.d.ts +152 -0
- package/dist/types/parser/utils.d.ts +32 -0
- package/package.json +54 -0
|
@@ -0,0 +1,564 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Inline parsing phase that builds inline nodes such as emphasis, links, images, code spans,
|
|
4
|
+
* autolinks, and HTML.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.InlineParser = void 0;
|
|
8
|
+
const escape_js_1 = require("./inlines/escape.js");
|
|
9
|
+
const entity_js_1 = require("./inlines/entity.js");
|
|
10
|
+
const code_span_js_1 = require("./inlines/code-span.js");
|
|
11
|
+
const emphasis_js_1 = require("./inlines/emphasis.js");
|
|
12
|
+
const autolink_js_1 = require("./inlines/autolink.js");
|
|
13
|
+
const link_js_1 = require("./inlines/link.js");
|
|
14
|
+
const text_js_1 = require("./inlines/text.js");
|
|
15
|
+
const utils_js_1 = require("./utils.js");
|
|
16
|
+
class InlineParser {
|
|
17
|
+
linkReferences = new Map();
|
|
18
|
+
setLinkReferences(refs) {
|
|
19
|
+
this.linkReferences = refs;
|
|
20
|
+
}
|
|
21
|
+
parse(text) {
|
|
22
|
+
const nodes = [];
|
|
23
|
+
let delimiterStack = null;
|
|
24
|
+
let bracketStack = null;
|
|
25
|
+
let pos = 0;
|
|
26
|
+
let textBuffer = '';
|
|
27
|
+
const flushText = () => {
|
|
28
|
+
if (textBuffer.length > 0) {
|
|
29
|
+
nodes.push((0, text_js_1.createTextNode)(textBuffer));
|
|
30
|
+
textBuffer = '';
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const pushDelimiter = (run, node) => {
|
|
34
|
+
const d = {
|
|
35
|
+
run,
|
|
36
|
+
node,
|
|
37
|
+
active: true,
|
|
38
|
+
prev: delimiterStack,
|
|
39
|
+
next: null,
|
|
40
|
+
};
|
|
41
|
+
if (delimiterStack) {
|
|
42
|
+
delimiterStack.next = d;
|
|
43
|
+
}
|
|
44
|
+
delimiterStack = d;
|
|
45
|
+
};
|
|
46
|
+
while (pos < text.length) {
|
|
47
|
+
const char = text[pos];
|
|
48
|
+
const escapeResult = (0, escape_js_1.parseEscape)(text, pos);
|
|
49
|
+
if (escapeResult) {
|
|
50
|
+
if (escapeResult.char === '\n') {
|
|
51
|
+
textBuffer = textBuffer.replace(/ *$/, '');
|
|
52
|
+
flushText();
|
|
53
|
+
nodes.push({ type: 'hardbreak' });
|
|
54
|
+
pos += escapeResult.length;
|
|
55
|
+
while (pos < text.length && text[pos] === ' ') {
|
|
56
|
+
pos++;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
else if (escapeResult.char === '*' || escapeResult.char === '_') {
|
|
60
|
+
flushText();
|
|
61
|
+
nodes.push((0, text_js_1.createTextNode)(escapeResult.char, true));
|
|
62
|
+
pos += escapeResult.length;
|
|
63
|
+
}
|
|
64
|
+
else if (escapeResult.char === '"' || escapeResult.char === "'" ||
|
|
65
|
+
escapeResult.char === '-' || escapeResult.char === '.') {
|
|
66
|
+
flushText();
|
|
67
|
+
const node = (0, text_js_1.createTextNode)(escapeResult.char);
|
|
68
|
+
node.noSmart = true;
|
|
69
|
+
nodes.push(node);
|
|
70
|
+
pos += escapeResult.length;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
textBuffer += escapeResult.char;
|
|
74
|
+
pos += escapeResult.length;
|
|
75
|
+
}
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const entityResult = (0, entity_js_1.parseEntity)(text, pos);
|
|
79
|
+
if (entityResult) {
|
|
80
|
+
textBuffer += entityResult.char;
|
|
81
|
+
pos += entityResult.length;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (char === '`') {
|
|
85
|
+
const codeSpanResult = (0, code_span_js_1.parseCodeSpan)(text, pos);
|
|
86
|
+
if (codeSpanResult) {
|
|
87
|
+
flushText();
|
|
88
|
+
nodes.push(codeSpanResult.node);
|
|
89
|
+
pos += codeSpanResult.length;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
textBuffer += char;
|
|
94
|
+
pos++;
|
|
95
|
+
while (pos < text.length && text[pos] === '`') {
|
|
96
|
+
textBuffer += text[pos];
|
|
97
|
+
pos++;
|
|
98
|
+
}
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (char === '<') {
|
|
103
|
+
const autolinkResult = (0, autolink_js_1.parseAutolink)(text, pos);
|
|
104
|
+
if (autolinkResult) {
|
|
105
|
+
flushText();
|
|
106
|
+
nodes.push(autolinkResult.node);
|
|
107
|
+
pos += autolinkResult.length;
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
const openingTagMatch = text.slice(pos).match(/^<[a-zA-Z][a-zA-Z0-9-]*(?:\s+[a-zA-Z_:][a-zA-Z0-9_.:-]*(?:\s*=\s*(?:[^\s"'=<>`]+|'[^']*'|"[^"]*"))?)*\s*\/?>/);
|
|
111
|
+
if (openingTagMatch) {
|
|
112
|
+
flushText();
|
|
113
|
+
nodes.push({ type: 'html_inline', literal: openingTagMatch[0] });
|
|
114
|
+
pos += openingTagMatch[0].length;
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
const closingTagMatch = text.slice(pos).match(/^<\/[a-zA-Z][a-zA-Z0-9-]*\s*>/);
|
|
118
|
+
if (closingTagMatch) {
|
|
119
|
+
flushText();
|
|
120
|
+
nodes.push({ type: 'html_inline', literal: closingTagMatch[0] });
|
|
121
|
+
pos += closingTagMatch[0].length;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
const shortCommentMatch = text.slice(pos).match(/^<!---?>/);
|
|
125
|
+
if (shortCommentMatch) {
|
|
126
|
+
flushText();
|
|
127
|
+
nodes.push({ type: 'html_inline', literal: '<!---->' });
|
|
128
|
+
pos += shortCommentMatch[0].length;
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
const commentMatch = text.slice(pos).match(/^<!--(?!>)(?!-?>)[\s\S]*?-->/);
|
|
132
|
+
if (commentMatch) {
|
|
133
|
+
flushText();
|
|
134
|
+
nodes.push({ type: 'html_inline', literal: commentMatch[0] });
|
|
135
|
+
pos += commentMatch[0].length;
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
const piMatch = text.slice(pos).match(/^<\?[\s\S]*?\?>/);
|
|
139
|
+
if (piMatch) {
|
|
140
|
+
flushText();
|
|
141
|
+
nodes.push({ type: 'html_inline', literal: piMatch[0] });
|
|
142
|
+
pos += piMatch[0].length;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
const cdataMatch = text.slice(pos).match(/^<!\[CDATA\[[\s\S]*?\]\]>/);
|
|
146
|
+
if (cdataMatch) {
|
|
147
|
+
flushText();
|
|
148
|
+
nodes.push({ type: 'html_inline', literal: cdataMatch[0] });
|
|
149
|
+
pos += cdataMatch[0].length;
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
const declMatch = text.slice(pos).match(/^<![a-zA-Z][\s\S]*?>/);
|
|
153
|
+
if (declMatch) {
|
|
154
|
+
flushText();
|
|
155
|
+
nodes.push({ type: 'html_inline', literal: declMatch[0] });
|
|
156
|
+
pos += declMatch[0].length;
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (char === ' ') {
|
|
161
|
+
let spaceCount = 0;
|
|
162
|
+
let j = pos;
|
|
163
|
+
while (j < text.length && text[j] === ' ') {
|
|
164
|
+
spaceCount++;
|
|
165
|
+
j++;
|
|
166
|
+
}
|
|
167
|
+
if (text[j] === '\n' && spaceCount >= 2) {
|
|
168
|
+
textBuffer = textBuffer.replace(/ *$/, '');
|
|
169
|
+
flushText();
|
|
170
|
+
nodes.push({ type: 'hardbreak' });
|
|
171
|
+
pos = j + 1;
|
|
172
|
+
while (pos < text.length && text[pos] === ' ') {
|
|
173
|
+
pos++;
|
|
174
|
+
}
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (char === '\n') {
|
|
179
|
+
textBuffer = textBuffer.replace(/ *$/, '');
|
|
180
|
+
flushText();
|
|
181
|
+
nodes.push({ type: 'softbreak' });
|
|
182
|
+
pos++;
|
|
183
|
+
while (pos < text.length && text[pos] === ' ') {
|
|
184
|
+
pos++;
|
|
185
|
+
}
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
if (char === '*' || char === '_') {
|
|
189
|
+
flushText();
|
|
190
|
+
const run = (0, emphasis_js_1.parseDelimiterRun)(text, pos);
|
|
191
|
+
if (run) {
|
|
192
|
+
const node = (0, text_js_1.createTextNode)(text.slice(pos, pos + run.length));
|
|
193
|
+
node.delimiterOrigLength = run.length;
|
|
194
|
+
nodes.push(node);
|
|
195
|
+
if (run.canOpen || run.canClose) {
|
|
196
|
+
pushDelimiter(run, node);
|
|
197
|
+
}
|
|
198
|
+
pos += run.length;
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (char === '!' && text[pos + 1] === '[') {
|
|
203
|
+
flushText();
|
|
204
|
+
const node = (0, text_js_1.createTextNode)('![');
|
|
205
|
+
nodes.push(node);
|
|
206
|
+
const bracket = {
|
|
207
|
+
type: 'image',
|
|
208
|
+
node,
|
|
209
|
+
nodeIndex: nodes.length - 1,
|
|
210
|
+
active: true,
|
|
211
|
+
prev: bracketStack,
|
|
212
|
+
bracketAfter: false,
|
|
213
|
+
delimiterBefore: delimiterStack,
|
|
214
|
+
textPos: pos + 2,
|
|
215
|
+
};
|
|
216
|
+
if (bracketStack)
|
|
217
|
+
bracketStack.bracketAfter = true;
|
|
218
|
+
bracketStack = bracket;
|
|
219
|
+
pos += 2;
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
if (char === '[') {
|
|
223
|
+
flushText();
|
|
224
|
+
const node = (0, text_js_1.createTextNode)('[');
|
|
225
|
+
nodes.push(node);
|
|
226
|
+
const bracket = {
|
|
227
|
+
type: 'link',
|
|
228
|
+
node,
|
|
229
|
+
nodeIndex: nodes.length - 1,
|
|
230
|
+
active: true,
|
|
231
|
+
prev: bracketStack,
|
|
232
|
+
bracketAfter: false,
|
|
233
|
+
delimiterBefore: delimiterStack,
|
|
234
|
+
textPos: pos + 1,
|
|
235
|
+
};
|
|
236
|
+
if (bracketStack)
|
|
237
|
+
bracketStack.bracketAfter = true;
|
|
238
|
+
bracketStack = bracket;
|
|
239
|
+
pos += 1;
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
if (char === ']' && bracketStack) {
|
|
243
|
+
flushText();
|
|
244
|
+
let opener = bracketStack;
|
|
245
|
+
if (!opener) {
|
|
246
|
+
textBuffer += ']';
|
|
247
|
+
pos++;
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
if (!opener.active) {
|
|
251
|
+
bracketStack = opener.prev;
|
|
252
|
+
textBuffer += ']';
|
|
253
|
+
pos++;
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
let matched = false;
|
|
257
|
+
let destination = '';
|
|
258
|
+
let title = '';
|
|
259
|
+
let consumed = 1;
|
|
260
|
+
if (text[pos + 1] === '(') {
|
|
261
|
+
const linkResult = (0, link_js_1.parseInlineLink)(text, pos + 1);
|
|
262
|
+
if (linkResult) {
|
|
263
|
+
matched = true;
|
|
264
|
+
destination = linkResult.destination;
|
|
265
|
+
title = linkResult.title;
|
|
266
|
+
consumed = 1 + linkResult.length;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
if (!matched && text[pos + 1] === '[') {
|
|
270
|
+
const labelResult = (0, link_js_1.parseLinkLabel)(text, pos + 1);
|
|
271
|
+
if (labelResult && labelResult.label) {
|
|
272
|
+
if (this.linkReferences.has(labelResult.label)) {
|
|
273
|
+
matched = true;
|
|
274
|
+
const ref = this.linkReferences.get(labelResult.label);
|
|
275
|
+
destination = ref.destination;
|
|
276
|
+
title = ref.title;
|
|
277
|
+
consumed = 1 + labelResult.length;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
else if (labelResult && labelResult.label === '') {
|
|
281
|
+
const innerText = this.extractTextFromNodes(nodes.slice(opener.nodeIndex + 1));
|
|
282
|
+
const label = (0, utils_js_1.normalizeLabel)(innerText);
|
|
283
|
+
if (this.linkReferences.has(label)) {
|
|
284
|
+
matched = true;
|
|
285
|
+
const ref = this.linkReferences.get(label);
|
|
286
|
+
destination = ref.destination;
|
|
287
|
+
title = ref.title;
|
|
288
|
+
consumed = 1 + labelResult.length;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
if (!matched && text[pos + 1] !== '[') {
|
|
293
|
+
const rawLabel = text.slice(opener.textPos, pos);
|
|
294
|
+
const label = (0, utils_js_1.normalizeLabel)(this.normalizeLabelForMatching(rawLabel));
|
|
295
|
+
if (label && this.linkReferences.has(label)) {
|
|
296
|
+
matched = true;
|
|
297
|
+
const ref = this.linkReferences.get(label);
|
|
298
|
+
destination = ref.destination;
|
|
299
|
+
title = ref.title;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if (matched) {
|
|
303
|
+
const innerNodes = nodes.splice(opener.nodeIndex + 1);
|
|
304
|
+
nodes.pop();
|
|
305
|
+
this.processEmphasis(innerNodes, opener.delimiterBefore);
|
|
306
|
+
if (opener.type === 'image') {
|
|
307
|
+
const alt = this.extractTextFromNodes(innerNodes);
|
|
308
|
+
nodes.push((0, link_js_1.createImageNode)(destination, title, alt));
|
|
309
|
+
}
|
|
310
|
+
else {
|
|
311
|
+
const linkNode = (0, link_js_1.createLinkNode)(destination, title);
|
|
312
|
+
linkNode.children = innerNodes;
|
|
313
|
+
nodes.push(linkNode);
|
|
314
|
+
}
|
|
315
|
+
if (opener.type === 'link') {
|
|
316
|
+
let b = opener.prev;
|
|
317
|
+
while (b) {
|
|
318
|
+
if (b.type === 'link')
|
|
319
|
+
b.active = false;
|
|
320
|
+
b = b.prev;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
bracketStack = opener.prev;
|
|
324
|
+
pos += consumed;
|
|
325
|
+
}
|
|
326
|
+
else {
|
|
327
|
+
bracketStack = opener.prev;
|
|
328
|
+
textBuffer += ']';
|
|
329
|
+
pos++;
|
|
330
|
+
}
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
textBuffer += char;
|
|
334
|
+
pos++;
|
|
335
|
+
}
|
|
336
|
+
flushText();
|
|
337
|
+
this.processEmphasis(nodes, null);
|
|
338
|
+
return nodes;
|
|
339
|
+
}
|
|
340
|
+
extractTextFromNodes(nodes) {
|
|
341
|
+
let result = '';
|
|
342
|
+
for (const node of nodes) {
|
|
343
|
+
if (node.type === 'text') {
|
|
344
|
+
result += node.literal;
|
|
345
|
+
}
|
|
346
|
+
else if (node.type === 'code_span') {
|
|
347
|
+
result += node.literal;
|
|
348
|
+
}
|
|
349
|
+
else if (node.type === 'image') {
|
|
350
|
+
result += node.alt;
|
|
351
|
+
}
|
|
352
|
+
else if (node.type === 'softbreak' || node.type === 'hardbreak') {
|
|
353
|
+
result += ' ';
|
|
354
|
+
}
|
|
355
|
+
else if ('children' in node && Array.isArray(node.children)) {
|
|
356
|
+
result += this.extractTextFromNodes(node.children);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
return result;
|
|
360
|
+
}
|
|
361
|
+
normalizeLabelForMatching(rawLabel) {
|
|
362
|
+
let result = '';
|
|
363
|
+
let i = 0;
|
|
364
|
+
while (i < rawLabel.length) {
|
|
365
|
+
if (rawLabel[i] === '\\' && i + 1 < rawLabel.length) {
|
|
366
|
+
const next = rawLabel[i + 1];
|
|
367
|
+
if (next === '[' || next === ']' || next === '\\') {
|
|
368
|
+
result += next;
|
|
369
|
+
i += 2;
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
result += rawLabel[i];
|
|
374
|
+
i++;
|
|
375
|
+
}
|
|
376
|
+
return result;
|
|
377
|
+
}
|
|
378
|
+
processEmphasis(nodes, stackBottom) {
|
|
379
|
+
const delimiters = [];
|
|
380
|
+
let prevDelim = null;
|
|
381
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
382
|
+
const node = nodes[i];
|
|
383
|
+
if (node.type === 'text' && (node.literal.match(/^[*_]+$/) || node.literal === '')) {
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
const nodeDelims = [];
|
|
387
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
388
|
+
const node = nodes[i];
|
|
389
|
+
if (node.type === 'text' && node.literal.length > 0 && !node.noDelim) {
|
|
390
|
+
const firstChar = node.literal[0];
|
|
391
|
+
if ((firstChar === '*' || firstChar === '_') && node.literal.match(/^[*_]+$/)) {
|
|
392
|
+
const beforeNode = i > 0 ? nodes[i - 1] : null;
|
|
393
|
+
const afterNode = i < nodes.length - 1 ? nodes[i + 1] : null;
|
|
394
|
+
let charBefore = '\n';
|
|
395
|
+
if (beforeNode) {
|
|
396
|
+
if (beforeNode.type === 'text') {
|
|
397
|
+
charBefore = this.getLastChar(beforeNode.literal);
|
|
398
|
+
}
|
|
399
|
+
else if (beforeNode.type === 'softbreak' || beforeNode.type === 'hardbreak') {
|
|
400
|
+
charBefore = '\n';
|
|
401
|
+
}
|
|
402
|
+
else {
|
|
403
|
+
charBefore = 'a';
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
let charAfter = '\n';
|
|
407
|
+
if (afterNode) {
|
|
408
|
+
if (afterNode.type === 'text') {
|
|
409
|
+
charAfter = this.getFirstChar(afterNode.literal);
|
|
410
|
+
}
|
|
411
|
+
else if (afterNode.type === 'softbreak' || afterNode.type === 'hardbreak') {
|
|
412
|
+
charAfter = '\n';
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
charAfter = 'a';
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
const run = this.computeDelimiterRun(node.literal, charBefore, charAfter);
|
|
419
|
+
node.delimiterOrigLength = node.delimiterOrigLength ?? node.literal.length;
|
|
420
|
+
run.origLength = node.delimiterOrigLength;
|
|
421
|
+
if (run.canOpen || run.canClose) {
|
|
422
|
+
nodeDelims.push({ node, index: i, run });
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
if (nodeDelims.length === 0)
|
|
428
|
+
return;
|
|
429
|
+
const openerBottomByChar = { '*': -1, '_': -1 };
|
|
430
|
+
let closerIdx = 0;
|
|
431
|
+
while (closerIdx < nodeDelims.length) {
|
|
432
|
+
const closer = nodeDelims[closerIdx];
|
|
433
|
+
if (!closer.run.canClose) {
|
|
434
|
+
closerIdx++;
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
let openerIdx = closerIdx - 1;
|
|
438
|
+
let openerFound = false;
|
|
439
|
+
while (openerIdx >= 0 && openerIdx > openerBottomByChar[closer.run.char]) {
|
|
440
|
+
const opener = nodeDelims[openerIdx];
|
|
441
|
+
if (opener.run.canOpen && opener.run.char === closer.run.char &&
|
|
442
|
+
(0, emphasis_js_1.canDelimitersMatch)(opener.run, closer.run)) {
|
|
443
|
+
openerFound = true;
|
|
444
|
+
break;
|
|
445
|
+
}
|
|
446
|
+
openerIdx--;
|
|
447
|
+
}
|
|
448
|
+
if (!openerFound) {
|
|
449
|
+
if (!closer.run.canOpen) {
|
|
450
|
+
openerBottomByChar[closer.run.char] = closerIdx - 1;
|
|
451
|
+
}
|
|
452
|
+
closerIdx++;
|
|
453
|
+
continue;
|
|
454
|
+
}
|
|
455
|
+
const opener = nodeDelims[openerIdx];
|
|
456
|
+
const strong = opener.run.length >= 2 && closer.run.length >= 2;
|
|
457
|
+
const numDelims = strong ? 2 : 1;
|
|
458
|
+
opener.node.literal = opener.node.literal.slice(0, -numDelims);
|
|
459
|
+
closer.node.literal = closer.node.literal.slice(numDelims);
|
|
460
|
+
opener.run.length -= numDelims;
|
|
461
|
+
closer.run.length -= numDelims;
|
|
462
|
+
const openerNodeIdx = nodes.indexOf(opener.node);
|
|
463
|
+
const closerNodeIdx = nodes.indexOf(closer.node);
|
|
464
|
+
const contentNodes = nodes.slice(openerNodeIdx + 1, closerNodeIdx);
|
|
465
|
+
const emphNode = strong
|
|
466
|
+
? { type: 'strong', children: contentNodes }
|
|
467
|
+
: { type: 'emphasis', children: contentNodes };
|
|
468
|
+
nodes.splice(openerNodeIdx + 1, closerNodeIdx - openerNodeIdx - 1, emphNode);
|
|
469
|
+
const shift = closerNodeIdx - openerNodeIdx - 2;
|
|
470
|
+
for (let i = openerIdx + 1; i < nodeDelims.length; i++) {
|
|
471
|
+
if (i < closerIdx) {
|
|
472
|
+
nodeDelims[i].run.length = 0;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
if (opener.run.length === 0) {
|
|
476
|
+
const idx = nodes.indexOf(opener.node);
|
|
477
|
+
if (idx !== -1 && opener.node.literal === '') {
|
|
478
|
+
nodes.splice(idx, 1);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
if (closer.run.length === 0) {
|
|
482
|
+
const idx = nodes.indexOf(closer.node);
|
|
483
|
+
if (idx !== -1 && closer.node.literal === '') {
|
|
484
|
+
nodes.splice(idx, 1);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
nodeDelims.length = 0;
|
|
488
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
489
|
+
const node = nodes[i];
|
|
490
|
+
if (node.type === 'text' && node.literal.length > 0 && !node.noDelim) {
|
|
491
|
+
const firstChar = node.literal[0];
|
|
492
|
+
if ((firstChar === '*' || firstChar === '_') && node.literal.match(/^[*_]+$/)) {
|
|
493
|
+
const beforeNode = i > 0 ? nodes[i - 1] : null;
|
|
494
|
+
const afterNode = i < nodes.length - 1 ? nodes[i + 1] : null;
|
|
495
|
+
let charBefore = '\n';
|
|
496
|
+
let charAfter = '\n';
|
|
497
|
+
if (beforeNode) {
|
|
498
|
+
if (beforeNode.type === 'text')
|
|
499
|
+
charBefore = this.getLastChar(beforeNode.literal);
|
|
500
|
+
else if (beforeNode.type !== 'softbreak' && beforeNode.type !== 'hardbreak')
|
|
501
|
+
charBefore = 'a';
|
|
502
|
+
}
|
|
503
|
+
if (afterNode) {
|
|
504
|
+
if (afterNode.type === 'text')
|
|
505
|
+
charAfter = this.getFirstChar(afterNode.literal);
|
|
506
|
+
else if (afterNode.type !== 'softbreak' && afterNode.type !== 'hardbreak')
|
|
507
|
+
charAfter = 'a';
|
|
508
|
+
}
|
|
509
|
+
const run = this.computeDelimiterRun(node.literal, charBefore, charAfter);
|
|
510
|
+
node.delimiterOrigLength = node.delimiterOrigLength ?? node.literal.length;
|
|
511
|
+
run.origLength = node.delimiterOrigLength;
|
|
512
|
+
if (run.canOpen || run.canClose) {
|
|
513
|
+
nodeDelims.push({ node, index: i, run });
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
closerIdx = 0;
|
|
519
|
+
}
|
|
520
|
+
for (let i = nodes.length - 1; i >= 0; i--) {
|
|
521
|
+
const node = nodes[i];
|
|
522
|
+
if (node.type === 'text' && node.literal === '') {
|
|
523
|
+
nodes.splice(i, 1);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
computeDelimiterRun(literal, charBefore, charAfter) {
|
|
528
|
+
const char = literal[0];
|
|
529
|
+
const length = literal.length;
|
|
530
|
+
const isWhitespace = (c) => /\s/.test(c) || c === '\n';
|
|
531
|
+
const isPunctuation = (c) => /[\p{P}\p{S}]/u.test(c);
|
|
532
|
+
const beforeIsWhitespace = isWhitespace(charBefore);
|
|
533
|
+
const afterIsWhitespace = isWhitespace(charAfter);
|
|
534
|
+
const beforeIsPunctuation = isPunctuation(charBefore);
|
|
535
|
+
const afterIsPunctuation = isPunctuation(charAfter);
|
|
536
|
+
const leftFlanking = !afterIsWhitespace &&
|
|
537
|
+
(!afterIsPunctuation || beforeIsWhitespace || beforeIsPunctuation);
|
|
538
|
+
const rightFlanking = !beforeIsWhitespace &&
|
|
539
|
+
(!beforeIsPunctuation || afterIsWhitespace || afterIsPunctuation);
|
|
540
|
+
let canOpen;
|
|
541
|
+
let canClose;
|
|
542
|
+
if (char === '*') {
|
|
543
|
+
canOpen = leftFlanking;
|
|
544
|
+
canClose = rightFlanking;
|
|
545
|
+
}
|
|
546
|
+
else {
|
|
547
|
+
canOpen = leftFlanking && (!rightFlanking || beforeIsPunctuation);
|
|
548
|
+
canClose = rightFlanking && (!leftFlanking || afterIsPunctuation);
|
|
549
|
+
}
|
|
550
|
+
return { char, length, canOpen, canClose, position: 0, origLength: length };
|
|
551
|
+
}
|
|
552
|
+
getFirstChar(text) {
|
|
553
|
+
if (!text)
|
|
554
|
+
return '\n';
|
|
555
|
+
return Array.from(text)[0] ?? '\n';
|
|
556
|
+
}
|
|
557
|
+
getLastChar(text) {
|
|
558
|
+
if (!text)
|
|
559
|
+
return '\n';
|
|
560
|
+
const chars = Array.from(text);
|
|
561
|
+
return chars.length > 0 ? chars[chars.length - 1] : '\n';
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
exports.InlineParser = InlineParser;
|