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,644 @@
1
+ "use strict";
2
+ /**
3
+ * Block parsing phase that builds block nodes like paragraphs, headings, lists, code blocks,
4
+ * blockquotes, and HTML blocks while collecting link reference definitions.
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.BlockParser = void 0;
8
+ const utils_js_1 = require("./utils.js");
9
+ const thematic_break_js_1 = require("./blocks/thematic-break.js");
10
+ const heading_atx_js_1 = require("./blocks/heading-atx.js");
11
+ const heading_setext_js_1 = require("./blocks/heading-setext.js");
12
+ const code_block_indented_js_1 = require("./blocks/code-block-indented.js");
13
+ const code_block_fenced_js_1 = require("./blocks/code-block-fenced.js");
14
+ const blockquote_js_1 = require("./blocks/blockquote.js");
15
+ const list_js_1 = require("./blocks/list.js");
16
+ const html_block_js_1 = 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
+ function parseLine(raw) {
20
+ let indent = 0;
21
+ let i = 0;
22
+ while (i < raw.length) {
23
+ if (raw[i] === ' ') {
24
+ indent++;
25
+ i++;
26
+ }
27
+ else if (raw[i] === '\t') {
28
+ indent += 4 - (indent % 4);
29
+ i++;
30
+ }
31
+ else {
32
+ break;
33
+ }
34
+ }
35
+ return { raw, content: raw.slice(i), indent };
36
+ }
37
+ function getIndent(raw) {
38
+ let indent = 0;
39
+ for (const char of raw) {
40
+ if (char === ' ')
41
+ indent++;
42
+ else if (char === '\t')
43
+ indent += 4 - (indent % 4);
44
+ else
45
+ break;
46
+ }
47
+ return indent;
48
+ }
49
+ class BlockParser {
50
+ lines = [];
51
+ lineIndex = 0;
52
+ linkReferences = new Map();
53
+ static LAZY_PREFIX = '\u0000';
54
+ parse(input) {
55
+ this.lines = (0, utils_js_1.normalizeLineEndings)(input).split('\n');
56
+ this.lineIndex = 0;
57
+ this.linkReferences = new Map();
58
+ const document = {
59
+ type: 'document',
60
+ children: [],
61
+ };
62
+ this.parseBlocks(document, 0);
63
+ return { document, linkReferences: this.linkReferences };
64
+ }
65
+ currentLine() {
66
+ return this.lines[this.lineIndex];
67
+ }
68
+ advance() {
69
+ this.lineIndex++;
70
+ }
71
+ parseBlocks(container, baseIndent) {
72
+ let paragraphLines = [];
73
+ let paragraphCanBeInterrupted = true;
74
+ const finishParagraph = () => {
75
+ if (paragraphLines.length > 0) {
76
+ let text = paragraphLines.join('\n');
77
+ let consumed = 0;
78
+ while (text.length > 0) {
79
+ const result = (0, link_reference_js_1.parseLinkReferenceDefinition)(text);
80
+ if (!result)
81
+ break;
82
+ if (!this.linkReferences.has(result.label)) {
83
+ this.linkReferences.set(result.label, result.definition);
84
+ }
85
+ text = text.slice(result.consumed);
86
+ consumed += result.consumed;
87
+ }
88
+ text = text.trim();
89
+ if (text.length > 0) {
90
+ const para = (0, paragraph_js_1.createParagraphNode)();
91
+ para.rawContent = text;
92
+ container.children.push(para);
93
+ }
94
+ paragraphLines = [];
95
+ paragraphCanBeInterrupted = true;
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(BlockParser.LAZY_PREFIX)) {
103
+ const raw = line.slice(BlockParser.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 dedentedIndent = getIndent(dedented);
117
+ if ((0, thematic_break_js_1.isThematicBreak)(line)) {
118
+ if (paragraphLines.length > 0 && /^ {0,3}-+[ \t]*$/.test(line)) {
119
+ const level = (0, heading_setext_js_1.getSetextLevel)(line);
120
+ let content = paragraphLines.join('\n');
121
+ while (content.length > 0) {
122
+ const result = (0, link_reference_js_1.parseLinkReferenceDefinition)(content);
123
+ if (!result)
124
+ break;
125
+ if (!this.linkReferences.has(result.label)) {
126
+ this.linkReferences.set(result.label, result.definition);
127
+ }
128
+ content = content.slice(result.consumed);
129
+ }
130
+ content = content.trim();
131
+ paragraphLines = [];
132
+ if (content.length > 0) {
133
+ const heading = (0, heading_atx_js_1.createHeadingNode)(level);
134
+ heading.rawContent = content;
135
+ container.children.push(heading);
136
+ }
137
+ else {
138
+ container.children.push((0, thematic_break_js_1.parseThematicBreak)(line));
139
+ }
140
+ this.advance();
141
+ continue;
142
+ }
143
+ finishParagraph();
144
+ container.children.push((0, thematic_break_js_1.parseThematicBreak)(line));
145
+ this.advance();
146
+ continue;
147
+ }
148
+ const atxResult = (0, heading_atx_js_1.parseAtxHeading)(line);
149
+ if (atxResult) {
150
+ finishParagraph();
151
+ const heading = (0, heading_atx_js_1.createHeadingNode)(atxResult.level);
152
+ heading.rawContent = atxResult.content;
153
+ container.children.push(heading);
154
+ this.advance();
155
+ continue;
156
+ }
157
+ if (paragraphLines.length > 0 && /^ {0,3}=+[ \t]*$/.test(line)) {
158
+ let content = paragraphLines.join('\n');
159
+ while (content.length > 0) {
160
+ const result = (0, link_reference_js_1.parseLinkReferenceDefinition)(content);
161
+ if (!result)
162
+ break;
163
+ if (!this.linkReferences.has(result.label)) {
164
+ this.linkReferences.set(result.label, result.definition);
165
+ }
166
+ content = content.slice(result.consumed);
167
+ }
168
+ content = content.trim();
169
+ paragraphLines = [];
170
+ if (content.length > 0) {
171
+ const heading = (0, heading_atx_js_1.createHeadingNode)(1);
172
+ heading.rawContent = content;
173
+ container.children.push(heading);
174
+ }
175
+ else {
176
+ paragraphLines.push(line);
177
+ }
178
+ this.advance();
179
+ continue;
180
+ }
181
+ const fenceInfo = (0, code_block_fenced_js_1.parseFenceOpen)(line);
182
+ if (fenceInfo) {
183
+ finishParagraph();
184
+ const codeLines = [];
185
+ this.advance();
186
+ let closedProperly = false;
187
+ while (this.lineIndex < this.lines.length) {
188
+ const codeLine = this.currentLine();
189
+ if ((0, code_block_fenced_js_1.isFenceClose)(codeLine, fenceInfo)) {
190
+ this.advance();
191
+ closedProperly = true;
192
+ break;
193
+ }
194
+ codeLines.push(codeLine);
195
+ this.advance();
196
+ }
197
+ if (!closedProperly) {
198
+ while (codeLines.length > 0 && codeLines[codeLines.length - 1] === '') {
199
+ codeLines.pop();
200
+ }
201
+ }
202
+ const info = (0, utils_js_1.unescapeString)((0, utils_js_1.decodeHtmlEntities)(fenceInfo.info));
203
+ container.children.push((0, code_block_fenced_js_1.createFencedCodeBlock)(info, codeLines, fenceInfo.indent));
204
+ continue;
205
+ }
206
+ if (paragraphLines.length === 0 && lineIndent >= 4) {
207
+ const codeLines = [(0, utils_js_1.removeIndent)(line, 4)];
208
+ this.advance();
209
+ while (this.lineIndex < this.lines.length) {
210
+ const codeLine = this.currentLine();
211
+ const codeIndent = getIndent(codeLine);
212
+ if (codeIndent >= 4) {
213
+ codeLines.push((0, utils_js_1.removeIndent)(codeLine, 4));
214
+ this.advance();
215
+ }
216
+ else if (codeLine.trim() === '') {
217
+ codeLines.push('');
218
+ this.advance();
219
+ }
220
+ else {
221
+ break;
222
+ }
223
+ }
224
+ while (codeLines.length > 0 && codeLines[codeLines.length - 1] === '') {
225
+ codeLines.pop();
226
+ }
227
+ container.children.push((0, code_block_indented_js_1.createIndentedCodeBlock)(codeLines));
228
+ continue;
229
+ }
230
+ const bqMatch = line.match(/^ {0,3}>([ \t]?)/);
231
+ if (bqMatch) {
232
+ finishParagraph();
233
+ const blockquote = (0, blockquote_js_1.createBlockquoteNode)();
234
+ const quoteLines = [];
235
+ let inFence = null;
236
+ while (this.lineIndex < this.lines.length) {
237
+ const quoteLine = this.currentLine();
238
+ const qMatch = quoteLine.match(/^ {0,3}>([ \t]?)/);
239
+ if (qMatch) {
240
+ const content = this.extractBlockquoteContent(quoteLine, qMatch);
241
+ quoteLines.push(content);
242
+ if (inFence) {
243
+ if ((0, code_block_fenced_js_1.isFenceClose)(content, inFence)) {
244
+ inFence = null;
245
+ }
246
+ }
247
+ else {
248
+ const fenceInfo = (0, code_block_fenced_js_1.parseFenceOpen)(content);
249
+ if (fenceInfo) {
250
+ inFence = fenceInfo;
251
+ }
252
+ }
253
+ this.advance();
254
+ }
255
+ else if (quoteLine.trim() === '') {
256
+ break;
257
+ }
258
+ else if (quoteLines.length > 0) {
259
+ if (inFence) {
260
+ break;
261
+ }
262
+ if ((0, thematic_break_js_1.isThematicBreak)(quoteLine) || (0, code_block_fenced_js_1.parseFenceOpen)(quoteLine)) {
263
+ break;
264
+ }
265
+ if (quoteLines[quoteLines.length - 1].trim() === '') {
266
+ break;
267
+ }
268
+ const lastContent = this.lastNonBlankLine(quoteLines);
269
+ if (lastContent &&
270
+ !(0, code_block_fenced_js_1.parseFenceOpen)(lastContent) &&
271
+ getIndent(lastContent) < 4 &&
272
+ this.allowsLazyAfterListMarker(lastContent)) {
273
+ const nestedPrefix = this.getBlockquotePrefix(lastContent);
274
+ quoteLines.push(nestedPrefix + BlockParser.LAZY_PREFIX + quoteLine);
275
+ this.advance();
276
+ }
277
+ else {
278
+ break;
279
+ }
280
+ }
281
+ else {
282
+ break;
283
+ }
284
+ }
285
+ const subParser = new BlockParser();
286
+ const result = subParser.parse(quoteLines.join('\n'));
287
+ blockquote.children = result.document.children;
288
+ result.linkReferences.forEach((def, label) => {
289
+ if (!this.linkReferences.has(label)) {
290
+ this.linkReferences.set(label, def);
291
+ }
292
+ });
293
+ container.children.push(blockquote);
294
+ continue;
295
+ }
296
+ const listMarker = (0, list_js_1.parseListMarker)(line);
297
+ if (listMarker) {
298
+ const canInterrupt = paragraphLines.length === 0 ||
299
+ (listMarker.type === 'bullet' || listMarker.start === 1) &&
300
+ line.slice(listMarker.indent + listMarker.marker.length + listMarker.padding).trim() !== '';
301
+ if (canInterrupt) {
302
+ finishParagraph();
303
+ const list = (0, list_js_1.createListNode)(listMarker);
304
+ this.parseList(list, listMarker);
305
+ container.children.push(list);
306
+ continue;
307
+ }
308
+ }
309
+ const htmlType = (0, html_block_js_1.getHtmlBlockType)(line, paragraphLines.length === 0);
310
+ if (htmlType !== null) {
311
+ finishParagraph();
312
+ const htmlLines = [line];
313
+ const closeOnSameLine = htmlType >= 1 && htmlType <= 5 && (0, html_block_js_1.isHtmlBlockClose)(line, htmlType);
314
+ this.advance();
315
+ if (!closeOnSameLine) {
316
+ while (this.lineIndex < this.lines.length) {
317
+ const htmlLine = this.currentLine();
318
+ if (htmlType === 6 || htmlType === 7) {
319
+ if (htmlLine.trim() === '') {
320
+ break;
321
+ }
322
+ htmlLines.push(htmlLine);
323
+ this.advance();
324
+ }
325
+ else {
326
+ htmlLines.push(htmlLine);
327
+ this.advance();
328
+ if ((0, html_block_js_1.isHtmlBlockClose)(htmlLine, htmlType)) {
329
+ break;
330
+ }
331
+ }
332
+ }
333
+ }
334
+ container.children.push((0, html_block_js_1.createHtmlBlockNode)(htmlLines.join('\n') + '\n'));
335
+ continue;
336
+ }
337
+ paragraphLines.push((0, utils_js_1.removeIndent)(line, Math.min(lineIndent, 3)));
338
+ this.advance();
339
+ }
340
+ finishParagraph();
341
+ }
342
+ parseList(list, firstMarker) {
343
+ let sawBlankLine = false;
344
+ let sawBlankLineInItem = false;
345
+ let sawBlankBetweenItems = false;
346
+ while (this.lineIndex < this.lines.length) {
347
+ const line = this.currentLine();
348
+ if ((0, thematic_break_js_1.isThematicBreak)(line)) {
349
+ break;
350
+ }
351
+ const marker = (0, list_js_1.parseListMarker)(line);
352
+ if (marker && (0, list_js_1.listsMatch)(firstMarker, marker)) {
353
+ if (sawBlankLine) {
354
+ sawBlankBetweenItems = true;
355
+ }
356
+ sawBlankLine = false;
357
+ const item = (0, list_js_1.createListItemNode)();
358
+ const itemResult = this.parseListItem(item, marker);
359
+ if (itemResult.blankInItem) {
360
+ sawBlankLineInItem = true;
361
+ }
362
+ if (itemResult.endsWithBlank) {
363
+ sawBlankLine = true;
364
+ }
365
+ list.children.push(item);
366
+ }
367
+ else if (line.trim() === '') {
368
+ sawBlankLine = true;
369
+ this.advance();
370
+ let nextIdx = this.lineIndex;
371
+ while (nextIdx < this.lines.length && this.lines[nextIdx].trim() === '') {
372
+ nextIdx++;
373
+ }
374
+ if (nextIdx < this.lines.length) {
375
+ const nextLine = this.lines[nextIdx];
376
+ const nextMarker = (0, list_js_1.parseListMarker)(nextLine);
377
+ if (!nextMarker || !(0, list_js_1.listsMatch)(firstMarker, nextMarker)) {
378
+ if (getIndent(nextLine) < firstMarker.contentIndent) {
379
+ break;
380
+ }
381
+ }
382
+ }
383
+ else {
384
+ break;
385
+ }
386
+ }
387
+ else {
388
+ break;
389
+ }
390
+ }
391
+ if (sawBlankLineInItem || sawBlankBetweenItems) {
392
+ list.tight = false;
393
+ }
394
+ }
395
+ parseListItem(item, marker) {
396
+ const itemLines = [];
397
+ let firstLine = this.currentLine();
398
+ if (firstLine.startsWith(BlockParser.LAZY_PREFIX)) {
399
+ firstLine = firstLine.slice(BlockParser.LAZY_PREFIX.length);
400
+ }
401
+ let firstContent = this.extractListItemContent(firstLine, marker);
402
+ if (firstContent.trim() === '' && firstLine.slice(marker.indentChars + marker.marker.length).match(/^[ \t]/)) {
403
+ itemLines.push('');
404
+ }
405
+ else {
406
+ itemLines.push(firstContent);
407
+ }
408
+ this.advance();
409
+ let sawBlankLine = false;
410
+ let blankLineInItem = false;
411
+ let trailingBlanks = 0;
412
+ const contentIndent = marker.contentIndent;
413
+ const baseThreshold = contentIndent + 1;
414
+ let lastNonBlankIndent = firstContent.trim() === '' ? null : contentIndent;
415
+ let lastNonBlankWasListMarker = false;
416
+ let sawNonBlankContent = firstContent.trim() !== '';
417
+ let inFence = null;
418
+ if (firstContent.trim() !== '') {
419
+ const firstFence = (0, code_block_fenced_js_1.parseFenceOpen)(firstContent);
420
+ if (firstFence) {
421
+ inFence = firstFence;
422
+ }
423
+ lastNonBlankWasListMarker = (0, list_js_1.parseListMarker)(firstContent) !== null;
424
+ }
425
+ while (this.lineIndex < this.lines.length) {
426
+ let line = this.currentLine();
427
+ let lazyLine = false;
428
+ if (line.startsWith(BlockParser.LAZY_PREFIX)) {
429
+ line = line.slice(BlockParser.LAZY_PREFIX.length);
430
+ lazyLine = true;
431
+ }
432
+ const lineIndent = getIndent(line);
433
+ if (inFence) {
434
+ const contentLine = lineIndent >= contentIndent ? (0, utils_js_1.removeIndent)(line, contentIndent) : line;
435
+ itemLines.push(contentLine);
436
+ if ((0, code_block_fenced_js_1.isFenceClose)(contentLine, inFence)) {
437
+ inFence = null;
438
+ }
439
+ this.advance();
440
+ continue;
441
+ }
442
+ if (line.trim() === '') {
443
+ sawBlankLine = true;
444
+ trailingBlanks++;
445
+ itemLines.push('');
446
+ this.advance();
447
+ continue;
448
+ }
449
+ const newMarker = lazyLine ? null : (0, list_js_1.parseListMarker)(line);
450
+ if (newMarker && (0, list_js_1.listsMatch)(marker, newMarker) && newMarker.indent < contentIndent) {
451
+ break;
452
+ }
453
+ if (!sawNonBlankContent && sawBlankLine && lineIndent >= contentIndent) {
454
+ break;
455
+ }
456
+ if (lineIndent >= contentIndent) {
457
+ const contentLine = (0, utils_js_1.removeIndent)(line, contentIndent);
458
+ const isListMarker = (0, list_js_1.parseListMarker)(contentLine) !== null;
459
+ if (sawBlankLine && (lineIndent <= baseThreshold ||
460
+ (lastNonBlankIndent !== null && lastNonBlankIndent <= baseThreshold && !lastNonBlankWasListMarker))) {
461
+ blankLineInItem = true;
462
+ }
463
+ sawBlankLine = false;
464
+ trailingBlanks = 0;
465
+ itemLines.push(contentLine);
466
+ lastNonBlankIndent = lineIndent;
467
+ lastNonBlankWasListMarker = isListMarker;
468
+ sawNonBlankContent = true;
469
+ if (!inFence) {
470
+ const fenceInfo = (0, code_block_fenced_js_1.parseFenceOpen)(contentLine);
471
+ if (fenceInfo) {
472
+ inFence = fenceInfo;
473
+ }
474
+ }
475
+ this.advance();
476
+ }
477
+ else if (sawBlankLine) {
478
+ break;
479
+ }
480
+ else {
481
+ if (!(0, thematic_break_js_1.isThematicBreak)(line) && !(0, heading_atx_js_1.parseAtxHeading)(line) &&
482
+ !(0, code_block_fenced_js_1.parseFenceOpen)(line) && !line.match(/^ {0,3}>/) &&
483
+ !newMarker) {
484
+ trailingBlanks = 0;
485
+ itemLines.push(line);
486
+ lastNonBlankIndent = lineIndent;
487
+ lastNonBlankWasListMarker = false;
488
+ sawNonBlankContent = true;
489
+ this.advance();
490
+ }
491
+ else {
492
+ break;
493
+ }
494
+ }
495
+ }
496
+ const endsWithBlank = trailingBlanks > 0;
497
+ while (itemLines.length > 0 && itemLines[itemLines.length - 1] === '') {
498
+ itemLines.pop();
499
+ }
500
+ const subParser = new BlockParser();
501
+ const result = subParser.parse(itemLines.join('\n'));
502
+ item.children = result.document.children;
503
+ result.linkReferences.forEach((def, label) => {
504
+ if (!this.linkReferences.has(label)) {
505
+ this.linkReferences.set(label, def);
506
+ }
507
+ });
508
+ const paragraphCount = item.children.filter(child => child.type === 'paragraph').length;
509
+ if (paragraphCount >= 2) {
510
+ blankLineInItem = true;
511
+ }
512
+ return { blankInItem: blankLineInItem, endsWithBlank };
513
+ }
514
+ lastNonBlankLine(lines) {
515
+ for (let i = lines.length - 1; i >= 0; i--) {
516
+ const line = lines[i];
517
+ if (line.trim() !== '') {
518
+ return line;
519
+ }
520
+ }
521
+ return null;
522
+ }
523
+ getBlockquotePrefix(line) {
524
+ let i = 0;
525
+ let prefix = '';
526
+ while (i < line.length) {
527
+ let spaces = 0;
528
+ const start = i;
529
+ while (i < line.length && line[i] === ' ' && spaces < 3) {
530
+ spaces++;
531
+ i++;
532
+ }
533
+ if (i < line.length && line[i] === '>') {
534
+ i++;
535
+ if (i < line.length && (line[i] === ' ' || line[i] === '\t')) {
536
+ i++;
537
+ }
538
+ prefix += line.slice(start, i);
539
+ }
540
+ else {
541
+ break;
542
+ }
543
+ }
544
+ return prefix;
545
+ }
546
+ allowsLazyAfterListMarker(line) {
547
+ const marker = (0, list_js_1.parseListMarker)(line);
548
+ if (!marker) {
549
+ return true;
550
+ }
551
+ const afterMarker = line.slice(marker.contentCharIndex);
552
+ return /^[ \t]*>/.test(afterMarker);
553
+ }
554
+ extractBlockquoteContent(line, match) {
555
+ const fullMatch = match[0];
556
+ const optionalSpace = match[1];
557
+ let column = 0;
558
+ let charIndex = 0;
559
+ while (charIndex < line.length && line[charIndex] === ' ' && charIndex < 3) {
560
+ column++;
561
+ charIndex++;
562
+ }
563
+ charIndex++;
564
+ column++;
565
+ let result = '';
566
+ if (optionalSpace === '\t') {
567
+ const tabWidth = 4 - (column % 4);
568
+ const remaining = tabWidth - 1;
569
+ if (remaining > 0) {
570
+ result = ' '.repeat(remaining);
571
+ }
572
+ charIndex++;
573
+ column += tabWidth;
574
+ }
575
+ else if (optionalSpace === ' ') {
576
+ charIndex++;
577
+ column++;
578
+ }
579
+ while (charIndex < line.length) {
580
+ const char = line[charIndex];
581
+ if (char === '\t') {
582
+ const tabWidth = 4 - (column % 4);
583
+ result += ' '.repeat(tabWidth);
584
+ column += tabWidth;
585
+ }
586
+ else {
587
+ result += char;
588
+ column++;
589
+ }
590
+ charIndex++;
591
+ }
592
+ return result;
593
+ }
594
+ extractListItemContent(line, marker) {
595
+ const contentStartColumn = marker.indent + marker.marker.length + marker.padding;
596
+ let column = 0;
597
+ let charIndex = 0;
598
+ let result = '';
599
+ while (charIndex < line.length && charIndex < marker.indentChars) {
600
+ if (line[charIndex] === '\t') {
601
+ column = column + (4 - (column % 4));
602
+ }
603
+ else {
604
+ column++;
605
+ }
606
+ charIndex++;
607
+ }
608
+ charIndex += marker.marker.length;
609
+ column += marker.marker.length;
610
+ if (charIndex < line.length && marker.paddingChars > 0) {
611
+ const paddingChar = line[charIndex];
612
+ if (paddingChar === '\t') {
613
+ const tabWidth = 4 - (column % 4);
614
+ const consumed = Math.min(tabWidth, marker.padding);
615
+ column += consumed;
616
+ charIndex += marker.paddingChars;
617
+ const remaining = tabWidth - consumed;
618
+ if (remaining > 0) {
619
+ result += ' '.repeat(remaining);
620
+ column += remaining;
621
+ }
622
+ }
623
+ else {
624
+ column += marker.padding;
625
+ charIndex += marker.paddingChars;
626
+ }
627
+ }
628
+ while (charIndex < line.length) {
629
+ const char = line[charIndex];
630
+ if (char === '\t') {
631
+ const tabWidth = 4 - (column % 4);
632
+ result += ' '.repeat(tabWidth);
633
+ column += tabWidth;
634
+ }
635
+ else {
636
+ result += char;
637
+ column++;
638
+ }
639
+ charIndex++;
640
+ }
641
+ return result;
642
+ }
643
+ }
644
+ exports.BlockParser = BlockParser;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ /**
3
+ * Parser for blockquote elements prefixed with >.
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.isBlockquoteStart = isBlockquoteStart;
7
+ exports.removeBlockquotePrefix = removeBlockquotePrefix;
8
+ exports.createBlockquoteNode = createBlockquoteNode;
9
+ exports.getBlockquoteContent = getBlockquoteContent;
10
+ const BLOCKQUOTE_REGEX = /^( {0,3})>( ?)/;
11
+ function isBlockquoteStart(line) {
12
+ return BLOCKQUOTE_REGEX.test(line);
13
+ }
14
+ function removeBlockquotePrefix(line) {
15
+ return line.replace(BLOCKQUOTE_REGEX, '');
16
+ }
17
+ function createBlockquoteNode() {
18
+ return {
19
+ type: 'blockquote',
20
+ children: [],
21
+ };
22
+ }
23
+ function getBlockquoteContent(line) {
24
+ const match = line.match(BLOCKQUOTE_REGEX);
25
+ if (!match)
26
+ return null;
27
+ return line.slice(match[0].length);
28
+ }