@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,561 @@
1
+ /**
2
+ * @import {
3
+ * Construct,
4
+ * Event,
5
+ * Resolver,
6
+ * State,
7
+ * TokenizeContext,
8
+ * Tokenizer,
9
+ * Token
10
+ * } from 'micromark-util-types'
11
+ */
12
+
13
+ import { factoryDestination } from 'micromark-factory-destination';
14
+ import { factoryLabel } from 'micromark-factory-label';
15
+ import { factoryTitle } from 'micromark-factory-title';
16
+ import { factoryWhitespace } from 'micromark-factory-whitespace';
17
+ import { markdownLineEndingOrSpace } from 'micromark-util-character';
18
+ import { push, splice } from 'micromark-util-chunked';
19
+ import { normalizeIdentifier } from 'micromark-util-normalize-identifier';
20
+ import { resolveAll } from 'micromark-util-resolve-all';
21
+ /** @type {Construct} */
22
+ export const labelEnd = {
23
+ name: 'labelEnd',
24
+ resolveAll: resolveAllLabelEnd,
25
+ resolveTo: resolveToLabelEnd,
26
+ tokenize: tokenizeLabelEnd
27
+ };
28
+
29
+ /** @type {Construct} */
30
+ const resourceConstruct = {
31
+ tokenize: tokenizeResource
32
+ };
33
+ /** @type {Construct} */
34
+ const referenceFullConstruct = {
35
+ tokenize: tokenizeReferenceFull
36
+ };
37
+ /** @type {Construct} */
38
+ const referenceCollapsedConstruct = {
39
+ tokenize: tokenizeReferenceCollapsed
40
+ };
41
+
42
+ /** @type {Resolver} */
43
+ function resolveAllLabelEnd(events) {
44
+ let index = -1;
45
+ /** @type {Array<Event>} */
46
+ const newEvents = [];
47
+ while (++index < events.length) {
48
+ const token = events[index][1];
49
+ newEvents.push(events[index]);
50
+ if (token.type === "labelImage" || token.type === "labelLink" || token.type === "labelEnd") {
51
+ // Remove the marker.
52
+ const offset = token.type === "labelImage" ? 4 : 2;
53
+ token.type = "data";
54
+ index += offset;
55
+ }
56
+ }
57
+
58
+ // If the events are equal, we don't have to copy newEvents to events
59
+ if (events.length !== newEvents.length) {
60
+ splice(events, 0, events.length, newEvents);
61
+ }
62
+ return events;
63
+ }
64
+
65
+ /** @type {Resolver} */
66
+ function resolveToLabelEnd(events, context) {
67
+ let index = events.length;
68
+ let offset = 0;
69
+ /** @type {number | undefined} */
70
+ let open;
71
+ /** @type {number | undefined} */
72
+ let close;
73
+ /** @type {Array<Event>} */
74
+ let media;
75
+
76
+ // Find an opening.
77
+ while (index--) {
78
+ const token = events[index][1];
79
+ if (open) {
80
+ // If we see another link, or inactive link label, we’ve been here before.
81
+ if (token.type === "link" || token.type === "labelLink" && token._inactive) {
82
+ break;
83
+ }
84
+
85
+ // Mark other link openings as inactive, as we can’t have links in
86
+ // links.
87
+ if (events[index][0] === 'enter' && token.type === "labelLink") {
88
+ token._inactive = true;
89
+ }
90
+ } else if (close) {
91
+ if (events[index][0] === 'enter' && (token.type === "labelImage" || token.type === "labelLink") && !token._balanced) {
92
+ open = index;
93
+ if (token.type !== "labelLink") {
94
+ offset = 2;
95
+ break;
96
+ }
97
+ }
98
+ } else if (token.type === "labelEnd") {
99
+ close = index;
100
+ }
101
+ }
102
+ const group = {
103
+ type: events[open][1].type === "labelLink" ? "link" : "image",
104
+ start: {
105
+ ...events[open][1].start
106
+ },
107
+ end: {
108
+ ...events[events.length - 1][1].end
109
+ }
110
+ };
111
+ const label = {
112
+ type: "label",
113
+ start: {
114
+ ...events[open][1].start
115
+ },
116
+ end: {
117
+ ...events[close][1].end
118
+ }
119
+ };
120
+ const text = {
121
+ type: "labelText",
122
+ start: {
123
+ ...events[open + offset + 2][1].end
124
+ },
125
+ end: {
126
+ ...events[close - 2][1].start
127
+ }
128
+ };
129
+ media = [['enter', group, context], ['enter', label, context]];
130
+
131
+ // Opening marker.
132
+ media = push(media, events.slice(open + 1, open + offset + 3));
133
+
134
+ // Text open.
135
+ media = push(media, [['enter', text, context]]);
136
+
137
+ // Always populated by defaults.
138
+
139
+ // Between.
140
+ media = push(media, resolveAll(context.parser.constructs.insideSpan.null, events.slice(open + offset + 4, close - 3), context));
141
+
142
+ // Text close, marker close, label close.
143
+ media = push(media, [['exit', text, context], events[close - 2], events[close - 1], ['exit', label, context]]);
144
+
145
+ // Reference, resource, or so.
146
+ media = push(media, events.slice(close + 1));
147
+
148
+ // Media close.
149
+ media = push(media, [['exit', group, context]]);
150
+ splice(events, open, events.length, media);
151
+ return events;
152
+ }
153
+
154
+ /**
155
+ * @this {TokenizeContext}
156
+ * Context.
157
+ * @type {Tokenizer}
158
+ */
159
+ function tokenizeLabelEnd(effects, ok, nok) {
160
+ const self = this;
161
+ const labelStarts = self._labelStarts;
162
+ /** @type {Token} */
163
+ let labelStart;
164
+ /** @type {boolean} */
165
+ let defined;
166
+ if (labelStarts) {
167
+ // Discard ones that failed to close,
168
+ // they’re not needed anymore.
169
+ while (labelStarts.length > 0 && labelStarts[labelStarts.length - 1]._balanced) {
170
+ labelStarts.pop();
171
+ }
172
+ labelStart = labelStarts[labelStarts.length - 1];
173
+ }
174
+ return start;
175
+
176
+ /**
177
+ * Start of label end.
178
+ *
179
+ * ```markdown
180
+ * > | [a](b) c
181
+ * ^
182
+ * > | [a][b] c
183
+ * ^
184
+ * > | [a][] b
185
+ * ^
186
+ * > | [a] b
187
+ * ```
188
+ *
189
+ * @type {State}
190
+ */
191
+ function start(code) {
192
+ // If there is not an okay opening.
193
+ if (!labelStart) {
194
+ return nok(code);
195
+ }
196
+
197
+ // If the corresponding label (link) start is marked as inactive,
198
+ // it means we’d be wrapping a link, like this:
199
+ //
200
+ // ```markdown
201
+ // > | a [b [c](d) e](f) g.
202
+ // ^
203
+ // ```
204
+ //
205
+ // We can’t have that, so it’s just balanced brackets.
206
+ if (labelStart._inactive) {
207
+ return labelEndNok(code);
208
+ }
209
+ defined = self.parser.defined.includes(normalizeIdentifier(self.sliceSerialize({
210
+ start: labelStart.end,
211
+ end: self.now()
212
+ })));
213
+ effects.enter("labelEnd");
214
+ effects.enter("labelMarker");
215
+ effects.consume(code);
216
+ effects.exit("labelMarker");
217
+ effects.exit("labelEnd");
218
+ return after;
219
+ }
220
+
221
+ /**
222
+ * After `]`.
223
+ *
224
+ * ```markdown
225
+ * > | [a](b) c
226
+ * ^
227
+ * > | [a][b] c
228
+ * ^
229
+ * > | [a][] b
230
+ * ^
231
+ * > | [a] b
232
+ * ^
233
+ * ```
234
+ *
235
+ * @type {State}
236
+ */
237
+ function after(code) {
238
+ // Note: `markdown-rs` also parses GFM footnotes here, which for us is in
239
+ // an extension.
240
+
241
+ // Resource (`[asd](fgh)`)?
242
+ if (code === 40) {
243
+ return effects.attempt(resourceConstruct, labelEndOk, defined ? labelEndOk : labelEndNok)(code);
244
+ }
245
+
246
+ // Full (`[asd][fgh]`) or collapsed (`[asd][]`) reference?
247
+ if (code === 91) {
248
+ return effects.attempt(referenceFullConstruct, labelEndOk, defined ? referenceNotFull : labelEndNok)(code);
249
+ }
250
+
251
+ // Shortcut (`[asd]`) reference?
252
+ return defined ? labelEndOk(code) : labelEndNok(code);
253
+ }
254
+
255
+ /**
256
+ * After `]`, at `[`, but not at a full reference.
257
+ *
258
+ * > 👉 **Note**: we only get here if the label is defined.
259
+ *
260
+ * ```markdown
261
+ * > | [a][] b
262
+ * ^
263
+ * > | [a] b
264
+ * ^
265
+ * ```
266
+ *
267
+ * @type {State}
268
+ */
269
+ function referenceNotFull(code) {
270
+ return effects.attempt(referenceCollapsedConstruct, labelEndOk, labelEndNok)(code);
271
+ }
272
+
273
+ /**
274
+ * Done, we found something.
275
+ *
276
+ * ```markdown
277
+ * > | [a](b) c
278
+ * ^
279
+ * > | [a][b] c
280
+ * ^
281
+ * > | [a][] b
282
+ * ^
283
+ * > | [a] b
284
+ * ^
285
+ * ```
286
+ *
287
+ * @type {State}
288
+ */
289
+ function labelEndOk(code) {
290
+ // Note: `markdown-rs` does a bunch of stuff here.
291
+ // This label start is now used up: discard it so it’s never found again.
292
+
293
+ labelStarts.pop();
294
+ return ok(code);
295
+ }
296
+
297
+ /**
298
+ * Done, it’s nothing.
299
+ *
300
+ * There was an okay opening, but we didn’t match anything.
301
+ *
302
+ * ```markdown
303
+ * > | [a](b c
304
+ * ^
305
+ * > | [a][b c
306
+ * ^
307
+ * > | [a] b
308
+ * ^
309
+ * ```
310
+ *
311
+ * @type {State}
312
+ */
313
+ function labelEndNok(code) {
314
+ labelStart._balanced = true;
315
+ return nok(code);
316
+ }
317
+ }
318
+
319
+ /**
320
+ * @this {TokenizeContext}
321
+ * Context.
322
+ * @type {Tokenizer}
323
+ */
324
+ function tokenizeResource(effects, ok, nok) {
325
+ return resourceStart;
326
+
327
+ /**
328
+ * At a resource.
329
+ *
330
+ * ```markdown
331
+ * > | [a](b) c
332
+ * ^
333
+ * ```
334
+ *
335
+ * @type {State}
336
+ */
337
+ function resourceStart(code) {
338
+ effects.enter("resource");
339
+ effects.enter("resourceMarker");
340
+ effects.consume(code);
341
+ effects.exit("resourceMarker");
342
+ return resourceBefore;
343
+ }
344
+
345
+ /**
346
+ * In resource, after `(`, at optional whitespace.
347
+ *
348
+ * ```markdown
349
+ * > | [a](b) c
350
+ * ^
351
+ * ```
352
+ *
353
+ * @type {State}
354
+ */
355
+ function resourceBefore(code) {
356
+ return markdownLineEndingOrSpace(code) ? factoryWhitespace(effects, resourceOpen)(code) : resourceOpen(code);
357
+ }
358
+
359
+ /**
360
+ * In resource, after optional whitespace, at `)` or a destination.
361
+ *
362
+ * ```markdown
363
+ * > | [a](b) c
364
+ * ^
365
+ * ```
366
+ *
367
+ * @type {State}
368
+ */
369
+ function resourceOpen(code) {
370
+ if (code === 41) {
371
+ return resourceEnd(code);
372
+ }
373
+ return factoryDestination(effects, resourceDestinationAfter, resourceDestinationMissing, "resourceDestination", "resourceDestinationLiteral", "resourceDestinationLiteralMarker", "resourceDestinationRaw", "resourceDestinationString", 32)(code);
374
+ }
375
+
376
+ /**
377
+ * In resource, after destination, at optional whitespace.
378
+ *
379
+ * ```markdown
380
+ * > | [a](b) c
381
+ * ^
382
+ * ```
383
+ *
384
+ * @type {State}
385
+ */
386
+ function resourceDestinationAfter(code) {
387
+ return markdownLineEndingOrSpace(code) ? factoryWhitespace(effects, resourceBetween)(code) : resourceEnd(code);
388
+ }
389
+
390
+ /**
391
+ * At invalid destination.
392
+ *
393
+ * ```markdown
394
+ * > | [a](<<) b
395
+ * ^
396
+ * ```
397
+ *
398
+ * @type {State}
399
+ */
400
+ function resourceDestinationMissing(code) {
401
+ return nok(code);
402
+ }
403
+
404
+ /**
405
+ * In resource, after destination and whitespace, at `(` or title.
406
+ *
407
+ * ```markdown
408
+ * > | [a](b ) c
409
+ * ^
410
+ * ```
411
+ *
412
+ * @type {State}
413
+ */
414
+ function resourceBetween(code) {
415
+ if (code === 34 || code === 39 || code === 40) {
416
+ return factoryTitle(effects, resourceTitleAfter, nok, "resourceTitle", "resourceTitleMarker", "resourceTitleString")(code);
417
+ }
418
+ return resourceEnd(code);
419
+ }
420
+
421
+ /**
422
+ * In resource, after title, at optional whitespace.
423
+ *
424
+ * ```markdown
425
+ * > | [a](b "c") d
426
+ * ^
427
+ * ```
428
+ *
429
+ * @type {State}
430
+ */
431
+ function resourceTitleAfter(code) {
432
+ return markdownLineEndingOrSpace(code) ? factoryWhitespace(effects, resourceEnd)(code) : resourceEnd(code);
433
+ }
434
+
435
+ /**
436
+ * In resource, at `)`.
437
+ *
438
+ * ```markdown
439
+ * > | [a](b) d
440
+ * ^
441
+ * ```
442
+ *
443
+ * @type {State}
444
+ */
445
+ function resourceEnd(code) {
446
+ if (code === 41) {
447
+ effects.enter("resourceMarker");
448
+ effects.consume(code);
449
+ effects.exit("resourceMarker");
450
+ effects.exit("resource");
451
+ return ok;
452
+ }
453
+ return nok(code);
454
+ }
455
+ }
456
+
457
+ /**
458
+ * @this {TokenizeContext}
459
+ * Context.
460
+ * @type {Tokenizer}
461
+ */
462
+ function tokenizeReferenceFull(effects, ok, nok) {
463
+ const self = this;
464
+ return referenceFull;
465
+
466
+ /**
467
+ * In a reference (full), at the `[`.
468
+ *
469
+ * ```markdown
470
+ * > | [a][b] d
471
+ * ^
472
+ * ```
473
+ *
474
+ * @type {State}
475
+ */
476
+ function referenceFull(code) {
477
+ return factoryLabel.call(self, effects, referenceFullAfter, referenceFullMissing, "reference", "referenceMarker", "referenceString")(code);
478
+ }
479
+
480
+ /**
481
+ * In a reference (full), after `]`.
482
+ *
483
+ * ```markdown
484
+ * > | [a][b] d
485
+ * ^
486
+ * ```
487
+ *
488
+ * @type {State}
489
+ */
490
+ function referenceFullAfter(code) {
491
+ return self.parser.defined.includes(normalizeIdentifier(self.sliceSerialize(self.events[self.events.length - 1][1]).slice(1, -1))) ? ok(code) : nok(code);
492
+ }
493
+
494
+ /**
495
+ * In reference (full) that was missing.
496
+ *
497
+ * ```markdown
498
+ * > | [a][b d
499
+ * ^
500
+ * ```
501
+ *
502
+ * @type {State}
503
+ */
504
+ function referenceFullMissing(code) {
505
+ return nok(code);
506
+ }
507
+ }
508
+
509
+ /**
510
+ * @this {TokenizeContext}
511
+ * Context.
512
+ * @type {Tokenizer}
513
+ */
514
+ function tokenizeReferenceCollapsed(effects, ok, nok) {
515
+ return referenceCollapsedStart;
516
+
517
+ /**
518
+ * In reference (collapsed), at `[`.
519
+ *
520
+ * > 👉 **Note**: we only get here if the label is defined.
521
+ *
522
+ * ```markdown
523
+ * > | [a][] d
524
+ * ^
525
+ * ```
526
+ *
527
+ * @type {State}
528
+ */
529
+ function referenceCollapsedStart(code) {
530
+ // We only attempt a collapsed label if there’s a `[`.
531
+
532
+ effects.enter("reference");
533
+ effects.enter("referenceMarker");
534
+ effects.consume(code);
535
+ effects.exit("referenceMarker");
536
+ return referenceCollapsedOpen;
537
+ }
538
+
539
+ /**
540
+ * In reference (collapsed), at `]`.
541
+ *
542
+ * > 👉 **Note**: we only get here if the label is defined.
543
+ *
544
+ * ```markdown
545
+ * > | [a][] d
546
+ * ^
547
+ * ```
548
+ *
549
+ * @type {State}
550
+ */
551
+ function referenceCollapsedOpen(code) {
552
+ if (code === 93) {
553
+ effects.enter("referenceMarker");
554
+ effects.consume(code);
555
+ effects.exit("referenceMarker");
556
+ effects.exit("reference");
557
+ return ok;
558
+ }
559
+ return nok(code);
560
+ }
561
+ }
@@ -0,0 +1,110 @@
1
+ /**
2
+ * @import {
3
+ * Construct,
4
+ * State,
5
+ * Token,
6
+ * TokenizeContext,
7
+ * Tokenizer
8
+ * } from 'micromark-util-types'
9
+ */
10
+
11
+ import { labelEnd } from './label-end.js';
12
+
13
+ /** @type {Construct} */
14
+ export const labelStartImage = {
15
+ name: 'labelStartImage',
16
+ resolveAll: labelEnd.resolveAll,
17
+ tokenize: tokenizeLabelStartImage
18
+ };
19
+
20
+ /**
21
+ * @this {TokenizeContext}
22
+ * Context.
23
+ * @type {Tokenizer}
24
+ */
25
+ function tokenizeLabelStartImage(effects, ok, nok) {
26
+ const self = this;
27
+ /** @type {Token} */
28
+ let labelImage;
29
+ return start;
30
+
31
+ /**
32
+ * Start of label (image) start.
33
+ *
34
+ * ```markdown
35
+ * > | a ![b] c
36
+ * ^
37
+ * ```
38
+ *
39
+ * @type {State}
40
+ */
41
+ function start(code) {
42
+ effects.enter("labelImage");
43
+ effects.enter("labelImageMarker");
44
+ effects.consume(code);
45
+ effects.exit("labelImageMarker");
46
+ return open;
47
+ }
48
+
49
+ /**
50
+ * After `!`, at `[`.
51
+ *
52
+ * ```markdown
53
+ * > | a ![b] c
54
+ * ^
55
+ * ```
56
+ *
57
+ * @type {State}
58
+ */
59
+ function open(code) {
60
+ if (code === 91) {
61
+ effects.enter("labelMarker");
62
+ effects.consume(code);
63
+ effects.exit("labelMarker");
64
+ labelImage = effects.exit("labelImage");
65
+ return after;
66
+ }
67
+ return nok(code);
68
+ }
69
+
70
+ /**
71
+ * After `![`.
72
+ *
73
+ * ```markdown
74
+ * > | a ![b] c
75
+ * ^
76
+ * ```
77
+ *
78
+ * This is needed in because, when GFM footnotes are enabled, images never
79
+ * form when started with a `^`.
80
+ * Instead, links form:
81
+ *
82
+ * ```markdown
83
+ * ![^a](b)
84
+ *
85
+ * ![^a][b]
86
+ *
87
+ * [b]: c
88
+ * ```
89
+ *
90
+ * ```html
91
+ * <p>!<a href=\"b\">^a</a></p>
92
+ * <p>!<a href=\"c\">^a</a></p>
93
+ * ```
94
+ *
95
+ * @type {State}
96
+ */
97
+ function after(code) {
98
+ // To do: use a new field to do this, this is still needed for
99
+ // `micromark-extension-gfm-footnote`, but the `label-start-link`
100
+ // behavior isn’t.
101
+ // Hidden footnotes hook.
102
+ /* c8 ignore next 6 */
103
+ if (code === 94 && '_hiddenFootnoteSupport' in self.parser.constructs) {
104
+ return nok(code);
105
+ }
106
+ self._labelStarts = self._labelStarts || [];
107
+ self._labelStarts.push(labelImage);
108
+ return ok(code);
109
+ }
110
+ }
@@ -0,0 +1,63 @@
1
+ /**
2
+ * @import {
3
+ * Construct,
4
+ * State,
5
+ * Token,
6
+ * TokenizeContext,
7
+ * Tokenizer
8
+ * } from 'micromark-util-types'
9
+ */
10
+
11
+ import { labelEnd } from './label-end.js';
12
+
13
+ /** @type {Construct} */
14
+ export const labelStartLink = {
15
+ name: 'labelStartLink',
16
+ resolveAll: labelEnd.resolveAll,
17
+ tokenize: tokenizeLabelStartLink
18
+ };
19
+
20
+ /**
21
+ * @this {TokenizeContext}
22
+ * Context.
23
+ * @type {Tokenizer}
24
+ */
25
+ function tokenizeLabelStartLink(effects, ok, nok) {
26
+ const self = this;
27
+ /** @type {Token} */
28
+ let labelLink;
29
+ return start;
30
+
31
+ /**
32
+ * Start of label (link) start.
33
+ *
34
+ * ```markdown
35
+ * > | a [b] c
36
+ * ^
37
+ * ```
38
+ *
39
+ * @type {State}
40
+ */
41
+ function start(code) {
42
+ effects.enter("labelLink");
43
+ effects.enter("labelMarker");
44
+ effects.consume(code);
45
+ effects.exit("labelMarker");
46
+ labelLink = effects.exit("labelLink");
47
+ return after;
48
+ }
49
+
50
+ /** @type {State} */
51
+ function after(code) {
52
+ // To do: this isn’t needed in `micromark-extension-gfm-footnote`,
53
+ // remove.
54
+ // Hidden footnotes hook.
55
+ /* c8 ignore next 6 */
56
+ if (code === 94 && '_hiddenFootnoteSupport' in self.parser.constructs) {
57
+ return nok(code);
58
+ }
59
+ self._labelStarts = self._labelStarts || [];
60
+ self._labelStarts.push(labelLink);
61
+ return ok(code);
62
+ }
63
+ }