@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,64 @@
1
+ /**
2
+ * @import {
3
+ * Construct,
4
+ * State,
5
+ * TokenizeContext,
6
+ * Tokenizer
7
+ * } from 'micromark-util-types'
8
+ */
9
+
10
+ import { asciiPunctuation } from 'micromark-util-character';
11
+ /** @type {Construct} */
12
+ export const characterEscape = {
13
+ name: 'characterEscape',
14
+ tokenize: tokenizeCharacterEscape
15
+ };
16
+
17
+ /**
18
+ * @this {TokenizeContext}
19
+ * Context.
20
+ * @type {Tokenizer}
21
+ */
22
+ function tokenizeCharacterEscape(effects, ok, nok) {
23
+ return start;
24
+
25
+ /**
26
+ * Start of character escape.
27
+ *
28
+ * ```markdown
29
+ * > | a\*b
30
+ * ^
31
+ * ```
32
+ *
33
+ * @type {State}
34
+ */
35
+ function start(code) {
36
+ effects.enter("characterEscape");
37
+ effects.enter("escapeMarker");
38
+ effects.consume(code);
39
+ effects.exit("escapeMarker");
40
+ return inside;
41
+ }
42
+
43
+ /**
44
+ * After `\`, at punctuation.
45
+ *
46
+ * ```markdown
47
+ * > | a\*b
48
+ * ^
49
+ * ```
50
+ *
51
+ * @type {State}
52
+ */
53
+ function inside(code) {
54
+ // ASCII punctuation.
55
+ if (asciiPunctuation(code)) {
56
+ effects.enter("characterEscapeValue");
57
+ effects.consume(code);
58
+ effects.exit("characterEscapeValue");
59
+ effects.exit("characterEscape");
60
+ return ok;
61
+ }
62
+ return nok(code);
63
+ }
64
+ }
@@ -0,0 +1,149 @@
1
+ /**
2
+ * @import {
3
+ * Code,
4
+ * Construct,
5
+ * State,
6
+ * TokenizeContext,
7
+ * Tokenizer
8
+ * } from 'micromark-util-types'
9
+ */
10
+
11
+ import { decodeNamedCharacterReference } from 'decode-named-character-reference';
12
+ import { asciiAlphanumeric, asciiDigit, asciiHexDigit } from 'micromark-util-character';
13
+ /** @type {Construct} */
14
+ export const characterReference = {
15
+ name: 'characterReference',
16
+ tokenize: tokenizeCharacterReference
17
+ };
18
+
19
+ /**
20
+ * @this {TokenizeContext}
21
+ * Context.
22
+ * @type {Tokenizer}
23
+ */
24
+ function tokenizeCharacterReference(effects, ok, nok) {
25
+ const self = this;
26
+ let size = 0;
27
+ /** @type {number} */
28
+ let max;
29
+ /** @type {(code: Code) => boolean} */
30
+ let test;
31
+ return start;
32
+
33
+ /**
34
+ * Start of character reference.
35
+ *
36
+ * ```markdown
37
+ * > | a&b
38
+ * ^
39
+ * > | a{b
40
+ * ^
41
+ * > | a	b
42
+ * ^
43
+ * ```
44
+ *
45
+ * @type {State}
46
+ */
47
+ function start(code) {
48
+ effects.enter("characterReference");
49
+ effects.enter("characterReferenceMarker");
50
+ effects.consume(code);
51
+ effects.exit("characterReferenceMarker");
52
+ return open;
53
+ }
54
+
55
+ /**
56
+ * After `&`, at `#` for numeric references or alphanumeric for named
57
+ * references.
58
+ *
59
+ * ```markdown
60
+ * > | a&b
61
+ * ^
62
+ * > | a{b
63
+ * ^
64
+ * > | a	b
65
+ * ^
66
+ * ```
67
+ *
68
+ * @type {State}
69
+ */
70
+ function open(code) {
71
+ if (code === 35) {
72
+ effects.enter("characterReferenceMarkerNumeric");
73
+ effects.consume(code);
74
+ effects.exit("characterReferenceMarkerNumeric");
75
+ return numeric;
76
+ }
77
+ effects.enter("characterReferenceValue");
78
+ max = 31;
79
+ test = asciiAlphanumeric;
80
+ return value(code);
81
+ }
82
+
83
+ /**
84
+ * After `#`, at `x` for hexadecimals or digit for decimals.
85
+ *
86
+ * ```markdown
87
+ * > | a{b
88
+ * ^
89
+ * > | a	b
90
+ * ^
91
+ * ```
92
+ *
93
+ * @type {State}
94
+ */
95
+ function numeric(code) {
96
+ if (code === 88 || code === 120) {
97
+ effects.enter("characterReferenceMarkerHexadecimal");
98
+ effects.consume(code);
99
+ effects.exit("characterReferenceMarkerHexadecimal");
100
+ effects.enter("characterReferenceValue");
101
+ max = 6;
102
+ test = asciiHexDigit;
103
+ return value;
104
+ }
105
+ effects.enter("characterReferenceValue");
106
+ max = 7;
107
+ test = asciiDigit;
108
+ return value(code);
109
+ }
110
+
111
+ /**
112
+ * After markers (`&#x`, `&#`, or `&`), in value, before `;`.
113
+ *
114
+ * The character reference kind defines what and how many characters are
115
+ * allowed.
116
+ *
117
+ * ```markdown
118
+ * > | a&b
119
+ * ^^^
120
+ * > | a{b
121
+ * ^^^
122
+ * > | a	b
123
+ * ^
124
+ * ```
125
+ *
126
+ * @type {State}
127
+ */
128
+ function value(code) {
129
+ if (code === 59 && size) {
130
+ const token = effects.exit("characterReferenceValue");
131
+ if (test === asciiAlphanumeric && !decodeNamedCharacterReference(self.sliceSerialize(token))) {
132
+ return nok(code);
133
+ }
134
+
135
+ // To do: `markdown-rs` uses a different name:
136
+ // `CharacterReferenceMarkerSemi`.
137
+ effects.enter("characterReferenceMarker");
138
+ effects.consume(code);
139
+ effects.exit("characterReferenceMarker");
140
+ effects.exit("characterReference");
141
+ return ok;
142
+ }
143
+ if (test(code) && size++ < max) {
144
+ effects.consume(code);
145
+ return value;
146
+ }
147
+ return nok(code);
148
+ }
149
+ }
@@ -0,0 +1,422 @@
1
+ /**
2
+ * @import {
3
+ * Code,
4
+ * Construct,
5
+ * State,
6
+ * TokenizeContext,
7
+ * Tokenizer
8
+ * } from 'micromark-util-types'
9
+ */
10
+
11
+ import { factorySpace } from 'micromark-factory-space';
12
+ import { markdownLineEnding, markdownSpace } from 'micromark-util-character';
13
+ import { nonLazyContinuation } from './partial-non-lazy-continuation.js';
14
+
15
+ /** @type {Construct} */
16
+ export const codeFenced = {
17
+ concrete: true,
18
+ name: 'codeFenced',
19
+ tokenize: tokenizeCodeFenced
20
+ };
21
+
22
+ /**
23
+ * @this {TokenizeContext}
24
+ * Context.
25
+ * @type {Tokenizer}
26
+ */
27
+ function tokenizeCodeFenced(effects, ok, nok) {
28
+ const self = this;
29
+ /** @type {Construct} */
30
+ const closeStart = {
31
+ partial: true,
32
+ tokenize: tokenizeCloseStart
33
+ };
34
+ let initialPrefix = 0;
35
+ let sizeOpen = 0;
36
+ /** @type {NonNullable<Code>} */
37
+ let marker;
38
+ return start;
39
+
40
+ /**
41
+ * Start of code.
42
+ *
43
+ * ```markdown
44
+ * > | ~~~js
45
+ * ^
46
+ * | alert(1)
47
+ * | ~~~
48
+ * ```
49
+ *
50
+ * @type {State}
51
+ */
52
+ function start(code) {
53
+ // To do: parse whitespace like `markdown-rs`.
54
+ return beforeSequenceOpen(code);
55
+ }
56
+
57
+ /**
58
+ * In opening fence, after prefix, at sequence.
59
+ *
60
+ * ```markdown
61
+ * > | ~~~js
62
+ * ^
63
+ * | alert(1)
64
+ * | ~~~
65
+ * ```
66
+ *
67
+ * @type {State}
68
+ */
69
+ function beforeSequenceOpen(code) {
70
+ const tail = self.events[self.events.length - 1];
71
+ initialPrefix = tail && tail[1].type === "linePrefix" ? tail[2].sliceSerialize(tail[1], true).length : 0;
72
+ marker = code;
73
+ effects.enter("codeFenced");
74
+ effects.enter("codeFencedFence");
75
+ effects.enter("codeFencedFenceSequence");
76
+ return sequenceOpen(code);
77
+ }
78
+
79
+ /**
80
+ * In opening fence sequence.
81
+ *
82
+ * ```markdown
83
+ * > | ~~~js
84
+ * ^
85
+ * | alert(1)
86
+ * | ~~~
87
+ * ```
88
+ *
89
+ * @type {State}
90
+ */
91
+ function sequenceOpen(code) {
92
+ if (code === marker) {
93
+ sizeOpen++;
94
+ effects.consume(code);
95
+ return sequenceOpen;
96
+ }
97
+ if (sizeOpen < 3) {
98
+ return nok(code);
99
+ }
100
+ effects.exit("codeFencedFenceSequence");
101
+ return markdownSpace(code) ? factorySpace(effects, infoBefore, "whitespace")(code) : infoBefore(code);
102
+ }
103
+
104
+ /**
105
+ * In opening fence, after the sequence (and optional whitespace), before info.
106
+ *
107
+ * ```markdown
108
+ * > | ~~~js
109
+ * ^
110
+ * | alert(1)
111
+ * | ~~~
112
+ * ```
113
+ *
114
+ * @type {State}
115
+ */
116
+ function infoBefore(code) {
117
+ if (code === null || markdownLineEnding(code)) {
118
+ effects.exit("codeFencedFence");
119
+ return self.interrupt ? ok(code) : effects.check(nonLazyContinuation, atNonLazyBreak, after)(code);
120
+ }
121
+ effects.enter("codeFencedFenceInfo");
122
+ effects.enter("chunkString", {
123
+ contentType: "string"
124
+ });
125
+ return info(code);
126
+ }
127
+
128
+ /**
129
+ * In info.
130
+ *
131
+ * ```markdown
132
+ * > | ~~~js
133
+ * ^
134
+ * | alert(1)
135
+ * | ~~~
136
+ * ```
137
+ *
138
+ * @type {State}
139
+ */
140
+ function info(code) {
141
+ if (code === null || markdownLineEnding(code)) {
142
+ effects.exit("chunkString");
143
+ effects.exit("codeFencedFenceInfo");
144
+ return infoBefore(code);
145
+ }
146
+ if (markdownSpace(code)) {
147
+ effects.exit("chunkString");
148
+ effects.exit("codeFencedFenceInfo");
149
+ return factorySpace(effects, metaBefore, "whitespace")(code);
150
+ }
151
+ if (code === 96 && code === marker) {
152
+ return nok(code);
153
+ }
154
+ effects.consume(code);
155
+ return info;
156
+ }
157
+
158
+ /**
159
+ * In opening fence, after info and whitespace, before meta.
160
+ *
161
+ * ```markdown
162
+ * > | ~~~js eval
163
+ * ^
164
+ * | alert(1)
165
+ * | ~~~
166
+ * ```
167
+ *
168
+ * @type {State}
169
+ */
170
+ function metaBefore(code) {
171
+ if (code === null || markdownLineEnding(code)) {
172
+ return infoBefore(code);
173
+ }
174
+ effects.enter("codeFencedFenceMeta");
175
+ effects.enter("chunkString", {
176
+ contentType: "string"
177
+ });
178
+ return meta(code);
179
+ }
180
+
181
+ /**
182
+ * In meta.
183
+ *
184
+ * ```markdown
185
+ * > | ~~~js eval
186
+ * ^
187
+ * | alert(1)
188
+ * | ~~~
189
+ * ```
190
+ *
191
+ * @type {State}
192
+ */
193
+ function meta(code) {
194
+ if (code === null || markdownLineEnding(code)) {
195
+ effects.exit("chunkString");
196
+ effects.exit("codeFencedFenceMeta");
197
+ return infoBefore(code);
198
+ }
199
+ if (code === 96 && code === marker) {
200
+ return nok(code);
201
+ }
202
+ effects.consume(code);
203
+ return meta;
204
+ }
205
+
206
+ /**
207
+ * At eol/eof in code, before a non-lazy closing fence or content.
208
+ *
209
+ * ```markdown
210
+ * > | ~~~js
211
+ * ^
212
+ * > | alert(1)
213
+ * ^
214
+ * | ~~~
215
+ * ```
216
+ *
217
+ * @type {State}
218
+ */
219
+ function atNonLazyBreak(code) {
220
+ return effects.attempt(closeStart, after, contentBefore)(code);
221
+ }
222
+
223
+ /**
224
+ * Before code content, not a closing fence, at eol.
225
+ *
226
+ * ```markdown
227
+ * | ~~~js
228
+ * > | alert(1)
229
+ * ^
230
+ * | ~~~
231
+ * ```
232
+ *
233
+ * @type {State}
234
+ */
235
+ function contentBefore(code) {
236
+ effects.enter("lineEnding");
237
+ effects.consume(code);
238
+ effects.exit("lineEnding");
239
+ return contentStart;
240
+ }
241
+
242
+ /**
243
+ * Before code content, not a closing fence.
244
+ *
245
+ * ```markdown
246
+ * | ~~~js
247
+ * > | alert(1)
248
+ * ^
249
+ * | ~~~
250
+ * ```
251
+ *
252
+ * @type {State}
253
+ */
254
+ function contentStart(code) {
255
+ return initialPrefix > 0 && markdownSpace(code) ? factorySpace(effects, beforeContentChunk, "linePrefix", initialPrefix + 1)(code) : beforeContentChunk(code);
256
+ }
257
+
258
+ /**
259
+ * Before code content, after optional prefix.
260
+ *
261
+ * ```markdown
262
+ * | ~~~js
263
+ * > | alert(1)
264
+ * ^
265
+ * | ~~~
266
+ * ```
267
+ *
268
+ * @type {State}
269
+ */
270
+ function beforeContentChunk(code) {
271
+ if (code === null || markdownLineEnding(code)) {
272
+ return effects.check(nonLazyContinuation, atNonLazyBreak, after)(code);
273
+ }
274
+ effects.enter("codeFlowValue");
275
+ return contentChunk(code);
276
+ }
277
+
278
+ /**
279
+ * In code content.
280
+ *
281
+ * ```markdown
282
+ * | ~~~js
283
+ * > | alert(1)
284
+ * ^^^^^^^^
285
+ * | ~~~
286
+ * ```
287
+ *
288
+ * @type {State}
289
+ */
290
+ function contentChunk(code) {
291
+ if (code === null || markdownLineEnding(code)) {
292
+ effects.exit("codeFlowValue");
293
+ return beforeContentChunk(code);
294
+ }
295
+ effects.consume(code);
296
+ return contentChunk;
297
+ }
298
+
299
+ /**
300
+ * After code.
301
+ *
302
+ * ```markdown
303
+ * | ~~~js
304
+ * | alert(1)
305
+ * > | ~~~
306
+ * ^
307
+ * ```
308
+ *
309
+ * @type {State}
310
+ */
311
+ function after(code) {
312
+ effects.exit("codeFenced");
313
+ return ok(code);
314
+ }
315
+
316
+ /**
317
+ * @this {TokenizeContext}
318
+ * Context.
319
+ * @type {Tokenizer}
320
+ */
321
+ function tokenizeCloseStart(effects, ok, nok) {
322
+ let size = 0;
323
+ return startBefore;
324
+
325
+ /**
326
+ *
327
+ *
328
+ * @type {State}
329
+ */
330
+ function startBefore(code) {
331
+ effects.enter("lineEnding");
332
+ effects.consume(code);
333
+ effects.exit("lineEnding");
334
+ return start;
335
+ }
336
+
337
+ /**
338
+ * Before closing fence, at optional whitespace.
339
+ *
340
+ * ```markdown
341
+ * | ~~~js
342
+ * | alert(1)
343
+ * > | ~~~
344
+ * ^
345
+ * ```
346
+ *
347
+ * @type {State}
348
+ */
349
+ function start(code) {
350
+ // Always populated by defaults.
351
+
352
+ // To do: `enter` here or in next state?
353
+ effects.enter("codeFencedFence");
354
+ return markdownSpace(code) ? factorySpace(effects, beforeSequenceClose, "linePrefix", self.parser.constructs.disable.null.includes('codeIndented') ? undefined : 4)(code) : beforeSequenceClose(code);
355
+ }
356
+
357
+ /**
358
+ * In closing fence, after optional whitespace, at sequence.
359
+ *
360
+ * ```markdown
361
+ * | ~~~js
362
+ * | alert(1)
363
+ * > | ~~~
364
+ * ^
365
+ * ```
366
+ *
367
+ * @type {State}
368
+ */
369
+ function beforeSequenceClose(code) {
370
+ if (code === marker) {
371
+ effects.enter("codeFencedFenceSequence");
372
+ return sequenceClose(code);
373
+ }
374
+ return nok(code);
375
+ }
376
+
377
+ /**
378
+ * In closing fence sequence.
379
+ *
380
+ * ```markdown
381
+ * | ~~~js
382
+ * | alert(1)
383
+ * > | ~~~
384
+ * ^
385
+ * ```
386
+ *
387
+ * @type {State}
388
+ */
389
+ function sequenceClose(code) {
390
+ if (code === marker) {
391
+ size++;
392
+ effects.consume(code);
393
+ return sequenceClose;
394
+ }
395
+ if (size >= sizeOpen) {
396
+ effects.exit("codeFencedFenceSequence");
397
+ return markdownSpace(code) ? factorySpace(effects, sequenceCloseAfter, "whitespace")(code) : sequenceCloseAfter(code);
398
+ }
399
+ return nok(code);
400
+ }
401
+
402
+ /**
403
+ * After closing fence sequence, after optional whitespace.
404
+ *
405
+ * ```markdown
406
+ * | ~~~js
407
+ * | alert(1)
408
+ * > | ~~~
409
+ * ^
410
+ * ```
411
+ *
412
+ * @type {State}
413
+ */
414
+ function sequenceCloseAfter(code) {
415
+ if (code === null || markdownLineEnding(code)) {
416
+ effects.exit("codeFencedFence");
417
+ return ok(code);
418
+ }
419
+ return nok(code);
420
+ }
421
+ }
422
+ }