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.
Files changed (199) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +124 -0
  3. package/dist/cjs/cli.js +36 -0
  4. package/dist/cjs/gfm.js +26 -0
  5. package/dist/cjs/index.js +26 -0
  6. package/dist/cjs/parser/block-parser.js +644 -0
  7. package/dist/cjs/parser/blocks/blockquote.js +28 -0
  8. package/dist/cjs/parser/blocks/code-block-fenced.js +58 -0
  9. package/dist/cjs/parser/blocks/code-block-indented.js +47 -0
  10. package/dist/cjs/parser/blocks/heading-atx.js +29 -0
  11. package/dist/cjs/parser/blocks/heading-setext.js +24 -0
  12. package/dist/cjs/parser/blocks/html-block.js +83 -0
  13. package/dist/cjs/parser/blocks/link-reference.js +109 -0
  14. package/dist/cjs/parser/blocks/list.js +155 -0
  15. package/dist/cjs/parser/blocks/paragraph.js +20 -0
  16. package/dist/cjs/parser/blocks/table.js +163 -0
  17. package/dist/cjs/parser/blocks/task-list.js +66 -0
  18. package/dist/cjs/parser/blocks/thematic-break.js +17 -0
  19. package/dist/cjs/parser/entities.js +2133 -0
  20. package/dist/cjs/parser/gfm/block-parser.js +773 -0
  21. package/dist/cjs/parser/gfm/index.js +125 -0
  22. package/dist/cjs/parser/gfm/inline-parser.js +813 -0
  23. package/dist/cjs/parser/gfm/renderer.js +513 -0
  24. package/dist/cjs/parser/index.js +104 -0
  25. package/dist/cjs/parser/inline-parser.js +564 -0
  26. package/dist/cjs/parser/inlines/autolink-extended.js +364 -0
  27. package/dist/cjs/parser/inlines/autolink.js +44 -0
  28. package/dist/cjs/parser/inlines/code-span.js +48 -0
  29. package/dist/cjs/parser/inlines/emphasis.js +64 -0
  30. package/dist/cjs/parser/inlines/entity.js +25 -0
  31. package/dist/cjs/parser/inlines/escape.js +25 -0
  32. package/dist/cjs/parser/inlines/footnote.js +41 -0
  33. package/dist/cjs/parser/inlines/hard-break.js +45 -0
  34. package/dist/cjs/parser/inlines/image.js +9 -0
  35. package/dist/cjs/parser/inlines/link.js +166 -0
  36. package/dist/cjs/parser/inlines/soft-break.js +18 -0
  37. package/dist/cjs/parser/inlines/strikethrough.js +48 -0
  38. package/dist/cjs/parser/inlines/text.js +20 -0
  39. package/dist/cjs/parser/renderer.js +345 -0
  40. package/dist/cjs/parser/types.js +5 -0
  41. package/dist/cjs/parser/utils.js +277 -0
  42. package/dist/cli.d.ts +6 -0
  43. package/dist/cli.js +36 -0
  44. package/dist/esm/cli.js +34 -0
  45. package/dist/esm/gfm.js +5 -0
  46. package/dist/esm/index.js +5 -0
  47. package/dist/esm/package.json +3 -0
  48. package/dist/esm/parser/block-parser.js +640 -0
  49. package/dist/esm/parser/blocks/blockquote.js +22 -0
  50. package/dist/esm/parser/blocks/code-block-fenced.js +52 -0
  51. package/dist/esm/parser/blocks/code-block-indented.js +42 -0
  52. package/dist/esm/parser/blocks/heading-atx.js +24 -0
  53. package/dist/esm/parser/blocks/heading-setext.js +19 -0
  54. package/dist/esm/parser/blocks/html-block.js +77 -0
  55. package/dist/esm/parser/blocks/link-reference.js +105 -0
  56. package/dist/esm/parser/blocks/list.js +145 -0
  57. package/dist/esm/parser/blocks/paragraph.js +15 -0
  58. package/dist/esm/parser/blocks/table.js +152 -0
  59. package/dist/esm/parser/blocks/task-list.js +61 -0
  60. package/dist/esm/parser/blocks/thematic-break.js +13 -0
  61. package/dist/esm/parser/entities.js +2130 -0
  62. package/dist/esm/parser/gfm/block-parser.js +769 -0
  63. package/dist/esm/parser/gfm/index.js +115 -0
  64. package/dist/esm/parser/gfm/inline-parser.js +809 -0
  65. package/dist/esm/parser/gfm/renderer.js +509 -0
  66. package/dist/esm/parser/index.js +80 -0
  67. package/dist/esm/parser/inline-parser.js +560 -0
  68. package/dist/esm/parser/inlines/autolink-extended.js +357 -0
  69. package/dist/esm/parser/inlines/autolink.js +40 -0
  70. package/dist/esm/parser/inlines/code-span.js +44 -0
  71. package/dist/esm/parser/inlines/emphasis.js +59 -0
  72. package/dist/esm/parser/inlines/entity.js +21 -0
  73. package/dist/esm/parser/inlines/escape.js +21 -0
  74. package/dist/esm/parser/inlines/footnote.js +38 -0
  75. package/dist/esm/parser/inlines/hard-break.js +41 -0
  76. package/dist/esm/parser/inlines/image.js +4 -0
  77. package/dist/esm/parser/inlines/link.js +156 -0
  78. package/dist/esm/parser/inlines/soft-break.js +14 -0
  79. package/dist/esm/parser/inlines/strikethrough.js +42 -0
  80. package/dist/esm/parser/inlines/text.js +16 -0
  81. package/dist/esm/parser/renderer.js +341 -0
  82. package/dist/esm/parser/types.js +4 -0
  83. package/dist/esm/parser/utils.js +254 -0
  84. package/dist/gfm.d.ts +6 -0
  85. package/dist/gfm.js +26 -0
  86. package/dist/index.d.ts +5 -0
  87. package/dist/index.js +26 -0
  88. package/dist/parser/block-parser.d.ts +25 -0
  89. package/dist/parser/block-parser.js +644 -0
  90. package/dist/parser/blocks/blockquote.d.ts +8 -0
  91. package/dist/parser/blocks/blockquote.js +28 -0
  92. package/dist/parser/blocks/code-block-fenced.d.ts +14 -0
  93. package/dist/parser/blocks/code-block-fenced.js +58 -0
  94. package/dist/parser/blocks/code-block-indented.d.ts +7 -0
  95. package/dist/parser/blocks/code-block-indented.js +47 -0
  96. package/dist/parser/blocks/heading-atx.d.ts +10 -0
  97. package/dist/parser/blocks/heading-atx.js +29 -0
  98. package/dist/parser/blocks/heading-setext.d.ts +8 -0
  99. package/dist/parser/blocks/heading-setext.js +24 -0
  100. package/dist/parser/blocks/html-block.d.ts +9 -0
  101. package/dist/parser/blocks/html-block.js +83 -0
  102. package/dist/parser/blocks/link-reference.d.ts +11 -0
  103. package/dist/parser/blocks/link-reference.js +109 -0
  104. package/dist/parser/blocks/list.d.ts +25 -0
  105. package/dist/parser/blocks/list.js +155 -0
  106. package/dist/parser/blocks/paragraph.d.ts +7 -0
  107. package/dist/parser/blocks/paragraph.js +20 -0
  108. package/dist/parser/blocks/table.d.ts +13 -0
  109. package/dist/parser/blocks/table.js +163 -0
  110. package/dist/parser/blocks/task-list.d.ts +10 -0
  111. package/dist/parser/blocks/task-list.js +66 -0
  112. package/dist/parser/blocks/thematic-break.d.ts +6 -0
  113. package/dist/parser/blocks/thematic-break.js +17 -0
  114. package/dist/parser/entities.d.ts +4 -0
  115. package/dist/parser/entities.js +2133 -0
  116. package/dist/parser/gfm/block-parser.d.ts +32 -0
  117. package/dist/parser/gfm/block-parser.js +773 -0
  118. package/dist/parser/gfm/index.d.ts +31 -0
  119. package/dist/parser/gfm/index.js +125 -0
  120. package/dist/parser/gfm/inline-parser.d.ts +25 -0
  121. package/dist/parser/gfm/inline-parser.js +813 -0
  122. package/dist/parser/gfm/renderer.d.ts +43 -0
  123. package/dist/parser/gfm/renderer.js +513 -0
  124. package/dist/parser/index.d.ts +33 -0
  125. package/dist/parser/index.js +104 -0
  126. package/dist/parser/inline-parser.d.ts +16 -0
  127. package/dist/parser/inline-parser.js +564 -0
  128. package/dist/parser/inlines/autolink-extended.d.ts +24 -0
  129. package/dist/parser/inlines/autolink-extended.js +364 -0
  130. package/dist/parser/inlines/autolink.d.ts +9 -0
  131. package/dist/parser/inlines/autolink.js +44 -0
  132. package/dist/parser/inlines/code-span.d.ts +9 -0
  133. package/dist/parser/inlines/code-span.js +48 -0
  134. package/dist/parser/inlines/emphasis.d.ts +14 -0
  135. package/dist/parser/inlines/emphasis.js +64 -0
  136. package/dist/parser/inlines/entity.d.ts +8 -0
  137. package/dist/parser/inlines/entity.js +25 -0
  138. package/dist/parser/inlines/escape.d.ts +8 -0
  139. package/dist/parser/inlines/escape.js +25 -0
  140. package/dist/parser/inlines/footnote.d.ts +9 -0
  141. package/dist/parser/inlines/footnote.js +41 -0
  142. package/dist/parser/inlines/hard-break.d.ts +9 -0
  143. package/dist/parser/inlines/hard-break.js +45 -0
  144. package/dist/parser/inlines/image.d.ts +4 -0
  145. package/dist/parser/inlines/image.js +9 -0
  146. package/dist/parser/inlines/link.d.ts +33 -0
  147. package/dist/parser/inlines/link.js +166 -0
  148. package/dist/parser/inlines/soft-break.d.ts +9 -0
  149. package/dist/parser/inlines/soft-break.js +18 -0
  150. package/dist/parser/inlines/strikethrough.d.ts +16 -0
  151. package/dist/parser/inlines/strikethrough.js +48 -0
  152. package/dist/parser/inlines/text.d.ts +6 -0
  153. package/dist/parser/inlines/text.js +20 -0
  154. package/dist/parser/renderer.d.ts +33 -0
  155. package/dist/parser/renderer.js +345 -0
  156. package/dist/parser/types.d.ts +152 -0
  157. package/dist/parser/types.js +5 -0
  158. package/dist/parser/utils.d.ts +32 -0
  159. package/dist/parser/utils.js +277 -0
  160. package/dist/types/cli.d.ts +6 -0
  161. package/dist/types/gfm.d.ts +6 -0
  162. package/dist/types/index.d.ts +5 -0
  163. package/dist/types/parser/block-parser.d.ts +25 -0
  164. package/dist/types/parser/blocks/blockquote.d.ts +8 -0
  165. package/dist/types/parser/blocks/code-block-fenced.d.ts +14 -0
  166. package/dist/types/parser/blocks/code-block-indented.d.ts +7 -0
  167. package/dist/types/parser/blocks/heading-atx.d.ts +10 -0
  168. package/dist/types/parser/blocks/heading-setext.d.ts +8 -0
  169. package/dist/types/parser/blocks/html-block.d.ts +9 -0
  170. package/dist/types/parser/blocks/link-reference.d.ts +11 -0
  171. package/dist/types/parser/blocks/list.d.ts +25 -0
  172. package/dist/types/parser/blocks/paragraph.d.ts +7 -0
  173. package/dist/types/parser/blocks/table.d.ts +13 -0
  174. package/dist/types/parser/blocks/task-list.d.ts +10 -0
  175. package/dist/types/parser/blocks/thematic-break.d.ts +6 -0
  176. package/dist/types/parser/entities.d.ts +4 -0
  177. package/dist/types/parser/gfm/block-parser.d.ts +32 -0
  178. package/dist/types/parser/gfm/index.d.ts +31 -0
  179. package/dist/types/parser/gfm/inline-parser.d.ts +25 -0
  180. package/dist/types/parser/gfm/renderer.d.ts +43 -0
  181. package/dist/types/parser/index.d.ts +33 -0
  182. package/dist/types/parser/inline-parser.d.ts +16 -0
  183. package/dist/types/parser/inlines/autolink-extended.d.ts +24 -0
  184. package/dist/types/parser/inlines/autolink.d.ts +9 -0
  185. package/dist/types/parser/inlines/code-span.d.ts +9 -0
  186. package/dist/types/parser/inlines/emphasis.d.ts +14 -0
  187. package/dist/types/parser/inlines/entity.d.ts +8 -0
  188. package/dist/types/parser/inlines/escape.d.ts +8 -0
  189. package/dist/types/parser/inlines/footnote.d.ts +9 -0
  190. package/dist/types/parser/inlines/hard-break.d.ts +9 -0
  191. package/dist/types/parser/inlines/image.d.ts +4 -0
  192. package/dist/types/parser/inlines/link.d.ts +33 -0
  193. package/dist/types/parser/inlines/soft-break.d.ts +9 -0
  194. package/dist/types/parser/inlines/strikethrough.d.ts +16 -0
  195. package/dist/types/parser/inlines/text.d.ts +6 -0
  196. package/dist/types/parser/renderer.d.ts +33 -0
  197. package/dist/types/parser/types.d.ts +152 -0
  198. package/dist/types/parser/utils.d.ts +32 -0
  199. 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;