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,773 @@
1
+ "use strict";
2
+ /**
3
+ * GFM block parser that extends CommonMark with table support.
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.GFMBlockParser = void 0;
7
+ const utils_js_1 = require("../utils.js");
8
+ const thematic_break_js_1 = require("../blocks/thematic-break.js");
9
+ const heading_atx_js_1 = require("../blocks/heading-atx.js");
10
+ const heading_setext_js_1 = require("../blocks/heading-setext.js");
11
+ const code_block_indented_js_1 = require("../blocks/code-block-indented.js");
12
+ const code_block_fenced_js_1 = require("../blocks/code-block-fenced.js");
13
+ const blockquote_js_1 = require("../blocks/blockquote.js");
14
+ const list_js_1 = require("../blocks/list.js");
15
+ const html_block_js_1 = require("../blocks/html-block.js");
16
+ const html_block_js_2 = require("../blocks/html-block.js");
17
+ const paragraph_js_1 = require("../blocks/paragraph.js");
18
+ const link_reference_js_1 = require("../blocks/link-reference.js");
19
+ const table_js_1 = require("../blocks/table.js");
20
+ const task_list_js_1 = require("../blocks/task-list.js");
21
+ const footnote_js_1 = require("../inlines/footnote.js");
22
+ function getIndent(raw) {
23
+ let indent = 0;
24
+ for (const char of raw) {
25
+ if (char === ' ')
26
+ indent++;
27
+ else if (char === '\t')
28
+ indent += 4 - (indent % 4);
29
+ else
30
+ break;
31
+ }
32
+ return indent;
33
+ }
34
+ class GFMBlockParser {
35
+ lines = [];
36
+ lineIndex = 0;
37
+ linkReferences = new Map();
38
+ footnoteDefinitions = new Map();
39
+ static LAZY_PREFIX = '\u0000';
40
+ enableTables;
41
+ enableTaskLists;
42
+ enableFootnotes;
43
+ constructor(extensions) {
44
+ this.enableTables = !extensions || extensions.has('table');
45
+ this.enableTaskLists = !extensions || extensions.has('tasklist');
46
+ this.enableFootnotes = !extensions || extensions.has('footnotes');
47
+ }
48
+ parse(input) {
49
+ this.lines = (0, utils_js_1.normalizeLineEndings)(input).split('\n');
50
+ this.lineIndex = 0;
51
+ this.linkReferences = new Map();
52
+ this.footnoteDefinitions = new Map();
53
+ const document = {
54
+ type: 'document',
55
+ children: [],
56
+ };
57
+ this.parseBlocks(document, 0);
58
+ if (this.enableTaskLists) {
59
+ (0, task_list_js_1.processTaskLists)(document.children);
60
+ }
61
+ return {
62
+ document,
63
+ linkReferences: this.linkReferences,
64
+ footnoteDefinitions: this.footnoteDefinitions,
65
+ };
66
+ }
67
+ currentLine() {
68
+ return this.lines[this.lineIndex];
69
+ }
70
+ advance() {
71
+ this.lineIndex++;
72
+ }
73
+ parseBlocks(container, baseIndent) {
74
+ let paragraphLines = [];
75
+ const finishParagraph = () => {
76
+ if (paragraphLines.length > 0) {
77
+ let text = paragraphLines.join('\n');
78
+ let consumed = 0;
79
+ while (text.length > 0) {
80
+ const result = (0, link_reference_js_1.parseLinkReferenceDefinition)(text);
81
+ if (!result)
82
+ break;
83
+ if (!this.linkReferences.has(result.label)) {
84
+ this.linkReferences.set(result.label, result.definition);
85
+ }
86
+ text = text.slice(result.consumed);
87
+ consumed += result.consumed;
88
+ }
89
+ text = text.trim();
90
+ if (text.length > 0) {
91
+ const para = (0, paragraph_js_1.createParagraphNode)();
92
+ para.rawContent = text;
93
+ container.children.push(para);
94
+ }
95
+ paragraphLines = [];
96
+ return consumed > 0 || text.length > 0;
97
+ }
98
+ return false;
99
+ };
100
+ while (this.lineIndex < this.lines.length) {
101
+ const line = this.currentLine();
102
+ if (line.startsWith(GFMBlockParser.LAZY_PREFIX)) {
103
+ const raw = line.slice(GFMBlockParser.LAZY_PREFIX.length);
104
+ const rawIndent = getIndent(raw);
105
+ paragraphLines.push((0, utils_js_1.removeIndent)(raw, Math.min(rawIndent, 3)));
106
+ this.advance();
107
+ continue;
108
+ }
109
+ const lineIndent = getIndent(line);
110
+ if (line.trim() === '') {
111
+ finishParagraph();
112
+ this.advance();
113
+ continue;
114
+ }
115
+ const dedented = (0, utils_js_1.removeIndent)(line, Math.min(3, lineIndent));
116
+ const trimmed = line.replace(/^ {0,3}/, '');
117
+ if (this.enableFootnotes) {
118
+ const footnoteLabel = (0, footnote_js_1.parseFootnoteLabel)(trimmed, 0);
119
+ if (footnoteLabel && trimmed[footnoteLabel.length] === ':') {
120
+ finishParagraph();
121
+ this.parseFootnoteDefinition(footnoteLabel.label, footnoteLabel.normalized, trimmed, footnoteLabel.length);
122
+ continue;
123
+ }
124
+ }
125
+ if (this.enableTables && paragraphLines.length >= 1) {
126
+ const lastLine = paragraphLines[paragraphLines.length - 1];
127
+ if ((0, table_js_1.isTableRow)(lastLine)) {
128
+ const alignments = (0, table_js_1.isTableStart)(lastLine, line);
129
+ if (alignments) {
130
+ if (paragraphLines.length > 1) {
131
+ const precedingLines = paragraphLines.slice(0, -1);
132
+ let text = precedingLines.join('\n');
133
+ while (text.length > 0) {
134
+ const result = (0, link_reference_js_1.parseLinkReferenceDefinition)(text);
135
+ if (!result)
136
+ break;
137
+ if (!this.linkReferences.has(result.label)) {
138
+ this.linkReferences.set(result.label, result.definition);
139
+ }
140
+ text = text.slice(result.consumed);
141
+ }
142
+ text = text.trim();
143
+ if (text.length > 0) {
144
+ const para = (0, paragraph_js_1.createParagraphNode)();
145
+ para.rawContent = text;
146
+ container.children.push(para);
147
+ }
148
+ }
149
+ const table = this.parseTable(lastLine, alignments);
150
+ paragraphLines = [];
151
+ container.children.push(table);
152
+ continue;
153
+ }
154
+ }
155
+ }
156
+ if (paragraphLines.length > 0) {
157
+ const setextLevel = (0, heading_setext_js_1.getSetextLevel)(line);
158
+ if (setextLevel) {
159
+ let content = paragraphLines.join('\n');
160
+ while (content.length > 0) {
161
+ const result = (0, link_reference_js_1.parseLinkReferenceDefinition)(content);
162
+ if (!result)
163
+ break;
164
+ if (!this.linkReferences.has(result.label)) {
165
+ this.linkReferences.set(result.label, result.definition);
166
+ }
167
+ content = content.slice(result.consumed);
168
+ }
169
+ content = content.trim();
170
+ paragraphLines = [];
171
+ if (content.length > 0) {
172
+ const heading = (0, heading_atx_js_1.createHeadingNode)(setextLevel);
173
+ heading.rawContent = content;
174
+ container.children.push(heading);
175
+ }
176
+ else if ((0, thematic_break_js_1.isThematicBreak)(line)) {
177
+ container.children.push((0, thematic_break_js_1.parseThematicBreak)(line));
178
+ }
179
+ else {
180
+ paragraphLines.push(line);
181
+ }
182
+ this.advance();
183
+ continue;
184
+ }
185
+ }
186
+ if ((0, thematic_break_js_1.isThematicBreak)(line)) {
187
+ finishParagraph();
188
+ container.children.push((0, thematic_break_js_1.parseThematicBreak)(line));
189
+ this.advance();
190
+ continue;
191
+ }
192
+ const atxResult = (0, heading_atx_js_1.parseAtxHeading)(line);
193
+ if (atxResult) {
194
+ finishParagraph();
195
+ const heading = (0, heading_atx_js_1.createHeadingNode)(atxResult.level);
196
+ heading.rawContent = atxResult.content;
197
+ container.children.push(heading);
198
+ this.advance();
199
+ continue;
200
+ }
201
+ if (paragraphLines.length > 0 && /^ {0,3}=+[ \t]*$/.test(line)) {
202
+ let content = paragraphLines.join('\n');
203
+ while (content.length > 0) {
204
+ const result = (0, link_reference_js_1.parseLinkReferenceDefinition)(content);
205
+ if (!result)
206
+ break;
207
+ if (!this.linkReferences.has(result.label)) {
208
+ this.linkReferences.set(result.label, result.definition);
209
+ }
210
+ content = content.slice(result.consumed);
211
+ }
212
+ content = content.trim();
213
+ paragraphLines = [];
214
+ if (content.length > 0) {
215
+ const heading = (0, heading_atx_js_1.createHeadingNode)(1);
216
+ heading.rawContent = content;
217
+ container.children.push(heading);
218
+ }
219
+ else {
220
+ paragraphLines.push(line);
221
+ }
222
+ this.advance();
223
+ continue;
224
+ }
225
+ const fenceInfo = (0, code_block_fenced_js_1.parseFenceOpen)(line);
226
+ if (fenceInfo) {
227
+ finishParagraph();
228
+ const codeLines = [];
229
+ this.advance();
230
+ let closedProperly = false;
231
+ while (this.lineIndex < this.lines.length) {
232
+ const codeLine = this.currentLine();
233
+ if ((0, code_block_fenced_js_1.isFenceClose)(codeLine, fenceInfo)) {
234
+ this.advance();
235
+ closedProperly = true;
236
+ break;
237
+ }
238
+ codeLines.push(codeLine);
239
+ this.advance();
240
+ }
241
+ if (!closedProperly) {
242
+ while (codeLines.length > 0 && codeLines[codeLines.length - 1] === '') {
243
+ codeLines.pop();
244
+ }
245
+ }
246
+ const info = (0, utils_js_1.unescapeString)((0, utils_js_1.decodeHtmlEntities)(fenceInfo.info));
247
+ container.children.push((0, code_block_fenced_js_1.createFencedCodeBlock)(info, codeLines, fenceInfo.indent));
248
+ continue;
249
+ }
250
+ if (paragraphLines.length === 0 && lineIndent >= 4) {
251
+ const codeLines = [(0, utils_js_1.removeIndent)(line, 4)];
252
+ this.advance();
253
+ while (this.lineIndex < this.lines.length) {
254
+ const codeLine = this.currentLine();
255
+ const codeIndent = getIndent(codeLine);
256
+ if (codeIndent >= 4) {
257
+ codeLines.push((0, utils_js_1.removeIndent)(codeLine, 4));
258
+ this.advance();
259
+ }
260
+ else if (codeLine.trim() === '') {
261
+ codeLines.push('');
262
+ this.advance();
263
+ }
264
+ else {
265
+ break;
266
+ }
267
+ }
268
+ while (codeLines.length > 0 && codeLines[codeLines.length - 1] === '') {
269
+ codeLines.pop();
270
+ }
271
+ container.children.push((0, code_block_indented_js_1.createIndentedCodeBlock)(codeLines));
272
+ continue;
273
+ }
274
+ const bqMatch = line.match(/^ {0,3}>([ \t]?)/);
275
+ if (bqMatch) {
276
+ finishParagraph();
277
+ const blockquote = (0, blockquote_js_1.createBlockquoteNode)();
278
+ const quoteLines = [];
279
+ let inFence = null;
280
+ while (this.lineIndex < this.lines.length) {
281
+ const quoteLine = this.currentLine();
282
+ const qMatch = quoteLine.match(/^ {0,3}>([ \t]?)/);
283
+ if (qMatch) {
284
+ const content = this.extractBlockquoteContent(quoteLine, qMatch);
285
+ quoteLines.push(content);
286
+ if (inFence) {
287
+ if ((0, code_block_fenced_js_1.isFenceClose)(content, inFence)) {
288
+ inFence = null;
289
+ }
290
+ }
291
+ else {
292
+ const fenceInfo = (0, code_block_fenced_js_1.parseFenceOpen)(content);
293
+ if (fenceInfo) {
294
+ inFence = fenceInfo;
295
+ }
296
+ }
297
+ this.advance();
298
+ }
299
+ else if (quoteLine.trim() === '') {
300
+ break;
301
+ }
302
+ else if (quoteLines.length > 0) {
303
+ if (inFence) {
304
+ break;
305
+ }
306
+ if ((0, thematic_break_js_1.isThematicBreak)(quoteLine) || (0, code_block_fenced_js_1.parseFenceOpen)(quoteLine)) {
307
+ break;
308
+ }
309
+ if (quoteLines[quoteLines.length - 1].trim() === '') {
310
+ break;
311
+ }
312
+ const lastContent = this.lastNonBlankLine(quoteLines);
313
+ if (lastContent &&
314
+ !(0, code_block_fenced_js_1.parseFenceOpen)(lastContent) &&
315
+ getIndent(lastContent) < 4 &&
316
+ this.allowsLazyAfterListMarker(lastContent)) {
317
+ const nestedPrefix = this.getBlockquotePrefix(lastContent);
318
+ quoteLines.push(nestedPrefix + GFMBlockParser.LAZY_PREFIX + quoteLine);
319
+ this.advance();
320
+ }
321
+ else {
322
+ break;
323
+ }
324
+ }
325
+ else {
326
+ break;
327
+ }
328
+ }
329
+ const subParser = new GFMBlockParser();
330
+ const result = subParser.parse(quoteLines.join('\n'));
331
+ blockquote.children = result.document.children;
332
+ result.linkReferences.forEach((def, label) => {
333
+ if (!this.linkReferences.has(label)) {
334
+ this.linkReferences.set(label, def);
335
+ }
336
+ });
337
+ result.footnoteDefinitions.forEach((def, label) => {
338
+ if (!this.footnoteDefinitions.has(label)) {
339
+ this.footnoteDefinitions.set(label, def);
340
+ }
341
+ });
342
+ container.children.push(blockquote);
343
+ continue;
344
+ }
345
+ const listMarker = (0, list_js_1.parseListMarker)(line);
346
+ if (listMarker) {
347
+ const canInterrupt = paragraphLines.length === 0 ||
348
+ (listMarker.type === 'bullet' || listMarker.start === 1) &&
349
+ line.slice(listMarker.indent + listMarker.marker.length + listMarker.padding).trim() !== '';
350
+ if (canInterrupt) {
351
+ finishParagraph();
352
+ const list = (0, list_js_1.createListNode)(listMarker);
353
+ this.parseList(list, listMarker);
354
+ container.children.push(list);
355
+ continue;
356
+ }
357
+ }
358
+ const htmlType = (0, html_block_js_1.getHtmlBlockType)(line, paragraphLines.length === 0);
359
+ if (htmlType !== null) {
360
+ finishParagraph();
361
+ const htmlLines = [line];
362
+ const closeOnSameLine = htmlType >= 1 && htmlType <= 5 && (0, html_block_js_1.isHtmlBlockClose)(line, htmlType);
363
+ this.advance();
364
+ if (!closeOnSameLine) {
365
+ while (this.lineIndex < this.lines.length) {
366
+ const htmlLine = this.currentLine();
367
+ if (htmlType === 6 || htmlType === 7) {
368
+ if (htmlLine.trim() === '') {
369
+ break;
370
+ }
371
+ htmlLines.push(htmlLine);
372
+ this.advance();
373
+ }
374
+ else {
375
+ htmlLines.push(htmlLine);
376
+ this.advance();
377
+ if ((0, html_block_js_1.isHtmlBlockClose)(htmlLine, htmlType)) {
378
+ break;
379
+ }
380
+ }
381
+ }
382
+ }
383
+ container.children.push((0, html_block_js_2.createHtmlBlockNode)(htmlLines.join('\n') + '\n'));
384
+ continue;
385
+ }
386
+ paragraphLines.push((0, utils_js_1.removeIndent)(line, Math.min(lineIndent, 3)));
387
+ this.advance();
388
+ }
389
+ finishParagraph();
390
+ }
391
+ parseTable(headerLine, alignments) {
392
+ const table = (0, table_js_1.createTableNode)(alignments);
393
+ const headerCells = (0, table_js_1.parseTableRowCells)(headerLine, alignments, true);
394
+ const headerRow = (0, table_js_1.createTableRowNode)(true, headerCells);
395
+ table.children.push(headerRow);
396
+ this.advance();
397
+ while (this.lineIndex < this.lines.length) {
398
+ const line = this.currentLine();
399
+ if (line.trim() === '') {
400
+ break;
401
+ }
402
+ if (line.match(/^ {0,3}>/)) {
403
+ break;
404
+ }
405
+ if ((0, thematic_break_js_1.isThematicBreak)(line)) {
406
+ break;
407
+ }
408
+ if ((0, heading_atx_js_1.parseAtxHeading)(line)) {
409
+ break;
410
+ }
411
+ if ((0, code_block_fenced_js_1.parseFenceOpen)(line)) {
412
+ break;
413
+ }
414
+ if ((0, html_block_js_1.getHtmlBlockType)(line, true) !== null) {
415
+ break;
416
+ }
417
+ const cells = (0, table_js_1.parseTableRowCells)(line, alignments, false);
418
+ const row = (0, table_js_1.createTableRowNode)(false, cells);
419
+ table.children.push(row);
420
+ this.advance();
421
+ }
422
+ return table;
423
+ }
424
+ parseFootnoteDefinition(label, key, trimmedLine, labelLength) {
425
+ if (!key) {
426
+ this.advance();
427
+ return;
428
+ }
429
+ const contentLines = [];
430
+ const afterColon = trimmedLine.slice(labelLength + 1);
431
+ const firstContent = afterColon.replace(/^[ \t]+/, '');
432
+ if (firstContent.length > 0) {
433
+ contentLines.push(firstContent);
434
+ }
435
+ this.advance();
436
+ while (this.lineIndex < this.lines.length) {
437
+ const line = this.currentLine();
438
+ if (line.trim() === '') {
439
+ let lookahead = this.lineIndex + 1;
440
+ while (lookahead < this.lines.length && this.lines[lookahead].trim() === '') {
441
+ lookahead++;
442
+ }
443
+ if (lookahead < this.lines.length && getIndent(this.lines[lookahead]) >= 4) {
444
+ contentLines.push('');
445
+ this.advance();
446
+ continue;
447
+ }
448
+ break;
449
+ }
450
+ if (getIndent(line) >= 4) {
451
+ contentLines.push((0, utils_js_1.removeIndent)(line, 4));
452
+ this.advance();
453
+ continue;
454
+ }
455
+ break;
456
+ }
457
+ const subParser = new GFMBlockParser();
458
+ const result = subParser.parse(contentLines.join('\n'));
459
+ result.linkReferences.forEach((def, refLabel) => {
460
+ if (!this.linkReferences.has(refLabel)) {
461
+ this.linkReferences.set(refLabel, def);
462
+ }
463
+ });
464
+ if (!this.footnoteDefinitions.has(key)) {
465
+ this.footnoteDefinitions.set(key, { label, blocks: result.document.children });
466
+ }
467
+ }
468
+ parseList(list, firstMarker) {
469
+ let sawBlankLine = false;
470
+ let sawBlankLineInItem = false;
471
+ let sawBlankBetweenItems = false;
472
+ while (this.lineIndex < this.lines.length) {
473
+ const line = this.currentLine();
474
+ if ((0, thematic_break_js_1.isThematicBreak)(line)) {
475
+ break;
476
+ }
477
+ const marker = (0, list_js_1.parseListMarker)(line);
478
+ if (marker && (0, list_js_1.listsMatch)(firstMarker, marker)) {
479
+ if (sawBlankLine) {
480
+ sawBlankBetweenItems = true;
481
+ }
482
+ sawBlankLine = false;
483
+ const item = (0, list_js_1.createListItemNode)();
484
+ const itemResult = this.parseListItem(item, marker);
485
+ if (itemResult.blankInItem) {
486
+ sawBlankLineInItem = true;
487
+ }
488
+ if (itemResult.endsWithBlank) {
489
+ sawBlankLine = true;
490
+ }
491
+ list.children.push(item);
492
+ }
493
+ else if (line.trim() === '') {
494
+ sawBlankLine = true;
495
+ this.advance();
496
+ let nextIdx = this.lineIndex;
497
+ while (nextIdx < this.lines.length && this.lines[nextIdx].trim() === '') {
498
+ nextIdx++;
499
+ }
500
+ if (nextIdx < this.lines.length) {
501
+ const nextLine = this.lines[nextIdx];
502
+ const nextMarker = (0, list_js_1.parseListMarker)(nextLine);
503
+ if (!nextMarker || !(0, list_js_1.listsMatch)(firstMarker, nextMarker)) {
504
+ if (getIndent(nextLine) < firstMarker.contentIndent) {
505
+ break;
506
+ }
507
+ }
508
+ }
509
+ else {
510
+ break;
511
+ }
512
+ }
513
+ else {
514
+ break;
515
+ }
516
+ }
517
+ if (sawBlankLineInItem || sawBlankBetweenItems) {
518
+ list.tight = false;
519
+ }
520
+ }
521
+ parseListItem(item, marker) {
522
+ const itemLines = [];
523
+ let firstLine = this.currentLine();
524
+ if (firstLine.startsWith(GFMBlockParser.LAZY_PREFIX)) {
525
+ firstLine = firstLine.slice(GFMBlockParser.LAZY_PREFIX.length);
526
+ }
527
+ let firstContent = this.extractListItemContent(firstLine, marker);
528
+ if (firstContent.trim() === '' && firstLine.slice(marker.indentChars + marker.marker.length).match(/^[ \t]/)) {
529
+ itemLines.push('');
530
+ }
531
+ else {
532
+ itemLines.push(firstContent);
533
+ }
534
+ this.advance();
535
+ let sawBlankLine = false;
536
+ let blankLineInItem = false;
537
+ let trailingBlanks = 0;
538
+ const contentIndent = marker.contentIndent;
539
+ const baseThreshold = contentIndent + 1;
540
+ let lastNonBlankIndent = firstContent.trim() === '' ? null : contentIndent;
541
+ let lastNonBlankWasListMarker = false;
542
+ let sawNonBlankContent = firstContent.trim() !== '';
543
+ let inFence = null;
544
+ if (firstContent.trim() !== '') {
545
+ const firstFence = (0, code_block_fenced_js_1.parseFenceOpen)(firstContent);
546
+ if (firstFence) {
547
+ inFence = firstFence;
548
+ }
549
+ lastNonBlankWasListMarker = (0, list_js_1.parseListMarker)(firstContent) !== null;
550
+ }
551
+ while (this.lineIndex < this.lines.length) {
552
+ let line = this.currentLine();
553
+ let lazyLine = false;
554
+ if (line.startsWith(GFMBlockParser.LAZY_PREFIX)) {
555
+ line = line.slice(GFMBlockParser.LAZY_PREFIX.length);
556
+ lazyLine = true;
557
+ }
558
+ const lineIndent = getIndent(line);
559
+ if (inFence) {
560
+ const contentLine = lineIndent >= contentIndent ? (0, utils_js_1.removeIndent)(line, contentIndent) : line;
561
+ itemLines.push(contentLine);
562
+ if ((0, code_block_fenced_js_1.isFenceClose)(contentLine, inFence)) {
563
+ inFence = null;
564
+ }
565
+ this.advance();
566
+ continue;
567
+ }
568
+ if (line.trim() === '') {
569
+ sawBlankLine = true;
570
+ trailingBlanks++;
571
+ itemLines.push('');
572
+ this.advance();
573
+ continue;
574
+ }
575
+ const newMarker = lazyLine ? null : (0, list_js_1.parseListMarker)(line);
576
+ if (newMarker && (0, list_js_1.listsMatch)(marker, newMarker) && newMarker.indent < contentIndent) {
577
+ break;
578
+ }
579
+ if (!sawNonBlankContent && sawBlankLine && lineIndent >= contentIndent) {
580
+ break;
581
+ }
582
+ if (lineIndent >= contentIndent) {
583
+ const contentLine = (0, utils_js_1.removeIndent)(line, contentIndent);
584
+ const isListMarker = (0, list_js_1.parseListMarker)(contentLine) !== null;
585
+ if (sawBlankLine && (lineIndent <= baseThreshold ||
586
+ (lastNonBlankIndent !== null && lastNonBlankIndent <= baseThreshold && !lastNonBlankWasListMarker))) {
587
+ blankLineInItem = true;
588
+ }
589
+ sawBlankLine = false;
590
+ trailingBlanks = 0;
591
+ itemLines.push(contentLine);
592
+ lastNonBlankIndent = lineIndent;
593
+ lastNonBlankWasListMarker = isListMarker;
594
+ sawNonBlankContent = true;
595
+ if (!inFence) {
596
+ const fenceInfo = (0, code_block_fenced_js_1.parseFenceOpen)(contentLine);
597
+ if (fenceInfo) {
598
+ inFence = fenceInfo;
599
+ }
600
+ }
601
+ this.advance();
602
+ }
603
+ else if (sawBlankLine) {
604
+ break;
605
+ }
606
+ else {
607
+ if (!(0, thematic_break_js_1.isThematicBreak)(line) && !(0, heading_atx_js_1.parseAtxHeading)(line) &&
608
+ !(0, code_block_fenced_js_1.parseFenceOpen)(line) && !line.match(/^ {0,3}>/) &&
609
+ !newMarker) {
610
+ trailingBlanks = 0;
611
+ itemLines.push(line);
612
+ lastNonBlankIndent = lineIndent;
613
+ lastNonBlankWasListMarker = false;
614
+ sawNonBlankContent = true;
615
+ this.advance();
616
+ }
617
+ else {
618
+ break;
619
+ }
620
+ }
621
+ }
622
+ const endsWithBlank = trailingBlanks > 0;
623
+ while (itemLines.length > 0 && itemLines[itemLines.length - 1] === '') {
624
+ itemLines.pop();
625
+ }
626
+ const subParser = new GFMBlockParser();
627
+ const result = subParser.parse(itemLines.join('\n'));
628
+ item.children = result.document.children;
629
+ result.linkReferences.forEach((def, label) => {
630
+ if (!this.linkReferences.has(label)) {
631
+ this.linkReferences.set(label, def);
632
+ }
633
+ });
634
+ result.footnoteDefinitions.forEach((def, label) => {
635
+ if (!this.footnoteDefinitions.has(label)) {
636
+ this.footnoteDefinitions.set(label, def);
637
+ }
638
+ });
639
+ const paragraphCount = item.children.filter((child) => child.type === 'paragraph').length;
640
+ if (paragraphCount >= 2) {
641
+ blankLineInItem = true;
642
+ }
643
+ return { blankInItem: blankLineInItem, endsWithBlank };
644
+ }
645
+ lastNonBlankLine(lines) {
646
+ for (let i = lines.length - 1; i >= 0; i--) {
647
+ const line = lines[i];
648
+ if (line.trim() !== '') {
649
+ return line;
650
+ }
651
+ }
652
+ return null;
653
+ }
654
+ getBlockquotePrefix(line) {
655
+ let i = 0;
656
+ let prefix = '';
657
+ while (i < line.length) {
658
+ let spaces = 0;
659
+ const start = i;
660
+ while (i < line.length && line[i] === ' ' && spaces < 3) {
661
+ spaces++;
662
+ i++;
663
+ }
664
+ if (i < line.length && line[i] === '>') {
665
+ i++;
666
+ if (i < line.length && (line[i] === ' ' || line[i] === '\t')) {
667
+ i++;
668
+ }
669
+ prefix += line.slice(start, i);
670
+ }
671
+ else {
672
+ break;
673
+ }
674
+ }
675
+ return prefix;
676
+ }
677
+ allowsLazyAfterListMarker(line) {
678
+ const marker = (0, list_js_1.parseListMarker)(line);
679
+ if (!marker) {
680
+ return true;
681
+ }
682
+ const afterMarker = line.slice(marker.contentCharIndex);
683
+ return /^[ \t]*>/.test(afterMarker);
684
+ }
685
+ extractBlockquoteContent(line, match) {
686
+ const optionalSpace = match[1];
687
+ let column = 0;
688
+ let charIndex = 0;
689
+ while (charIndex < line.length && line[charIndex] === ' ' && charIndex < 3) {
690
+ column++;
691
+ charIndex++;
692
+ }
693
+ charIndex++;
694
+ column++;
695
+ let result = '';
696
+ if (optionalSpace === '\t') {
697
+ const tabWidth = 4 - (column % 4);
698
+ const remaining = tabWidth - 1;
699
+ if (remaining > 0) {
700
+ result = ' '.repeat(remaining);
701
+ }
702
+ charIndex++;
703
+ column += tabWidth;
704
+ }
705
+ else if (optionalSpace === ' ') {
706
+ charIndex++;
707
+ column++;
708
+ }
709
+ while (charIndex < line.length) {
710
+ const char = line[charIndex];
711
+ if (char === '\t') {
712
+ const tabWidth = 4 - (column % 4);
713
+ result += ' '.repeat(tabWidth);
714
+ column += tabWidth;
715
+ }
716
+ else {
717
+ result += char;
718
+ column++;
719
+ }
720
+ charIndex++;
721
+ }
722
+ return result;
723
+ }
724
+ extractListItemContent(line, marker) {
725
+ let column = 0;
726
+ let charIndex = 0;
727
+ let result = '';
728
+ while (charIndex < line.length && charIndex < marker.indentChars) {
729
+ if (line[charIndex] === '\t') {
730
+ column = column + (4 - (column % 4));
731
+ }
732
+ else {
733
+ column++;
734
+ }
735
+ charIndex++;
736
+ }
737
+ charIndex += marker.marker.length;
738
+ column += marker.marker.length;
739
+ if (charIndex < line.length && marker.paddingChars > 0) {
740
+ const paddingChar = line[charIndex];
741
+ if (paddingChar === '\t') {
742
+ const tabWidth = 4 - (column % 4);
743
+ const consumed = Math.min(tabWidth, marker.padding);
744
+ column += consumed;
745
+ charIndex += marker.paddingChars;
746
+ const remaining = tabWidth - consumed;
747
+ if (remaining > 0) {
748
+ result += ' '.repeat(remaining);
749
+ column += remaining;
750
+ }
751
+ }
752
+ else {
753
+ column += marker.padding;
754
+ charIndex += marker.paddingChars;
755
+ }
756
+ }
757
+ while (charIndex < line.length) {
758
+ const char = line[charIndex];
759
+ if (char === '\t') {
760
+ const tabWidth = 4 - (column % 4);
761
+ result += ' '.repeat(tabWidth);
762
+ column += tabWidth;
763
+ }
764
+ else {
765
+ result += char;
766
+ column++;
767
+ }
768
+ charIndex++;
769
+ }
770
+ return result;
771
+ }
772
+ }
773
+ exports.GFMBlockParser = GFMBlockParser;