@tinta-lang/syntax 0.0.0-stage → 0.1.0-dev.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 (245) hide show
  1. package/LICENSE +21 -0
  2. package/THIRD_PARTY_NOTICES.txt +13651 -0
  3. package/dist/directive.d.ts +40 -0
  4. package/dist/editing.d.ts +46 -0
  5. package/dist/enrichment.d.ts +5 -0
  6. package/dist/expression.d.ts +26 -0
  7. package/dist/image-destinations.d.ts +3 -0
  8. package/dist/index.d.ts +8 -0
  9. package/dist/index.js +10118 -0
  10. package/dist/parser.d.ts +40 -0
  11. package/dist/registry.d.ts +144 -0
  12. package/dist/source.d.ts +28 -0
  13. package/package.json +280 -3
  14. package/parser-provenance.json +1419 -0
  15. package/patches/README.md +5 -0
  16. package/patches/micromark.patch +84 -0
  17. package/vendor/@noble/hashes/LICENSE +21 -0
  18. package/vendor/@noble/hashes/_md.js +209 -0
  19. package/vendor/@noble/hashes/_u64.js +77 -0
  20. package/vendor/@noble/hashes/package.json +91 -0
  21. package/vendor/@noble/hashes/sha2.js +446 -0
  22. package/vendor/@noble/hashes/utils.js +687 -0
  23. package/vendor/character-entities/index.js +2132 -0
  24. package/vendor/character-entities/license +22 -0
  25. package/vendor/character-entities/package.json +78 -0
  26. package/vendor/decode-named-character-reference/index.js +19 -0
  27. package/vendor/decode-named-character-reference/license +22 -0
  28. package/vendor/decode-named-character-reference/package.json +90 -0
  29. package/vendor/devlop/lib/default.js +9 -0
  30. package/vendor/devlop/license +22 -0
  31. package/vendor/devlop/package.json +80 -0
  32. package/vendor/longest-streak/index.js +36 -0
  33. package/vendor/longest-streak/license +22 -0
  34. package/vendor/longest-streak/package.json +73 -0
  35. package/vendor/markdown-table/index.js +393 -0
  36. package/vendor/markdown-table/license +22 -0
  37. package/vendor/markdown-table/package.json +77 -0
  38. package/vendor/mdast-util-from-markdown/index.js +2 -0
  39. package/vendor/mdast-util-from-markdown/lib/index.js +1270 -0
  40. package/vendor/mdast-util-from-markdown/license +22 -0
  41. package/vendor/mdast-util-from-markdown/package.json +98 -0
  42. package/vendor/mdast-util-gfm-strikethrough/index.js +5 -0
  43. package/vendor/mdast-util-gfm-strikethrough/lib/index.js +133 -0
  44. package/vendor/mdast-util-gfm-strikethrough/license +22 -0
  45. package/vendor/mdast-util-gfm-strikethrough/package.json +84 -0
  46. package/vendor/mdast-util-gfm-table/index.js +2 -0
  47. package/vendor/mdast-util-gfm-table/lib/index.js +300 -0
  48. package/vendor/mdast-util-gfm-table/license +22 -0
  49. package/vendor/mdast-util-gfm-table/package.json +101 -0
  50. package/vendor/mdast-util-gfm-task-list-item/index.js +4 -0
  51. package/vendor/mdast-util-gfm-task-list-item/lib/index.js +141 -0
  52. package/vendor/mdast-util-gfm-task-list-item/license +22 -0
  53. package/vendor/mdast-util-gfm-task-list-item/package.json +90 -0
  54. package/vendor/mdast-util-phrasing/index.js +1 -0
  55. package/vendor/mdast-util-phrasing/lib/index.js +45 -0
  56. package/vendor/mdast-util-phrasing/license +23 -0
  57. package/vendor/mdast-util-phrasing/package.json +78 -0
  58. package/vendor/mdast-util-to-markdown/index.js +3 -0
  59. package/vendor/mdast-util-to-markdown/lib/configure.js +98 -0
  60. package/vendor/mdast-util-to-markdown/lib/handle/blockquote.js +40 -0
  61. package/vendor/mdast-util-to-markdown/lib/handle/break.js +41 -0
  62. package/vendor/mdast-util-to-markdown/lib/handle/code.js +86 -0
  63. package/vendor/mdast-util-to-markdown/lib/handle/definition.js +83 -0
  64. package/vendor/mdast-util-to-markdown/lib/handle/emphasis.js +67 -0
  65. package/vendor/mdast-util-to-markdown/lib/handle/heading.js +84 -0
  66. package/vendor/mdast-util-to-markdown/lib/handle/html.js +27 -0
  67. package/vendor/mdast-util-to-markdown/lib/handle/image-reference.js +78 -0
  68. package/vendor/mdast-util-to-markdown/lib/handle/image.js +92 -0
  69. package/vendor/mdast-util-to-markdown/lib/handle/index.js +45 -0
  70. package/vendor/mdast-util-to-markdown/lib/handle/inline-code.js +86 -0
  71. package/vendor/mdast-util-to-markdown/lib/handle/link-reference.js +78 -0
  72. package/vendor/mdast-util-to-markdown/lib/handle/link.js +129 -0
  73. package/vendor/mdast-util-to-markdown/lib/handle/list-item.js +73 -0
  74. package/vendor/mdast-util-to-markdown/lib/handle/list.js +107 -0
  75. package/vendor/mdast-util-to-markdown/lib/handle/paragraph.js +27 -0
  76. package/vendor/mdast-util-to-markdown/lib/handle/root.js +34 -0
  77. package/vendor/mdast-util-to-markdown/lib/handle/strong.js +67 -0
  78. package/vendor/mdast-util-to-markdown/lib/handle/text.js +30 -0
  79. package/vendor/mdast-util-to-markdown/lib/handle/thematic-break.js +27 -0
  80. package/vendor/mdast-util-to-markdown/lib/index.js +213 -0
  81. package/vendor/mdast-util-to-markdown/lib/join.js +56 -0
  82. package/vendor/mdast-util-to-markdown/lib/unsafe.js +160 -0
  83. package/vendor/mdast-util-to-markdown/lib/util/association.js +33 -0
  84. package/vendor/mdast-util-to-markdown/lib/util/character.js +32 -0
  85. package/vendor/mdast-util-to-markdown/lib/util/check-bullet-ordered.js +25 -0
  86. package/vendor/mdast-util-to-markdown/lib/util/check-bullet-other.js +42 -0
  87. package/vendor/mdast-util-to-markdown/lib/util/check-bullet.js +25 -0
  88. package/vendor/mdast-util-to-markdown/lib/util/check-emphasis.js +25 -0
  89. package/vendor/mdast-util-to-markdown/lib/util/check-fence.js +25 -0
  90. package/vendor/mdast-util-to-markdown/lib/util/check-list-item-indent.js +25 -0
  91. package/vendor/mdast-util-to-markdown/lib/util/check-quote.js +25 -0
  92. package/vendor/mdast-util-to-markdown/lib/util/check-rule-repetition.js +25 -0
  93. package/vendor/mdast-util-to-markdown/lib/util/check-rule.js +25 -0
  94. package/vendor/mdast-util-to-markdown/lib/util/check-strong.js +25 -0
  95. package/vendor/mdast-util-to-markdown/lib/util/compile-pattern.js +30 -0
  96. package/vendor/mdast-util-to-markdown/lib/util/container-flow.js +93 -0
  97. package/vendor/mdast-util-to-markdown/lib/util/container-phrasing.js +704 -0
  98. package/vendor/mdast-util-to-markdown/lib/util/encode-character-reference.js +11 -0
  99. package/vendor/mdast-util-to-markdown/lib/util/encode-info.js +82 -0
  100. package/vendor/mdast-util-to-markdown/lib/util/format-code-as-indented.js +27 -0
  101. package/vendor/mdast-util-to-markdown/lib/util/format-heading-as-setext.js +39 -0
  102. package/vendor/mdast-util-to-markdown/lib/util/format-link-as-autolink.js +39 -0
  103. package/vendor/mdast-util-to-markdown/lib/util/html-kind.js +63 -0
  104. package/vendor/mdast-util-to-markdown/lib/util/indent-lines.js +40 -0
  105. package/vendor/mdast-util-to-markdown/lib/util/pattern-in-scope.js +52 -0
  106. package/vendor/mdast-util-to-markdown/lib/util/safe.js +263 -0
  107. package/vendor/mdast-util-to-markdown/lib/util/track.js +55 -0
  108. package/vendor/mdast-util-to-markdown/license +22 -0
  109. package/vendor/mdast-util-to-markdown/package.json +88 -0
  110. package/vendor/mdast-util-to-string/index.js +5 -0
  111. package/vendor/mdast-util-to-string/lib/index.js +108 -0
  112. package/vendor/mdast-util-to-string/license +22 -0
  113. package/vendor/mdast-util-to-string/package.json +78 -0
  114. package/vendor/micromark/index.js +60 -0
  115. package/vendor/micromark/lib/compile.js +1068 -0
  116. package/vendor/micromark/lib/constructs.js +85 -0
  117. package/vendor/micromark/lib/create-tokenizer.js +615 -0
  118. package/vendor/micromark/lib/initialize/content.js +79 -0
  119. package/vendor/micromark/lib/initialize/document.js +370 -0
  120. package/vendor/micromark/lib/initialize/flow.js +58 -0
  121. package/vendor/micromark/lib/initialize/text.js +216 -0
  122. package/vendor/micromark/lib/parse.js +56 -0
  123. package/vendor/micromark/lib/postprocess.js +18 -0
  124. package/vendor/micromark/lib/preprocess.js +112 -0
  125. package/vendor/micromark/license +22 -0
  126. package/vendor/micromark/package.json +87 -0
  127. package/vendor/micromark-core-commonmark/index.js +22 -0
  128. package/vendor/micromark-core-commonmark/lib/attention.js +231 -0
  129. package/vendor/micromark-core-commonmark/lib/autolink.js +233 -0
  130. package/vendor/micromark-core-commonmark/lib/blank-line.js +61 -0
  131. package/vendor/micromark-core-commonmark/lib/block-quote.js +143 -0
  132. package/vendor/micromark-core-commonmark/lib/character-escape.js +64 -0
  133. package/vendor/micromark-core-commonmark/lib/character-reference.js +149 -0
  134. package/vendor/micromark-core-commonmark/lib/code-fenced.js +422 -0
  135. package/vendor/micromark-core-commonmark/lib/code-indented.js +159 -0
  136. package/vendor/micromark-core-commonmark/lib/code-text.js +225 -0
  137. package/vendor/micromark-core-commonmark/lib/content.js +163 -0
  138. package/vendor/micromark-core-commonmark/lib/definition.js +254 -0
  139. package/vendor/micromark-core-commonmark/lib/hard-break-escape.js +60 -0
  140. package/vendor/micromark-core-commonmark/lib/heading-atx.js +191 -0
  141. package/vendor/micromark-core-commonmark/lib/html-flow.js +827 -0
  142. package/vendor/micromark-core-commonmark/lib/html-text.js +678 -0
  143. package/vendor/micromark-core-commonmark/lib/label-end.js +561 -0
  144. package/vendor/micromark-core-commonmark/lib/label-start-image.js +110 -0
  145. package/vendor/micromark-core-commonmark/lib/label-start-link.js +63 -0
  146. package/vendor/micromark-core-commonmark/lib/line-ending.js +33 -0
  147. package/vendor/micromark-core-commonmark/lib/list.js +213 -0
  148. package/vendor/micromark-core-commonmark/lib/partial-non-lazy-continuation.js +61 -0
  149. package/vendor/micromark-core-commonmark/lib/setext-underline.js +187 -0
  150. package/vendor/micromark-core-commonmark/lib/thematic-break.js +102 -0
  151. package/vendor/micromark-core-commonmark/license +22 -0
  152. package/vendor/micromark-core-commonmark/package.json +62 -0
  153. package/vendor/micromark-extension-gfm-strikethrough/index.js +3 -0
  154. package/vendor/micromark-extension-gfm-strikethrough/lib/html.js +26 -0
  155. package/vendor/micromark-extension-gfm-strikethrough/lib/syntax.js +142 -0
  156. package/vendor/micromark-extension-gfm-strikethrough/license +22 -0
  157. package/vendor/micromark-extension-gfm-strikethrough/package.json +129 -0
  158. package/vendor/micromark-extension-gfm-table/index.js +2 -0
  159. package/vendor/micromark-extension-gfm-table/lib/edit-map.js +222 -0
  160. package/vendor/micromark-extension-gfm-table/lib/html.js +135 -0
  161. package/vendor/micromark-extension-gfm-table/lib/infer.js +52 -0
  162. package/vendor/micromark-extension-gfm-table/lib/syntax.js +846 -0
  163. package/vendor/micromark-extension-gfm-table/license +22 -0
  164. package/vendor/micromark-extension-gfm-table/package.json +86 -0
  165. package/vendor/micromark-extension-gfm-task-list-item/index.js +2 -0
  166. package/vendor/micromark-extension-gfm-task-list-item/lib/html.js +29 -0
  167. package/vendor/micromark-extension-gfm-task-list-item/lib/syntax.js +158 -0
  168. package/vendor/micromark-extension-gfm-task-list-item/license +22 -0
  169. package/vendor/micromark-extension-gfm-task-list-item/package.json +131 -0
  170. package/vendor/micromark-factory-destination/index.js +206 -0
  171. package/vendor/micromark-factory-destination/license +22 -0
  172. package/vendor/micromark-factory-destination/package.json +57 -0
  173. package/vendor/micromark-factory-label/index.js +148 -0
  174. package/vendor/micromark-factory-label/license +22 -0
  175. package/vendor/micromark-factory-label/package.json +60 -0
  176. package/vendor/micromark-factory-space/index.js +116 -0
  177. package/vendor/micromark-factory-space/license +22 -0
  178. package/vendor/micromark-factory-space/package.json +46 -0
  179. package/vendor/micromark-factory-title/index.js +158 -0
  180. package/vendor/micromark-factory-title/license +22 -0
  181. package/vendor/micromark-factory-title/package.json +58 -0
  182. package/vendor/micromark-factory-whitespace/index.js +44 -0
  183. package/vendor/micromark-factory-whitespace/license +22 -0
  184. package/vendor/micromark-factory-whitespace/package.json +57 -0
  185. package/vendor/micromark-util-character/index.js +246 -0
  186. package/vendor/micromark-util-character/license +22 -0
  187. package/vendor/micromark-util-character/package.json +57 -0
  188. package/vendor/micromark-util-chunked/index.js +81 -0
  189. package/vendor/micromark-util-chunked/license +22 -0
  190. package/vendor/micromark-util-chunked/package.json +57 -0
  191. package/vendor/micromark-util-classify-character/index.js +27 -0
  192. package/vendor/micromark-util-classify-character/license +22 -0
  193. package/vendor/micromark-util-classify-character/package.json +59 -0
  194. package/vendor/micromark-util-combine-extensions/index.js +143 -0
  195. package/vendor/micromark-util-combine-extensions/license +22 -0
  196. package/vendor/micromark-util-combine-extensions/package.json +52 -0
  197. package/vendor/micromark-util-decode-numeric-character-reference/index.js +32 -0
  198. package/vendor/micromark-util-decode-numeric-character-reference/license +22 -0
  199. package/vendor/micromark-util-decode-numeric-character-reference/package.json +59 -0
  200. package/vendor/micromark-util-decode-string/index.js +45 -0
  201. package/vendor/micromark-util-decode-string/license +22 -0
  202. package/vendor/micromark-util-decode-string/package.json +63 -0
  203. package/vendor/micromark-util-edit-map/index.js +225 -0
  204. package/vendor/micromark-util-edit-map/license +22 -0
  205. package/vendor/micromark-util-edit-map/package.json +48 -0
  206. package/vendor/micromark-util-encode/index.js +33 -0
  207. package/vendor/micromark-util-encode/license +22 -0
  208. package/vendor/micromark-util-encode/package.json +47 -0
  209. package/vendor/micromark-util-html-tag-name/index.js +93 -0
  210. package/vendor/micromark-util-html-tag-name/license +22 -0
  211. package/vendor/micromark-util-html-tag-name/package.json +47 -0
  212. package/vendor/micromark-util-normalize-identifier/index.js +33 -0
  213. package/vendor/micromark-util-normalize-identifier/license +22 -0
  214. package/vendor/micromark-util-normalize-identifier/package.json +58 -0
  215. package/vendor/micromark-util-resolve-all/index.js +32 -0
  216. package/vendor/micromark-util-resolve-all/license +22 -0
  217. package/vendor/micromark-util-resolve-all/package.json +48 -0
  218. package/vendor/micromark-util-sanitize-uri/index.js +107 -0
  219. package/vendor/micromark-util-sanitize-uri/license +22 -0
  220. package/vendor/micromark-util-sanitize-uri/package.json +59 -0
  221. package/vendor/micromark-util-subtokenize/index.js +228 -0
  222. package/vendor/micromark-util-subtokenize/lib/splice-buffer.js +251 -0
  223. package/vendor/micromark-util-subtokenize/license +22 -0
  224. package/vendor/micromark-util-subtokenize/package.json +60 -0
  225. package/vendor/unist-util-is/index.js +7 -0
  226. package/vendor/unist-util-is/lib/index.js +296 -0
  227. package/vendor/unist-util-is/license +22 -0
  228. package/vendor/unist-util-is/package.json +103 -0
  229. package/vendor/unist-util-stringify-position/index.js +1 -0
  230. package/vendor/unist-util-stringify-position/lib/index.js +84 -0
  231. package/vendor/unist-util-stringify-position/license +22 -0
  232. package/vendor/unist-util-stringify-position/package.json +80 -0
  233. package/vendor/unist-util-visit/index.js +2 -0
  234. package/vendor/unist-util-visit/lib/index.js +312 -0
  235. package/vendor/unist-util-visit/license +22 -0
  236. package/vendor/unist-util-visit/package.json +108 -0
  237. package/vendor/unist-util-visit-parents/index.js +2 -0
  238. package/vendor/unist-util-visit-parents/lib/color.js +7 -0
  239. package/vendor/unist-util-visit-parents/lib/index.js +399 -0
  240. package/vendor/unist-util-visit-parents/license +22 -0
  241. package/vendor/unist-util-visit-parents/package.json +106 -0
  242. package/vendor/zwitch/index.js +118 -0
  243. package/vendor/zwitch/license +22 -0
  244. package/vendor/zwitch/package.json +72 -0
  245. package/README.md +0 -3
