@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,846 @@
1
+ /**
2
+ * @import {Event, Extension, Point, Resolver, State, Token, TokenizeContext, Tokenizer} from 'micromark-util-types'
3
+ */
4
+
5
+ /**
6
+ * @typedef {[number, number, number, number]} Range
7
+ * Cell info.
8
+ *
9
+ * @typedef {0 | 1 | 2 | 3} RowKind
10
+ * Where we are: `1` for head row, `2` for delimiter row, `3` for body row.
11
+ */
12
+
13
+ import { factorySpace } from 'micromark-factory-space';
14
+ import { markdownLineEnding, markdownLineEndingOrSpace, markdownSpace } from 'micromark-util-character';
15
+ import { EditMap } from './edit-map.js';
16
+ import { gfmTableAlign } from './infer.js';
17
+
18
+ /**
19
+ * Create an HTML extension for `micromark` to support GitHub tables syntax.
20
+ *
21
+ * @returns {Extension}
22
+ * Extension for `micromark` that can be passed in `extensions` to enable GFM
23
+ * table syntax.
24
+ */
25
+ export function gfmTable() {
26
+ return {
27
+ flow: {
28
+ null: {
29
+ name: 'table',
30
+ tokenize: tokenizeTable,
31
+ resolveAll: resolveTable
32
+ }
33
+ }
34
+ };
35
+ }
36
+
37
+ /**
38
+ * Tokenizer for GFM tables.
39
+ *
40
+ * @this {TokenizeContext}
41
+ * @type {Tokenizer}
42
+ */
43
+ function tokenizeTable(effects, ok, nok) {
44
+ const self = this;
45
+ let size = 0;
46
+ let sizeB = 0;
47
+ /** @type {boolean | undefined} */
48
+ let seen;
49
+ return start;
50
+
51
+ /**
52
+ * Start of a GFM table.
53
+ *
54
+ * If there is a valid table row or table head before, then we try to parse
55
+ * another row.
56
+ * Otherwise, we try to parse a head.
57
+ *
58
+ * ```markdown
59
+ * > | | a |
60
+ * ^
61
+ * | | - |
62
+ * > | | b |
63
+ * ^
64
+ * ```
65
+ * @type {State}
66
+ */
67
+ function start(code) {
68
+ let index = self.events.length - 1;
69
+ while (index > -1) {
70
+ const {
71
+ type
72
+ } = self.events[index][1];
73
+ if (type === "lineEnding" ||
74
+ // Note: markdown-rs uses `whitespace` instead of `linePrefix`
75
+ type === "linePrefix") {
76
+ index--;
77
+ } else {
78
+ break;
79
+ }
80
+ }
81
+ const tail = index > -1 ? self.events[index][1].type : null;
82
+ const next = tail === 'tableHead' || tail === 'tableRow' ? bodyRowStart : headRowBefore;
83
+
84
+ // Don’t allow lazy body rows.
85
+ if (next === bodyRowStart && self.parser.lazy[self.now().line]) {
86
+ return nok(code);
87
+ }
88
+ return next(code);
89
+ }
90
+
91
+ /**
92
+ * Before table head row.
93
+ *
94
+ * ```markdown
95
+ * > | | a |
96
+ * ^
97
+ * | | - |
98
+ * | | b |
99
+ * ```
100
+ *
101
+ * @type {State}
102
+ */
103
+ function headRowBefore(code) {
104
+ effects.enter('tableHead');
105
+ effects.enter('tableRow');
106
+ return headRowStart(code);
107
+ }
108
+
109
+ /**
110
+ * Before table head row, after whitespace.
111
+ *
112
+ * ```markdown
113
+ * > | | a |
114
+ * ^
115
+ * | | - |
116
+ * | | b |
117
+ * ```
118
+ *
119
+ * @type {State}
120
+ */
121
+ function headRowStart(code) {
122
+ if (code === 124) {
123
+ return headRowBreak(code);
124
+ }
125
+
126
+ // To do: micromark-js should let us parse our own whitespace in extensions,
127
+ // like `markdown-rs`:
128
+ //
129
+ // ```js
130
+ // // 4+ spaces.
131
+ // if (markdownSpace(code)) {
132
+ // return nok(code)
133
+ // }
134
+ // ```
135
+
136
+ seen = true;
137
+ // Count the first character, that isn’t a pipe, double.
138
+ sizeB += 1;
139
+ return headRowBreak(code);
140
+ }
141
+
142
+ /**
143
+ * At break in table head row.
144
+ *
145
+ * ```markdown
146
+ * > | | a |
147
+ * ^
148
+ * ^
149
+ * ^
150
+ * | | - |
151
+ * | | b |
152
+ * ```
153
+ *
154
+ * @type {State}
155
+ */
156
+ function headRowBreak(code) {
157
+ if (code === null) {
158
+ // Note: in `markdown-rs`, we need to reset, in `micromark-js` we don‘t.
159
+ return nok(code);
160
+ }
161
+ if (markdownLineEnding(code)) {
162
+ // If anything other than one pipe (ignoring whitespace) was used, it’s fine.
163
+ if (sizeB > 1) {
164
+ sizeB = 0;
165
+ // To do: check if this works.
166
+ // Feel free to interrupt:
167
+ self.interrupt = true;
168
+ effects.exit('tableRow');
169
+ effects.enter("lineEnding");
170
+ effects.consume(code);
171
+ effects.exit("lineEnding");
172
+ return headDelimiterStart;
173
+ }
174
+
175
+ // Note: in `markdown-rs`, we need to reset, in `micromark-js` we don‘t.
176
+ return nok(code);
177
+ }
178
+ if (markdownSpace(code)) {
179
+ // To do: check if this is fine.
180
+ // effects.attempt(State::Next(StateName::GfmTableHeadRowBreak), State::Nok)
181
+ // State::Retry(space_or_tab(tokenizer))
182
+ return factorySpace(effects, headRowBreak, "whitespace")(code);
183
+ }
184
+ sizeB += 1;
185
+ if (seen) {
186
+ seen = false;
187
+ // Header cell count.
188
+ size += 1;
189
+ }
190
+ if (code === 124) {
191
+ effects.enter('tableCellDivider');
192
+ effects.consume(code);
193
+ effects.exit('tableCellDivider');
194
+ // Whether a delimiter was seen.
195
+ seen = true;
196
+ return headRowBreak;
197
+ }
198
+
199
+ // Anything else is cell data.
200
+ effects.enter("data");
201
+ return headRowData(code);
202
+ }
203
+
204
+ /**
205
+ * In table head row data.
206
+ *
207
+ * ```markdown
208
+ * > | | a |
209
+ * ^
210
+ * | | - |
211
+ * | | b |
212
+ * ```
213
+ *
214
+ * @type {State}
215
+ */
216
+ function headRowData(code) {
217
+ if (code === null || code === 124 || markdownLineEndingOrSpace(code)) {
218
+ effects.exit("data");
219
+ return headRowBreak(code);
220
+ }
221
+ effects.consume(code);
222
+ return code === 92 ? headRowEscape : headRowData;
223
+ }
224
+
225
+ /**
226
+ * In table head row escape.
227
+ *
228
+ * ```markdown
229
+ * > | | a\-b |
230
+ * ^
231
+ * | | ---- |
232
+ * | | c |
233
+ * ```
234
+ *
235
+ * @type {State}
236
+ */
237
+ function headRowEscape(code) {
238
+ if (code === 92 || code === 124) {
239
+ effects.consume(code);
240
+ return headRowData;
241
+ }
242
+ return headRowData(code);
243
+ }
244
+
245
+ /**
246
+ * Before delimiter row.
247
+ *
248
+ * ```markdown
249
+ * | | a |
250
+ * > | | - |
251
+ * ^
252
+ * | | b |
253
+ * ```
254
+ *
255
+ * @type {State}
256
+ */
257
+ function headDelimiterStart(code) {
258
+ // Reset `interrupt`.
259
+ self.interrupt = false;
260
+
261
+ // Note: in `markdown-rs`, we need to handle piercing here too.
262
+ if (self.parser.lazy[self.now().line]) {
263
+ return nok(code);
264
+ }
265
+ effects.enter('tableDelimiterRow');
266
+ // Track if we’ve seen a `:` or `|`.
267
+ seen = false;
268
+ if (markdownSpace(code)) {
269
+ return factorySpace(effects, headDelimiterBefore, "linePrefix", self.parser.constructs.disable.null.includes('codeIndented') ? undefined : 4)(code);
270
+ }
271
+ return headDelimiterBefore(code);
272
+ }
273
+
274
+ /**
275
+ * Before delimiter row, after optional whitespace.
276
+ *
277
+ * Reused when a `|` is found later, to parse another cell.
278
+ *
279
+ * ```markdown
280
+ * | | a |
281
+ * > | | - |
282
+ * ^
283
+ * | | b |
284
+ * ```
285
+ *
286
+ * @type {State}
287
+ */
288
+ function headDelimiterBefore(code) {
289
+ if (code === 45 || code === 58) {
290
+ return headDelimiterValueBefore(code);
291
+ }
292
+ if (code === 124) {
293
+ seen = true;
294
+ // If we start with a pipe, we open a cell marker.
295
+ effects.enter('tableCellDivider');
296
+ effects.consume(code);
297
+ effects.exit('tableCellDivider');
298
+ return headDelimiterCellBefore;
299
+ }
300
+
301
+ // More whitespace / empty row not allowed at start.
302
+ return headDelimiterNok(code);
303
+ }
304
+
305
+ /**
306
+ * After `|`, before delimiter cell.
307
+ *
308
+ * ```markdown
309
+ * | | a |
310
+ * > | | - |
311
+ * ^
312
+ * ```
313
+ *
314
+ * @type {State}
315
+ */
316
+ function headDelimiterCellBefore(code) {
317
+ if (markdownSpace(code)) {
318
+ return factorySpace(effects, headDelimiterValueBefore, "whitespace")(code);
319
+ }
320
+ return headDelimiterValueBefore(code);
321
+ }
322
+
323
+ /**
324
+ * Before delimiter cell value.
325
+ *
326
+ * ```markdown
327
+ * | | a |
328
+ * > | | - |
329
+ * ^
330
+ * ```
331
+ *
332
+ * @type {State}
333
+ */
334
+ function headDelimiterValueBefore(code) {
335
+ // Align: left.
336
+ if (code === 58) {
337
+ sizeB += 1;
338
+ seen = true;
339
+ effects.enter('tableDelimiterMarker');
340
+ effects.consume(code);
341
+ effects.exit('tableDelimiterMarker');
342
+ return headDelimiterLeftAlignmentAfter;
343
+ }
344
+
345
+ // Align: none.
346
+ if (code === 45) {
347
+ sizeB += 1;
348
+ // To do: seems weird that this *isn’t* left aligned, but that state is used?
349
+ return headDelimiterLeftAlignmentAfter(code);
350
+ }
351
+ if (code === null || markdownLineEnding(code)) {
352
+ return headDelimiterCellAfter(code);
353
+ }
354
+ return headDelimiterNok(code);
355
+ }
356
+
357
+ /**
358
+ * After delimiter cell left alignment marker.
359
+ *
360
+ * ```markdown
361
+ * | | a |
362
+ * > | | :- |
363
+ * ^
364
+ * ```
365
+ *
366
+ * @type {State}
367
+ */
368
+ function headDelimiterLeftAlignmentAfter(code) {
369
+ if (code === 45) {
370
+ effects.enter('tableDelimiterFiller');
371
+ return headDelimiterFiller(code);
372
+ }
373
+
374
+ // Anything else is not ok after the left-align colon.
375
+ return headDelimiterNok(code);
376
+ }
377
+
378
+ /**
379
+ * In delimiter cell filler.
380
+ *
381
+ * ```markdown
382
+ * | | a |
383
+ * > | | - |
384
+ * ^
385
+ * ```
386
+ *
387
+ * @type {State}
388
+ */
389
+ function headDelimiterFiller(code) {
390
+ if (code === 45) {
391
+ effects.consume(code);
392
+ return headDelimiterFiller;
393
+ }
394
+
395
+ // Align is `center` if it was `left`, `right` otherwise.
396
+ if (code === 58) {
397
+ seen = true;
398
+ effects.exit('tableDelimiterFiller');
399
+ effects.enter('tableDelimiterMarker');
400
+ effects.consume(code);
401
+ effects.exit('tableDelimiterMarker');
402
+ return headDelimiterRightAlignmentAfter;
403
+ }
404
+ effects.exit('tableDelimiterFiller');
405
+ return headDelimiterRightAlignmentAfter(code);
406
+ }
407
+
408
+ /**
409
+ * After delimiter cell right alignment marker.
410
+ *
411
+ * ```markdown
412
+ * | | a |
413
+ * > | | -: |
414
+ * ^
415
+ * ```
416
+ *
417
+ * @type {State}
418
+ */
419
+ function headDelimiterRightAlignmentAfter(code) {
420
+ if (markdownSpace(code)) {
421
+ return factorySpace(effects, headDelimiterCellAfter, "whitespace")(code);
422
+ }
423
+ return headDelimiterCellAfter(code);
424
+ }
425
+
426
+ /**
427
+ * After delimiter cell.
428
+ *
429
+ * ```markdown
430
+ * | | a |
431
+ * > | | -: |
432
+ * ^
433
+ * ```
434
+ *
435
+ * @type {State}
436
+ */
437
+ function headDelimiterCellAfter(code) {
438
+ if (code === 124) {
439
+ return headDelimiterBefore(code);
440
+ }
441
+ if (code === null || markdownLineEnding(code)) {
442
+ // Exit when:
443
+ // * there was no `:` or `|` at all (it’s a thematic break or setext
444
+ // underline instead)
445
+ // * the header cell count is not the delimiter cell count
446
+ if (!seen || size !== sizeB) {
447
+ return headDelimiterNok(code);
448
+ }
449
+
450
+ // Note: in markdown-rs`, a reset is needed here.
451
+ effects.exit('tableDelimiterRow');
452
+ effects.exit('tableHead');
453
+ // To do: in `markdown-rs`, resolvers need to be registered manually.
454
+ // effects.register_resolver(ResolveName::GfmTable)
455
+ return ok(code);
456
+ }
457
+ return headDelimiterNok(code);
458
+ }
459
+
460
+ /**
461
+ * In delimiter row, at a disallowed byte.
462
+ *
463
+ * ```markdown
464
+ * | | a |
465
+ * > | | x |
466
+ * ^
467
+ * ```
468
+ *
469
+ * @type {State}
470
+ */
471
+ function headDelimiterNok(code) {
472
+ // Note: in `markdown-rs`, we need to reset, in `micromark-js` we don‘t.
473
+ return nok(code);
474
+ }
475
+
476
+ /**
477
+ * Before table body row.
478
+ *
479
+ * ```markdown
480
+ * | | a |
481
+ * | | - |
482
+ * > | | b |
483
+ * ^
484
+ * ```
485
+ *
486
+ * @type {State}
487
+ */
488
+ function bodyRowStart(code) {
489
+ // Note: in `markdown-rs` we need to manually take care of a prefix,
490
+ // but in `micromark-js` that is done for us, so if we’re here, we’re
491
+ // never at whitespace.
492
+ effects.enter('tableRow');
493
+ return bodyRowBreak(code);
494
+ }
495
+
496
+ /**
497
+ * At break in table body row.
498
+ *
499
+ * ```markdown
500
+ * | | a |
501
+ * | | - |
502
+ * > | | b |
503
+ * ^
504
+ * ^
505
+ * ^
506
+ * ```
507
+ *
508
+ * @type {State}
509
+ */
510
+ function bodyRowBreak(code) {
511
+ if (code === 124) {
512
+ effects.enter('tableCellDivider');
513
+ effects.consume(code);
514
+ effects.exit('tableCellDivider');
515
+ return bodyRowBreak;
516
+ }
517
+ if (code === null || markdownLineEnding(code)) {
518
+ effects.exit('tableRow');
519
+ return ok(code);
520
+ }
521
+ if (markdownSpace(code)) {
522
+ return factorySpace(effects, bodyRowBreak, "whitespace")(code);
523
+ }
524
+
525
+ // Anything else is cell content.
526
+ effects.enter("data");
527
+ return bodyRowData(code);
528
+ }
529
+
530
+ /**
531
+ * In table body row data.
532
+ *
533
+ * ```markdown
534
+ * | | a |
535
+ * | | - |
536
+ * > | | b |
537
+ * ^
538
+ * ```
539
+ *
540
+ * @type {State}
541
+ */
542
+ function bodyRowData(code) {
543
+ if (code === null || code === 124 || markdownLineEndingOrSpace(code)) {
544
+ effects.exit("data");
545
+ return bodyRowBreak(code);
546
+ }
547
+ effects.consume(code);
548
+ return code === 92 ? bodyRowEscape : bodyRowData;
549
+ }
550
+
551
+ /**
552
+ * In table body row escape.
553
+ *
554
+ * ```markdown
555
+ * | | a |
556
+ * | | ---- |
557
+ * > | | b\-c |
558
+ * ^
559
+ * ```
560
+ *
561
+ * @type {State}
562
+ */
563
+ function bodyRowEscape(code) {
564
+ if (code === 92 || code === 124) {
565
+ effects.consume(code);
566
+ return bodyRowData;
567
+ }
568
+ return bodyRowData(code);
569
+ }
570
+ }
571
+
572
+ /** @type {Resolver} */
573
+
574
+ function resolveTable(events, context) {
575
+ let index = -1;
576
+ let inFirstCellAwaitingPipe = true;
577
+ /** @type {RowKind} */
578
+ let rowKind = 0;
579
+ /** @type {Range} */
580
+ let lastCell = [0, 0, 0, 0];
581
+ /** @type {Range} */
582
+ let cell = [0, 0, 0, 0];
583
+ let afterHeadAwaitingFirstBodyRow = false;
584
+ let lastTableEnd = 0;
585
+ /** @type {Token | undefined} */
586
+ let currentTable;
587
+ /** @type {Token | undefined} */
588
+ let currentBody;
589
+ /** @type {Token | undefined} */
590
+ let currentCell;
591
+ const map = new EditMap();
592
+ while (++index < events.length) {
593
+ const event = events[index];
594
+ const token = event[1];
595
+ if (event[0] === 'enter') {
596
+ // Start of head.
597
+ if (token.type === 'tableHead') {
598
+ afterHeadAwaitingFirstBodyRow = false;
599
+
600
+ // Inject previous (body end and) table end.
601
+ if (lastTableEnd !== 0) {
602
+ flushTableEnd(map, context, lastTableEnd, currentTable, currentBody);
603
+ currentBody = undefined;
604
+ lastTableEnd = 0;
605
+ }
606
+
607
+ // Inject table start.
608
+ currentTable = {
609
+ type: 'table',
610
+ start: Object.assign({}, token.start),
611
+ // Note: correct end is set later.
612
+ end: Object.assign({}, token.end)
613
+ };
614
+ map.add(index, 0, [['enter', currentTable, context]]);
615
+ } else if (token.type === 'tableRow' || token.type === 'tableDelimiterRow') {
616
+ inFirstCellAwaitingPipe = true;
617
+ currentCell = undefined;
618
+ lastCell = [0, 0, 0, 0];
619
+ cell = [0, index + 1, 0, 0];
620
+
621
+ // Inject table body start.
622
+ if (afterHeadAwaitingFirstBodyRow) {
623
+ afterHeadAwaitingFirstBodyRow = false;
624
+ currentBody = {
625
+ type: 'tableBody',
626
+ start: Object.assign({}, token.start),
627
+ // Note: correct end is set later.
628
+ end: Object.assign({}, token.end)
629
+ };
630
+ map.add(index, 0, [['enter', currentBody, context]]);
631
+ }
632
+ rowKind = token.type === 'tableDelimiterRow' ? 2 : currentBody ? 3 : 1;
633
+ }
634
+ // Cell data.
635
+ else if (rowKind && (token.type === "data" || token.type === 'tableDelimiterMarker' || token.type === 'tableDelimiterFiller')) {
636
+ inFirstCellAwaitingPipe = false;
637
+
638
+ // First value in cell.
639
+ if (cell[2] === 0) {
640
+ if (lastCell[1] !== 0) {
641
+ cell[0] = cell[1];
642
+ currentCell = flushCell(map, context, lastCell, rowKind, undefined, currentCell);
643
+ lastCell = [0, 0, 0, 0];
644
+ }
645
+ cell[2] = index;
646
+ }
647
+ } else if (token.type === 'tableCellDivider') {
648
+ if (inFirstCellAwaitingPipe) {
649
+ inFirstCellAwaitingPipe = false;
650
+ } else {
651
+ if (lastCell[1] !== 0) {
652
+ cell[0] = cell[1];
653
+ currentCell = flushCell(map, context, lastCell, rowKind, undefined, currentCell);
654
+ }
655
+ lastCell = cell;
656
+ cell = [lastCell[1], index, 0, 0];
657
+ }
658
+ }
659
+ }
660
+ // Exit events.
661
+ else if (token.type === 'tableHead') {
662
+ afterHeadAwaitingFirstBodyRow = true;
663
+ lastTableEnd = index;
664
+ } else if (token.type === 'tableRow' || token.type === 'tableDelimiterRow') {
665
+ lastTableEnd = index;
666
+ if (lastCell[1] !== 0) {
667
+ cell[0] = cell[1];
668
+ currentCell = flushCell(map, context, lastCell, rowKind, index, currentCell);
669
+ } else if (cell[1] !== 0) {
670
+ currentCell = flushCell(map, context, cell, rowKind, index, currentCell);
671
+ }
672
+ rowKind = 0;
673
+ } else if (rowKind && (token.type === "data" || token.type === 'tableDelimiterMarker' || token.type === 'tableDelimiterFiller')) {
674
+ cell[3] = index;
675
+ }
676
+ }
677
+ if (lastTableEnd !== 0) {
678
+ flushTableEnd(map, context, lastTableEnd, currentTable, currentBody);
679
+ }
680
+ map.consume(context.events);
681
+
682
+ // To do: move this into `html`, when events are exposed there.
683
+ // That’s what `markdown-rs` does.
684
+ // That needs updates to `mdast-util-gfm-table`.
685
+ index = -1;
686
+ while (++index < context.events.length) {
687
+ const event = context.events[index];
688
+ if (event[0] === 'enter' && event[1].type === 'table') {
689
+ event[1]._align = gfmTableAlign(context.events, index);
690
+ }
691
+ }
692
+ return events;
693
+ }
694
+
695
+ /**
696
+ * Generate a cell.
697
+ *
698
+ * @param {EditMap} map
699
+ * Edit map to apply to.
700
+ * @param {Readonly<TokenizeContext>} context
701
+ * Tokenize context.
702
+ * @param {Readonly<Range>} range
703
+ * Range of the cell within events.
704
+ * @param {RowKind} rowKind
705
+ * Type of row.
706
+ * @param {number | undefined} rowEnd
707
+ * Index of the end of the row, if known.
708
+ * @param {Token | undefined} previousCell
709
+ * Previous cell token, if any.
710
+ * @returns {Token | undefined}
711
+ * Current cell token after flushing, if any.
712
+ */
713
+ // eslint-disable-next-line max-params
714
+ function flushCell(map, context, range, rowKind, rowEnd, previousCell) {
715
+ // `markdown-rs` uses:
716
+ // rowKind === 2 ? 'tableDelimiterCell' : 'tableCell'
717
+ const groupName = rowKind === 1 ? 'tableHeader' : rowKind === 2 ? 'tableDelimiter' : 'tableData';
718
+ // `markdown-rs` uses:
719
+ // rowKind === 2 ? 'tableDelimiterCellValue' : 'tableCellText'
720
+ const valueName = 'tableContent';
721
+
722
+ // Insert an exit for the previous cell, if there is one.
723
+ //
724
+ // ```markdown
725
+ // > | | aa | bb | cc |
726
+ // ^-- exit
727
+ // ^^^^-- this cell
728
+ // ```
729
+ if (range[0] !== 0) {
730
+ previousCell.end = Object.assign({}, getPoint(context.events, range[0]));
731
+ map.add(range[0], 0, [['exit', previousCell, context]]);
732
+ }
733
+
734
+ // Insert enter of this cell.
735
+ //
736
+ // ```markdown
737
+ // > | | aa | bb | cc |
738
+ // ^-- enter
739
+ // ^^^^-- this cell
740
+ // ```
741
+ const now = getPoint(context.events, range[1]);
742
+ previousCell = {
743
+ type: groupName,
744
+ start: Object.assign({}, now),
745
+ // Note: correct end is set later.
746
+ end: Object.assign({}, now)
747
+ };
748
+ map.add(range[1], 0, [['enter', previousCell, context]]);
749
+
750
+ // Insert text start at first data start and end at last data end, and
751
+ // remove events between.
752
+ //
753
+ // ```markdown
754
+ // > | | aa | bb | cc |
755
+ // ^-- enter
756
+ // ^-- exit
757
+ // ^^^^-- this cell
758
+ // ```
759
+ if (range[2] !== 0) {
760
+ const relatedStart = getPoint(context.events, range[2]);
761
+ const relatedEnd = getPoint(context.events, range[3]);
762
+ /** @type {Token} */
763
+ const valueToken = {
764
+ type: valueName,
765
+ start: Object.assign({}, relatedStart),
766
+ end: Object.assign({}, relatedEnd)
767
+ };
768
+ map.add(range[2], 0, [['enter', valueToken, context]]);
769
+ if (rowKind !== 2) {
770
+ // Fix positional info on remaining events
771
+ const start = context.events[range[2]];
772
+ const end = context.events[range[3]];
773
+ start[1].end = Object.assign({}, end[1].end);
774
+ start[1].type = "chunkText";
775
+ start[1].contentType = "text";
776
+
777
+ // Remove if needed.
778
+ if (range[3] > range[2] + 1) {
779
+ const a = range[2] + 1;
780
+ const b = range[3] - range[2] - 1;
781
+ map.add(a, b, []);
782
+ }
783
+ }
784
+ map.add(range[3] + 1, 0, [['exit', valueToken, context]]);
785
+ }
786
+
787
+ // Insert an exit for the last cell, if at the row end.
788
+ //
789
+ // ```markdown
790
+ // > | | aa | bb | cc |
791
+ // ^-- exit
792
+ // ^^^^^^-- this cell (the last one contains two “between” parts)
793
+ // ```
794
+ if (rowEnd !== undefined) {
795
+ previousCell.end = Object.assign({}, getPoint(context.events, rowEnd));
796
+ map.add(rowEnd, 0, [['exit', previousCell, context]]);
797
+ previousCell = undefined;
798
+ }
799
+ return previousCell;
800
+ }
801
+
802
+ /**
803
+ * Generate table end (and table body end).
804
+ *
805
+ * @param {Readonly<EditMap>} map
806
+ * Edit map to apply to.
807
+ * @param {Readonly<TokenizeContext>} context
808
+ * Tokenize context.
809
+ * @param {number} index
810
+ * Index within the events where the table end should be inserted.
811
+ * @param {Token} table
812
+ * Table token.
813
+ * @param {Token | undefined} tableBody
814
+ * Table body token, if any.
815
+ * @returns {undefined}
816
+ * Nothing.
817
+ */
818
+ // eslint-disable-next-line max-params
819
+ function flushTableEnd(map, context, index, table, tableBody) {
820
+ /** @type {Array<Event>} */
821
+ const exits = [];
822
+ const related = getPoint(context.events, index);
823
+ if (tableBody) {
824
+ tableBody.end = Object.assign({}, related);
825
+ exits.push(['exit', tableBody, context]);
826
+ }
827
+ table.end = Object.assign({}, related);
828
+ exits.push(['exit', table, context]);
829
+ map.add(index + 1, 0, exits);
830
+ }
831
+
832
+ /**
833
+ * Get the point (start or end) for a given event in the list of events.
834
+ *
835
+ * @param {Readonly<Array<Event>>} events
836
+ * List of events.
837
+ * @param {number} index
838
+ * Index of the event to get the point for.
839
+ * @returns {Readonly<Point>}
840
+ * Start point for enter and end point for exit.
841
+ */
842
+ function getPoint(events, index) {
843
+ const event = events[index];
844
+ const side = event[0] === 'enter' ? 'start' : 'end';
845
+ return event[1][side];
846
+ }