@@ -0,0 +1,79 @@
1
+ /**
2
+ * @import {
3
+ * InitialConstruct,
4
+ * Initializer,
5
+ * State,
6
+ * TokenizeContext,
7
+ * Token
8
+ * } from 'micromark-util-types'
9
+ */
10
+
11
+ import { factorySpace } from 'micromark-factory-space';
12
+ import { markdownLineEnding } from 'micromark-util-character';
13
+ /** @type {InitialConstruct} */
14
+ export const content = {
15
+ tokenize: initializeContent
16
+ };
17
+
18
+ /**
19
+ * @this {TokenizeContext}
20
+ * Context.
21
+ * @type {Initializer}
22
+ * Content.
23
+ */
24
+ function initializeContent(effects) {
25
+ const contentStart = effects.attempt(this.parser.constructs.contentInitial, afterContentStartConstruct, paragraphInitial);
26
+ /** @type {Token} */
27
+ let previous;
28
+ return contentStart;
29
+
30
+ /** @type {State} */
31
+ function afterContentStartConstruct(code) {
32
+ if (code === null) {
33
+ effects.consume(code);
34
+ return;
35
+ }
36
+ effects.enter("lineEnding");
37
+ effects.consume(code);
38
+ effects.exit("lineEnding");
39
+ return factorySpace(effects, contentStart, "linePrefix");
40
+ }
41
+
42
+ /** @type {State} */
43
+ function paragraphInitial(code) {
44
+ effects.enter("paragraph");
45
+ return lineStart(code);
46
+ }
47
+
48
+ /** @type {State} */
49
+ function lineStart(code) {
50
+ const token = effects.enter("chunkText", {
51
+ contentType: "text",
52
+ previous
53
+ });
54
+ if (previous) {
55
+ previous.next = token;
56
+ }
57
+ previous = token;
58
+ return data(code);
59
+ }
60
+
61
+ /** @type {State} */
62
+ function data(code) {
63
+ if (code === null) {
64
+ effects.exit("chunkText");
65
+ effects.exit("paragraph");
66
+ effects.consume(code);
67
+ return;
68
+ }
69
+ if (markdownLineEnding(code)) {
70
+ effects.consume(code);
71
+ effects.exit("chunkText");
72
+ return lineStart;
73
+ }
74
+
75
+ // Data.
76
+ effects.consume(code);
77
+ return data;
78
+ }
79
+ }
@@ -0,0 +1,370 @@
1
+ /**
2
+ * @import {
3
+ * Construct,
4
+ * ContainerState,
5
+ * InitialConstruct,
6
+ * Initializer,
7
+ * Point,
8
+ * State,
9
+ * TokenizeContext,
10
+ * Tokenizer,
11
+ * Token
12
+ * } from 'micromark-util-types'
13
+ */
14
+
15
+ /**
16
+ * @typedef {[Construct, ContainerState]} StackItem
17
+ * Construct and its state.
18
+ */
19
+
20
+ import { factorySpace } from 'micromark-factory-space';
21
+ import { markdownLineEnding } from 'micromark-util-character';
22
+ import { EditMap } from 'micromark-util-edit-map';
23
+ /** @type {InitialConstruct} */
24
+ export const document = {
25
+ tokenize: initializeDocument
26
+ };
27
+
28
+ /** @type {Construct} */
29
+ const containerConstruct = {
30
+ tokenize: tokenizeContainer
31
+ };
32
+
33
+ /**
34
+ * @this {TokenizeContext}
35
+ * Self.
36
+ * @type {Initializer}
37
+ * Initializer.
38
+ */
39
+ function initializeDocument(effects) {
40
+ const self = this;
41
+ /** @type {Array<StackItem>} */
42
+ const stack = [];
43
+ let continued = 0;
44
+ /** @type {TokenizeContext | undefined} */
45
+ let childFlow;
46
+ /** @type {Token | undefined} */
47
+ let childToken;
48
+ /** @type {number} */
49
+ let lineStartOffset;
50
+ return start;
51
+
52
+ /** @type {State} */
53
+ function start(code) {
54
+ // First we iterate through the open blocks, starting with the root
55
+ // document, and descending through last children down to the last open
56
+ // block.
57
+ // Each block imposes a condition that the line must satisfy if the block is
58
+ // to remain open.
59
+ // For example, a block quote requires a `>` character.
60
+ // A paragraph requires a non-blank line.
61
+ // In this phase we may match all or just some of the open blocks.
62
+ // But we cannot close unmatched blocks yet, because we may have a lazy
63
+ // continuation line.
64
+ if (continued < stack.length) {
65
+ const item = stack[continued];
66
+ self.containerState = item[1];
67
+ return effects.attempt(item[0].continuation, documentContinue, checkNewContainers)(code);
68
+ }
69
+
70
+ // Done.
71
+ return checkNewContainers(code);
72
+ }
73
+
74
+ /** @type {State} */
75
+ function documentContinue(code) {
76
+ continued++;
77
+
78
+ // Note: this field is called `_closeFlow` but it also closes containers.
79
+ // Perhaps a good idea to rename it but it’s already used in the wild by
80
+ // extensions.
81
+ if (self.containerState._closeFlow) {
82
+ self.containerState._closeFlow = undefined;
83
+ if (childFlow) {
84
+ closeFlow();
85
+ }
86
+
87
+ // Note: this algorithm for moving events around is similar to the
88
+ // algorithm when dealing with lazy lines in `writeToChild`.
89
+ const indexBeforeExits = self.events.length;
90
+ let indexBeforeFlow = indexBeforeExits;
91
+ /** @type {Point | undefined} */
92
+ let point;
93
+
94
+ // Find the flow chunk.
95
+ while (indexBeforeFlow--) {
96
+ if (self.events[indexBeforeFlow][0] === 'exit' && self.events[indexBeforeFlow][1].type === "chunkFlow") {
97
+ point = self.events[indexBeforeFlow][1].end;
98
+ break;
99
+ }
100
+ }
101
+ exitContainers(continued);
102
+
103
+ // Fix positions.
104
+ let index = indexBeforeExits;
105
+ while (index < self.events.length) {
106
+ self.events[index][1].end = {
107
+ ...point
108
+ };
109
+ index++;
110
+ }
111
+ const editMap = new EditMap();
112
+ // Inject the exits earlier (they’re still also at the end).
113
+ editMap.add(indexBeforeFlow + 1, 0, self.events.slice(indexBeforeExits));
114
+ // Discard the duplicate exits.
115
+ editMap.add(indexBeforeExits, index - indexBeforeExits, []);
116
+ editMap.consume(self.events);
117
+ return checkNewContainers(code);
118
+ }
119
+ return start(code);
120
+ }
121
+
122
+ /** @type {State} */
123
+ function checkNewContainers(code) {
124
+ // Next, after consuming the continuation markers for existing blocks, we
125
+ // look for new block starts (e.g. `>` for a block quote).
126
+ // If we encounter a new block start, we close any blocks unmatched in
127
+ // step 1 before creating the new block as a child of the last matched
128
+ // block.
129
+ if (continued === stack.length) {
130
+ // No need to `check` whether there’s a container, of `exitContainers`
131
+ // would be moot.
132
+ // We can instead immediately `attempt` to parse one.
133
+ if (!childFlow) {
134
+ return documentContinued(code);
135
+ }
136
+
137
+ // If we have concrete content, such as block HTML or fenced code,
138
+ // we can’t have containers “pierce” into them, so we can immediately
139
+ // start.
140
+ if (childFlow.currentConstruct && childFlow.currentConstruct.concrete) {
141
+ return flowStart(code);
142
+ }
143
+
144
+ // If we do have flow, it could still be a blank line,
145
+ // but we’d be interrupting it w/ a new container if there’s a current
146
+ // construct.
147
+ // To do: next major: remove `_gfmTableDynamicInterruptHack` (no longer
148
+ // needed in micromark-extension-gfm-table@1.0.6).
149
+ self.interrupt = Boolean(childFlow.currentConstruct && !childFlow._gfmTableDynamicInterruptHack);
150
+ }
151
+
152
+ // Check if there is a new container.
153
+ self.containerState = {};
154
+ return effects.check(containerConstruct, thereIsANewContainer, thereIsNoNewContainer)(code);
155
+ }
156
+
157
+ /** @type {State} */
158
+ function thereIsANewContainer(code) {
159
+ if (childFlow) {
160
+ closeFlow();
161
+ }
162
+ exitContainers(continued);
163
+ return documentContinued(code);
164
+ }
165
+
166
+ /** @type {State} */
167
+ function thereIsNoNewContainer(code) {
168
+ self.parser.lazy[self.now().line] = continued !== stack.length;
169
+ lineStartOffset = self.now().offset;
170
+ return flowStart(code);
171
+ }
172
+
173
+ /** @type {State} */
174
+ function documentContinued(code) {
175
+ // Try new containers.
176
+ self.containerState = {};
177
+ return effects.attempt(containerConstruct, containerContinue, flowStart)(code);
178
+ }
179
+
180
+ /** @type {State} */
181
+ function containerContinue(code) {
182
+ continued++;
183
+ stack.push([self.currentConstruct, self.containerState]);
184
+ // Try another.
185
+ return documentContinued(code);
186
+ }
187
+
188
+ /** @type {State} */
189
+ function flowStart(code) {
190
+ if (code === null) {
191
+ if (childFlow) {
192
+ closeFlow();
193
+ }
194
+ exitContainers(0);
195
+ effects.consume(code);
196
+ return;
197
+ }
198
+ childFlow = childFlow || self.parser.flow(self.now());
199
+ effects.enter("chunkFlow", {
200
+ _tokenizer: childFlow,
201
+ contentType: "flow",
202
+ previous: childToken
203
+ });
204
+ return flowContinue(code);
205
+ }
206
+
207
+ /** @type {State} */
208
+ function flowContinue(code) {
209
+ if (code === null) {
210
+ writeToChild(effects.exit("chunkFlow"), true);
211
+ exitContainers(0);
212
+ effects.consume(code);
213
+ return;
214
+ }
215
+ if (markdownLineEnding(code)) {
216
+ effects.consume(code);
217
+ writeToChild(effects.exit("chunkFlow"));
218
+ // Get ready for the next line.
219
+ continued = 0;
220
+ self.interrupt = undefined;
221
+ return start;
222
+ }
223
+ effects.consume(code);
224
+ return flowContinue;
225
+ }
226
+
227
+ /**
228
+ * @param {Token} token
229
+ * Token.
230
+ * @param {boolean | undefined} [endOfFile]
231
+ * Whether the token is at the end of the file (default: `false`).
232
+ * @returns {undefined}
233
+ * Nothing.
234
+ */
235
+ function writeToChild(token, endOfFile) {
236
+ const stream = self.sliceStream(token);
237
+ if (endOfFile) {
238
+ stream.push(null);
239
+ }
240
+ token.previous = childToken;
241
+ if (childToken) {
242
+ childToken.next = token;
243
+ }
244
+ childToken = token;
245
+ childFlow.defineSkip(token.start);
246
+ childFlow.write(stream);
247
+
248
+ // Alright, so we just added a lazy line:
249
+ //
250
+ // ```markdown
251
+ // > a
252
+ // b.
253
+ //
254
+ // Or:
255
+ //
256
+ // > ~~~c
257
+ // d
258
+ //
259
+ // Or:
260
+ //
261
+ // > | e |
262
+ // f
263
+ // ```
264
+ //
265
+ // The construct in the second example (fenced code) does not accept lazy
266
+ // lines, so it marked itself as done at the end of its first line, and
267
+ // then the content construct parses `d`.
268
+ // Most constructs in markdown match on the first line: if the first line
269
+ // forms a construct, a non-lazy line can’t “unmake” it.
270
+ //
271
+ // The construct in the third example is potentially a GFM table, and
272
+ // those are *weird*.
273
+ // It *could* be a table, from the first line, if the following line
274
+ // matches a condition.
275
+ // In this case, that second line is lazy, which “unmakes” the first line
276
+ // and turns the whole into one content block.
277
+ //
278
+ // We’ve now parsed the non-lazy and the lazy line, and can figure out
279
+ // whether the lazy line started a new flow block.
280
+ // If it did, we exit the current containers between the two flow blocks.
281
+ if (self.parser.lazy[token.start.line]) {
282
+ let index = childFlow.events.length;
283
+ while (index--) {
284
+ if (
285
+ // The token starts before the line ending…
286
+ childFlow.events[index][1].start.offset < lineStartOffset && (
287
+ // …and either is not ended yet…
288
+ !childFlow.events[index][1].end ||
289
+ // …or ends after it.
290
+ childFlow.events[index][1].end.offset > lineStartOffset)) {
291
+ // Exit: there’s still something open, which means it’s a lazy line
292
+ // part of something.
293
+ return;
294
+ }
295
+ }
296
+
297
+ // Note: this algorithm for moving events around is similar to the
298
+ // algorithm when closing flow in `documentContinue`.
299
+ const indexBeforeExits = self.events.length;
300
+ let indexBeforeFlow = indexBeforeExits;
301
+ /** @type {boolean | undefined} */
302
+ let seen;
303
+ /** @type {Point | undefined} */
304
+ let point;
305
+
306
+ // Find the previous chunk (the one before the lazy line).
307
+ while (indexBeforeFlow--) {
308
+ if (self.events[indexBeforeFlow][0] === 'exit' && self.events[indexBeforeFlow][1].type === "chunkFlow") {
309
+ if (seen) {
310
+ point = self.events[indexBeforeFlow][1].end;
311
+ break;
312
+ }
313
+ seen = true;
314
+ }
315
+ }
316
+ exitContainers(continued);
317
+
318
+ // Fix positions.
319
+ index = indexBeforeExits;
320
+ while (index < self.events.length) {
321
+ self.events[index][1].end = {
322
+ ...point
323
+ };
324
+ index++;
325
+ }
326
+ const editMap = new EditMap();
327
+ // Inject the exits earlier (they’re still also at the end).
328
+ editMap.add(indexBeforeFlow + 1, 0, self.events.slice(indexBeforeExits));
329
+ // Discard the duplicate exits.
330
+ editMap.add(indexBeforeExits, index - indexBeforeExits, []);
331
+ editMap.consume(self.events);
332
+ }
333
+ }
334
+
335
+ /**
336
+ * @param {number} size
337
+ * Size.
338
+ * @returns {undefined}
339
+ * Nothing.
340
+ */
341
+ function exitContainers(size) {
342
+ let index = stack.length;
343
+
344
+ // Exit open containers.
345
+ while (index-- > size) {
346
+ const entry = stack[index];
347
+ self.containerState = entry[1];
348
+ entry[0].exit.call(self, effects);
349
+ }
350
+ stack.length = size;
351
+ }
352
+ function closeFlow() {
353
+ childFlow.write([null]);
354
+ childToken = undefined;
355
+ childFlow = undefined;
356
+ self.containerState._closeFlow = undefined;
357
+ }
358
+ }
359
+
360
+ /**
361
+ * @this {TokenizeContext}
362
+ * Context.
363
+ * @type {Tokenizer}
364
+ * Tokenizer.
365
+ */
366
+ function tokenizeContainer(effects, ok, nok) {
367
+ // Always populated by defaults.
368
+
369
+ return factorySpace(effects, effects.attempt(this.parser.constructs.document, ok, nok), "linePrefix", this.parser.constructs.disable.null.includes('codeIndented') ? undefined : 4);
370
+ }
@@ -0,0 +1,58 @@
1
+ /**
2
+ * @import {
3
+ * InitialConstruct,
4
+ * Initializer,
5
+ * State,
6
+ * TokenizeContext
7
+ * } from 'micromark-util-types'
8
+ */
9
+
10
+ import { blankLine, content } from 'micromark-core-commonmark';
11
+ import { factorySpace } from 'micromark-factory-space';
12
+ import { markdownLineEnding } from 'micromark-util-character';
13
+ /** @type {InitialConstruct} */
14
+ export const flow = {
15
+ tokenize: initializeFlow
16
+ };
17
+
18
+ /**
19
+ * @this {TokenizeContext}
20
+ * Self.
21
+ * @type {Initializer}
22
+ * Initializer.
23
+ */
24
+ function initializeFlow(effects) {
25
+ const self = this;
26
+ const initial = effects.attempt(
27
+ // Try to parse a blank line.
28
+ blankLine, atBlankEnding,
29
+ // Try to parse initial flow (essentially, only code).
30
+ effects.attempt(this.parser.constructs.flowInitial, afterConstruct, factorySpace(effects, effects.attempt(this.parser.constructs.flow, afterConstruct, effects.attempt(content, afterConstruct)), "linePrefix")));
31
+ return initial;
32
+
33
+ /** @type {State} */
34
+ function atBlankEnding(code) {
35
+ if (code === null) {
36
+ effects.consume(code);
37
+ return;
38
+ }
39
+ effects.enter("lineEndingBlank");
40
+ effects.consume(code);
41
+ effects.exit("lineEndingBlank");
42
+ self.currentConstruct = undefined;
43
+ return initial;
44
+ }
45
+
46
+ /** @type {State} */
47
+ function afterConstruct(code) {
48
+ if (code === null) {
49
+ effects.consume(code);
50
+ return;
51
+ }
52
+ effects.enter("lineEnding");
53
+ effects.consume(code);
54
+ effects.exit("lineEnding");
55
+ self.currentConstruct = undefined;
56
+ return initial;
57
+ }
58
+ }